api

package module
v0.0.0-...-723ec97 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2025 License: MIT Imports: 8 Imported by: 0

README

go-API

This package defines the common interfaces and boilerplate for creating api clients for various APIs.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Warning = errors.New("warning")

this can be users to indicate errors that are more like warning and should be logged, but should not stop execution flow.

Functions

This section is empty.

Types

type BaseClient

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

func NewClient

func NewClient(host, baseEndpoint string, timeout time.Duration, doers ...Middleware) *BaseClient

NewClient creates a new BaseClient The (optional) Middleware Do funcs will run in sequence when the Do func is called with a request. The baseEndpoint is not added automatically with Do or Get etc... It is added in if you use the BaseClient.NewURL to generate your new URL. This allows the client to be used with or without assuming the base part. This is useful if you are using the "links" part of responses which already have the base part in them.

func (BaseClient) Do

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

Do calles the Do func that was built up from the middleware

func (BaseClient) Get

func (c BaseClient) Get(ctx context.Context, endpoint string) (*http.Response, error)

Get is similar to http.Client{}.Get except it uses the BearerTokenClient and defaults to JSON as the payload to and from the server.

func (BaseClient) NewRequestWithContext

func (c BaseClient) NewRequestWithContext(ctx context.Context, method, endpoint string, body io.Reader) (*http.Request, error)

NewRequestWithContext wraps the http version and sets the url. This allows the endpoint being passed to not include the host or base part of the url.

func (BaseClient) NewURL

func (c BaseClient) NewURL(endpoint string) (*url.URL, error)

type Client

type Client interface {
	NewURL(endpoint string) (*url.URL, error)
	NewRequestWithContext(ctx context.Context, method, endpoint string, body io.Reader) (*http.Request, error)
	Get(ctx context.Context, endpoint string) (*http.Response, error)
	Do(req *http.Request) (*http.Response, error)
}

type Dofn

type Dofn func(req *http.Request) (*http.Response, error)

Dofn is the func def for the http.Client{}.Do func

type Middleware

type Middleware func(doer Dofn) Dofn

type ParseError

type ParseError struct {
	Raw []byte
	Err error
}

func (ParseError) Error

func (pe ParseError) Error() string

Directories

Path Synopsis
client

Jump to

Keyboard shortcuts

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