http

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package http provides shared HTTP client patterns for integration clients.

Index

Constants

View Source
const DefaultMaxRetries = 3

DefaultMaxRetries is the default number of retry attempts.

View Source
const DefaultRetryWait = 1 * time.Second

DefaultRetryWait is the default initial wait between retries.

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout is the default HTTP request timeout.

Variables

View Source
var (
	// ErrNotFound indicates the requested resource does not exist.
	ErrNotFound = errors.New("resource not found")

	// ErrUnauthorized indicates invalid or missing authentication.
	ErrUnauthorized = errors.New("authentication failed")

	// ErrForbidden indicates the user lacks permission for the operation.
	ErrForbidden = errors.New("permission denied")

	// ErrRateLimited indicates the API rate limit was exceeded.
	ErrRateLimited = errors.New("rate limit exceeded")

	// ErrBadRequest indicates the request was malformed.
	ErrBadRequest = errors.New("bad request")

	// ErrServerError indicates a server-side error occurred.
	ErrServerError = errors.New("server error")
)

Standard sentinel errors for integration clients.

Functions

func IsForbidden

func IsForbidden(err error) bool

IsForbidden reports whether the error indicates permission was denied.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether the error indicates a resource was not found.

func IsRateLimited

func IsRateLimited(err error) bool

IsRateLimited reports whether the error indicates rate limiting.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable reports whether the error is transient and should be retried.

func IsUnauthorized

func IsUnauthorized(err error) bool

IsUnauthorized reports whether the error indicates authentication failed.

Types

type APIError

type APIError struct {
	// Service is the name of the integration (e.g., "jira", "gitlab").
	Service string

	// StatusCode is the HTTP status code returned.
	StatusCode int

	// Message is the error message from the API.
	Message string

	// Endpoint is the API endpoint that was called.
	Endpoint string

	// RequestID is the request ID for debugging (if available).
	RequestID string
}

APIError represents an error from an external API.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface.

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

Unwrap returns the underlying sentinel error based on status code.

type AuthError

type AuthError struct {
	// Service is the integration that failed authentication.
	Service string

	// Reason explains why authentication failed.
	Reason string
}

AuthError represents an authentication failure.

func (*AuthError) Error

func (e *AuthError) Error() string

Error implements the error interface.

func (*AuthError) Unwrap

func (e *AuthError) Unwrap() error

Unwrap returns ErrUnauthorized.

type Client

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

Client provides common HTTP functionality for integration clients.

func NewClient

func NewClient(cfg ClientConfig) *Client

NewClient creates a new Client with the given configuration.

func (*Client) Delete

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

Delete performs a DELETE request.

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, result any) error

Get performs a GET request and decodes the response into result.

func (*Client) GetRaw

func (c *Client) GetRaw(ctx context.Context, path string) ([]byte, error)

GetRaw performs a GET request and returns the raw response body.

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, body, result any) error

Post performs a POST request and decodes the response into result.

func (*Client) Put

func (c *Client) Put(ctx context.Context, path string, body, result any) error

Put performs a PUT request and decodes the response into result.

func (*Client) Request

func (c *Client) Request(ctx context.Context, method, path string, body any) (*http.Response, error)

Request executes an HTTP request with retries for transient errors.

func (*Client) RequestWithHeaders

func (c *Client) RequestWithHeaders(
	ctx context.Context,
	method, path string,
	body any,
	headers map[string]string,
) (*http.Response, error)

RequestWithHeaders executes an HTTP request with custom headers.

type ClientConfig

type ClientConfig struct {
	Client        *http.Client
	BaseURL       string
	ServiceName   string
	MaxRetries    int
	RetryWait     time.Duration
	BeforeRequest func(req *http.Request)
}

ClientConfig holds configuration for Client.

type PageFetcher

type PageFetcher[T any] func(ctx context.Context, page int) (items []T, hasMore bool, err error)

PageFetcher is a function that fetches a page of items. Returns the items, whether there are more pages, and any error.

type PageIterator

type PageIterator[T any] struct {
	// contains filtered or unexported fields
}

PageIterator provides iteration over paginated API results. It lazily fetches pages as needed.

func NewPageIterator

func NewPageIterator[T any](fetch PageFetcher[T]) *PageIterator[T]

NewPageIterator creates a new iterator with the given fetch function.

func (*PageIterator[T]) All

func (p *PageIterator[T]) All(ctx context.Context) ([]T, error)

All collects all items from the iterator into a slice. This will fetch all pages and may be slow for large result sets.

func (*PageIterator[T]) Err

func (p *PageIterator[T]) Err() error

Err returns any error that occurred during iteration.

func (*PageIterator[T]) Fetched

func (p *PageIterator[T]) Fetched() int

Fetched returns the number of items fetched so far.

func (*PageIterator[T]) ForEach

func (p *PageIterator[T]) ForEach(ctx context.Context, fn func(T) error) error

ForEach calls fn for each item in the iterator. If fn returns an error, iteration stops and that error is returned.

func (*PageIterator[T]) Next

func (p *PageIterator[T]) Next(ctx context.Context) (T, bool, error)

Next returns the next item from the iterator. Returns the item, true if an item was returned, and any error. When iteration is complete, returns (zero, false, nil).

func (*PageIterator[T]) Reset

func (p *PageIterator[T]) Reset()

Reset resets the iterator to the beginning. Any buffered items are discarded.

func (*PageIterator[T]) SetTotal

func (p *PageIterator[T]) SetTotal(total int)

SetTotal sets the total count (called by fetch functions that know the total).

func (*PageIterator[T]) Skip

func (p *PageIterator[T]) Skip(ctx context.Context, n int) error

Skip advances the iterator by n items.

func (*PageIterator[T]) Take

func (p *PageIterator[T]) Take(ctx context.Context, n int) ([]T, error)

Take returns up to n items from the iterator.

func (*PageIterator[T]) Total

func (p *PageIterator[T]) Total() int

Total returns the total number of items if known, -1 otherwise. This may only be accurate after at least one page has been fetched.

type RateLimitError

type RateLimitError struct {
	// Service is the integration that rate limited.
	Service string

	// RetryAfter is how long to wait before retrying.
	RetryAfter time.Duration

	// Limit is the rate limit that was exceeded (if known).
	Limit int

	// Remaining is how many requests remain (usually 0).
	Remaining int
}

RateLimitError represents a rate limit being exceeded.

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

Error implements the error interface.

func (*RateLimitError) Unwrap

func (e *RateLimitError) Unwrap() error

Unwrap returns ErrRateLimited.

type ValidationError

type ValidationError struct {
	// Service is the integration that rejected the request.
	Service string

	// Field is the field that failed validation.
	Field string

	// Message explains the validation failure.
	Message string
}

ValidationError represents validation failures for request data.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface.

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

Unwrap returns ErrBadRequest.

Jump to

Keyboard shortcuts

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