http

package
v0.1.368 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2025 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

func NewClient

func NewClient(d Dependencies, opts ClientOptions) (Client, error)

type ClientOption added in v0.1.14

type ClientOption func(*ClientOptions)

func WithTimeout added in v0.1.14

func WithTimeout(x time.Duration) ClientOption

type ClientOptions added in v0.1.14

type ClientOptions struct {
	Timeout time.Duration
}

func ApplyClientOptions added in v0.1.14

func ApplyClientOptions(opts *ClientOptions, modifiers ...ClientOption) ClientOptions

type DefaultTool added in v0.1.11

type DefaultTool struct {
	T *contract.Tool[Client]
}

func NewTool

func NewTool(c Client, opts contract.ToolOptions[Client]) *DefaultTool

func (*DefaultTool) CopyHeaders added in v0.1.11

func (d *DefaultTool) CopyHeaders(ctx context.Context, dst, src *http.Request, headers ...string) error

CopyHeaders copies the specified headers from the source request to the destination request. If no headers are specified, all headers from the source request will be copied to the destination request.

func (*DefaultTool) DecodeBody added in v0.1.11

func (d *DefaultTool) DecodeBody(ctx context.Context, r *http.Request, v any) error

DecodeBody reads and unmarshals the request body into the provided variable. It ensures the content type is JSON before attempting to decode.

func (*DefaultTool) Do added in v0.1.11

Do sends an HTTP request and returns an HTTP response.

func (*DefaultTool) GetHeaders added in v0.1.11

func (d *DefaultTool) GetHeaders(ctx context.Context, r *http.Request) (http.Header, error)

GetHeaders extracts and returns all headers from an HTTP request as an http.Header map.

func (*DefaultTool) NewRequest added in v0.1.11

func (d *DefaultTool) NewRequest(ctx context.Context, method, url string, opts contract.RequestOptions) (*http.Request, error)

NewRequest creates a new HTTP request with the specified method, URL, and options.

func (*DefaultTool) QueryString added in v0.1.11

func (d *DefaultTool) QueryString(ctx context.Context, pairs ...string) (string, error)

QueryString constructs a query parameter string from a variadic number of key-value pairs. The values passed should not already be URL-escaped, as the function will handle escaping.

func (*DefaultTool) RespondJSON added in v0.1.11

func (d *DefaultTool) RespondJSON(ctx context.Context, w http.ResponseWriter, v any, status int, escapeHTML bool) error

RespondJSON writes the JSON data to the response writer with appropriate headers. The Content-Type header is set to application/json. The Content-Length header is set with the length of the uncompressed data. The Content-Length is not set if the Content-Encoding header is set to gzip. The status code is set based on the provided code parameter.

func (*DefaultTool) ServeHTML added in v0.1.11

func (d *DefaultTool) ServeHTML(ctx context.Context, w http.ResponseWriter, body io.Reader, status int) error

ServeHTML writes an HTML response to the provided http.ResponseWriter with the given HTML content and HTTP status code.

func (*DefaultTool) SetQueryParams added in v0.1.326

func (d *DefaultTool) SetQueryParams(ctx context.Context, r *http.Request, q map[string][]string, override bool) error

SetQueryParams updates or appends query parameters to the request URL based on the override flag.

func (*DefaultTool) ValidateQueryParams added in v0.1.11

func (d *DefaultTool) ValidateQueryParams(ctx context.Context, q map[string][]string, rules ...contract.QueryParamRule) error

ValidateQueryParams is a function that validates the presence and values of mandatory query parameters in a given request. It takes a map of query parameters and a variadic slice of QueryParam pointers as arguments. The function iterates over each QueryParam in the mandatoryQueryParams slice and checks if its key is present in the queryParams map. If the key is not present and the Optional field of the QueryParam is set to false, an error is returned indicating that the query parameter is mandatory. If the key is not present but the Optional field of the QueryParam is set to true, the function skips the checks and continues with the next query parameter. For each query parameter that is present, the function checks if its values are valid according to the following rules:

  • The value cannot be an empty string.
  • The value must be one of the accepted values if the AcceptedValues field of the QueryParam is not nil and has at least one element.
  • The value cannot be a duplicate of a previously seen value for the same key.
  • If the MultipleValuesAllowed field of the QueryParam is set to false, the query parameter cannot have more than one value.

If any of these checks fail, an error is returned with a message indicating the nature of the failure. If all checks pass, the function returns nil, indicating that all mandatory query parameters are present and their values are valid.

type Tool added in v0.1.245

Jump to

Keyboard shortcuts

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