Documentation
¶
Overview ¶
Implements a generic REST API client which can be used for creating gateway-specific clients. Basic usage:
package main import ( client "github.com/mutablelogic/go-client" ) func main() { // Create a new client c := client.New(client.OptEndpoint("https://api.example.com/api/v1")) // Send a GET request, populating a struct with the response var response struct { Message string `json:"message"` } if err := c.Do(nil, &response, OptPath("test")); err != nil { // Handle error } // Print the response fmt.Println(response.Message) }
Index ¶
- Constants
- Variables
- func NewLogTransport(w io.Writer, parent http.RoundTripper, verbose bool) http.RoundTripper
- type Client
- type ClientOpt
- func OptEndpoint(value string) ClientOpt
- func OptHeader(key, value string) ClientOpt
- func OptParent(v any) ClientOpt
- func OptRateLimit(value float32) ClientOpt
- func OptReqToken(value Token) ClientOpt
- func OptSkipVerify() ClientOpt
- func OptStrict() ClientOpt
- func OptTimeout(value time.Duration) ClientOpt
- func OptTrace(w io.Writer, verbose bool) ClientOpt
- func OptUserAgent(value string) ClientOpt
- type Payload
- func NewFormRequest(payload any, accept string) (Payload, error)
- func NewJSONRequest(payload any) (Payload, error)
- func NewJSONRequestEx(method string, payload any, accept string) (Payload, error)
- func NewMultipartRequest(payload any, accept string) (Payload, error)
- func NewRequest() Payload
- func NewRequestEx(method, accept string) Payload
- type RequestOpt
- type Token
- type Unmarshaler
Constants ¶
const ( DefaultTimeout = time.Second * 30 DefaultUserAgent = "github.com/mutablelogic/go-client" PathSeparator = string(os.PathSeparator) ContentTypeAny = "*/*" ContentTypeJson = "application/json" ContentTypeJsonStream = "application/x-ndjson" ContentTypeTextXml = "text/xml" ContentTypeApplicationXml = "application/xml" ContentTypeTextPlain = "text/plain" ContentTypeTextHTML = "text/html" ContentTypeBinary = "application/octet-stream" )
const (
Bearer = "Bearer"
)
Variables ¶
var ( MethodGet = NewRequestEx(http.MethodGet, ContentTypeAny) MethodDelete = NewRequestEx(http.MethodDelete, ContentTypeAny) )
Functions ¶
func NewLogTransport ¶
func NewLogTransport(w io.Writer, parent http.RoundTripper, verbose bool) http.RoundTripper
NewLogTransport creates middleware into the request/response so you can log the transmission on the wire. Setting verbose to true also displays the body of each response
Types ¶
type Client ¶
type Client struct { sync.Mutex *http.Client // Parent object for client options Parent any // contains filtered or unexported fields }
func New ¶
New creates a new client with options. OptEndpoint is required as an option to set the endpoint for all requests.
func (*Client) Do ¶
func (client *Client) Do(in Payload, out any, opts ...RequestOpt) error
Do a JSON request with a payload, populate an object with the response and return any errors
func (*Client) DoWithContext ¶
func (client *Client) DoWithContext(ctx context.Context, in Payload, out any, opts ...RequestOpt) error
Do a JSON request with a payload, populate an object with the response and return any errors. The context can be used to cancel the request
type ClientOpt ¶
func OptEndpoint ¶
OptEndpoint sets the endpoint for all requests.
func OptParent ¶ added in v1.0.2
OptParent sets the parent client for this client, which is used for setting additional client options in the parent
func OptRateLimit ¶
OptRateLimit sets the limit on number of requests per second and the API will sleep when exceeded. For account tokens this is 1 per second
func OptReqToken ¶
OptReqToken sets a request token for all client requests. This can be overridden by the client for individual requests using OptToken.
func OptSkipVerify ¶
func OptSkipVerify() ClientOpt
OptSkipVerify skips TLS certificate domain verification
func OptStrict ¶
func OptStrict() ClientOpt
OptStrict turns on strict content type checking on anything returned from the API
func OptTimeout ¶
OptTimeout sets the timeout on any request. By default, a timeout of 10 seconds is used if OptTimeout is not set
func OptTrace ¶
OptTrace allows you to be the "man in the middle" on any requests so you can see traffic move back and forth. Setting verbose to true also displays the JSON response
func OptUserAgent ¶
OptUserAgent sets the user agent string on each API request It is set to the default if empty string is passed
type Payload ¶
func NewFormRequest ¶
Return a new request with a Form data payload which defaults to POST. The accept parameter is the accepted mime-type of the response.
func NewJSONRequest ¶
Return a new request with a JSON payload which defaults to POST.
func NewJSONRequestEx ¶
Return a new request with a JSON payload with method. The accept parameter is the accepted mime-type of the response.
func NewMultipartRequest ¶
Return a new request with a Multipart Form data payload which defaults to POST. The accept parameter is the accepted mime-type of the response.
func NewRequestEx ¶
Return a new empty request. The accept parameter is the accepted mime-type of the response.
type RequestOpt ¶
type RequestOpt func(*requestOpts) error
func OptNoTimeout ¶
func OptNoTimeout() RequestOpt
OptNoTimeout disables the timeout for this request, useful for long-running requests. The context can be used instead for cancelling requests
func OptPath ¶
func OptPath(value ...string) RequestOpt
OptPath appends path elements onto a request
func OptQuery ¶
func OptQuery(value url.Values) RequestOpt
OptQuery adds query parameters to a request
func OptReqEndpoint ¶
func OptReqEndpoint(value string) RequestOpt
OptReqEndpoint modifies the request endpoint for this request only
func OptReqHeader ¶
func OptReqHeader(name, value string) RequestOpt
OptReqHeader adds a header value to the request
func OptResponse ¶
func OptResponse(fn func() error) RequestOpt
OptResponse calls a function when a response has been decoded, used for streaming responses. The function can return an error to stop the request immediately
func OptToken ¶
func OptToken(value Token) RequestOpt
OptToken adds an authorization header. The header format is "Authorization: Bearer <token>"
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
pkg
|
|
anthropic
anthropic implements an API client for anthropic (https://docs.anthropic.com/en/api/getting-started)
|
anthropic implements an API client for anthropic (https://docs.anthropic.com/en/api/getting-started) |
bitwarden
bitwarden implements an API client for bitwarden
|
bitwarden implements an API client for bitwarden |
elevenlabs
elevenlabs implements an API client for elevenlabs (https://elevenlabs.io/docs/api-reference/text-to-speech)
|
elevenlabs implements an API client for elevenlabs (https://elevenlabs.io/docs/api-reference/text-to-speech) |
homeassistant
homeassistant implements an API client for Home Assistant API https://developers.home-assistant.io/docs/api/rest/
|
homeassistant implements an API client for Home Assistant API https://developers.home-assistant.io/docs/api/rest/ |
ipify
ipify implements a generic API client which parses a JSON response.
|
ipify implements a generic API client which parses a JSON response. |
mistral
mistral implements an API client for mistral (https://docs.mistral.ai/api/)
|
mistral implements an API client for mistral (https://docs.mistral.ai/api/) |
newsapi
newsapi implements an API client for NewsAPI (https://newsapi.org/docs)
|
newsapi implements an API client for NewsAPI (https://newsapi.org/docs) |
ollama
ollama implements an API client for ollama https://github.com/ollama/ollama/blob/main/docs/api.md
|
ollama implements an API client for ollama https://github.com/ollama/ollama/blob/main/docs/api.md |
openai
openai implements an API client for OpenAI https://platform.openai.com/docs/api-reference
|
openai implements an API client for OpenAI https://platform.openai.com/docs/api-reference |