http

package
v0.18.2 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 19 Imported by: 4

Documentation

Index

Constants

View Source
const (
	InstrumentationRequest = "http-client-request"
)

Variables

This section is empty.

Functions

func InstrRequest

func InstrRequest(op string, args ...any) (*Request, *Response, bool)

InstrRequest returns request and response if the operation is HTTP client request event.

Types

type BaseURL

type BaseURL string

BaseURL sets the base URL for the HTTP client requests.

type Client

type Client interface {
	// WithConfiguration returns a new client with specific named configuration.
	WithConfiguration(name string) (Client, error)
	// WithContext returns a new client with specified context.
	WithContext(ctx context.Context) Client
	// WithBaseURL returns a new client with specified base URL.
	WithBaseURL(url string) Client
	// WithOptions returns a new client with additional options applied.
	WithOptions(opt ...Option) Client

	// UserAgent returns client user agent.
	UserAgent() string
	// BaseURL returns client base URL.
	BaseURL() string

	// NewRequest creates a new HTTP request.
	//
	// The returned request must be released after use by calling ReleaseRequest.
	NewRequest() *Request
	// NewResponse creates a new HTTP response.
	//
	// The returned response must be released after use by calling ReleaseResponse.
	NewResponse() *Response
	// ReleaseRequest releases the HTTP request back to pool.
	ReleaseRequest(req *Request)
	// ReleaseResponse releases the HTTP response back to pool.
	ReleaseResponse(resp *Response)

	// Do sends an HTTP request and returns an HTTP response.
	Do(req *Request, resp *Response) error
	// Get sends an HTTP GET request and returns an HTTP response body.
	Get(url string, opt ...RequestOption) ([]byte, error)
	// GetJSON sends an HTTP GET request and unmarshals the response body into v.
	GetJSON(url string, v any, opt ...RequestOption) error
	// Head sends an HTTP POST request and returns an HTTP response body.
	Post(url string, body []byte, opt ...RequestOption) ([]byte, error)
	// PostJSON sends an HTTP POST request and unmarshals response body into v.
	PostJSON(url string, body, v any, opt ...RequestOption) error
	// PostForm sends an HTTP POST request with form data and returns an HTTP response body.
	PostForm(url string, form map[string][]string, opt ...RequestOption) ([]byte, error)
	// PostMultipartForm sends an HTTP POST request with multipart form data and returns an HTTP response body.
	PostMultipartForm(url string, form *multipart.Form, opt ...RequestOption) ([]byte, error)
	// Put sends an HTTP PUT request and returns an HTTP response body.
	Put(url string, body []byte, opt ...RequestOption) ([]byte, error)
	// PutJSON sends an HTTP PUT request and unmarshals response body into v.
	PutJSON(url string, body, v any, opt ...RequestOption) error
	// Patch sends an HTTP PATCH request and returns an HTTP response body.
	Patch(url string, body []byte, opt ...RequestOption) ([]byte, error)
	// PatchJSON sends an HTTP PATCH request and unmarshals response body into v.
	PatchJSON(url string, body, v any, opt ...RequestOption) error
	// Delete sends an HTTP DELETE request.
	Delete(url string, opt ...RequestOption) error
}

Client is the interface that provides HTTP client.

func NewClient

func NewClient(opt ...Option) Client

type ClientProvider added in v0.16.0

type ClientProvider interface {
	// HTTPClient returns HTTP client instance.
	HTTPClient() Client
}

ClientProvider is the interface that provides HTTP client.

type Configuration

type Configuration struct {
	Clients map[string]NamedClient `mapstructure:"clients"`
}

Configuration represents the configuration for the HTTP client.

func (*Configuration) Validate

func (c *Configuration) Validate(valid *validation.Validate) error

Validate Metrics configuration section.

type DialContextFunc

type DialContextFunc func(ctx context.Context, network, addr string) (net.Conn, error)

DialContextFunc is a function that dials a network address.

type ErrorResponse

type ErrorResponse struct {
	Errors []*ErrorResponseError `json:"errors" yaml:"errors" xml:"Errors>Error"`
}

ErrorResponse represents an error response.

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

Error implements error interface.

type ErrorResponseError

type ErrorResponseError struct {
	Type    string `json:"type" yaml:"type" xml:"Type"`
	Message string `json:"message" yaml:"message" xml:"Message"`
}

ErrorResponseError is an error response error details.

type ForbiddenError

type ForbiddenError struct{}

ForbiddenError is an error that occurs when user access is denied.

func (ForbiddenError) Error

func (ForbiddenError) Error() string

func (ForbiddenError) StatusCode

func (ForbiddenError) StatusCode() int

type Instrumenter

type Instrumenter instrumenter.Instrumenter

Instrumenter is a function that instruments HTTP client operations.

type NamedClient

type NamedClient struct {
	BaseURL string `mapstructure:"base_url" validate:"required http_url"`
}

NamedClient represents the configuration for the named client instance.

type NotFoundError

type NotFoundError struct {
	Resource string
}

NotFoundError is an error that occurs when searched resource is not found.

func (NotFoundError) Error

func (e NotFoundError) Error() string

func (NotFoundError) StatusCode

func (NotFoundError) StatusCode() int

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is a functional option for configuring the HTTP client.

func Context

func Context(ctx context.Context) Option

Context sets the custom context for the HTTP client.

type Request

type Request struct {
	*fasthttp.Request
	// contains filtered or unexported fields
}

Request represents an HTTP request.

func (Request) BaseURL

func (r Request) BaseURL() string

BaseURL returns the base URL of the client.

func (Request) SetRequestURL

func (r Request) SetRequestURL(u string) error

SetRequestURL sets the request URL.

type RequestFunc

type RequestFunc func(context.Context, *Request) error

RequestFunc is a function that modifies the HTTP request.

type RequestOption

type RequestOption interface {
	// contains filtered or unexported methods
}

RequestOption is a functional option for configuring the HTTP request.

func WithHeader

func WithHeader(key, value string) RequestOption

WithHeader sets the specified header key and value for the request.

func WithQueryArg

func WithQueryArg(key string, value any, override ...bool) RequestOption

WithQueryArg sets the specified query argument key and value for the request.

type Response

type Response struct {
	*fasthttp.Response
}

Response represents HTTP response.

It is forbidden copying Response instances. Create new instances and use CopyTo instead.

Response instance MUST NOT be used from concurrently running goroutines.

func (Response) Error

func (r Response) Error() error

Error if the response status code is not 2xx.

func (Response) Success

func (r Response) Success() bool

Success returns true if the response status code is 2xx.

type ResponseFunc

type ResponseFunc func(context.Context, *Response, error) error

ResponseFunc is a function that modifies the HTTP response.

type ResponseStatusCode

type ResponseStatusCode interface {
	// StatusCode to be set for the response.
	StatusCode() int
}

ResponseStatusCode is an interface that error can implement to return status code that will be set for the response.

type RetryIf added in v0.18.0

type RetryIf fasthttp.RetryIfErrFunc

RetryIf is a function that determines if the request should be retried.

type TLSConfig

type TLSConfig tls.Config

TLSConfig configures the TLS settings that client will use.

type UnauthorizedError

type UnauthorizedError struct{}

UnauthorizedError is an error that occurs when user is not authorized.

func (UnauthorizedError) Error

func (UnauthorizedError) Error() string

func (UnauthorizedError) StatusCode added in v0.15.4

func (UnauthorizedError) StatusCode() int

type UserAgent

type UserAgent string

UserAgent sets the user agent for the HTTP client requests.

Jump to

Keyboard shortcuts

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