api

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrApiKeyInvalid = errors.New("invalid api key")
	ErrNotFound      = errors.New("resource not found")
	ErrRateLimited   = errors.New("rate limit exceeded")
)

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Code   int         `json:"code"`
	Detail string      `json:"detail"`
	Title  string      `json:"title"`
	Status int         `json:"status,omitempty"`
	Errors []ErrorItem `json:"errors,omitempty" xml:"errors,omitempty"`
}

func (*APIError) GetError

func (a *APIError) GetError() ErrorItem

type ErrorItem

type ErrorItem struct {
	Name   string         `json:"name" description:"For example, name of the parameter that caused the error"`
	Reason string         `json:"reason" description:"Human readable error message"`
	More   map[string]any `json:"more,omitempty" description:"Additional information about the error"`
}

type ForbiddenError added in v1.2.0

type ForbiddenError struct {
	Code  int    `json:"code"`
	Error string `json:"error"`
}

type HTTPAPI

type HTTPAPI struct {
	BaseURL string

	APIKey string
	// contains filtered or unexported fields
}

HTTPAPI represents an HTTP API client

func NewHTTPAPI

func NewHTTPAPI(baseURL, apiKey string, client ...*http.Client) (*HTTPAPI, error)

NewHTTPAPI creates a new API client with default rate limit of 100 reqs/min

func (*HTTPAPI) DeleteJSON

func (g *HTTPAPI) DeleteJSON(ctx context.Context, uri string, post interface{}, dest interface{}, options ...ReqOptions) (interface{}, error)

func (*HTTPAPI) GetJSON

func (g *HTTPAPI) GetJSON(ctx context.Context, uri string, dest interface{}, options ...ReqOptions) (interface{}, error)

func (*HTTPAPI) PostJSON

func (g *HTTPAPI) PostJSON(ctx context.Context, uri string, post interface{}, dest interface{}, options ...ReqOptions) (interface{}, error)

func (*HTTPAPI) PutJSON

func (g *HTTPAPI) PutJSON(ctx context.Context, uri string, post interface{}, dest interface{}, options ...ReqOptions) (interface{}, error)

func (*HTTPAPI) ReqJSON

func (g *HTTPAPI) ReqJSON(ctx context.Context, method string, uri string, post interface{}, dest interface{}, options ...ReqOptions) (interface{}, error)

func (*HTTPAPI) ResolveBase

func (g *HTTPAPI) ResolveBase(base *url.URL, uri string) string

type RateInfo

type RateInfo struct {
	Limit      int    // x-ratelimit-limit
	Remaining  int    // x-ratelimit-remaining
	Used       int    // x-ratelimit-used
	Reset      int64  // x-ratelimit-reset
	RetryAfter int64  // x-ratelimit-retry-after
	Resource   string // x-ratelimit-resource
}

RateInfo contains information about API rate limits

type RateLimiter

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

RateLimiter wraps the golang.org/x/time/rate Limiter

func NewRateLimiter

func NewRateLimiter(requestsPerMinute int) *RateLimiter

NewRateLimiter creates a new rate limiter with default values

func (*RateLimiter) Allow

func (r *RateLimiter) Allow() bool

Allow checks if a request is allowed based on rate limits and consumes a token if available

func (*RateLimiter) IsAllowed

func (r *RateLimiter) IsAllowed() (bool, time.Duration)

IsAllowed checks if a request is allowed based on rate limits without blocking Returns true if allowed, false if rate limited, and duration to wait if limited

func (*RateLimiter) RemainingTokens

func (r *RateLimiter) RemainingTokens() float64

RemainingTokens returns an estimate of the number of remaining requests

func (*RateLimiter) Wait

func (r *RateLimiter) Wait(ctx context.Context) error

Wait blocks until a request can be allowed or the context is done

func (*RateLimiter) WaitMaxDuration

func (r *RateLimiter) WaitMaxDuration(ctx context.Context, maxWait time.Duration) error

WaitMaxDuration tries to wait for a token but only up to the specified max duration

type ReqOptions

type ReqOptions struct {
	Params  url.Values
	Headers url.Values
}

Jump to

Keyboard shortcuts

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