httpc

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 20 Imported by: 0

README

httpc

This repository contains an net/http client wrapper that handles authentication and can be configured with rate limiting, retries and Open Telemetry instrumentation.

Example client setup

cfg := &httpc.Config{
    BaseUrl:        "google.com",
    Timeout:        10,
    RetryEnabled:   true,
    TlsConfig: &tls.Config{
        InsecureSkipVerify: false,
        MinVersion:         tls.VersionTLS12,
    },
}

client, err := httpc.NewClient(ctx, cfg)
if err != nil {
    // handle error
}

GET Request

var response Response
resp, err := client.Get(ctx, "/resource", nil, &response)
if err != nil {
    // handle error
}
defer resp.Body.Close()

POST Request

var response Response
resp, err := client.Post(ctx, "/resource", bytes.NewReader(body), nil, &response)
if err != nil {
    // handle error
}
defer resp.Body.Close()

Documentation

Index

Constants

View Source
const (
	Kib int64
	Mib
	Gib
)
View Source
const (
	DefaultTimeout       int   = 10
	DefaultReadByteLimit int64 = 15 * Mib

	MaxRateLimitKeys int = 65536
	MaxIdleConns     int = 100
	MaxConnsPerHost  int = 100
)
View Source
const (
	DefaultRetryMax int = 3
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(ctx context.Context, cfg *Config, opts ...ClientOpt) (*Client, error)

NewClient creates a new Client

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, resource string, body io.Reader, headers map[string]string, decoded interface{}) (*http.Response, error)

Delete makes a DELETE request to the supplied endpoint and returns the response. If a struct pointer is supplied, the response body will be decoded into it

func (*Client) Get

func (c *Client) Get(ctx context.Context, resource string, headers map[string]string) (*http.Response, error)

Get makes a GET request to the supplied endpoint and returns the response.

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, resource string, body io.Reader, headers map[string]string, decoded interface{}) (*http.Response, error)

Patch makes a PATCH request to the supplied endpoint and returns the response. If a struct pointer is supplied, the response body will be decoded into it

func (*Client) Post

func (c *Client) Post(ctx context.Context, resource string, body io.Reader, headers map[string]string, decoded interface{}) (*http.Response, error)

Post makes a POST request to the supplied endpoint and returns the response. If a struct pointer is supplied, the response body will be decoded into it

func (*Client) Put

func (c *Client) Put(ctx context.Context, resource string, body io.Reader, headers map[string]string, decoded interface{}) (*http.Response, error)

Put makes a PUT request to the supplied endpoint and returns the response. If a struct pointer is supplied, the response body will be decoded into it

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, method string, resource string, body io.Reader, headers map[string]string) (io.Reader, error)

Stream makes a request to the supplied endpoint and pipes the response body to the returned io.Reader

type ClientOpt added in v0.0.2

type ClientOpt func(c *Client) error

func WithCredentials

func WithCredentials(ctx context.Context, id string, secret string, baseUrl string, resource string, scopes ...string) ClientOpt

WithCredentials sets up oauth2 and replaces the default http client

func WithCustomClient

func WithCustomClient(client *http.Client) ClientOpt

WithCustomClient replaces the default http client with the supplied one

func WithDefaultHeaders

func WithDefaultHeaders(headers map[string]string) ClientOpt

WithDefaultHeaders adds default headers to the client

func WithRateLimiter

func WithRateLimiter(rateLimit int) ClientOpt

WithRateLimiter configures a rate limiter with the supplied limit (per minute)

type Config

type Config struct {
	TlsConfig     *tls.Config
	BaseUrl       string
	Timeout       int
	OTelEnabled   bool
	RetryEnabled  bool
	RetryLimit    int
	ReadByteLimit int64
}

type ErrCopy added in v0.0.2

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

func (*ErrCopy) Error added in v0.0.2

func (e *ErrCopy) Error() string

type ErrDecode added in v0.0.2

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

func (*ErrDecode) Error added in v0.0.2

func (e *ErrDecode) Error() string

type ErrInvalidResource added in v0.0.2

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

func (*ErrInvalidResource) Error added in v0.0.2

func (e *ErrInvalidResource) Error() string

type ErrNewRequest added in v0.0.2

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

func (*ErrNewRequest) Error added in v0.0.2

func (e *ErrNewRequest) Error() string

type ErrRateLimit added in v0.0.2

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

func (*ErrRateLimit) Error added in v0.0.2

func (e *ErrRateLimit) Error() string

type ErrRequest added in v0.0.2

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

func (*ErrRequest) Error added in v0.0.2

func (e *ErrRequest) Error() string

type ErrRetryCopy added in v0.0.2

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

func (*ErrRetryCopy) Error added in v0.0.2

func (e *ErrRetryCopy) Error() string

type ErrStatusCode added in v0.0.2

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

func (*ErrStatusCode) Error added in v0.0.2

func (e *ErrStatusCode) Error() string

type RetryTransport

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

func NewRetryTransport

func NewRetryTransport(transport *http.Transport, maxRetry int) (*RetryTransport, error)

NewRetryTransport wraps the supplied http transport with a retryable implementation

func (*RetryTransport) RoundTrip

func (t *RetryTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface with retries

Jump to

Keyboard shortcuts

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