rest

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 5 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAuth added in v0.1.5

func IsAuth(err error) bool

IsAuth checks if the error is a 401/403 authentication error.

func IsNotFound added in v0.1.5

func IsNotFound(err error) bool

IsNotFound checks if the error is a 404 Not Found.

func IsRateLimit added in v0.1.5

func IsRateLimit(err error) bool

IsRateLimit checks if the error is a 429 Too Many Requests.

func IsRetryable added in v0.1.5

func IsRetryable(err error) bool

IsRetryable checks if the error can be retried.

func IsServerError added in v0.1.5

func IsServerError(err error) bool

IsServerError checks if the error is a 5xx server error.

func IsTimeout added in v0.1.5

func IsTimeout(err error) bool

IsTimeout checks if the error is a timeout.

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) Close added in v0.1.5

func (c *Client) Close(ctx context.Context) error

Close releases resources (implements provider.Closeable).

func (*Client) HTTP

func (c *Client) HTTP() *httpclient.Adapter

HTTP returns the underlying HTTP adapter.

func (*Client) IsAvailable added in v0.1.5

func (c *Client) IsAvailable(ctx context.Context) bool

IsAvailable checks if the adapter is ready (implements provider.Provider).

func (*Client) Name added in v0.1.5

func (c *Client) Name() string

Name returns the adapter name (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.

func Post

func Post[T any](ctx context.Context, c *Client, path string, body any, opts ...RequestOption) (*Response[T], error)

Post performs a POST request with a JSON body and decodes the response into type T.

func Put

func Put[T any](ctx context.Context, c *Client, path string, body any, opts ...RequestOption) (*Response[T], error)

Put performs a PUT request with a JSON body and decodes the response into type T.

Jump to

Keyboard shortcuts

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