http

package
v0.0.0-...-f308040 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package http defines higher level helpers for the net/http package

Index

Constants

This section is empty.

Variables

View Source
var DefaultTransport = tls13Transport()

DefaultTransport is the default HTTP transport to use by default it is a transport that only allows TLS 1.3

View Source
var UserAgent string

UserAgent is the user agent that is used for requests

Functions

func ConstructURL

func ConstructURL(u *url.URL, params URLParameters) (string, error)

ConstructURL creates a URL with the included parameters.

func EnsureValidURL

func EnsureValidURL(s string, trailing bool) (string, error)

EnsureValidURL ensures that the input URL is valid to be used internally It does the following - Sets the scheme to https if none is given - It 'cleans' up the path using path.Clean - It makes sure that the URL ends with a / It returns an error if the URL cannot be parsed.

func JoinURLPath

func JoinURLPath(u string, p string) (string, error)

JoinURLPath joins url's path, in go 1.19 we can use url.JoinPath

func RegisterAgent

func RegisterAgent(client string, verApp string)

RegisterAgent registers the user agent for client and version

Types

type Client

type Client struct {
	// Client is the HTTP Client that sends the request
	Client *http.Client
	// ReadLimit denotes the maximum amount of bytes that are read in HTTP responses
	// This is used to prevent servers from sending huge amounts of data
	// A limit of 16MB, although maybe much larger than needed, ensures that we do not run into problems
	ReadLimit int64

	// Timeout denotes the default timeout for each request
	Timeout time.Duration
}

Client is a wrapper around http.Client with some convenience features - A default timeout of 5 seconds - A read limiter to prevent servers from sending large amounts of data - Checking on http code with custom errors

func NewClient

func NewClient(client *http.Client) *Client

NewClient returns a HTTP client with some default settings

func (*Client) Do

func (c *Client) Do(ctx context.Context, method string, urlStr string, opts *OptionalParams) (http.Header, []byte, error)

Do sends a HTTP request using a method (e.g. GET, POST), an url and optional parameters It returns the HTTP headers, the body and an error if there is one.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) (http.Header, []byte, error)

Get creates a Get request and returns the headers, body and an error.

type OptionalParams

type OptionalParams struct {
	Headers       http.Header
	URLParameters URLParameters
	Body          url.Values
	Timeout       time.Duration
}

OptionalParams is a structure that defines the optional parameters that are given when making a HTTP call.

type StatusError

type StatusError struct {
	URL    string
	Body   string
	Status int
}

StatusError indicates that we have received a HTTP status error.

func (*StatusError) Error

func (e *StatusError) Error() string

Error returns the StatusError as an error string.

type TimeoutError

type TimeoutError struct {
	URL    string
	Method string
}

TimeoutError indicates that we have gotten a timeout

func (*TimeoutError) Error

func (e *TimeoutError) Error() string

Error returns the TimeoutError as an error string.

type URLParameters

type URLParameters map[string]string

URLParameters is a type used for the parameters in the URL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