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
- func CheckStatus(resp *http.Response, expected int) (err error)
- func ParseHTTPURL(s string) (u *url.URL, err error)
- func UserAgent() (ua string)
- type Client
- func (c *Client) Get(ctx context.Context, u *url.URL) (resp *http.Response, err error)
- func (c *Client) Post(ctx context.Context, u *url.URL, contentType string, body io.Reader) (resp *http.Response, err error)
- func (c *Client) Put(ctx context.Context, u *url.URL, contentType string, body io.Reader) (resp *http.Response, err error)
- type ClientConfig
- type ServerError
- type StatusError
Constants ¶
const ( HdrValApplicationJSON = "application/json" HdrValTextCSV = "text/csv" HdrValTextHTML = "text/html" HdrValTextPlain = "text/plain" HdrValWildcard = "*" )
HTTP header value constants.
const RobotsDisallowAll = "User-agent: *\nDisallow: /\n"
RobotsDisallowAll is a predefined robots disallow all content.
Variables ¶
This section is empty.
Functions ¶
func CheckStatus ¶
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 ¶
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?
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 ¶
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 ¶
ClientConfig is the configuration structure for Client.
type ServerError ¶
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 ¶
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.