Documentation
¶
Overview ¶
Package rest provides a JSON-focused REST client built on the HTTP adapter.
It inherits all features from httpclient (auth, TLS, resilience, streaming) and adds typed convenience methods for common REST operations:
client := rest.New(httpclient.Config{
BaseURL: "https://api.example.com",
Auth: httpclient.BearerAuth("token"),
Retry: httpclient.DefaultRetryConfig(),
})
// Typed GET
user, err := rest.Get[User](ctx, client, "/users/123")
// Typed POST
created, err := rest.Post[User](ctx, client, "/users", CreateUserRequest{Name: "Alice"})
Alternatively, use the generic functions directly on the HTTP adapter:
adapter, _ := httpclient.New(cfg) user, err := httpclient.Get[User](adapter, ctx, "/users/123")
Index ¶
- func IsAuth(err error) bool
- func IsNotFound(err error) bool
- func IsRateLimit(err error) bool
- func IsRetryable(err error) bool
- func IsServerError(err error) bool
- func IsTimeout(err error) bool
- type Client
- type RequestOption
- type Response
- func Delete[T any](ctx context.Context, c *Client, path string, opts ...RequestOption) (*Response[T], error)
- func Get[T any](ctx context.Context, c *Client, path string, opts ...RequestOption) (*Response[T], error)
- func Patch[T any](ctx context.Context, c *Client, path string, body any, opts ...RequestOption) (*Response[T], error)
- func Post[T any](ctx context.Context, c *Client, path string, body any, opts ...RequestOption) (*Response[T], error)
- func Put[T any](ctx context.Context, c *Client, path string, body any, opts ...RequestOption) (*Response[T], error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsNotFound ¶ added in v0.1.5
IsNotFound checks if the error is a 404 Not Found.
func IsRateLimit ¶ added in v0.1.5
IsRateLimit checks if the error is a 429 Too Many Requests.
func IsRetryable ¶ added in v0.1.5
IsRetryable checks if the error can be retried.
func IsServerError ¶ added in v0.1.5
IsServerError checks if the error is a 5xx server error.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a JSON-focused REST client that wraps the HTTP adapter. All requests use Content-Type: application/json and Accept: application/json.
Client implements provider.Provider (Name, IsAvailable, Close) by delegating to the underlying HTTP adapter, so it composes naturally with gokit middleware.
func New ¶
func New(cfg httpclient.Config) (*Client, error)
New creates a new REST client from the given config. JSON headers are applied automatically.
func NewFromAdapter ¶ added in v0.1.5
func NewFromAdapter(a *httpclient.Adapter) *Client
NewFromAdapter creates a REST client from an existing HTTP adapter.
func (*Client) HTTP ¶
func (c *Client) HTTP() *httpclient.Adapter
HTTP returns the underlying HTTP adapter.
func (*Client) IsAvailable ¶ added in v0.1.5
IsAvailable checks if the adapter is ready (implements provider.Provider).
type RequestOption ¶
type RequestOption func(*httpclient.Request)
RequestOption configures a single REST request.
func WithAuth ¶
func WithAuth(auth *httpclient.AuthConfig) RequestOption
WithAuth overrides authentication for the request.
func WithHeaders ¶
func WithHeaders(headers map[string]string) RequestOption
WithHeaders adds headers to the request.
func WithQuery ¶
func WithQuery(params map[string]string) RequestOption
WithQuery adds query parameters to the request.
type Response ¶
type Response[T any] struct { // StatusCode is the HTTP status code. StatusCode int // Headers are the response headers. Headers map[string]string // Data is the decoded response body. Data T }
Response wraps a typed REST response.
func Delete ¶
func Delete[T any](ctx context.Context, c *Client, path string, opts ...RequestOption) (*Response[T], error)
Delete performs a DELETE request and decodes the response into type T.
func Get ¶
func Get[T any](ctx context.Context, c *Client, path string, opts ...RequestOption) (*Response[T], error)
Get performs a GET request and decodes the JSON response into type T.
func Patch ¶
func Patch[T any](ctx context.Context, c *Client, path string, body any, opts ...RequestOption) (*Response[T], error)
Patch performs a PATCH request with a JSON body and decodes the response into type T.