Documentation
¶
Overview ¶
Package http provides shared HTTP client patterns for integration clients.
Index ¶
- Constants
- Variables
- func IsForbidden(err error) bool
- func IsNotFound(err error) bool
- func IsRateLimited(err error) bool
- func IsRetryable(err error) bool
- func IsUnauthorized(err error) bool
- type APIError
- type AuthError
- type Client
- func (c *Client) Delete(ctx context.Context, path string) error
- func (c *Client) Get(ctx context.Context, path string, result any) error
- func (c *Client) GetRaw(ctx context.Context, path string) ([]byte, error)
- func (c *Client) Post(ctx context.Context, path string, body, result any) error
- func (c *Client) Put(ctx context.Context, path string, body, result any) error
- func (c *Client) Request(ctx context.Context, method, path string, body any) (*http.Response, error)
- func (c *Client) RequestWithHeaders(ctx context.Context, method, path string, body any, headers map[string]string) (*http.Response, error)
- type ClientConfig
- type PageFetcher
- type PageIterator
- func (p *PageIterator[T]) All(ctx context.Context) ([]T, error)
- func (p *PageIterator[T]) Err() error
- func (p *PageIterator[T]) Fetched() int
- func (p *PageIterator[T]) ForEach(ctx context.Context, fn func(T) error) error
- func (p *PageIterator[T]) Next(ctx context.Context) (T, bool, error)
- func (p *PageIterator[T]) Reset()
- func (p *PageIterator[T]) SetTotal(total int)
- func (p *PageIterator[T]) Skip(ctx context.Context, n int) error
- func (p *PageIterator[T]) Take(ctx context.Context, n int) ([]T, error)
- func (p *PageIterator[T]) Total() int
- type RateLimitError
- type ValidationError
Constants ¶
const DefaultMaxRetries = 3
DefaultMaxRetries is the default number of retry attempts.
const DefaultRetryWait = 1 * time.Second
DefaultRetryWait is the default initial wait between retries.
const DefaultTimeout = 30 * time.Second
DefaultTimeout is the default HTTP request timeout.
Variables ¶
var ( // ErrNotFound indicates the requested resource does not exist. ErrNotFound = errors.New("resource not found") 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 ¶
IsForbidden reports whether the error indicates permission was denied.
func IsNotFound ¶
IsNotFound reports whether the error indicates a resource was not found.
func IsRateLimited ¶
IsRateLimited reports whether the error indicates rate limiting.
func IsRetryable ¶
IsRetryable reports whether the error is transient and should be retried.
func IsUnauthorized ¶
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.
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.
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.
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 ¶
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.