core

package
v2.7.3 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 17 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MergeHeaders added in v2.5.2

func MergeHeaders(left, right http.Header) http.Header

MergeHeaders merges the given headers together, where the right takes precedence over the left.

func QueryValues added in v2.5.2

func QueryValues(v interface{}) (url.Values, error)

QueryValues encodes url.Values from request objects.

Note: This type is inspired by Google's query encoding library, but supports far less customization and is tailored to fit this SDK's use case.

Ref: https://github.com/google/go-querystring

func StringifyJSON

func StringifyJSON(value interface{}) (string, error)

StringifyJSON returns a pretty JSON string representation of the given value.

func WriteMultipartJSON

func WriteMultipartJSON(writer *multipart.Writer, field string, value interface{}) error

WriteMultipartJSON writes the given value as a JSON part. This is used to serialize non-primitive multipart properties (i.e. lists, objects, etc).

Types

type APIError

type APIError struct {
	StatusCode int `json:"-"`
	// contains filtered or unexported fields
}

APIError is a lightweight wrapper around the standard error interface that preserves the status code from the RPC, if any.

func NewAPIError

func NewAPIError(statusCode int, err error) *APIError

NewAPIError constructs a new API error.

func (*APIError) Error

func (a *APIError) Error() string

Error returns the API error's message.

func (*APIError) Unwrap

func (a *APIError) Unwrap() error

Unwrap returns the underlying error. This also makes the error compatible with errors.As and errors.Is.

type BaseURLOption added in v2.5.2

type BaseURLOption struct {
	BaseURL string
}

BaseURLOption implements the RequestOption interface.

type CallParams

type CallParams struct {
	URL                string
	Method             string
	MaxAttempts        uint
	Headers            http.Header
	Client             HTTPClient
	Request            interface{}
	Response           interface{}
	ResponseIsOptional bool
	ErrorDecoder       ErrorDecoder
}

CallParams represents the parameters used to issue an API call.

type Caller

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

Caller calls APIs and deserializes their response, if any.

func NewCaller

func NewCaller(params *CallerParams) *Caller

NewCaller returns a new *Caller backed by the given parameters.

func (*Caller) Call

func (c *Caller) Call(ctx context.Context, params *CallParams) error

Call issues an API call according to the given call parameters.

type CallerParams added in v2.5.2

type CallerParams struct {
	Client      HTTPClient
	MaxAttempts uint
}

CallerParams represents the parameters used to constrcut a new *Caller.

type ClientNameOption added in v2.5.2

type ClientNameOption struct {
	ClientName *string
}

ClientNameOption implements the RequestOption interface.

type Date added in v2.6.0

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

DateTime wraps time.Time and adapts its JSON representation to conform to a RFC3339 date (e.g. 2006-01-02).

Ref: https://ijmacd.github.io/rfc3339-iso8601

func NewDate added in v2.6.0

func NewDate(t time.Time) *Date

NewDate returns a new *Date. If the given time.Time is nil, nil will be returned.

func NewOptionalDate added in v2.6.0

func NewOptionalDate(t *time.Time) *Date

NewOptionalDate returns a new *Date. If the given time.Time is nil, nil will be returned.

func (*Date) MarshalJSON added in v2.6.0

func (d *Date) MarshalJSON() ([]byte, error)

func (*Date) Time added in v2.6.0

func (d *Date) Time() time.Time

Time returns the Date's underlying time, if any. If the date is nil, the zero value is returned.

func (*Date) TimePtr added in v2.6.0

func (d *Date) TimePtr() *time.Time

TimePtr returns a pointer to the Date's underlying time.Time, if any.

func (*Date) UnmarshalJSON added in v2.6.0

func (d *Date) UnmarshalJSON(data []byte) error

type DateTime added in v2.6.0

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

DateTime wraps time.Time and adapts its JSON representation to conform to a RFC3339 date-time (e.g. 2017-07-21T17:32:28Z).

Ref: https://ijmacd.github.io/rfc3339-iso8601

func NewDateTime added in v2.6.0

func NewDateTime(t time.Time) *DateTime

NewDateTime returns a new *DateTime.

func NewOptionalDateTime added in v2.6.0

func NewOptionalDateTime(t *time.Time) *DateTime

NewOptionalDateTime returns a new *DateTime. If the given time.Time is nil, nil will be returned.

func (*DateTime) MarshalJSON added in v2.6.0

func (d *DateTime) MarshalJSON() ([]byte, error)

func (*DateTime) Time added in v2.6.0

func (d *DateTime) Time() time.Time

Time returns the DateTime's underlying time, if any. If the date-time is nil, the zero value is returned.

func (*DateTime) TimePtr added in v2.6.0

func (d *DateTime) TimePtr() *time.Time

TimePtr returns a pointer to the DateTime's underlying time.Time, if any.

func (*DateTime) UnmarshalJSON added in v2.6.0

func (d *DateTime) UnmarshalJSON(data []byte) error

