Documentation
¶
Index ¶
- Variables
- func CloneURL(o *url.URL) *url.URL
- func DetectContentTypeAndSize(filePath string) (string, int64, error)
- func Download(ctx context.Context, url string) ([]byte, error)
- func ZapJSONMarshal(obj zapcore.ObjectMarshaler) ([]byte, error)
- type AfterHookFunc
- type BaseParam
- type BeforeHookFunc
- type BodyParam
- func NewBodyParam(contentType string, value io.Reader) *BodyParam
- func NewFormBody(fields map[string]string, files map[string][]byte) (*BodyParam, error)
- func NewFormBodyBuilder(builder *FormDataBuilder) (*BodyParam, error)
- func NewJSONBody(obj any) (*BodyParam, error)
- func NewXMLBody(obj any) (*BodyParam, error)
- type Client
- func (c *Client) Do(ctx context.Context, req IRequest) (IResponse, error)
- func (c *Client) Execute(ctx context.Context, method, resource string, params ...IParam) (IResponse, error)
- func (c *Client) Get(ctx context.Context, resource string, params ...IParam) (IResponse, error)
- func (c *Client) Post(ctx context.Context, resource string, params ...IParam) (IResponse, error)
- func (c *Client) Put(ctx context.Context, resource string, params ...IParam) (IResponse, error)
- type CookieParam
- type FileParam
- type FormDataBuilder
- type FormDataParam
- type HeaderParam
- type IParam
- type IRequest
- type IResponse
- type OptionFn
- func WithAfterHook(hook AfterHookFunc) OptionFn
- func WithBaseURL(baseURL string) OptionFn
- func WithBeforeHook(hook BeforeHookFunc) OptionFn
- func WithCert(certPool *x509.CertPool, cert tls.Certificate) OptionFn
- func WithCheckRedirect(checkRedirect func(req *http.Request, via []*http.Request) error) OptionFn
- func WithCookies(u *url.URL, cookies ...*http.Cookie) OptionFn
- func WithGlobalHeader(header http.Header) OptionFn
- func WithJar(jar *cookiejar.Jar) OptionFn
- func WithTimeout(timeout time.Duration) OptionFn
- func WithTransport(transport http.RoundTripper) OptionFn
- type Request
- func (r *Request) AddCookie(name, value string) IRequest
- func (r *Request) AddFileBytes(fieldName, fileName string, bytes []byte) IRequest
- func (r *Request) AddFilePath(fieldName, filePath string) IRequest
- func (r *Request) AddFormItem(name, value string) IRequest
- func (r *Request) AddHeader(name, value string) IRequest
- func (r *Request) AddParam(param IParam) IRequest
- func (r *Request) AddParams(params ...IParam) IRequest
- func (r *Request) AddURLQuery(name, value string) IRequest
- func (r *Request) AddURLSegment(name, value, format string) IRequest
- func (r *Request) GetMethod() string
- func (r *Request) MakeRequestBody() (io.Reader, error)
- func (r *Request) MakeURL(baseURL *url.URL) (string, error)
- func (r *Request) SetBody(contentType string, value io.Reader) IRequest
- func (r *Request) SetFormBody(fields map[string]string, files map[string][]byte) IRequest
- func (r *Request) SetJSONBody(obj any) IRequest
- func (r *Request) SetXMLBody(obj any) IRequest
- func (r *Request) WithContentType(contentType string) IRequest
- func (r *Request) WrapperHTTPRequest(req *http.Request)
- type Response
- func (r *Response) Data() ([]byte, error)
- func (r *Response) ExplicitCloseBody() error
- func (r *Response) GetResponse() *http.Response
- func (r *Response) JSONUnmarshal(i any) error
- func (r *Response) Pipe(writer io.Writer) error
- func (r *Response) SaveFile(fileName string) error
- func (r *Response) StatusCode() int
- func (r *Response) XMLUnmarshal(i any) error
- type URLQueryParam
- type URLSegmentParam
- type WriterFunc
Constants ¶
This section is empty.
Variables ¶
var (
DefaultClient = New()
)
Functions ¶
func DetectContentTypeAndSize ¶
DetectContentTypeAndSize 发现文件的ContentType和大小
func ZapJSONMarshal ¶
func ZapJSONMarshal(obj zapcore.ObjectMarshaler) ([]byte, error)
ZapJSONMarshal Zap JSON 序列化
Types ¶
type BodyParam ¶
BodyParam 将参数作为HTTP Body携带,具体序列化方式通过参数内容类型而定 同一个request有且仅有一个BodyParam
func NewFormBody ¶ added in v0.1.8
NewFormBody 创建 multipart/form-data 格式的请求体 参数:fields 为表单字段 map,files 为文件字段 map(字段名 -> 文件内容)
func NewFormBodyBuilder ¶ added in v0.1.8
func NewFormBodyBuilder(builder *FormDataBuilder) (*BodyParam, error)
NewFormBodyBuilder 使用 FormDataBuilder 创建 multipart/form-data 格式的请求体
func NewJSONBody ¶
func NewXMLBody ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
type CookieParam ¶
CookieParam 将参数通过cookie携带
func NewCookieParam ¶
func NewCookieParam(cookie *http.Cookie) *CookieParam
func (CookieParam) ParamName ¶
func (p CookieParam) ParamName() string
type FileParam ¶
type FileParam struct {
Name string
FileName string
ContentType string
ContentLength int64
FileWriterFunc WriterFunc
}
FileParam 将文件作为参数携带(multipart/form-data)
func NewBytesFileParam ¶
func NewPathFileParam ¶
type FormDataBuilder ¶ added in v0.1.8
FormDataBuilder 用于构建 multipart/form-data 请求体
func NewFormDataBuilder ¶ added in v0.1.8
func NewFormDataBuilder() *FormDataBuilder
NewFormDataBuilder 创建一个新的 FormDataBuilder
func (*FormDataBuilder) AddField ¶ added in v0.1.8
func (fdb *FormDataBuilder) AddField(name, value string) *FormDataBuilder
AddField 添加字符串字段
func (*FormDataBuilder) AddFile ¶ added in v0.1.8
func (fdb *FormDataBuilder) AddFile(fieldName string, fileContent []byte) *FormDataBuilder
AddFile 添加文件内容
type FormDataParam ¶
type FormDataParam struct {
BaseParam
}
FormDataParam 将参数通过Form表单(multipart/form-data或application/x-www-form-urlencoded)携带
func NewFormDataParam ¶
func NewFormDataParam(name, value string) *FormDataParam
type HeaderParam ¶
type HeaderParam struct {
BaseParam
}
HeaderParam 将参数通过HTTP Header携带
func NewHeaderParam ¶
func NewHeaderParam(name, value string) *HeaderParam
type IRequest ¶
type IRequest interface {
AddParam(param IParam) IRequest
AddParams(params ...IParam) IRequest
AddCookie(name, value string) IRequest
AddHeader(name, value string) IRequest
AddURLQuery(name, value string) IRequest
AddURLSegment(name, value, format string) IRequest
AddFormItem(name, value string) IRequest
AddFileBytes(fieldName, fileName string, bytes []byte) IRequest
AddFilePath(fieldName, filePath string) IRequest
SetBody(contentType string, value io.Reader) IRequest
SetJSONBody(obj any) IRequest
SetXMLBody(obj any) IRequest
SetFormBody(fields map[string]string, files map[string][]byte) IRequest
WithContentType(contentType string) IRequest
MakeURL(baseURL *url.URL) (string, error)
GetMethod() string
MakeRequestBody() (io.Reader, error)
WrapperHTTPRequest(req *http.Request)
}
type IResponse ¶
type IResponse interface {
// GetResponse get http response
GetResponse() *http.Response
// StatusCode get http status code
StatusCode() int
// Data get response data
// it will automatically close response body
Data() ([]byte, error)
// Pipe : pipe response data to writer
// it will automatically close response body
Pipe(writer io.Writer) error
// JSONUnmarshal unmarshal response data to json
// it will automatically close response body
JSONUnmarshal(i any) error
// XMLUnmarshal unmarshal response data to xml
// it will automatically close response body
XMLUnmarshal(i any) error
// SaveFile save response data to file
// it will automatically close response body
SaveFile(fileName string) error
// ExplicitCloseBody close response body
// If you get a response but never access it's body,
// you should call this method to close response body.
// Otherwise, it will cause resource leak.
// Actually, you can use GetResponse().Body.Close() to close response body,
// but this method is more convenient and remind you to close response body.
ExplicitCloseBody() error
}
func NewResponse ¶
type OptionFn ¶
type OptionFn func(opt *option)
func WithBaseURL ¶
func WithCheckRedirect ¶
func WithGlobalHeader ¶
func WithTimeout ¶
func WithTransport ¶
func WithTransport(transport http.RoundTripper) OptionFn
type Request ¶
type Request struct {
Resource string
Method string
Cookies []*CookieParam
Headers []*HeaderParam
URLQueries []*URLQueryParam
URLSegments []*URLSegmentParam
FormItems []*FormDataParam
Files []*FileParam
Body *BodyParam
ContentType string
Err error
}
func NewRequest ¶
func (*Request) AddFileBytes ¶
func (*Request) AddFilePath ¶
func (*Request) AddFormItem ¶
func (*Request) AddURLQuery ¶
func (*Request) AddURLSegment ¶
func (*Request) SetFormBody ¶ added in v0.1.8
func (*Request) SetJSONBody ¶
func (*Request) SetXMLBody ¶
func (*Request) WithContentType ¶
func (*Request) WrapperHTTPRequest ¶
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
func (*Response) ExplicitCloseBody ¶ added in v0.1.5
func (*Response) GetResponse ¶
func (*Response) JSONUnmarshal ¶
func (*Response) StatusCode ¶
func (*Response) XMLUnmarshal ¶
type URLQueryParam ¶
type URLQueryParam struct {
BaseParam
}
URLQueryParam 将参数通过URL Query携带
func NewURLQueryParam ¶
func NewURLQueryParam(name, value string) *URLQueryParam
type URLSegmentParam ¶
URLSegmentParam 将参数通过URL segment携带 eg: path = "tag/:Resource",name = "Resource"
func NewURLSegmentParam ¶
func NewURLSegmentParam(name, value, format string) *URLSegmentParam
type WriterFunc ¶
func BytesWriter ¶
func BytesWriter(buff []byte) WriterFunc
func FileWriter ¶
func FileWriter(filePath string) WriterFunc