api

package
v1.29.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package api provides a flexible HTTP API client with retry logic, rate limiting, and authentication support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIKeyAuth

type APIKeyAuth struct {
	Key    string
	Header string
}

APIKeyAuth implements API key authentication

func (*APIKeyAuth) Authenticate

func (a *APIKeyAuth) Authenticate(req *http.Request) error

func (*APIKeyAuth) Refresh

func (a *APIKeyAuth) Refresh(ctx context.Context) error

type Authenticator

type Authenticator interface {
	// Authenticate adds authentication to a request
	Authenticate(req *http.Request) error

	// Refresh refreshes authentication credentials if needed
	Refresh(ctx context.Context) error
}

Authenticator provides authentication for requests

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

BasicAuth implements HTTP basic authentication

func (*BasicAuth) Authenticate

func (a *BasicAuth) Authenticate(req *http.Request) error

func (*BasicAuth) Refresh

func (a *BasicAuth) Refresh(ctx context.Context) error

type BearerTokenAuth

type BearerTokenAuth struct {
	Token string
}

BearerTokenAuth implements bearer token authentication

func (*BearerTokenAuth) Authenticate

func (a *BearerTokenAuth) Authenticate(req *http.Request) error

func (*BearerTokenAuth) Refresh

func (a *BearerTokenAuth) Refresh(ctx context.Context) error

type Client

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

Client represents an HTTP API client

func NewClient

func NewClient(config ClientConfig) *Client

NewClient creates a new API client

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, path string) (*Response, error)

Delete performs a DELETE request

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *Request) (*Response, error)

Do executes an API request

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, query url.Values) (*Response, error)

Get performs a GET request

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, body interface{}) (*Response, error)

Post performs a POST request

func (*Client) Put

func (c *Client) Put(ctx context.Context, path string, body interface{}) (*Response, error)

Put performs a PUT request

func (*Client) SetHeader

func (c *Client) SetHeader(key, value string)

SetHeader sets a default header

type ClientConfig

type ClientConfig struct {
	BaseURL     string
	Timeout     time.Duration
	Auth        Authenticator
	RateLimit   int // Requests per second
	RetryConfig *RetryConfig
	Headers     map[string]string
}

ClientConfig configures the API client

type Error

type Error struct {
	StatusCode int    `json:"status_code"`
	Message    string `json:"message"`
	Details    string `json:"details,omitempty"`
}

Error represents an API error

func (*Error) Error

func (e *Error) Error() string

type RESTClient

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

RESTClient provides a higher-level REST API client

func NewRESTClient

func NewRESTClient(config ClientConfig) *RESTClient

NewRESTClient creates a new REST client

func (*RESTClient) DeleteJSON

func (r *RESTClient) DeleteJSON(ctx context.Context, path string, result interface{}) error

DeleteJSON performs a DELETE request and decodes response

func (*RESTClient) GetJSON

func (r *RESTClient) GetJSON(ctx context.Context, path string, query url.Values, result interface{}) error

GetJSON performs a GET request and decodes JSON response

func (*RESTClient) Paginate

func (r *RESTClient) Paginate(ctx context.Context, path string, pageSize int, handler func(page interface{}) error) error

Paginate handles paginated API responses

func (*RESTClient) PostJSON

func (r *RESTClient) PostJSON(ctx context.Context, path string, body, result interface{}) error

PostJSON performs a POST request with JSON body and decodes response

func (*RESTClient) PutJSON

func (r *RESTClient) PutJSON(ctx context.Context, path string, body, result interface{}) error

PutJSON performs a PUT request with JSON body and decodes response

type Request

type Request struct {
	Method  string
	Path    string
	Query   url.Values
	Headers map[string]string
	Body    interface{}
}

Request represents an API request

type Response

type Response struct {
	StatusCode int
	Headers    http.Header
	Body       []byte
}

Response represents an API response

type RetryConfig

type RetryConfig struct {
	MaxRetries  int
	InitialWait time.Duration
	MaxWait     time.Duration
	Multiplier  float64
	RetryOn     []int // HTTP status codes to retry on
}

RetryConfig configures retry behavior

func DefaultRetryConfig

func DefaultRetryConfig() *RetryConfig

DefaultRetryConfig returns default retry configuration

Jump to

Keyboard shortcuts

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