agdhttp

package
v0.0.0-...-b6e3791 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package agdhttp contains common constants, functions, and types for working with HTTP.

TODO(a.garipov): Consider moving all or some of this stuff to module golibs.

Index

Constants

View Source
const (
	HdrValApplicationJSON = "application/json"
	HdrValTextCSV         = "text/csv"
	HdrValTextHTML        = "text/html"
	HdrValTextPlain       = "text/plain"
	HdrValWildcard        = "*"
)

HTTP header value constants.

View Source
const RobotsDisallowAll = "User-agent: *\nDisallow: /\n"

RobotsDisallowAll is a predefined robots disallow all content.

Variables

This section is empty.

Functions

func CheckStatus

func CheckStatus(resp *http.Response, expected int) (err error)

CheckStatus returns a non-nil error with the data from resp if the status code in resp is not equal to expected. resp must be non-nil.

Any error returned will have the underlying type of *StatusError.

func ParseHTTPURL

func ParseHTTPURL(s string) (u *url.URL, err error)

ParseHTTPURL parses an absolute URL and makes sure that it is a valid HTTP(S) URL. All returned errors will have the underlying type *url.Error.

TODO(a.garipov): Define as a type?

func UserAgent

func UserAgent() (ua string)

UserAgent returns the ID of the service as a User-Agent string. It can also be used as the value of the Server HTTP header.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a wrapper around http.Client.

func NewClient

func NewClient(conf *ClientConfig) (c *Client)

NewClient returns a new client. c must not be nil.

func (*Client) Get

func (c *Client) Get(ctx context.Context, u *url.URL) (resp *http.Response, err error)

Get is a wrapper around http.Client.Get.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

See also go doc http.Client.Get.

func (*Client) Post

func (c *Client) Post(
	ctx context.Context,
	u *url.URL,
	contentType string,
	body io.Reader,
) (resp *http.Response, err error)

Post is a wrapper around http.Client.Post.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

See also go doc http.Client.Post.

func (*Client) Put

func (c *Client) Put(
	ctx context.Context,
	u *url.URL,
	contentType string,
	body io.Reader,
) (resp *http.Response, err error)

Put is a wrapper around http.Client.Do.

When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

type ClientConfig

type ClientConfig struct {
	// Timeout is the timeout for all requests.
	Timeout time.Duration
}

ClientConfig is the configuration structure for Client.

type ServerError

type ServerError struct {
	Err        error
	ServerName string
}

ServerError is returned as general error in case header Server was specified.

func WrapServerError

func WrapServerError(err error, resp *http.Response) (wrapped *ServerError)

WrapServerError wraps err inside a *ServerError including data from resp. resp must not be nil.

func (*ServerError) Error

func (err *ServerError) Error() (msg string)

Error implements the error interface for *ServerError.

func (*ServerError) Unwrap

func (err *ServerError) Unwrap() (unwrapped error)

Unwrap implements the errors.Wrapper interface for *ServerError.

type StatusError

type StatusError struct {
	ServerName string
	Expected   int
	Got        int
}

StatusError is returned by methods when the HTTP status code is different from the expected.

func (*StatusError) Error

func (err *StatusError) Error() (msg string)

Error implements the error interface for *StatusError.

Jump to

Keyboard shortcuts

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