wrap

package module
v0.0.0-...-b0f7fae Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2025 License: MIT Imports: 8 Imported by: 0

README

wrap-http

Simple wrapper over https://github.com/imroc/req

Provided as is, without any warranty or support.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConn = errors.New("connection") // Represents an error when a connection cannot be established or maintained.

	ErrStatus = errors.New("status") // Represents an error when a failed status code is encountered.

	ErrUnknownCode = errors.New("unknown status code")

	ErrUnmarshal = errors.New("unmarshal")

	ErrInvalidProxy = errors.New("invalid proxy")

	ErrInvalidProxyURL = errors.New("invalid proxy url")

	ErrInvalidProxyCheckURL = errors.New("invalid proxy check url")
)
View Source
var (
	ErrInvalidHeadersType = errors.New("invalid headers type")
	ErrInvalidCookiesType = errors.New("invalid cookies type")
)
View Source
var (
	// 4xx Client Errors
	ErrBadRequest                   = errors.New("bad request")
	ErrUnauthorized                 = errors.New("unauthorized")
	ErrPaymentRequired              = errors.New("payment required")
	ErrForbidden                    = errors.New("forbidden")
	ErrNotFound                     = errors.New("not found")
	ErrMethodNotAllowed             = errors.New("method not allowed")
	ErrNotAcceptable                = errors.New("not acceptable")
	ErrProxyAuthRequired            = errors.New("proxy authentication required")
	ErrRequestTimeout               = errors.New("request timeout")
	ErrConflict                     = errors.New("conflict")
	ErrGone                         = errors.New("gone")
	ErrLengthRequired               = errors.New("length required")
	ErrPreconditionFailed           = errors.New("precondition failed")
	ErrRequestEntityTooLarge        = errors.New("request entity too large")
	ErrRequestURITooLong            = errors.New("request uri too long")
	ErrUnsupportedMediaType         = errors.New("unsupported media type")
	ErrRequestedRangeNotSatisfiable = errors.New("requested range not satisfiable")
	ErrExpectationFailed            = errors.New("expectation failed")
	ErrTeapot                       = errors.New("i'm a teapot")
	ErrMisdirectedRequest           = errors.New("misdirected request")
	ErrUnprocessableEntity          = errors.New("unprocessable entity")
	ErrLocked                       = errors.New("locked")
	ErrFailedDependency             = errors.New("failed dependency")
	ErrTooEarly                     = errors.New("too early")
	ErrUpgradeRequired              = errors.New("upgrade required")
	ErrPreconditionRequired         = errors.New("precondition required")
	ErrTooManyRequests              = errors.New("too many requests")
	ErrRequestHeaderFieldsTooLarge  = errors.New("request header fields too large")
	ErrUnavailableForLegalReasons   = errors.New("unavailable for legal reasons")

	// 5xx Server Errors
	ErrInternalServerError           = errors.New("internal server error")
	ErrNotImplemented                = errors.New("not implemented")
	ErrBadGateway                    = errors.New("bad gateway")
	ErrServiceUnavailable            = errors.New("service unavailable")
	ErrGatewayTimeout                = errors.New("gateway timeout")
	ErrHTTPVersionNotSupported       = errors.New("http version not supported")
	ErrVariantAlsoNegotiates         = errors.New("variant also negotiates")
	ErrInsufficientStorage           = errors.New("insufficient storage")
	ErrLoopDetected                  = errors.New("loop detected")
	ErrNotExtended                   = errors.New("not extended")
	ErrNetworkAuthenticationRequired = errors.New("network authentication required")
)

Functions

func CodeToErr

func CodeToErr(code int) error

func NewReqClient

func NewReqClient() *httpClient

Types

type Client

