api

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package api provides the api client

Index

Constants

View Source
const (
	// APIBasePath is the entry point of the API
	APIBasePath = "/api/v0/"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is the low-level HTTP API client

func New

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

New creates a new low-level API client using functional options

func (*Client) DeleteJSON

func (c *Client) DeleteJSON(ctx context.Context, endpoint string, out any) error

DeleteJSON makes a DELETE request with JSON handling

func (*Client) GetJSON

func (c *Client) GetJSON(ctx context.Context, endpoint string, query map[string]string, out any) error

GetJSON makes a GET request with JSON handling

func (*Client) PatchJSON

func (c *Client) PatchJSON(ctx context.Context, endpoint string, query map[string]string, in, out any) error

PatchJSON makes a PATCH request with JSON handling

func (*Client) PostJSON

func (c *Client) PostJSON(ctx context.Context, endpoint string, query map[string]string, in, out any) error

PostJSON makes a POST request with JSON handling

func (*Client) PutJSON

func (c *Client) PutJSON(ctx context.Context, endpoint string, in any) error

PutJSON makes a PUT request with JSON handling

func (*Client) Request

func (c *Client) Request(ctx context.Context, method, endpoint string, query map[string]string, body any) (*http.Response, error)

Request makes a raw HTTP request to the API

func (*Client) SetClientInfo

func (c *Client) SetClientInfo(id, uuid, version string)

SetClientInfo configures client-identification headers.

Empty values mean "do not set" for that header.

func (*Client) Stats

func (c *Client) Stats() *models.Stats

Stats returns current statistics snapshot

type DoFunc

type DoFunc func(*http.Request) (*http.Response, error)

DoFunc represents the signature for making HTTP requests

type HTTPError

type HTTPError struct {
	StatusCode int
}

HTTPError represents an HTTP error

func (*HTTPError) Error

func (e *HTTPError) Error() string

type Middleware

type Middleware func(DoFunc) DoFunc

Middleware wraps a DoFunc to provide additional functionality

func LogRequestBodyMiddleware

func LogRequestBodyMiddleware(logger *slog.Logger) Middleware

LogRequestBodyMiddleware logs request bodies for debugging

func LoggingMiddleware

func LoggingMiddleware(logger *slog.Logger) Middleware

LoggingMiddleware logs HTTP requests and responses

func RetryMiddleware

func RetryMiddleware(policy RetryPolicy) Middleware

RetryMiddleware implements retry logic with exponential backoff

func StatsMiddleware

func StatsMiddleware(stats *Stats) Middleware

StatsMiddleware collects API call statistics

func UserAgentMiddleware

func UserAgentMiddleware(userAgent string) Middleware

UserAgentMiddleware adds a User-Agent header to requests

type Option

type Option func(*Options)

Option is a functional option for configuring the API client

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient sets the HTTP client

func WithMiddlewares

func WithMiddlewares(middlewares ...Middleware) Option

WithMiddlewares sets the middleware chain

func WithStats

func WithStats() Option

WithStats enables statistics collection

type Options

type Options struct {
	HTTPClient  *http.Client
	Middlewares []Middleware
	EnableStats bool
}

Options configures the API client

type RetryPolicy

type RetryPolicy struct {
	MaxRetries    int
	InitialDelay  time.Duration
	MaxDelay      time.Duration
	BackoffFactor float64
}

RetryPolicy defines retry behavior

func DefaultRetryPolicy

func DefaultRetryPolicy() RetryPolicy

DefaultRetryPolicy returns a sensible default retry policy

type Stats

type Stats struct {
	models.Stats
	// contains filtered or unexported fields
}

Stats holds basic API call statistics

func NewStats

func NewStats() *Stats

NewStats creates a new stats instance

func (*Stats) GetSnapshot

func (s *Stats) GetSnapshot() *models.Stats

GetSnapshot returns a thread-safe copy of current stats

func (*Stats) RecordCall

func (s *Stats) RecordCall(method, endpoint string, status int, duration time.Duration)

RecordCall records a single API call

Jump to

Keyboard shortcuts

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