bce

package
v0.9.185 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: Apache-2.0 Imports: 16 Imported by: 118

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

View Source
const (
	SDK_VERSION                          = "0.9.185"
	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
	DEFAULT_WARN_LOG_TIMEOUT_IN_MILLS    = 5 * 1000
)

Constants and default values for the package bce

View Source
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

View Source
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 NewBceClientWithAkSk(ak, sk, endPoint string) (*BceClient, error)

func (*BceClient) GetBceClientConfig added in v0.9.10

func (c *BceClient) GetBceClientConfig() *BceClientConfiguration

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

func (*BceClient) SendRequestFromBytes added in v0.9.18

func (c *BceClient) SendRequestFromBytes(req *BceRequest, resp *BceResponse, content []byte) error

SendRequestFromBytes - 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
  • content: the content of body

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
	// CnameEnabled should be true when use custom domain as endpoint to visit bos resource
	CnameEnabled     bool
	BackupEndpoint   string
	RedirectDisabled bool
}

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 BceError

type BceError interface {
	error
}

BceError abstracts the error for BCE

type BceRequest

type BceRequest struct {
	http.Request
	// contains filtered or unexported fields
}

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

type BceServiceError struct {
	Code       string
	Message    string
	RequestId  string
	StatusCode int
}

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

func NewBodyFromBytes(stream []byte) (*Body, error)

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

func NewBodyFromFile(fname string) (*Body, error)

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

func NewBodyFromSectionFile(file *os.File, off, size int64) (*Body, error)

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

func NewBodyFromSizedReader(r io.Reader, size int64) (*Body, error)

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, -1 is read all

RETURNS:

  • *Body: the return Body object
  • error: error if any specific error occurs

func NewBodyFromString

func NewBodyFromString(str string) (*Body, error)

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 (b *Body) ContentMD5() string

func (*Body) SetStream

func (b *Body) SetStream(stream io.ReadCloser)

func (*Body) Size

func (b *Body) Size() int64

func (*Body) Stream

func (b *Body) Stream() io.ReadCloser

type Client

type Client interface {
	SendRequest(*BceRequest, *BceResponse) error
	SendRequestFromBytes(*BceRequest, *BceResponse, []byte) error
	GetBceClientConfig() *BceClientConfiguration
}

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

type RetryPolicy

type RetryPolicy interface {
	ShouldRetry(BceError, int) bool
	GetDelayBeforeNextRetryInMillis(BceError, int) time.Duration
}

RetryPolicy defines the two methods to retry for sending request.

Jump to

Keyboard shortcuts

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