ghttpex

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GET     = Method("GET")
	HEAD    = Method("HEAD")
	POST    = Method("POST")
	PUT     = Method("PUT")
	PATCH   = Method("PATCH") // RFC 5789
	DELETE  = Method("DELETE")
	CONNECT = Method("CONNECT")
	OPTIONS = Method("OPTIONS")
	TRACE   = Method("TRACE")
)

Common HTTP methods.

Unless otherwise noted, these are defined in RFC 7231 section 4.3.

Variables

View Source
var DefaultClient = NewClient(&Options{})

DefaultClient client with default options

Functions

This section is empty.

Types

type Client

type Client struct {
	*http.Client
	// contains filtered or unexported fields
}

Client provides more useful features than http.Client

func NewClient

func NewClient(opt *Options) *Client

NewClient creates an httpex.Client object

func (*Client) Delete

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

Delete returns *Request with DELETE method.

func (*Client) Get

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

Get returns *Request with GET method.

func (*Client) Head

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

Head returns *Request with HEAD method.

func (*Client) NewRequest

func (c *Client) NewRequest(method Method, rawurl string) *Request

NewRequest returns *Request with specific method based on the Client

func (*Client) Post

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

Post returns *Request with POST method.

func (*Client) Put

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

Put returns *Request with PUT method.

type Filter

type Filter func(req *Request) (*http.Response, error)

type FilterChain

type FilterChain func(next Filter) Filter

type Logger

type Logger interface {
	Println(v ...interface{})
}

Logger use in Client to log something

type Method

type Method string

type Options

type Options struct {
	Logger       Logger
	Timeout      time.Duration // timeout for http.Client
	Retries      int           // 0 means no retried; -1 means retried forever; others means retried times.
	RetryDelay   time.Duration // the delay between each request when set Retries
	FilterChains []FilterChain // set ordered filters to intercept each http request to do some custom things (like AOP)
	UserAgent    string        // default value for http header "User-Agent"
	ShowDebug    bool          // can get dump info by Request.DumpRequest after send request if set true.
	DumpBody     bool          // set whether need to dump the request body
}

Options includes useful client setting options

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

Request provides more useful methods for requesting one url than http.Request.

func Delete

func Delete(url string) *Request

Delete returns *Request with DELETE method based on DefaultClient

func Get

func Get(url string) *Request

Get returns *Request with GET method based on DefaultClient

func Head(url string) *Request

Head returns *Request with HEAD method based on DefaultClient

func Post

func Post(url string) *Request

Post returns *Request with POST method based on DefaultClient

func Put

func Put(url string) *Request

Put returns *Request with PUT method based on DefaultClient

func (*Request) BasicAuth

func (r *Request) BasicAuth(username, password string) *Request

BasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.

func (*Request) BearerAuth

func (r *Request) BearerAuth(token string) *Request

BearerAuth sets the request's Authorization header to use bearer token

func (*Request) Bytes

func (r *Request) Bytes(ctx context.Context) ([]byte, error)

Bytes returns the body []byte in response. it calls getResponse inner.

func (*Request) BytesBody

func (r *Request) BytesBody(data []byte) *Request

BytesBody adds request raw body by []byte

func (*Request) Command

func (r *Request) Command(command string) *Request

Command sets command in request manually

func (*Request) ContentType

func (r *Request) ContentType(value string) *Request

ContentType sets Content-Type header field

func (*Request) DoRequest

func (r *Request) DoRequest(ctx context.Context) (*http.Response, error)

DoRequest executes client.Do with extra options

func (*Request) DumpRequest

func (r *Request) DumpRequest() []byte

DumpRequest returns the dump request

func (*Request) GetCommand

func (r *Request) GetCommand() string

GetCommand gets command in request

func (*Request) GetFile

func (r *Request) GetFile(formname string) string

GetFile gets file in request by formname

func (*Request) GetFiles

func (r *Request) GetFiles() map[string]string

GetFiles gets files in request

func (*Request) GetHeader

func (r *Request) GetHeader() http.Header

GetHeader gets header in request

func (*Request) GetMethod

func (r *Request) GetMethod() Method

GetMethod gets method in request

func (*Request) GetParam

func (r *Request) GetParam(key string) string

GetParam gets the first param value in request by key

func (*Request) GetParams

func (r *Request) GetParams() map[string][]string

GetParams gets params in request

func (*Request) GetReqBody

func (r *Request) GetReqBody() []byte

GetReqBody returns the request body

func (*Request) GetURL

func (r *Request) GetURL() string

GetURL gets url in request

func (*Request) Header

func (r *Request) Header(key, value string) *Request

Header sets header item string in request

func (*Request) JSONBody

func (r *Request) JSONBody(obj interface{}) (*Request, error)

JSONBody adds request raw body encoding by JSON

func (*Request) Param

func (r *Request) Param(key, value string) *Request

Param adds query param in to request. params build query string as ?key1=value1&key2=value2...

func (*Request) PostFile

func (r *Request) PostFile(formname, filename string) *Request

PostFile adds a post file to the request

func (*Request) Response

func (r *Request) Response(ctx context.Context) (*http.Response, error)

Response executes request client gets response manually. The result (*http.Response) will be saved to the instance.

func (*Request) SetBody

func (r *Request) SetBody(body io.Reader) *Request

SetBody adds request body independently copied from net/http/request.go#NewRequestWithContext

func (*Request) SignFunc

func (r *Request) SignFunc(signFunc func(*Request) error) *Request

SignFunc adds a sign function to the request (can modify Request before sent such as set header)

func (*Request) StatusCode

func (r *Request) StatusCode() int

StatusCode gets response's status code (should be called after Response)

func (*Request) String

func (r *Request) String(ctx context.Context) (string, error)

ToString returns the body string in response. it calls getResponse inner.

func (*Request) StringBody

func (r *Request) StringBody(data string) *Request

StringBody adds request raw body by string

func (*Request) ToJSON

func (r *Request) ToJSON(ctx context.Context, v interface{}) error

ToJSON returns the map that marshals from the body bytes as json in response. it calls getResponse inner.

Jump to

Keyboard shortcuts

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