bce

package
v0.0.0-...-386b7fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2019 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package bce defined a set of core data structure and functions for Baidu Cloud API.

Index

Constants

View Source
const (
	// Version for bce
	Version = "0.0.1"

	// ExpirationPeriodInSeconds 1800s is the default expiration period.
	ExpirationPeriodInSeconds = 1800
)

Variables

View Source
var DefaultUserAgent = strings.Join([]string{
	"baiducloud-sdk-go",
	Version,
	runtime.GOOS,
	runtime.Version(),
}, "/")

DefaultUserAgent is the default value of http request UserAgent header. We can change it by specifying the UserAgent field of bce.Config.

View Source
var Region = map[string]string{
	"bj": "bj",
	"gz": "gz",
	"hk": "hk",
	"su": "su",
	"bd": "bd",
}

Region contains all regions of Baidu Cloud.

Functions

func GenerateAuthorization

func GenerateAuthorization(credentials Credentials, req Request, option *SignOption) string

GenerateAuthorization generates authorization code for authorization process of Baidu Cloud API.

Types

type AccessControlListItem

type AccessControlListItem struct {
	Eid        string   `json:"eid"`
	Service    string   `json:"service"`
	Region     string   `json:"region"`
	Effect     string   `json:"effect"`
	Resource   []string `json:"resource"`
	Permission []string `json:"permission"`
}

AccessControlListItem contains sub options for bce.SessionTokenRequest

For details, please refer https://cloud.baidu.com/doc/BOS/API.html#STS.E7.AE.80.E4.BB.8B

type Billing

type Billing struct {
	PaymentTiming string `json:"paymentTiming"`
	BillingMethod string `json:"billingMethod"`
}

Billing json

type Client

type Client struct {
	*Config
	// contains filtered or unexported fields
}

Client is the base client implemention for Baidu Cloud API.

func NewClient

func NewClient(config *Config) *Client

NewClient retunrs client

func (*Client) GenerateClientToken

func (c *Client) GenerateClientToken() string

GenerateClientToken generates the Client Token with random string

func (*Client) GetSessionToken

func (c *Client) GetSessionToken(sessionTokenRequest SessionTokenRequest,
	option *SignOption) (*SessionTokenResponse, error)

GetSessionToken gets response for STS(Security Token Service)of Baidu Cloud API.

For details, please refer https://cloud.baidu.com/doc/BOS/API.html#STS.E7.AE.80.E4.BB.8B

func (*Client) GetURL

func (c *Client) GetURL(host, uriPath string, params map[string]string) string

GetURL generates the full URL of http request for Baidu Cloud API.

func (*Client) SendRequest

func (c *Client) SendRequest(req *Request, option *SignOption) (bceResponse *Response, err error)

SendRequest sends a http request to the endpoint of Baidu Cloud API.

func (*Client) SetDebug

func (c *Client) SetDebug(debug bool)

SetDebug enables debug mode of bce.Client instance.

type Config

type Config struct {
	*Credentials
	Region     string `json:"region"`
	Endpoint   string
	APIVersion string
	Protocol   string
	UserAgent  string
	ProxyHost  string
	ProxyPort  int
	//ConnectionTimeoutInMillis time.Duration // default value: 10 * time.Second in http.DefaultTransport
	MaxConnections int           // default value: 2 in http.DefaultMaxIdleConnsPerHost
	Timeout        time.Duration // default value: 0 in http.Client
	RetryPolicy    RetryPolicy
	Checksum       bool
}

Config contains all options for bce.Client.

func NewConfig

func NewConfig(credentials *Credentials) *Config

NewConfig create a config from credentials

func NewConfigFromFile

func NewConfigFromFile(filePath string) (*Config, error)

NewConfigFromFile create a new config from cloud config.

func NewConfigWithParams

func NewConfigWithParams(accessKeyID, secretAccessKey, region string) *Config

NewConfigWithParams creates a new config from filled info

func (*Config) GetRegion

func (config *Config) GetRegion() string

GetRegion gets region from bce.Config.

If no region specified in bce.Config, the bj region will be return.

func (*Config) GetUserAgent

func (config *Config) GetUserAgent() string

GetUserAgent gets UserAgent from bce.Config.

If no UserAgent specified in bce.Config, the bce.DefaultUserAgent will be return.

type Credentials

type Credentials struct {
	AccessKeyID     string `json:"AccessKeyID"`
	SecretAccessKey string `json:"SecretAccessKey"`
}

Credentials json

func NewCredentials

func NewCredentials(AccessKeyID, secretAccessKey string) *Credentials

NewCredentials returns Credentials

func NewCredentialsFromFile

func NewCredentialsFromFile(filePath string) (*Credentials, error)

NewCredentialsFromFile returns Credentials

type DefaultRetryPolicy

type DefaultRetryPolicy struct {
	MaxErrorRetry int
	MaxDelay      time.Duration
}

DefaultRetryPolicy is the default implemention of interface bce.RetryPolicy.

func NewDefaultRetryPolicy

func NewDefaultRetryPolicy(maxErrorRetry int, maxDelay time.Duration) *DefaultRetryPolicy

