httpclient

package
v0.0.0-...-9c91b52 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2022 License: GPL-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package httpclient provides a simple http client interface to access web resource.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Timeout time.Duration
	UA      string
	// contains filtered or unexported fields
}

Client is a simple wrap of http client. It provides a simple way to handle cookies, UA, and timeouts.

func New

func New(o *Options) (*Client, error)

New creates a new Client with given option.

func (*Client) Get

func (c *Client) Get(url string) *Request

Get creates HTTP GET request.

func (*Client) PostForm

func (c *Client) PostForm(url string, values url.Values) *Request

PostForm creates a HTTP POST request with form body.

func (*Client) PostJSON

func (c *Client) PostJSON(url string, body interface{}) *Request

PostJSON creates a HTTP POST request with json body.

type Options

type Options struct {
	// Proxy sets a http proxy. It uses environments if is empty.
	// The proxy type is determined by the URL scheme. "http",
	// "https", and "socks5" are supported. If the scheme is empty,
	// "http" is assumed.
	Proxy string
	// UA of HTTP request header User-Agent.
	UA string
	// Timeout of HTTP request.
	Timeout time.Duration
}

Options for creating a Client.

type Request

type Request struct {
	*http.Request
	Err    error
	Cancel func()
	Client *Client
}

A Request represents an HTTP request received by a server or to be sent by a client. It contains a vlaid request if Err != nil.

func (*Request) Invoke

func (req *Request) Invoke() *Response

Invode sends an HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth) as configured on the client.

type Response

type Response struct {
	*http.Response
	Request *Request
	Err     error
}

Response represents the response from an HTTP request. It contains a valid http response if Err != nil.

func (*Response) Drop

func (rsp *Response) Drop() error

Drop drop the response body and return a nil. It also returns error if HTTP response status code is not 200.

func (*Response) JSON

func (rsp *Response) JSON(o interface{}) error

JSON unmarshal HTTP response body into input object. It also returns error if HTTP response status code is not 200.

func (*Response) WriteTo

func (rsp *Response) WriteTo(w io.Writer) (int64, error)

WriteTo writes HTTP response body into a writer. It also returns error if HTTP response status code is not 200.

Jump to

Keyboard shortcuts

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