clients

package
v4.13.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NoopTokenProvider is a token provider that returns an empty string which is ignored by `DoRequest`.
	NoopTokenProvider = TokenProvider(func() (token string, err error) { return "", nil })
)

Functions

func GetStatusCode added in v4.4.0

func GetStatusCode(err error) int

GetStatusCode returns the api error code, if it exists. When err is nil, it always returns 200.

func IsRetryable added in v4.4.0

func IsRetryable(_ *http.Response, err error) bool

IsRetryable is the default test to check if the client should retry a request.

Types

type APIError

type APIError struct {
	// Status is the HTTP status of the API response
	Status int
	// Header is the set of response headers
	Header http.Header
	// Response is the bytes of the response body
	Response []byte
}

APIError describes an error during unsuccessful HTTP request to an API

func (APIError) Error

func (e APIError) Error() (message string)

Error implements the error interface. Builds a complete error message out of all the errors served in the API response. This should be used in logging, tracing, debugging, etc. For user facing errors use `ValidationErrors` function.

func (APIError) ResponseErrors

func (e APIError) ResponseErrors() (errs []error)

ResponseErrors returns a slice of all errors that were present in the API response. All the field errors are folded into a single validation error map. All the general errors are mapped to regular errors. Returns `nil` if the response contained no errors in the expected JSON format. For the 522 status code it handles a special case where all the general errors become validation errors for the `connection` field.

func (APIError) ValidationErrors

func (e APIError) ValidationErrors() (errs cerrors.ValidationErrors)

ValidationErrors returns a map of validation errors in case they were present in response errors. Otherwise, returns `nil` if there were no validation errors in the response.

type BaseAPIClient

type BaseAPIClient interface {
	// GetBaseURL returns the base URL of the service which can be used in HTTP request tasks.
	GetBaseURL() string
	// DoRequest performs the HTTP request with the given parameters, marshals the payload and
	// unmarshals the response into the given output if the status code is successful
	DoRequest(ctx context.Context, method, path string, query url.Values, payload, out interface{}) error

	// DoRequestWithResponse performs the HTTP request with the given parameters, marshals the payload, parses the standard error cases
	// and returns the http.Response for success cases. This allows standard request object for advanced use-cases.
	//
	// Callers should generally prefer DoRequest.
	DoRequestWithResponse(ctx context.Context, method, path string, query url.Values, payload interface{}) (*http.Response, error)

	// WithTokenProvider returns a new BaseAPIClient, replacing the current TokenProvider with the one provided.
	WithTokenProvider(tokenProvider TokenProvider) BaseAPIClient
	// WithHeader returns a new BaseAPIClient, replacing the current set of headers with the one provided.
	// 	"Content-Type" and `tokenHeaderName` headers will be always overridden by the client.
	// If the TokenProvider returns a non-empty token it will be set as a `tokenHeaderName`-named header
	// overriding the matching header in the this set.
	WithHeader(http.Header) BaseAPIClient

	// WithRetry adds logic to automatically retry requests on specific error cases.
	// The default implementation returns on the first error.
	//
	// The typical plans to user are backoff.NewConstantBackoff() or backoff.NewExponentialBackoff().
	//
	// Setting maxAttempts = 0 is the same as setting maxAttempts = 1, it will run the requests exactly once.
	// Use testRetryable = nil for the default implementation implemented in IsRetryable
	WithRetry(plan backoff.BackOff, maxAttempts uint64, testRetryable func(*http.Response, error) bool) BaseAPIClient
}

BaseAPIClient describes all basic HTTP client operations required to work with a JSON API

func NewBaseAPIClient

func NewBaseAPIClient(basePath, tokenHeaderName string, tokenProvider TokenProvider, client *http.Client, debug bool) BaseAPIClient

NewBaseAPIClient creates a new instance of the base API client implementation. Never use `debug=true` in production environments, it will leak sensitive data

type TokenProvider

type TokenProvider func() (token string, err error)

TokenProvider is a function that gets the token string for each request

func TokenProviderFromClaims added in v4.2.0

func TokenProviderFromClaims(claims authorization.Claims) TokenProvider

TokenProviderFromClaims creates a TokenProvider that simply returns the original source token that the claims was created from. This can be used to make a request on behalf of the user/client in the claims, but does not extend the timeout, this is only appropriate for synchronous requests, like HTTP calls.

func TokenProviderFromCreator

func TokenProviderFromCreator(tc tokens.Creator, reference string, opts tokens.Options) TokenProvider

TokenProviderFromCreator creates a token provider out of token creator.

Jump to

Keyboard shortcuts

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