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
- type Client
- func (client *Client) Debugf(f string, args ...any)
- func (client *Client) Do(in Payload, out any, opts ...RequestOpt) error
- func (client *Client) DoWithContext(ctx context.Context, in Payload, out any, opts ...RequestOpt) error
- func (client *Client) Request(req *http.Request, out any, opts ...RequestOpt) error
- func (client *Client) String() string
- 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 JsonStreamCallback
- 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
- func OptJsonStreamCallback(fn JsonStreamCallback) RequestOpt
- func OptNoTimeout() RequestOpt
- func OptPath(value ...string) RequestOpt
- func OptQuery(value url.Values) RequestOpt
- func OptReqEndpoint(value string) RequestOpt
- func OptReqHeader(name, value string) RequestOpt
- func OptTextStreamCallback(fn TextStreamCallback) RequestOpt
- func OptToken(value Token) RequestOpt
- type TextStream
- type TextStreamCallback
- type TextStreamEvent
- type Token
- type Unmarshaler
Constants ¶
const ( DefaultTimeout = time.Second * 30 DefaultUserAgent = "github.com/mutablelogic/go-client" PathSeparator = string(os.PathSeparator) ContentTypeAny = "*/*" ContentTypeJson = "application/json" ContentTypeTextXml = "text/xml" ContentTypeApplicationXml = "application/xml" ContentTypeTextPlain = "text/plain" ContentTypeTextHTML = "text/html" ContentTypeBinary = "application/octet-stream" )
const (
Bearer = "Bearer"
)
const (
// Mime type for text stream
ContentTypeTextStream = "text/event-stream"
)
Variables ¶
var ( MethodGet = NewRequestEx(http.MethodGet, ContentTypeAny) MethodDelete = NewRequestEx(http.MethodDelete, ContentTypeAny) MethodPut = NewRequestEx(http.MethodPut, ContentTypeAny) )
Functions ¶
This section is empty.
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 JsonStreamCallback ¶ added in v1.0.9
Callback for json stream events, return an error if you want to stop streaming with an error and io.EOF if you want to stop streaming and return success
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 OptJsonStreamCallback ¶ added in v1.0.9
func OptJsonStreamCallback(fn JsonStreamCallback) RequestOpt
OptJsonStreamCallback is called for each decoded JSON event
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 sets a header value to the request
func OptTextStreamCallback ¶ added in v1.0.5
func OptTextStreamCallback(fn TextStreamCallback) RequestOpt
OptTextStreamCallback is called for each event in a text stream
func OptToken ¶
func OptToken(value Token) RequestOpt
OptToken adds an authorization header. The header format is "Authorization: Bearer <token>"
type TextStream ¶ added in v1.0.5
type TextStream struct {
// contains filtered or unexported fields
}
Implementation of a text stream, as per https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
func NewTextStream ¶ added in v1.0.5
func NewTextStream() *TextStream
Create a new text stream decoder
func (*TextStream) Decode ¶ added in v1.0.5
func (t *TextStream) Decode(r io.Reader, callback TextStreamCallback) error
Decode a text stream. The reader should be a stream of text/event-stream data and the method will return when all the data has been scanned, or the callback returns an error
type TextStreamCallback ¶ added in v1.0.5
type TextStreamCallback func(TextStreamEvent) error
Callback for text stream events, return an error if you want to return from the Decode method
type TextStreamEvent ¶ added in v1.0.5
type TextStreamEvent struct { // The event ID to set the EventSource object's last event ID value. Id string `json:"id,omitempty"` // A string identifying the type of event described Event string `json:"event,omitempty"` // The data field for the message Data string `json:"data"` // The reconnection time. If the connection to the server is lost, // the client should wait for the specified time before attempting to reconnect. Retry time.Duration `json:"retry,omitempty"` }
Implementation of a text stream, as per https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format
func (*TextStreamEvent) IsZero ¶ added in v1.0.5
func (t *TextStreamEvent) IsZero() bool
Return true if the event contains no content
func (*TextStreamEvent) Json ¶ added in v1.0.5
func (t *TextStreamEvent) Json(v any) error
Decode the text stream event data as JSON
func (TextStreamEvent) String ¶ added in v1.0.5
func (t TextStreamEvent) String() string
Return the text stream event as a string
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
etc
|
|
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 |
weatherapi
weatherapi implements an API client for WeatherAPI (https://www.weatherapi.com/docs/)
|
weatherapi implements an API client for WeatherAPI (https://www.weatherapi.com/docs/) |