Documentation ¶
Overview ¶
Package bce implements the infrastructure to access BCE services.
- BceClient: It is the general client of BCE to access all services. It builds http request to access the services based on the given client configuration.
- BceClientConfiguration: The client configuration data structure which contains endpoint, region, credentials, retry policy, sign options and so on. It supports most of the default value and user can also access or change the default with its public fields' name.
- Error types: The error types when making request or receiving response to the BCE services contains two types: the BceClientError when making request to BCE services and the BceServiceError when recieving response from them.
- BceRequest: The request instance stands for an request to access the BCE services.
- BceResponse: The response instance stands for an response from the BCE services.
Index ¶
- Constants
- Variables
- type BackOffRetryPolicy
- type BceClient
- type BceClientConfiguration
- type BceClientError
- type BceError
- type BceRequest
- func (b *BceRequest) BuildHttpRequest()
- func (b *BceRequest) ClientError() *BceClientError
- func (b *BceRequest) RequestId() string
- func (b *BceRequest) SetBody(body *Body)
- func (b *BceRequest) SetClientError(err *BceClientError)
- func (b *BceRequest) SetRequestId(val string)
- func (b *BceRequest) String() string
- type BceResponse
- func (r *BceResponse) Body() io.ReadCloser
- func (r *BceResponse) DebugId() string
- func (r *BceResponse) ElapsedTime() time.Duration
- func (r *BceResponse) Header(key string) string
- func (r *BceResponse) Headers() map[string]string
- func (r *BceResponse) IsFail() bool
- func (r *BceResponse) ParseJsonBody(result interface{}) error
- func (r *BceResponse) ParseResponse()
- func (r *BceResponse) RequestId() string
- func (r *BceResponse) ServiceError() *BceServiceError
- func (r *BceResponse) SetHttpResponse(response *http.Response)
- func (r *BceResponse) StatusCode() int
- func (r *BceResponse) StatusText() string
- type BceServiceError
- type Body
- type Client
- type NoRetryPolicy
- type RequestBuilder
- func (b *RequestBuilder) Do() error
- func (b *RequestBuilder) WithBody(body interface{}) *RequestBuilder
- func (b *RequestBuilder) WithHeader(key, value string) *RequestBuilder
- func (b *RequestBuilder) WithHeaders(headers map[string]string) *RequestBuilder
- func (b *RequestBuilder) WithMethod(method string) *RequestBuilder
- func (b *RequestBuilder) WithQueryParam(key, value string) *RequestBuilder
- func (b *RequestBuilder) WithQueryParamFilter(key, value string) *RequestBuilder
- func (b *RequestBuilder) WithQueryParams(params map[string]string) *RequestBuilder
- func (b *RequestBuilder) WithResult(result interface{}) *RequestBuilder
- func (b *RequestBuilder) WithURL(url string) *RequestBuilder
- type RetryPolicy
Constants ¶
const ( SDK_VERSION = "0.9.6" URI_PREFIX = "/" // now support uri without prefix "v1" so just set root path DEFAULT_DOMAIN = "baidubce.com" DEFAULT_PROTOCOL = "http" DEFAULT_REGION = "bj" DEFAULT_CONTENT_TYPE = "application/json;charset=utf-8" DEFAULT_CONNECTION_TIMEOUT_IN_MILLIS = 1200 * 1000 )
Constants and default values for the package bce
const ( EACCESS_DENIED = "AccessDenied" EINAPPROPRIATE_JSON = "InappropriateJSON" EINTERNAL_ERROR = "InternalError" EINVALID_ACCESS_KEY_ID = "InvalidAccessKeyId" EINVALID_HTTP_AUTH_HEADER = "InvalidHTTPAuthHeader" EINVALID_HTTP_REQUEST = "InvalidHTTPRequest" EINVALID_URI = "InvalidURI" EMALFORMED_JSON = "MalformedJSON" EINVALID_VERSION = "InvalidVersion" EOPT_IN_REQUIRED = "OptInRequired" EPRECONDITION_FAILED = "PreconditionFailed" EREQUEST_EXPIRED = "RequestExpired" ESIGNATURE_DOES_NOT_MATCH = "SignatureDoesNotMatch" )
Variables ¶
var ( DEFAULT_USER_AGENT string DEFAULT_RETRY_POLICY = NewBackOffRetryPolicy(3, 20000, 300) )
Functions ¶
This section is empty.
Types ¶
type BackOffRetryPolicy ¶
type BackOffRetryPolicy struct {
// contains filtered or unexported fields
}
BackOffRetryPolicy implements a policy that retries with exponential back-off strategy. This policy will keep retrying until the maximum number of retries is reached. The delay time will be a fixed interval for the first time then 2 * interval for the second, 4 * internal for the third, and so on. In general, the delay time will be 2^number_of_retries_attempted*interval. When a maximum of delay time is specified, the delay time will never exceed this limit.
func NewBackOffRetryPolicy ¶
func NewBackOffRetryPolicy(maxRetry int, maxDelay, base int64) *BackOffRetryPolicy
func (*BackOffRetryPolicy) GetDelayBeforeNextRetryInMillis ¶
func (b *BackOffRetryPolicy) GetDelayBeforeNextRetryInMillis( err BceError, attempts int) time.Duration
func (*BackOffRetryPolicy) ShouldRetry ¶
func (b *BackOffRetryPolicy) ShouldRetry(err BceError, attempts int) bool
type BceClient ¶
type BceClient struct { Config *BceClientConfiguration Signer auth.Signer // the sign algorithm }
BceClient defines the general client to access the BCE services.
func NewBceClient ¶
func NewBceClient(conf *BceClientConfiguration, sign auth.Signer) *BceClient
func NewBceClientWithAkSk ¶
func (*BceClient) SendRequest ¶
func (c *BceClient) SendRequest(req *BceRequest, resp *BceResponse) error
SendRequest - the client performs sending the http request with retry policy and receive the response from the BCE services.
PARAMS:
- req: the request object to be sent to the BCE service
- resp: the response object to receive the content from BCE service
RETURNS:
- error: nil if ok otherwise the specific error
type BceClientConfiguration ¶
type BceClientConfiguration struct { Endpoint string ProxyUrl string Region string UserAgent string Credentials *auth.BceCredentials SignOption *auth.SignOptions Retry RetryPolicy ConnectionTimeoutInMillis int }
BceClientConfiguration defines the config components structure.
func (*BceClientConfiguration) String ¶
func (c *BceClientConfiguration) String() string
type BceClientError ¶
type BceClientError struct{ Message string }
BceClientError defines the error struct for the client when making request
func NewBceClientError ¶
func NewBceClientError(msg string) *BceClientError
func (*BceClientError) Error ¶
func (b *BceClientError) Error() string
type BceRequest ¶
BceRequest defines the request structure for accessing BCE services
func (*BceRequest) BuildHttpRequest ¶
func (b *BceRequest) BuildHttpRequest()
func (*BceRequest) ClientError ¶
func (b *BceRequest) ClientError() *BceClientError
func (*BceRequest) RequestId ¶
func (b *BceRequest) RequestId() string
func (*BceRequest) SetBody ¶
func (b *BceRequest) SetBody(body *Body)
func (*BceRequest) SetClientError ¶
func (b *BceRequest) SetClientError(err *BceClientError)
func (*BceRequest) SetRequestId ¶
func (b *BceRequest) SetRequestId(val string)
func (*BceRequest) String ¶
func (b *BceRequest) String() string
type BceResponse ¶
type BceResponse struct {
// contains filtered or unexported fields
}
BceResponse defines the response structure for receiving BCE services response.
func (*BceResponse) Body ¶
func (r *BceResponse) Body() io.ReadCloser
func (*BceResponse) DebugId ¶
func (r *BceResponse) DebugId() string
func (*BceResponse) ElapsedTime ¶
func (r *BceResponse) ElapsedTime() time.Duration
func (*BceResponse) Header ¶
func (r *BceResponse) Header(key string) string
func (*BceResponse) Headers ¶
func (r *BceResponse) Headers() map[string]string
func (*BceResponse) IsFail ¶
func (r *BceResponse) IsFail() bool
func (*BceResponse) ParseJsonBody ¶
func (r *BceResponse) ParseJsonBody(result interface{}) error
func (*BceResponse) ParseResponse ¶
func (r *BceResponse) ParseResponse()
func (*BceResponse) RequestId ¶
func (r *BceResponse) RequestId() string
func (*BceResponse) ServiceError ¶
func (r *BceResponse) ServiceError() *BceServiceError
func (*BceResponse) SetHttpResponse ¶
func (r *BceResponse) SetHttpResponse(response *http.Response)
func (*BceResponse) StatusCode ¶
func (r *BceResponse) StatusCode() int
func (*BceResponse) StatusText ¶
func (r *BceResponse) StatusText() string
type BceServiceError ¶
BceServiceError defines the error struct for the BCE service when receiving response
func NewBceServiceError ¶
func NewBceServiceError(code, msg, reqId string, status int) *BceServiceError
func (*BceServiceError) Error ¶
func (b *BceServiceError) Error() string
type Body ¶
type Body struct {
// contains filtered or unexported fields
}
Body defines the data structure used in BCE request. Every BCE request that sets the body field must set its content-length and content-md5 headers to ensure the correctness of the body content forcely, and users can also set the content-sha256 header to strengthen the correctness with the "SetHeader" method.
func NewBodyFromBytes ¶
NewBodyFromBytes - build a Body object from the byte stream to be used in the http request, it calculates the content-md5 of the byte stream and store the size as well as the stream.
PARAMS:
- stream: byte stream
RETURNS:
- *Body: the return Body object
- error: error if any specific error occurs
func NewBodyFromFile ¶
NewBodyFromFile - build a Body object from the given file name to be used in the http request, it calculates the content-md5 of the byte stream and store the size as well as the stream.
PARAMS:
- fname: the given file name
RETURNS:
- *Body: the return Body object
- error: error if any specific error occurs
func NewBodyFromSectionFile ¶
NewBodyFromSectionFile - build a Body object from the given file pointer with offset and size. It calculates the content-md5 of the given content and store the size as well as the stream.
PARAMS:
- file: the input file pointer
- off: offset of current section body
- size: current section body size
RETURNS:
- *Body: the return Body object
- error: error if any specific error occurs
func NewBodyFromSizedReader ¶
NewBodyFromSizedReader - build a Body object from the given reader with size. It calculates the content-md5 of the given content and store the size as well as the stream.
PARAMS:
- r: the input reader
- size: the size to be read from the input reader which must be <= reader size
RETURNS:
- *Body: the return Body object
- error: error if any specific error occurs
func NewBodyFromString ¶
NewBodyFromString - build a Body object from the string to be used in the http request, it calculates the content-md5 of the byte stream and store the size as well as the stream.
PARAMS:
- str: the input string
RETURNS:
- *Body: the return Body object
- error: error if any specific error occurs
func (*Body) ContentMD5 ¶
func (*Body) SetStream ¶
func (b *Body) SetStream(stream io.ReadCloser)
func (*Body) Stream ¶
func (b *Body) Stream() io.ReadCloser
type Client ¶
type Client interface {
SendRequest(*BceRequest, *BceResponse) error
}
Client is the general interface which can perform sending request. Different service will define its own client in case of specific extension.
type NoRetryPolicy ¶
type NoRetryPolicy struct{}
NoRetryPolicy just does not retry.
func NewNoRetryPolicy ¶
func NewNoRetryPolicy() *NoRetryPolicy
func (*NoRetryPolicy) GetDelayBeforeNextRetryInMillis ¶
func (_ *NoRetryPolicy) GetDelayBeforeNextRetryInMillis( err BceError, attempts int) time.Duration
func (*NoRetryPolicy) ShouldRetry ¶
func (_ *NoRetryPolicy) ShouldRetry(err BceError, attempts int) bool
type RequestBuilder ¶
type RequestBuilder struct {
// contains filtered or unexported fields
}
RequestBuilder holds config data for bce request. Some of fields are required and the others are optional. The builder pattern can simplify the execution of requests.
func NewRequestBuilder ¶
func NewRequestBuilder(client Client) *RequestBuilder
create RequestBuilder with the client.
func (*RequestBuilder) Do ¶
func (b *RequestBuilder) Do() error
Do will send request to bce and get result with the builder's parameters.
func (*RequestBuilder) WithBody ¶
func (b *RequestBuilder) WithBody(body interface{}) *RequestBuilder
func (*RequestBuilder) WithHeader ¶
func (b *RequestBuilder) WithHeader(key, value string) *RequestBuilder
func (*RequestBuilder) WithHeaders ¶
func (b *RequestBuilder) WithHeaders(headers map[string]string) *RequestBuilder
func (*RequestBuilder) WithMethod ¶
func (b *RequestBuilder) WithMethod(method string) *RequestBuilder
func (*RequestBuilder) WithQueryParam ¶
func (b *RequestBuilder) WithQueryParam(key, value string) *RequestBuilder
set query param with the key/value directly.
func (*RequestBuilder) WithQueryParamFilter ¶
func (b *RequestBuilder) WithQueryParamFilter(key, value string) *RequestBuilder
set query param with the key/value only when the value is not blank.
func (*RequestBuilder) WithQueryParams ¶
func (b *RequestBuilder) WithQueryParams(params map[string]string) *RequestBuilder
func (*RequestBuilder) WithResult ¶
func (b *RequestBuilder) WithResult(result interface{}) *RequestBuilder
func (*RequestBuilder) WithURL ¶
func (b *RequestBuilder) WithURL(url string) *RequestBuilder