httpClient

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GET

func GET[Res any](ctx context.Context, url string, opts ...HTTPConfigOptions) (*Res, error)

GET http method with Res as response type

func GetCallerFunctionName

func GetCallerFunctionName() string

GetCallerFunctionName for extracting name of next to next caller function

func POST

func POST[Req, Res any](ctx context.Context, url string, req *Req, opts ...HTTPConfigOptions) (*Res, error)

POST http method with Req as request type and Res as response type

func POSTFormData

func POSTFormData[Res any](ctx context.Context, url string, opts ...HTTPConfigOptions) (*Res, error)

POSTFormData with Res as response type and allows application/x-www-form-urlencoded -> formData

func POSTMultiPartFormData

func POSTMultiPartFormData[Res any](ctx context.Context, url string, files map[string]string, opts ...HTTPConfigOptions) (*Res, error)

POSTMultiPartFormData with Res as response type, map of files with key as fieldName and value as filePath

func PUT

func PUT[Req, Res any](ctx context.Context, url string, req *Req, opts ...HTTPConfigOptions) (*Res, error)

PUT http method with Req as request type and Res as response type

Types

type HTTPClient

type HTTPClient struct {
	Client       *http.Client
	TraceEnabled bool
	ServiceName  string
}

HTTPClient wrapper over net/http client with tracing

func NewHTTPClient

func NewHTTPClient(opt ...HTTPClientOption) *HTTPClient

NewHTTPClient creates a new HTTPClient with DefaultTransport TODO: add options to configure transport

func (*HTTPClient) Do

func (c *HTTPClient) Do(request *http.Request) (*http.Response, error)

Do makes an HTTP request with the native `http.Do` interface

type HTTPClientOption

type HTTPClientOption func(client *HTTPClient)

HTTPClientOption different options to configure the HTTPClient

func WithTraceEnabled

func WithTraceEnabled(e bool) HTTPClientOption

WithTraceEnabled allows to toggle tracing

type HTTPConfig

type HTTPConfig struct {
	HttpClient          *HTTPClient
	Timeout             time.Duration
	Headers             http.Header
	QueryParams         netUrl.Values
	FormParams          netUrl.Values
	MultipartFormParams map[string]string
	CircuitBreaker      *gobreaker.CircuitBreaker[any] // currently only github.com/sony/gobreaker/v2 is supported
}

HTTPConfig different http configurations

type HTTPConfigOptions

type HTTPConfigOptions func(*HTTPConfig)

func WithFormParams

func WithFormParams(c netUrl.Values) HTTPConfigOptions

func WithHeaders

func WithHeaders(c http.Header) HTTPConfigOptions

func WithHttpClient

func WithHttpClient(c *HTTPClient) HTTPConfigOptions

func WithMultiPartFormParams

func WithMultiPartFormParams(c map[string]string) HTTPConfigOptions

func WithQueryParams

func WithQueryParams(c netUrl.Values) HTTPConfigOptions

func WithTimeout

func WithTimeout(t time.Duration) HTTPConfigOptions

type HTTPError

type HTTPError struct {
	StatusCode int
	Status     string
	Body       string
}

HTTPError Custom error type to be returned for status code < 200 and >300

func (*HTTPError) Error

func (e *HTTPError) Error() string

Directories

Path Synopsis
example
httpGet command
httpPost command
httpPut command

Jump to

Keyboard shortcuts

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