jsonclient

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractBearerToken

func ExtractBearerToken(h *http.Header) (string, error)

ExtractBearerToken extracts a bearer token from an HTTP request or returns an error if none is found or if it's malformed. NOTE: this doesn't enforce that it's a JWT, much less a valid one.

func GetHTTPStatusCode

func GetHTTPStatusCode(err error) int

GetHTTPStatusCode returns the underlying HTTP status code or -1 if no code could be extracted.

Types

type Client

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

Client defines a JSON-focused HTTP client TODO: someday this should use a custom http.Client etc

func New

func New(url string, opts ...Option) *Client

New returns a new Client

func (*Client) Apply

func (c *Client) Apply(opts ...Option)

Apply applies options to an existing client (useful for updating a header/cookie/etc on an existing client).

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, path string, opts ...Option) error

Delete makes an HTTP delete using this client

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, response interface{}, opts ...Option) error

Get makes an HTTP get using this client TODO: need to support query params soon :)

func (*Client) GetBearerToken

func (c *Client) GetBearerToken() (string, error)

GetBearerToken returns the bearer token associated with this client, if one exists, or an error otherwise.

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, path string, body, response interface{}, opts ...Option) error

Patch makes an HTTP patch using this client If response is nil, the response isn't decoded and merely the success or failure is returned

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, body, response interface{}, opts ...Option) error

Post makes an HTTP post using this client If response is nil, the response isn't decoded and merely the success or failure is returned

func (*Client) Put

func (c *Client) Put(ctx context.Context, path string, body, response interface{}, opts ...Option) error

Put makes an HTTP put using this client If response is nil, the response isn't decoded and merely the success or failure is returned

func (*Client) RefreshBearerToken

func (c *Client) RefreshBearerToken() error

RefreshBearerToken checks if the current token is invalid or expired, and refreshes it via the Client Credentials Flow if needed.

func (*Client) ValidateBearerTokenHeader

func (c *Client) ValidateBearerTokenHeader() error

ValidateBearerTokenHeader ensures that there is a non-expired bearer token specified directly OR that there's a valid token source to refresh it if not specified or expired.

type DecodeFunc

type DecodeFunc func(ctx context.Context, body io.ReadCloser) error

DecodeFunc is a callback used with the CustomDecoder option to control deserializing the response from an HTTP request. Instead of automatically deserializing into the response object provided to the method (which must be nil instead), this method is invoked.

type Error

type Error struct {
	StatusCode int
}

Error defines a jsonclient error for non-2XX/3XX status codes TODO: decide how to handle 3XX results

func (Error) Error

func (e Error) Error() string

Error implements Error

type HeaderFunc

type HeaderFunc func(context.Context) (key string, value string)

HeaderFunc is a callback that's invoked on every request to generate a header This is useful when the header should change per-request based on the context used for that request, eg. a long-lived client that makes requests on behalf of numerous customers with different X-Forwarded-For IPs, etc Note that returning a blank key indicates "no header to add this request"

type Option

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

Option makes jsonclient extensible

func ClientCredentialsTokenSource

func ClientCredentialsTokenSource(tokenURL, clientID, clientSecret string, customAudiences []string) Option

ClientCredentialsTokenSource can be specified to enable support for RefreshBearerToken automatically refreshing the token if expired. TODO: deprecate this in favor of the more generic TokenSource option

func Cookie(cookie http.Cookie) Option

Cookie allows you to add cookies to jsonclient requests

func CustomDecoder

func CustomDecoder(f DecodeFunc) Option

CustomDecoder allows the caller to control deserializing the HTTP response. It is most useful when the exact structure of the response is not known ahead of time, and custom logic is required (e.g. for API compatibility).

func Header(k, v string) Option

Header allows you to add arbitrary headers to jsonclient requests

func ParseOAuthError

func ParseOAuthError() Option

ParseOAuthError allows deserializing & capturing the last call's error into an OAuthError object for deeper inspection. This is richer than a jsonclient.Error but only makes sense on a call that is expected to be OAuth/OIDC compliant.

func PerRequestHeader

func PerRequestHeader(f HeaderFunc) Option

PerRequestHeader allows you to pass in a callback that takes a context and returns a header k,v that will be called on each request and a new header appended to the request

func StopLogging

func StopLogging() Option

StopLogging causes the client not to log failures

func TokenSource

func TokenSource(ts oidc.TokenSource) Option

TokenSource takes an arbitrary token source

func UnmarshalOnError

func UnmarshalOnError() Option

UnmarshalOnError causes the response struct to be deserialized if a HTTP 400+ code is returned. The default behavior is to not deserialize and to return an error.

Jump to

Keyboard shortcuts

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