httpc

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 16 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
_, err := client.Get(ctx, "/resource", nil, &response)
if err != nil {
    // handle error
}

POST Request

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

Documentation

Index

Constants

View Source
const (
	DefaultTimeout   int = 10
	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 BadStatusCode

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

func (*BadStatusCode) Error

func (e *BadStatusCode) Error() string

type Client

type Client struct {
	Http        *http.Client
	Credentials *clientcredentials.Config
	BaseUrl     *url.URL
	RateLimiter *throttled.GCRARateLimiterCtx
	Headers     map[string]string
}

func NewClient

func NewClient(ctx context.Context, cfg *Config, opts ...ClientOption) (*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, decoded interface{}) (*http.Response, error)

Get makes a GET 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) 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 ClientOption

type ClientOption func(c *Client) error

func WithCredentials

func WithCredentials(ctx context.Context, clientId, key, tokenUrl string) ClientOption

WithCredentials sets up oauth2 and replaces the default http client

func WithCustomClient

func WithCustomClient(client *http.Client) ClientOption

WithCustomClient replaces the default http client with the supplied one

func WithDefaultHeaders

func WithDefaultHeaders(headers map[string]string) ClientOption

WithDefaultHeaders adds default headers to the client

func WithRateLimiter

func WithRateLimiter(rateLimit int) ClientOption

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
	RetryMax     int
}

type CopyError

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

func (*CopyError) Error

func (e *CopyError) Error() string

type DecodeError

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

func (*DecodeError) Error

func (e *DecodeError) Error() string

type InvalidResource

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

func (*InvalidResource) Error

func (e *InvalidResource) Error() string

type RequestError

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

func (*RequestError) Error

func (e *RequestError) 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