NewDefaultRetryPolicy returns DefaultRetryPolicy

func (*DefaultRetryPolicy) GetDelayBeforeNextRetry

func (policy *DefaultRetryPolicy) GetDelayBeforeNextRetry(err error, retriesAttempted int) time.Duration

GetDelayBeforeNextRetry specifies the delay time for next retry.

func (*DefaultRetryPolicy) GetMaxDelay

func (policy *DefaultRetryPolicy) GetMaxDelay() time.Duration

GetMaxDelay specifies the max delay time for retrying.

func (*DefaultRetryPolicy) GetMaxErrorRetry

func (policy *DefaultRetryPolicy) GetMaxErrorRetry() int

GetMaxErrorRetry specifies the max retry count.

type Error

type Error struct {
	StatusCode               int
	Code, Message, RequestID string
}

Error implements the error interface

Most methods in the SDK will return bce.Error instance.

func (*Error) Error

func (err *Error) Error() string

Error returns the formatted error message.

type Request

type Request http.Request

Request is http request, but has some custom functions.

func NewRequest

func NewRequest(method, url string, body io.Reader) (*Request, error)

NewRequest returns a request client

func (*Request) AddHeaders

func (req *Request) AddHeaders(headerMap map[string]string)

AddHeaders Add headers to http request

func (*Request) SetHeaders

func (req *Request) SetHeaders(headerMap map[string]string)

SetHeaders Set headers to http request

type Reservation

type Reservation struct {
	ReservationLength   int    `json:"reservationLength"`
	ReservationTimeUnit string `json:"reservationTimeUnit"`
}

Reservation json

type Response

type Response struct {
	BodyContent []byte
	*http.Response
}

Response holds an instance of type `http response`, and has some custom data and functions.

func NewResponse

func NewResponse(res *http.Response) *Response

NewResponse returns a response

func (*Response) GetBodyContent

func (res *Response) GetBodyContent() ([]byte, error)

GetBodyContent gets body from http response.

type RetryPolicy

type RetryPolicy interface {
	GetMaxErrorRetry() int      // GetMaxErrorRetry specifies the max retry count.
	GetMaxDelay() time.Duration // GetMaxDelay specifies the max delay time for retrying.

	// GetDelayBeforeNextRetry specifies the delay time for next retry.
	GetDelayBeforeNextRetry(err error, retriesAttempted int) time.Duration
}

RetryPolicy defined an interface for retrying of bce.Client.

type SessionTokenRequest

type SessionTokenRequest struct {
	DurationSeconds   int                     `json:"durationSeconds"`
	Id                string                  `json:"id"`
	AccessControlList []AccessControlListItem `json:"accessControlList"`
}

SessionTokenRequest contains all options for STS(Security Token Service)of Baidu Cloud API.

For details, please refer https://cloud.baidu.com/doc/BOS/API.html#STS.E7.AE.80.E4.BB.8B

type SessionTokenResponse

type SessionTokenResponse struct {
	AccessKeyId     string `json:"accessKeyId"`
	SecretAccessKey string `json:"secretAccessKey"`
	SessionToken    string `json:"sessionToken"`
	CreateTime      string `json:"createTime"`
	Expiration      string `json:"expiration"`
	UserId          string `json:"userId"`
}

SessionTokenResponse contains all response fields for STS(Security Token Service)of Baidu Cloud API.

For details, please refer https://cloud.baidu.com/doc/BOS/API.html#STS.E7.AE.80.E4.BB.8B

type SignOption

type SignOption struct {
	Timestamp                 string
	ExpirationPeriodInSeconds int
	Headers                   map[string]string
	HeadersToSign             []string
	Credentials               *Credentials // for STS(Security Token Service) only
	// contains filtered or unexported fields
}

SignOption contains all signature options of Baidu Cloud API.

func CheckSignOption

func CheckSignOption(option *SignOption) *SignOption

CheckSignOption returns a new empty bce.SignOption instance if no option specified.

func NewSignOption

func NewSignOption(timestamp string, expirationPeriodInSeconds int,
	headers map[string]string, headersToSign []string) *SignOption

NewSignOption returns NewSignOption

func (*SignOption) AddHeader

func (option *SignOption) AddHeader(key, value string)

AddHeader adds a header and it's value for authentication process of Baidu Cloud API.

For details, please refer https://cloud.baidu.com/doc/Reference/AuthenticationMechanism.html#1.1.20.E6.A6.82.E8.BF.B0

func (*SignOption) AddHeaders

func (option *SignOption) AddHeaders(headers map[string]string)

AddHeaders adds some headers for authentication process of Baidu Cloud API.

For details, please refer https://cloud.baidu.com/doc/Reference/AuthenticationMechanism.html#1.1.20.E6.A6.82.E8.BF.B0

func (*SignOption) AddHeadersToSign

func (option *SignOption) AddHeadersToSign(headers ...string)

AddHeadersToSign adds some headers for authentication process of Baidu Cloud API.

For details, please refer https://cloud.baidu.com/doc/Reference/AuthenticationMechanism.html#1.1.20.E6.A6.82.E8.BF.B0

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL