api

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 32 Imported by: 2

Documentation

Index

Constants

View Source
const (
	JSON       = "application/json"
	URLEncoded = "application/x-www-form-urlencoded"
	Multipart  = "multipart/form-data"
	PlainText  = "text/plain"
)

Variables

View Source
var (
	ErrUnsupportedMimetype        = errors.New("Unsupported content type")
	ErrUnsupportedContentEncoding = errors.New("Unsupported content encoding")
	ErrUnexpectedStatusCode       = errors.New("Unexpected status code")
	ErrCouldNotAuthorize          = errors.New("Could not authorize request")
	ErrCouldNotUnmarshalResponse  = errors.New("Could not unmarshal response")
)
View Source
var (
	ErrNotFound            = errors.New("Not found")
	ErrBadRequest          = errors.New("Bad request")
	ErrUnauthorized        = errors.New("Unauthorized")
	ErrForbidden           = errors.New("Forbidden")
	ErrUnprocessableEntity = errors.New("Unprocessable entity")
	ErrInternalServerError = errors.New("Internal server error")
)

Sentinal errors are wrapped to provide a simpler test for common conditions that are related to response status codes.

Functions

func Delete

func Delete(cxt context.Context, u string, input, output interface{}) (*http.Response, error)

A convenience for Exec with a DELETE request

func Get

func Get(cxt context.Context, u string, entity interface{}) (*http.Response, error)

A convenience for Exec with a GET request

func Marshal

func Marshal(ctype string, entity interface{}) (io.Reader, error)

func Post

func Post(cxt context.Context, u string, input, output interface{}) (*http.Response, error)

A convenience for Exec with a POST request

func Put

func Put(cxt context.Context, u string, input, output interface{}) (*http.Response, error)

A convenience for Exec with a PUT request

func URLWithParams

func URLWithParams(s string, params interface{}) (string, error)

func Unmarshal

func Unmarshal(rsp *http.Response, entity interface{}) error

Types

type Authorizer

type Authorizer interface {
	Authorize(*http.Request) error
}

An authorizer authorizes requests

type BasicAuthorizer

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

func NewBasicAuthorizer

func NewBasicAuthorizer(u, p string) BasicAuthorizer

func (BasicAuthorizer) Authorize

func (a BasicAuthorizer) Authorize(req *http.Request) error

type BearerAuthorizer

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

func NewBearerAuthorizer

func NewBearerAuthorizer(t string) BearerAuthorizer

func (BearerAuthorizer) Authorize

func (a BearerAuthorizer) Authorize(req *http.Request) error

type Client

type Client struct {
	*http.Client
	// contains filtered or unexported fields
}

An API client

func New

func New(opts ...Option) (*Client, error)

Create a new client

func NewWithConfig

func NewWithConfig(conf Config) (*Client, error)

Create a new client with a configuration

func (*Client) Authorizer

func (c *Client) Authorizer() Authorizer

func (*Client) Base

func (c *Client) Base() *url.URL

func (*Client) Delete

func (c *Client) Delete(cxt context.Context, u string, input, output interface{}, opts ...Option) (*http.Response, error)

A convenience for Exec with a DELETE request

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Perform a request. The client may mutate the parameter request.

func (*Client) Exec

func (c *Client) Exec(req *http.Request, entity interface{}, opts ...Option) (*http.Response, error)

Perform a request and attempt to unmarshal the response into an entity.

func (*Client) Get

func (c *Client) Get(cxt context.Context, u string, output interface{}, opts ...Option) (*http.Response, error)

A convenience for Exec with a GET request

func (*Client) Log added in v0.6.0

func (c *Client) Log() *slog.Logger

func (*Client) Observers

func (c *Client) Observers() *events.Observers

func (*Client) Patch

func (c *Client) Patch(cxt context.Context, u string, input, output interface{}, opts ...Option) (*http.Response, error)

A convenience for Exec with a PATCH request. This is the same as PUT and it is included for the benefit of those misguided APIs that use PATCH operations.

func (*Client) Post

func (c *Client) Post(cxt context.Context, u string, input, output interface{}, opts ...Option) (*http.Response, error)

A convenience for Exec with a POST request

func (*Client) Put

func (c *Client) Put(cxt context.Context, u string, input, output interface{}, opts ...Option) (*http.Response, error)

A convenience for Exec with a PUT request

func (*Client) RoundTrip

func (c *Client) RoundTrip(req *http.Request) (*http.Response, error)

Route-trip a request. The client may mutate the parameter request.

func (*Client) WithAuthorizer

func (c *Client) WithAuthorizer(a Authorizer) *Client

func (*Client) WithBase

func (c *Client) WithBase(b *url.URL) *Client

func (*Client) WithObservers

func (c *Client) WithObservers(o *events.Observers) *Client

type Config

type Config struct {
	Name        string
	BaseURL     string
	Timeout     time.Duration
	Client      *http.Client
	Authorizer  Authorizer
	Observers   *events.Observers
	RateLimiter ratelimit.Limiter
	RetryStatus []int
	RetryDelay  time.Duration
	Header      http.Header
	ContentType string
	Logger      *slog.Logger
	Verbose     bool
	Debug       bool
}

Client configuration

func (Config) With

func (c Config) With(opts []Option) Config

func (Config) WithOptions

func (c Config) WithOptions(opts []Option) Config

type Debug

type Debug struct {
	Debug     bool
	Verbose   bool
	FilterURL *regexp.Regexp
}

func (Debug) Matches

func (d Debug) Matches(req *http.Request) bool

func (Debug) WithEnv

func (d Debug) WithEnv() (Debug, error)

type Entity

type Entity struct {
	ContentType string
	Data        []byte
}

func (Entity) String

func (e Entity) String() string

type EntityMarshaler

type EntityMarshaler interface {
	MarshalEntity() ([]byte, error)
}

type EntityUnmarshaler

type EntityUnmarshaler interface {
	UnmarshalEntity(string, []byte) error
}

type Error

type Error struct {
	ReqId   int64
	Status  int
	Method  string
	URL     string
	Entity  *Entity
	Message string
	Cause   error
}

func Errorf

func Errorf(s int, f string, a ...interface{}) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Redacted

func (e *Error) Redacted() error

func (*Error) SetCause

func (e *Error) SetCause(err error) *Error

func (*Error) SetEntity

func (e *Error) SetEntity(ent *Entity) *Error

func (*Error) SetEntityFromResponse

func (e *Error) SetEntityFromResponse(rsp *http.Response) *Error

func (*Error) SetId

func (e *Error) SetId(id int64) *Error

func (*Error) SetRequest

func (e *Error) SetRequest(req *http.Request) *Error

func (*Error) Unwrap

func (e *Error) Unwrap() error

type HeaderAuthorizer

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

func NewHeaderAuthorizer

func NewHeaderAuthorizer(h http.Header) HeaderAuthorizer

func (HeaderAuthorizer) Authorize

func (a HeaderAuthorizer) Authorize(req *http.Request) error

type OAuthAuthorizer

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

func NewOAuthAuthorizer

func NewOAuthAuthorizer(src oauth2.TokenSource) OAuthAuthorizer

func (OAuthAuthorizer) Authorize

func (a OAuthAuthorizer) Authorize(req *http.Request) error

func (OAuthAuthorizer) Token

func (a OAuthAuthorizer) Token() (*oauth2.Token, error)

type Option

type Option func(Config) Config

func WithAuthorizer

func WithAuthorizer(auth Authorizer) Option

func WithBaseURL

func WithBaseURL(base string) Option

func WithDebug

func WithDebug(on bool) Option

func WithHeader

func WithHeader(key, val string) Option

func WithHeaders

func WithHeaders(hdr http.Header) Option

func WithLogger added in v0.6.0

func WithLogger(log *slog.Logger) Option

func WithName added in v0.6.0

func WithName(name string) Option

func WithObservers

func WithObservers(obs ...interface{}) Option

func WithRateLimiter

func WithRateLimiter(l ratelimit.Limiter) Option

func WithRetryDelay

func WithRetryDelay(d time.Duration) Option

func WithRetryStatus

func WithRetryStatus(s ...int) Option

type QueryAuthorizer

type QueryAuthorizer struct {
	Params url.Values
}

func NewQueryAuthorizer

func NewQueryAuthorizer(params url.Values) QueryAuthorizer

func (QueryAuthorizer) Authorize

func (a QueryAuthorizer) Authorize(req *http.Request) error

Directories

Path Synopsis
The events interface provides a mechanism to observe events for an API client in a central place.
The events interface provides a mechanism to observe events for an API client in a central place.

Jump to

Keyboard shortcuts

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