bce

package
v0.9.266 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 19 Imported by: 202

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.266"
	URI_PREFIX                           = "/" // now support uri without prefix "v1" so just set root path
	DEFAULT_DOMAIN                       = "baidubce.com"
	DEFAULT_PROTOCOL                     = "http"
	HTTPS_PROTOCAL                       = "https"
	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"
)
View Source
const (
	RateLimiterSlotRx int = iota
	RateLimiterSlotTx
	RateLimiterSlots
)

Variables

View Source
var (
	DEFAULT_USER_AGENT   string
	DEFAULT_RETRY_POLICY = NewBackOffRetryPolicy(3, 20000, 300)
)

Functions

func NewTeeReadNopCloser added in v0.9.257

func NewTeeReadNopCloser(reader io.Reader, writers ...io.Writer) io.ReadCloser

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
	RateLimiters RateLimiters
	HTTPClient   *net_http.Client
}

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 NewBceClientWithExclusiveHTTPClient added in v0.9.246

func NewBceClientWithExclusiveHTTPClient(conf *BceClientConfiguration, sign auth.Signer) (*BceClient, error)

func NewBceClientWithTimeout added in v0.9.235

func NewBceClientWithTimeout(conf *BceClientConfiguration, sign auth.Signer) *BceClient

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

func (*BceClient) SendRequestV2 added in v0.9.257

func (c *BceClient) SendRequestV2(req *BceRequest, resp *BceResponse) 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
	DisableKeepAlives     bool
	NoVerifySSL           bool
	DialTimeout           *time.Duration // timeout of building a connection
	KeepAlive             *time.Duration // the interval between keep-alive probes for an active connection
	ReadTimeout           *time.Duration // read timeout of net.Conn
	WriteTimeOut          *time.Duration // write timeout of net.Conn
	TLSHandshakeTimeout   *time.Duration // http.Transport.TLSHandshakeTimeout
	IdleConnectionTimeout *time.Duration // http.Transport.IdleConnTimeout
	ResponseHeaderTimeout *time.Duration // http.Transport.ResponseHeaderTimeout
	HTTPClientTimeout     *time.Duration // http.Client.Timeout
	HTTPClient            *http.Client   // customized http client
	UploadRatelimit       *int64         // the limit of upload rate, unit:KB/s
	DownloadRatelimit     *int64         // the limit of download rate, unit:KB/s
}

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 NewBodyFromBytesV2 added in v0.9.257

func NewBodyFromBytesV2(stream []byte, calcMd5 bool) (*Body, error)

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 NewBodyFromFileV2 added in v0.9.257

func NewBodyFromFileV2(fname string, calcMd5 bool) (*Body, error)

func NewBodyFromReader added in v0.9.260

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

func NewBodyFromReaderV2 added in v0.9.260

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

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 NewBodyFromSectionFileV2 added in v0.9.257

func NewBodyFromSectionFileV2(file *os.File, off, size int64, calcMd5 bool) (*Body, error)

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 NewBodyFromSizedReaderV2 added in v0.9.257

func NewBodyFromSizedReaderV2(r io.Reader, size int64, calcMd5 bool) (*Body, error)

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 NewBodyFromStringV2 added in v0.9.257

func NewBodyFromStringV2(str string, calcMd5 bool) (*Body, error)

func (*Body) Close added in v0.9.232

func (b *Body) Close() error

func (*Body) ContentMD5

func (b *Body) ContentMD5() string

func (*Body) Crc32 added in v0.9.232

func (b *Body) Crc32() uint32

func (*Body) Read added in v0.9.232

func (b *Body) Read(p []byte) (int, error)

func (*Body) SetContentMD5 added in v0.9.232

func (b *Body) SetContentMD5(md5Str string)

func (*Body) SetSize added in v0.9.232

func (b *Body) SetSize(size int64)

func (*Body) SetStream

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

func (*Body) SetWriter added in v0.9.232

func (b *Body) SetWriter(w io.Writer)

func (*Body) Size

func (b *Body) Size() int64

func (*Body) Stream

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

func (*Body) Writer added in v0.9.232

func (b *Body) Writer() io.Writer

type Client

type Client interface {
	SendRequest(*BceRequest, *BceResponse) error
	SendRequestV2(*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 RateLimiter added in v0.9.246

type RateLimiter struct {
	Bandwidth int64 // Byte/S
	Limiter   *rate.Limiter
}

func (*RateLimiter) LimitBandwidth added in v0.9.246

func (rl *RateLimiter) LimitBandwidth(n int)

type RateLimiters added in v0.9.246

type RateLimiters [RateLimiterSlots]*RateLimiter

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.

type TeeReadNopCloser added in v0.9.257

type TeeReadNopCloser struct {
	// contains filtered or unexported fields
}

func (*TeeReadNopCloser) AddWriter added in v0.9.257

func (t *TeeReadNopCloser) AddWriter(writer io.Writer)

func (*TeeReadNopCloser) Close added in v0.9.257

func (t *TeeReadNopCloser) Close() error

func (*TeeReadNopCloser) IsSeekable added in v0.9.257

func (t *TeeReadNopCloser) IsSeekable() bool

func (*TeeReadNopCloser) Mark added in v0.9.257

func (t *TeeReadNopCloser) Mark()

marks the currenct offset in this reader, . A subsequent call to the Reset method repositions this reader at the last marked position so that subsequent reads re-read the same bytes.

func (*TeeReadNopCloser) Read added in v0.9.257

func (t *TeeReadNopCloser) Read(p []byte) (int, error)

func (*TeeReadNopCloser) Reset added in v0.9.257

func (t *TeeReadNopCloser) Reset() error

func (*TeeReadNopCloser) Seek added in v0.9.257

func (t *TeeReadNopCloser) Seek(offset int64, whence int) (int64, error)

Jump to

Keyboard shortcuts

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