superhttp

package module
v0.1.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

README

superhttp

Example

Method Get
header := superhttp.DefaultJsonHeader()
params := superhttp.NewParams()

resp := superhttp.Default().Get(ctx, url, params, header)
Method Post
header := superhttp.DefaultFormUrlEncodedHeader()
form := superhttp.NewForm().Add("name", "superwhys").Encode()

resp := superhttp.Default().Post(ctx, url, nil, header, form)
To get string resp
respStr, err := resp.BodyString()
To get Bytes resp
respBytes, err := resp.BodyBytes()
To get json resp
err := resp.BodyJson(&respStruct)
Headers
creat a new header

superhttp.NewHeader()

Add value to header

header.Add(key, value)

It also has a number of different headers built in

superhttp.DefaultJsonHeader() superhttp.DefaultFormUrlEncodedHeader() superhttp.DefaultFormHeader()

Params
creat a new params

superhttp.NewParams()

Add value to params

params.Add(key, value)

Get value from params

params.Get(key)

Form
creat a new form

superhttp.NewForm()

Add value to form

form.Add(key, value)

Encode form

form.Encode()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func Default

func Default() *Client

func New

func New(conf *Config) *Client

func (*Client) DoRequest

func (cli *Client) DoRequest(ctx context.Context, url, method string, queryParams Params, header *Header, body []byte, callBack ...HandleFunc) *Response

func (*Client) Get

func (cli *Client) Get(ctx context.Context, url string, queryParams Params, header *Header, callBack ...HandleFunc) *Response

func (*Client) Post

func (cli *Client) Post(ctx context.Context, url string, queryParams Params, header *Header, body []byte, callBack ...HandleFunc) *Response

func (*Client) SetTimeout

func (cli *Client) SetTimeout(timeout time.Duration)

func (*Client) Start

func (cli *Client) Start() *Context

func (*Client) Use

func (cli *Client) Use(handler ...HandleFunc)

type Config

type Config struct {
	RequestTimeOut time.Duration
	Proxy          string
}

type Context

type Context struct {
	Header *Header
	Method string
	Url    string
	Params Params
	Body   []byte

	Request      *http.Request
	Response     *http.Response
	ResponseBody []byte
	// contains filtered or unexported fields
}

func NewContext

func NewContext(client *Client) *Context

func (*Context) Abort

func (c *Context) Abort()

func (*Context) AddError

func (c *Context) AddError(err error)

func (*Context) Fetch

func (c *Context) Fetch(ctx context.Context) *Response

func (*Context) FormBody

func (c *Context) FormBody(form *Form) *Context

func (*Context) GetError

func (c *Context) GetError() error

func (*Context) Next

func (c *Context) Next()

func (*Context) SetBody

func (c *Context) SetBody(body []byte) *Context

func (*Context) SetHeaders

func (c *Context) SetHeaders(header *Header) *Context

func (*Context) SetMethod

func (c *Context) SetMethod(method string) *Context

func (*Context) SetQueryParams

func (c *Context) SetQueryParams(params Params) *Context

func (*Context) SetURL

func (c *Context) SetURL(url string) *Context

type Form

type Form struct {
	url.Values
}

func NewForm

func NewForm() *Form

func (*Form) Add

func (f *Form) Add(key, value string) *Form

func (*Form) Encode

func (f *Form) Encode() []byte

type HandleFunc

type HandleFunc func(c *Context)

func DefaultHTTPHandler

func DefaultHTTPHandler() HandleFunc

func DefaultResponseBodyHandler

func DefaultResponseBodyHandler() HandleFunc

func HandlerDebugDuration

func HandlerDebugDuration() HandleFunc

func HandlerDuration

func HandlerDuration() HandleFunc

func RequestBodyReaderHandler

func RequestBodyReaderHandler() HandleFunc

func RequestDefaultHeaderHandler

func RequestDefaultHeaderHandler() HandleFunc

func RequestParamsHandler

func RequestParamsHandler() HandleFunc

type HandlerGroup

type HandlerGroup struct {
	Handlers HandlersChain
	// contains filtered or unexported fields
}

func (*HandlerGroup) Use

func (group *HandlerGroup) Use(handler ...HandleFunc)

type HandlersChain

type HandlersChain []HandleFunc
type Header struct {
	http.Header
}

func DefaultFormHeader

func DefaultFormHeader() *Header

func DefaultFormUrlEncodedHeader

func DefaultFormUrlEncodedHeader() *Header

func DefaultJsonHeader

func DefaultJsonHeader() *Header

func NewHeader

func NewHeader() *Header

func (*Header) Add

func (h *Header) Add(key, value string) *Header

type Params

type Params map[string]string

func NewParams

func NewParams() Params

func (Params) Add

func (p Params) Add(key, value string) Params

func (Params) Get

func (p Params) Get(key string) string

type Response

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

func (*Response) BodyBytes

func (r *Response) BodyBytes() ([]byte, error)

func (*Response) BodyJson

func (r *Response) BodyJson(v interface{}) error

func (*Response) BodyString

func (r *Response) BodyString() (string, error)

func (*Response) Error

func (r *Response) Error() error

Jump to

Keyboard shortcuts

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