Documentation ¶
Index ¶
- Variables
- func CtxGet(ctx context.Context, url string, opts ...RequestOption) ([]byte, error)
- func CtxPatch(ctx context.Context, url string, body io.Reader, opts ...RequestOption) ([]byte, error)
- func CtxPost(ctx context.Context, url string, body io.Reader, opts ...RequestOption) ([]byte, error)
- func DefaultClient() *http.Client
- func DoRequest(method string, url string, body io.Reader) (statusCode int, content []byte, err error)
- func DoRequestWithContext(ctx context.Context, method string, url string, opts []RequestOption, ...) (statusCode int, content []byte, err error)
- func DoRequestWithOptions(method string, url string, opts []RequestOption, body io.Reader) (statusCode int, content []byte, respHeaders http.Header, err error)
- func Get(url string, opts ...RequestOption) ([]byte, error)
- func Patch(url string, body io.Reader, opts ...RequestOption) ([]byte, error)
- func Post(url string, body io.Reader, opts ...RequestOption) ([]byte, error)
- func SetDefaultClient(client *http.Client)
- type JSONResult
- type Pagination
- type RequestOption
- type RespCode
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // WithHeader ... WithHeader = func(key string, values ...string) RequestOption { return func(req *http.Request) *http.Request { for _, value := range values { req.Header.Add(key, value) } return req } } // WithHeaders ... WithHeaders = func(header map[string][]string) RequestOption { return func(req *http.Request) *http.Request { for key, values := range header { for _, value := range values { req.Header.Add(key, value) } } return req } } // WithSetHeader set k,v in header WithSetHeader = func(key, value string) RequestOption { return func(req *http.Request) *http.Request { req.Header.Set(key, value) return req } } // WithoutHeader ... WithoutHeader = func(key string) RequestOption { return func(req *http.Request) *http.Request { req.Header.Del(key) return req } } // WithContentType ... WithContentType = func(contentType string) RequestOption { return func(req *http.Request) *http.Request { req.Header.Set("Content-Type", contentType) return req } } // WithContentTypeJSON set content type as json WithContentTypeJSON = func() RequestOption { return WithContentType("application/json") } // WithAuthToken set "Authorization" header with token WithAuthToken = func(token string) RequestOption { return func(req *http.Request) *http.Request { req.Header.Set("Authorization", token) return req } } // WithContext wrap request with context WithContext = func(ctx context.Context) RequestOption { return func(req *http.Request) *http.Request { return req.WithContext(ctx) } } )
Functions ¶
func CtxPatch ¶
func CtxPatch(ctx context.Context, url string, body io.Reader, opts ...RequestOption) ([]byte, error)
CtxPatch ...
func CtxPost ¶
func CtxPost(ctx context.Context, url string, body io.Reader, opts ...RequestOption) ([]byte, error)
CtxPost ...
func DoRequest ¶
func DoRequest(method string, url string, body io.Reader) (statusCode int, content []byte, err error)
DoRequest 进行HTTP请求
func DoRequestWithContext ¶
func DoRequestWithContext(ctx context.Context, method string, url string, opts []RequestOption, body io.Reader) (statusCode int, content []byte, err error)
DoRequestWithContext 进行HTTP请求
func DoRequestWithOptions ¶
func DoRequestWithOptions(method string, url string, opts []RequestOption, body io.Reader) (statusCode int, content []byte, respHeaders http.Header, err error)
DoRequestWithOptions 进行HTTP请求并返回响应头
func SetDefaultClient ¶
SetDefaultClient set client replace default client
Types ¶
type JSONResult ¶ added in v0.1.6
type JSONResult struct { Code RespCode `json:"code" example:"200"` Msg string `json:"msg" example:""` Data interface{} `json:"data,omitempty"` // Pagination 用于存放分页数据,对于不需要分页的 API,将不会出现这个字段 Pagination *Pagination `json:"pagination,omitempty"` }
JSONResult 表示 API 返回的数据
type Pagination ¶ added in v0.1.6
type Pagination struct { Page int `json:"page" example:"1"` Total int `json:"total" example:"1024"` PageSize int `json:"page_size" example:"20"` }
Pagination 分页的基本信息
Click to show internal directories.
Click to hide internal directories.