internal

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeURL

func EncodeURL(urlFormat string, args ...interface{}) string

EncodeURL encodes the given arguments into the URL, escaping values as needed.

func ExtractExtraProperties

func ExtractExtraProperties(bytes []byte, value interface{}, exclude ...string) (map[string]interface{}, error)

ExtractExtraProperties extracts any extra properties from the given value.

func MarshalJSONWithExtraProperties

func MarshalJSONWithExtraProperties(marshaler interface{}, extraProperties map[string]interface{}) ([]byte, error)

MarshalJSONWithExtraProperties marshals the given value to JSON, including any extra properties.

func MarshalJSONWithExtraProperty

func MarshalJSONWithExtraProperty(marshaler interface{}, key string, value interface{}) ([]byte, error)

MarshalJSONWithExtraProperty marshals the given value to JSON, including the extra property.

func MergeHeaders

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

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

func QueryValues

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 ResolveBaseURL

func ResolveBaseURL(values ...string) string

ResolveBaseURL resolves the base URL from the given arguments, preferring the first non-empty value.

func StringifyJSON

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

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

Types

type CallParams

type CallParams struct {
	URL                string
	Method             string
	MaxAttempts        uint
	Headers            http.Header
	BodyProperties     map[string]interface{}
	QueryParameters    url.Values
	Client             core.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

type CallerParams struct {
	Client      core.HTTPClient
	MaxAttempts uint
}

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

type ContentTyped

type ContentTyped interface {
	ContentType() string
}

ContentTyped is implemented by types that define a Content-Type.

type Date

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

func NewDate(t time.Time) *Date

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

func NewOptionalDate

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

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

func (*Date) Time

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

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

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

func (*Date) UnmarshalJSON

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

type DateTime

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

func NewDateTime(t time.Time) *DateTime

NewDateTime returns a new *DateTime.

func NewOptionalDateTime

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

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

func (*DateTime) Time

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

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

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

func (*DateTime) UnmarshalJSON

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

type ErrorCodes

type ErrorCodes map[int]func(*core.APIError) error

ErrorCodes maps HTTP status codes to error constructors.

type ErrorDecoder

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

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

func NewErrorDecoder

func NewErrorDecoder(errorCodes ErrorCodes) ErrorDecoder

NewErrorDecoder returns a new ErrorDecoder backed by the given error codes.

type HTTPClient

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

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

type MultipartWriter

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

MultipartWriter writes multipart/form-data requests.

func NewMultipartWriter

func NewMultipartWriter() *MultipartWriter

NewMultipartWriter creates a new multipart writer.

func (*MultipartWriter) Buffer

func (w *MultipartWriter) Buffer() *bytes.Buffer

Buffer returns the underlying buffer.

func (*MultipartWriter) Close

func (w *MultipartWriter) Close() error

Close closes the writer.

func (*MultipartWriter) ContentType

func (w *MultipartWriter) ContentType() string

ContentType returns the Content-Type for an HTTP multipart/form-data.

func (*MultipartWriter) WriteField

func (w *MultipartWriter) WriteField(
	field string,
	value string,
	opts ...WriteMultipartOption,
) error

WriteField writes the given value as a form field.

func (*MultipartWriter) WriteFile

func (w *MultipartWriter) WriteFile(
	field string,
	file io.Reader,
	opts ...WriteMultipartOption,
) error

WriteFile writes the given file part.

func (*MultipartWriter) WriteJSON

func (w *MultipartWriter) WriteJSON(
	field string,
	value interface{},
	opts ...WriteMultipartOption,
) error

WriteJSON writes the given value as a JSON form field.

type Named

type Named interface {
	Name() string
}

Named is implemented by types that define a name.

type PageRequest

type PageRequest[Cursor comparable] struct {
	Cursor Cursor

	// Holds the value of the response type (populated by the *Caller).
	Response any
}

PageRequest represents the information required to identify a single page.

type PageRequestFunc

type PageRequestFunc[Cursor comparable] func(request *PageRequest[Cursor]) *CallParams

PageRequestFunc prepares the *CallParams from the given page request.

type PageResponse

type PageResponse[Cursor comparable, Result any] struct {
	Results []Result
	Next    Cursor
	Done    bool
}

PageResponse represents the information associated with a single page.

type PageResponseFunc

type PageResponseFunc[
	Cursor comparable,
	Response any,
	Results any,
] func(Response) *PageResponse[Cursor, Results]

PageResponseFunc extracts the next page information from the response.

type Pager

type Pager[
	Cursor comparable,
	Response any,
	Results any,
] struct {
	// contains filtered or unexported fields
}

Pager is the primary abstraction used to call paginated APIs.

func NewCursorPager

func NewCursorPager[Cursor comparable, Response any, Results any](
	caller *Caller,
	prepareCall PageRequestFunc[Cursor],
	readPageResponse PageResponseFunc[Cursor, Response, Results],
) *Pager[Cursor, Response, Results]

NewCursorPager constructs a new Pager that fetches pages from a paginated endpoint.

func NewOffsetPager

func NewOffsetPager[Cursor comparable, Response any, Results any](
	caller *Caller,
	prepareCall PageRequestFunc[Cursor],
	readPageResponse PageResponseFunc[Cursor, Response, Results],
) *Pager[Cursor, Response, Results]

NewOffsetPager constructs a new Pager that fetches pages from an offset paginated endpoint.

func (*Pager[Cursor, Response, Results]) GetPage

func (p *Pager[
	Cursor,
	Response,
	Results,
]) GetPage(ctx context.Context, cursor Cursor) (*core.Page[Results], error)

GetPage retrieves the page associated with the given cursor.

type PagerMode

type PagerMode uint

PagerMode represents the different types of pagination modes.

const (
	PagerModeCursor PagerMode = iota + 1
	PagerModeOffset
)

The available set of pagination modes.

type QueryEncoder

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 Retrier

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

func NewRetrier(opts ...RetryOption) *Retrier

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

func (*Retrier) Run

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 retryable and the number of retry attempts has not grown larger than the configured retry limit.

type RetryFunc

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

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

type RetryOption

type RetryOption func(*retryOptions)

RetryOption adapts the behavior the *Retrier.

func WithMaxAttempts

func WithMaxAttempts(attempts uint) RetryOption

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

type WriteMultipartOption

type WriteMultipartOption func(*writeMultipartOptions)

WriteMultipartOption adapts the behavior of the multipart writer.

func WithDefaultContentType

func WithDefaultContentType(contentType string) WriteMultipartOption

WithDefaultContentType sets the default Content-Type for the part written to the MultipartWriter.

Note that if the part is a FileParam, the file's Content-Type takes precedence over the value provided here.

Jump to

Keyboard shortcuts

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