Documentation
¶
Index ¶
- Variables
- func AsFiles(data map[string]File) bodyArgument
- func AsForm(data map[string]string) bodyArgument
- func AsHeaders(data map[string]string) bodyArgument
- func AsJson(data any) bodyArgument
- func AsParams(data map[string]string) bodyArgument
- func ToStruct[T any](r *Response) (T, error)
- type Client
- func (c *Client) Get() (*Response, error)
- func (c *Client) NewRequest() *Request
- func (c *Client) NewRequestWithContext(ctx context.Context) *Request
- func (c *Client) Post() (*Response, error)
- func (c *Client) SetBinary(data []byte)
- func (c *Client) SetCookiesFromMap(cookies map[string]string) error
- func (c *Client) SetFiles(files map[string]File)
- func (c *Client) SetFormData(data map[string]string)
- func (c *Client) SetHeaders(h map[string]string)
- func (c *Client) SetJsonData(data any)
- func (c *Client) SetParams(params map[string]string)
- func (c *Client) Use(middlewares ...Middleware)
- type ClientOption
- type ConcurrencyManager
- type File
- type Files
- type FormData
- type Headers
- type JsonData
- type ManagerOption
- type Middleware
- type Next
- type Params
- type Request
- func (r *Request) Cookies() []*http.Cookie
- func (r *Request) CookiesMap() map[string]string
- func (r *Request) Delete() (*Response, error)
- func (r *Request) Get() (*Response, error)
- func (r *Request) JoinPath(p string) *Request
- func (r *Request) Post() (*Response, error)
- func (r *Request) Put() (*Response, error)
- func (r *Request) SetBinary(data []byte) *Request
- func (r *Request) SetCookiesFromMap(cookies map[string]string) *Request
- func (r *Request) SetFiles(files map[string]File) *Request
- func (r *Request) SetFormData(data map[string]string) *Request
- func (r *Request) SetHeaders(h map[string]string) *Request
- func (r *Request) SetJsonData(data any) *Request
- func (r *Request) SetParams(params map[string]string) *Request
- func (r *Request) SetURL(rawURL string) *Request
- type Response
- func (r *Response) Bytes() ([]byte, error)
- func (r *Response) Cookies() []*http.Cookie
- func (r *Response) CookiesMap() map[string]string
- func (r *Response) Header() http.Header
- func (r *Response) Json() (json_results map[string]any, err error)
- func (r *Response) StatusCode() int
- func (r *Response) Text() (string, error)
- func (r *Response) Unmarshal(v any) error
- type Result
- type Task
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func AsJson ¶
func AsJson(data any) bodyArgument
AsJson marks the provided data to be sent as a JSON request body.
Types ¶
type Client ¶
type Client struct {
CookieJar http.CookieJar
BaseURL string
Headers http.Header
Params map[string]string
FormData map[string]string
JsonData any
BodyBytes []byte
Files map[string]File
// contains filtered or unexported fields
}
Client is a reusable HTTP client that manages a connection pool, cookies, and default settings for requests.
func NewClient ¶
func NewClient(baseUrl string, opts ...ClientOption) *Client
NewClient creates and returns a new Client instance. It initializes a default http.Client with a cookie jar and applies any provided ClientOption functions.
func (*Client) NewRequest ¶
NewRequest creates a new Request instance with a default background context.
func (*Client) NewRequestWithContext ¶
NewRequestWithContext creates a new Request instance with the provided context. If the provided context is nil, it defaults to context.Background().
func (*Client) Post ¶
Post creates and sends a new POST request using the client's default settings.
func (*Client) SetBinary ¶
SetBinary sets a default raw binary body for requests made by this client.
func (*Client) SetCookiesFromMap ¶
SetCookiesFromMap adds cookies to the client's cookie jar from a map. It requires the client to have a valid BaseURL and returns an error if it's missing or invalid.
func (*Client) SetFiles ¶
SetFiles sets default files to be uploaded with requests made by this client.
func (*Client) SetFormData ¶
SetFormData sets a default form-urlencoded body for requests made by this client.
func (*Client) SetHeaders ¶
SetHeaders sets default headers that will be sent with every request from this client.
func (*Client) SetJsonData ¶
SetJsonData sets a default JSON body for requests made by this client.
func (*Client) SetParams ¶
SetParams sets default query parameters that will be added to every request from this client.
func (*Client) Use ¶
func (c *Client) Use(middlewares ...Middleware)
Use adds one or more middleware handlers to the client's middleware chain.
type ClientOption ¶
type ClientOption func(*clientConfig)
ClientOption is a function that configures a Client.
func WithFollowRedirects ¶
func WithFollowRedirects(follow bool) ClientOption
WithFollowRedirects controls whether the client should automatically follow HTTP redirects. The default behavior is to follow redirects (true).
func WithTimeout ¶
func WithTimeout(timeout time.Duration) ClientOption
WithTimeout sets the global request timeout for the http.Client.
func WithTransport ¶
func WithTransport(transport http.RoundTripper) ClientOption
WithTransport allows setting a custom http.RoundTripper (transport) for the client.
type ConcurrencyManager ¶
type ConcurrencyManager struct {
// contains filtered or unexported fields
}
ConcurrencyManager manages and executes a pool of concurrent HTTP requests.
func NewManager ¶
func NewManager(client *Client, opts ...ManagerOption) *ConcurrencyManager
NewManager creates and returns a new ConcurrencyManager. It takes a configured requesto.Client and a set of options to initialize its settings.
func (*ConcurrencyManager) AddTasks ¶
func (cm *ConcurrencyManager) AddTasks(tasks ...Task) *ConcurrencyManager
AddTasks adds one or more pre-configured Tasks to the manager's queue.
func (*ConcurrencyManager) AddURLs ¶
func (cm *ConcurrencyManager) AddURLs(urls ...string) *ConcurrencyManager
AddURLs is a helper function to quickly create and add tasks from a list of URL strings.
func (*ConcurrencyManager) Run ¶
func (cm *ConcurrencyManager) Run() []Result
Run starts the worker pool, executes all queued tasks, and blocks until they are all completed. It returns a slice of Results. The order of the results is not guaranteed to match the order in which tasks were added.
type File ¶
File represents a file to be uploaded, containing its name and content as an io.Reader.
func FileFromBytes ¶
FileFromBytes creates a File object from a byte slice. It uses the provided file name and wraps the byte slice in an io.Reader.
func FileFromPath ¶
FileFromPath creates a File object from a given file path. It opens the file and uses its base name as the file name.
type JsonData ¶
type JsonData any
JsonData is a type alias for any data that can be marshaled to JSON.
type ManagerOption ¶
type ManagerOption func(*managerConfig)
ManagerOption is a function type for configuring a ConcurrencyManager.
func WithContext ¶
func WithContext(ctx context.Context) ManagerOption
WithContext sets a master context for the entire set of concurrent tasks, useful for global timeouts or cancellations.
func WithPoolSize ¶
func WithPoolSize(size int) ManagerOption
WithPoolSize sets the size of the concurrency pool, which determines the number of worker goroutines running in parallel.
type Middleware ¶
Middleware defines the function signature for a requesto middleware.
type Next ¶
Next defines the next handler in the middleware chain. A middleware must call the next function to pass the request along to the next handler.
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request represents a single HTTP request that can be configured and sent.
func (*Request) CookiesMap ¶
CookiesMap parses any cookies set in the request headers and returns them as a map.
func (*Request) JoinPath ¶
JoinPath intelligently appends a path segment to the request's URL. If the provided path `p` is an absolute URL, it will replace the current request URL entirely. Otherwise, it joins the path to the existing URL's path.
func (*Request) SetBinary ¶
SetBinary sets the request body to the provided raw byte slice. If the Content-Type header is not already set, it defaults to "application/octet-stream".
func (*Request) SetCookiesFromMap ¶
SetCookiesFromMap adds cookies to the client's underlying cookie jar from a map. This requires the client to have a valid BaseURL to determine the cookie domain.
func (*Request) SetFiles ¶
SetFiles sets the files to be uploaded as part of a multipart/form-data request.
func (*Request) SetFormData ¶
SetFormData sets the request body to be form-urlencoded from the provided map. It also sets the Content-Type header to "application/x-www-form-urlencoded".
func (*Request) SetHeaders ¶
SetHeaders sets the request headers.
func (*Request) SetJsonData ¶
SetJsonData sets the request body to be JSON-encoded from the provided data (struct or map). It also sets the Content-Type header to "application/json; charset=utf-8".
type Response ¶
Response wraps the standard http.Response to provide convenient access to the response body and other common properties. It also stores any error that occurred during the reading of the response body.
func Get ¶
Get sends a GET request to the specified URL. It accepts optional arguments, which can be of type Params or Headers. Body-related arguments are ignored. An error is returned for any unsupported argument type.
func Post ¶
Post sends a convenient POST request.
It accepts a URL and one or more optional arguments, which can be: - Headers: to set request headers. - Params: to set URL query parameters. - AsJson, AsForm, or AsFiles: to set the request body.
func (*Response) Cookies ¶
Cookies returns a slice of *http.Cookie containing all cookies set by the server in the response.
func (*Response) CookiesMap ¶
CookiesMap parses all response cookies into a map of name-value pairs.
func (*Response) StatusCode ¶
StatusCode returns the HTTP status code of the response. It returns -1 if an error occurred while reading the response body.