type ErrorDecoder

type ErrorDecoder func(statusCode int, body io.Reader) error

ErrorDecoder decodes *http.Response errors and returns a typed API error (e.g. *APIError).

type HTTPClient

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

HTTPClient is an interface for a subset of the *http.Client.

type HTTPClientOption added in v2.5.2

type HTTPClientOption struct {
	HTTPClient HTTPClient
}

HTTPClientOption implements the RequestOption interface.

type HTTPHeaderOption added in v2.5.2

type HTTPHeaderOption struct {
	HTTPHeader http.Header
}

HTTPHeaderOption implements the RequestOption interface.

type MaxAttemptsOption added in v2.5.2

type MaxAttemptsOption struct {
	MaxAttempts uint
}

MaxAttemptsOption implements the RequestOption interface.

type QueryEncoder added in v2.5.2

type QueryEncoder interface {
	EncodeQueryValues(key string, v *url.Values) error
}

QueryEncoder is an interface implemented by any type that wishes to encode itself into URL values in a non-standard way.

type RequestOption added in v2.5.2

type RequestOption interface {
	// contains filtered or unexported methods
}

RequestOption adapts the behavior of the client or an individual request.

type RequestOptions added in v2.5.2

type RequestOptions struct {
	BaseURL     string
	HTTPClient  HTTPClient
	HTTPHeader  http.Header
	MaxAttempts uint
	Token       string
	ClientName  *string
}

RequestOptions defines all of the possible request options.

This type is primarily used by the generated code and is not meant to be used directly; use the option package instead.

func NewRequestOptions added in v2.5.2

func NewRequestOptions(opts ...RequestOption) *RequestOptions

NewRequestOptions returns a new *RequestOptions value.

This function is primarily used by the generated code and is not meant to be used directly; use RequestOption instead.

func (*RequestOptions) ToHeader added in v2.5.2

func (r *RequestOptions) ToHeader() http.Header

ToHeader maps the configured request options into a http.Header used for the request(s).

type Retrier added in v2.5.2

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

Retrier retries failed requests a configurable number of times with an exponential back-off between each retry.

func NewRetrier added in v2.5.2

func NewRetrier(opts ...RetryOption) *Retrier

NewRetrier constructs a new *Retrier with the given options, if any.

func (*Retrier) Run added in v2.5.2

func (r *Retrier) Run(
	fn RetryFunc,
	request *http.Request,
	errorDecoder ErrorDecoder,
	opts ...RetryOption,
) (*http.Response, error)

Run issues the request and, upon failure, retries the request if possible.

The request will be retried as long as the request is deemed retriable and the number of retry attempts has not grown larger than the configured retry limit.

type RetryFunc added in v2.5.2

type RetryFunc func(*http.Request) (*http.Response, error)

RetryFunc is a retriable HTTP function call (i.e. *http.Client.Do).

type RetryOption added in v2.5.2

type RetryOption func(*retryOptions)

RetryOption adapts the behavior the *Retrier.

func WithMaxAttempts added in v2.5.2

func WithMaxAttempts(attempts uint) RetryOption

WithMaxAttempts configures the maximum number of attempts of the *Retrier.

type Stream

type Stream[T any] struct {
	// contains filtered or unexported fields
}

Stream represents a stream of messages sent from a server.

func NewStream

func NewStream[T any](response *http.Response, opts ...StreamOption) *Stream[T]

NewStream constructs a new Stream from the given *http.Response.

func (Stream[T]) Close

func (s Stream[T]) Close() error

Close closes the Stream.

func (Stream[T]) Recv

func (s Stream[T]) Recv() (T, error)

Recv reads a message from the stream, returning io.EOF when all the messages have been read.

type StreamOption

type StreamOption func(*streamOptions)

StreamOption adapts the behavior of the Stream.

func WithDelimiter

func WithDelimiter(delimiter string) StreamOption

WithDelimiter overrides the delimiter for the Stream.

By default, the Stream is newline-delimited.

type StreamParams

type StreamParams struct {
	URL          string
	Method       string
	Delimiter    string
	MaxAttempts  uint
	Headers      http.Header
	Client       HTTPClient
	Request      interface{}
	ErrorDecoder ErrorDecoder
}

StreamParams represents the parameters used to issue an API streaming call.

type Streamer

type Streamer[T any] struct {
	// contains filtered or unexported fields
}

Streamer calls APIs and streams responses using a *Stream.

func NewStreamer

func NewStreamer[T any](caller *Caller) *Streamer[T]

NewStreamer returns a new *Streamer backed by the given caller's HTTP client.

func (*Streamer[T]) Stream

func (s *Streamer[T]) Stream(ctx context.Context, params *StreamParams) (*Stream[T], error)

Stream issues an API streaming call according to the given stream parameters.

type TokenOption added in v2.5.2

type TokenOption struct {
	Token string
}

TokenOption implements the RequestOption interface.

Jump to

Keyboard shortcuts

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