type Client interface {
	// Do execute an HTTP request with the specified method, URL, headers, and body.
	// - ctx: Context for request cancellation and timeout.
	// - method: HTTP method (e.g., "GET", "POST").
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// - body: Optional request body (can be nil).
	// Returns the HTTP response.
	Do(ctx context.Context, method, url string, headers map[string]string, body any) *req.Response

	// DoWithErrorHandling execute an HTTP request with the specified method, URL, headers, and body.
	// - ctx: Context for request cancellation and timeout.
	// - method: HTTP method (e.g., "GET", "POST").
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// - body: Optional request body (can be nil).
	// Returns the HTTP response.
	// Does additional checks on the response status code and returns an error if it's not successful.
	DoWithErrorHandling(ctx context.Context, method, url string, headers map[string]string, body any) *req.Response

	// DoWithResult execute an HTTP request with the specified method, URL, headers, and body.
	// - ctx: Context for request cancellation and timeout.
	// - method: HTTP method (e.g., "GET", "POST").
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// - body: Optional request body (can be nil).
	// - resultTo: Pointer to the variable where we unmarshal the response body OR EasyJSON Unmarshal Factory(to avoid memory allocations when response is unsuccesful).
	// Returns the HTTP response and any error encountered.
	DoWithResult(ctx context.Context, method, url string, headers map[string]string, body any, resultTo any) *req.Response

	// DoWithRequest execute the given req.Request.
	// - request: The req.Request to be executed.
	// Returns the HTTP response.
	DoWithRequest(request *req.Request) *req.Response

	// DoWithRequest execute the given http.Request.
	// - request: The http.Request to be executed.
	// Returns the HTTP response and any error encountered.
	DoWithHTTPRequest(request *http.Request) (*http.Response, error)

	// SetHeaders Sets the headers for the request
	// headers can be a map[string]string, http.Header or nil if you want to delete all headers
	SetHeaders(headers any) error

	// SetCookies Sets cookies for all requests.
	// Cookies can be []*http.Cookie, []http.Cookie ,map[string]string, or nil to remove all cookies.
	SetCookiesArray(cookies any) error

	// SetProxy Sets proxy for all requests.
	// proxy can be a string, *url.URL, or nil to remove the proxy
	// Also give the checkURL to check if the proxy is working
	SetProxy(proxyURL string, checkURL string) error

	// Client returns the underlying req.Client used by the Client.
	Client() *req.Client

	// Get sends an HTTP GET request to the specified URL with optional headers.
	// - ctx: Context for request cancellation and timeout.
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// Returns the HTTP response and any error encountered.
	Get(ctx context.Context, url string, headers map[string]string) *req.Response

	// Post sends an HTTP POST request to the specified URL with optional headers and a request body.
	// - ctx: Context for request cancellation and timeout.
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// - body: Optional request body (can be nil).
	// Returns the HTTP response and any error encountered.
	Post(ctx context.Context, url string, headers map[string]string, body any) *req.Response

	// Put sends an HTTP PUT request to the specified URL with optional headers and a request body.
	// - ctx: Context for request cancellation and timeout.
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// - body: Optional request body (can be nil).
	// Returns the HTTP response and any error encountered.
	Put(ctx context.Context, url string, headers map[string]string, body any) *req.Response

	// Delete sends an HTTP DELETE request to the specified URL with optional headers.
	// - ctx: Context for request cancellation and timeout.
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// Returns the HTTP response and any error encountered.
	Delete(ctx context.Context, url string, headers map[string]string) *req.Response

	// Patch sends an HTTP PATCH request to the specified URL with optional headers and a request body.
	// - ctx: Context for request cancellation and timeout.
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// - body: Optional request body (can be nil).
	// Returns the HTTP response and any error encountered.
	Patch(ctx context.Context, url string, headers map[string]string, body any) *req.Response

	// Head sends an HTTP HEAD request to the specified URL with optional headers.
	// - ctx: Context for request cancellation and timeout.
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// Returns the HTTP response and any error encountered.
	Head(ctx context.Context, url string, headers map[string]string) *req.Response

	// Options sends an HTTP OPTIONS request to the specified URL with optional headers.
	// - ctx: Context for request cancellation and timeout.
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// Returns the HTTP response and any error encountered.
	Options(ctx context.Context, url string, headers map[string]string) *req.Response

	// Trace sends an HTTP TRACE request to the specified URL with optional headers.
	// - ctx: Context for request cancellation and timeout.
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// Returns the HTTP response and any error encountered.
	Trace(ctx context.Context, url string, headers map[string]string) *req.Response

	// Connect sends an HTTP CONNECT request to the specified URL with optional headers.
	// - ctx: Context for request cancellation and timeout.
	// - url: The request URL.
	// - headers: Optional headers to include in the request (map of header name to value).
	// Returns the HTTP response and any error encountered.
	Connect(ctx context.Context, url string, headers map[string]string) *req.Response

	// CloseConnection closes all connections, goroutine safe
	CloseConnections()

	// Close closes the HTTP client, it's forbidden to use the client after this method is called.
	Close()
}

type UnmarshallerFactory

type UnmarshallerFactory func() easyjson.Unmarshaler

UnmarshallerFactory is a function that creates a new instance of an easyjson.Unmarshaler Used to avoid unnecessary allocations when response is unsuccessful

Jump to

Keyboard shortcuts

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