httpclient

package
v0.49.11 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: MIT Imports: 21 Imported by: 0

README

简述

方便使用的http

功能

  • 链式调用
  • 超时设置
  • 代理
  • 缓存

使用

基本使用

client := zerohttpclient.NewClient()

ctx := client.Get("https://www.keylala.cn")
result, _ := ctx.ToString()
fmt.Println(result)

添加参数

client := zerohttpclient.NewClient()

ctx := client.WithParams(map[string]interface{}{
    "id": "123456",
    "name": "zero",
}).Get("https://www.keylala.cn")


ctx = client.WithBody(map[string]interface{}{
    "id": "123456",
    "name": "zero",
}).Post("https://www.keylala.cn")


// 指定 Content-Type 格式,默认 application/x-www-form-urlencoded
ctx = client.WithBody(map[string]interface{}{
    "id": "123456",
    "name": "zero",
}).WithContextTypeJSON().Post("https://www.keylala.cn")

设置超时

默认连接超时时间 2 秒,每次调用超时时间 5 秒

client := zerohttpclient.NewClient().WithDialTimeout(time.Second * time.Duration(1)).WithTimeout(time.Second * time.Duration(2))
ctx := client.Get("https://www.keylala.cn")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithSign added in v0.30.6

func WithSign(client *HTTPClient, secret string, calcSignFN CalcSignHandler)

WithSign 自动签名中间件,自动添加 timestamp, nonce, sign 值 secret: 签名使用,签名方式见 github.com/zerogo-hub/zero-api-middleware/sign

zerohttpclient.WithSign(client, "secret")

Types

type AfterHandler added in v0.30.6

type AfterHandler func(ctx *Context)

AfterHandler 执行后中间件

type BeforeHandler added in v0.30.6

type BeforeHandler func(client *HTTPClient, method, url string) error

BeforeHandler 执行前中间件

type CalcSignHandler added in v0.47.11

type CalcSignHandler func(client *HTTPClient, secret string, values map[string]interface{}) (string, error)

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context 一次调用过程的上下文

func Connect added in v0.29.6

func Connect(url string) *Context

Connect .

func Delete added in v0.29.6

func Delete(url string) *Context

Delete .

func Get added in v0.29.6

func Get(url string) *Context

Get .

func Options added in v0.29.6

func Options(url string) *Context

Options .

func Patch added in v0.29.6

func Patch(url string) *Context

Patch .

func Post added in v0.29.6

func Post(url string) *Context

Post .

func Put added in v0.29.6

func Put(url string) *Context

Put .

func Trace added in v0.29.6

func Trace(url string) *Context

Trace .

func (*Context) Error added in v0.34.9

func (ctx *Context) Error() error

Error 获取错误信息

func (*Context) IsOK

func (ctx *Context) IsOK() bool

IsOK 结果是否正常

func (*Context) IsTimeout

func (ctx *Context) IsTimeout() bool

IsTimeout 是否超时

func (*Context) Request

func (ctx *Context) Request() *http.Request

Request 获取原始请求

func (*Context) Response

func (ctx *Context) Response() *http.Response

Response 获取原始响应

func (*Context) ToBytes added in v0.34.9

func (ctx *Context) ToBytes() ([]byte, error)

ToBytes .

func (*Context) ToJSON

func (ctx *Context) ToJSON(out interface{}) error

ToJSON .

func (*Context) ToString

func (ctx *Context) ToString() (string, error)

ToString .

type HTTPClient

type HTTPClient struct {
	// contains filtered or unexported fields
}

HTTPClient 封装客户端,可以使用该客户端重复调用

var DefaultClient *HTTPClient

DefaultClient 默认全局对象

func NewClient

func NewClient() *HTTPClient

NewClient .

func WithBody added in v0.29.6

func WithBody(body map[string]interface{}) *HTTPClient

WithBody 设置 body,interface{} 可以设置为 string 或者 []string 也可以替换为 int8, []int8 等其它基础类型

func WithCache added in v0.29.6

func WithCache(isCache bool) *HTTPClient

WithCache 设置缓存开关

func WithCacheTTL added in v0.29.6

func WithCacheTTL(ttl time.Duration) *HTTPClient

WithCacheTTL 设置缓存时长

func WithContextTypeJSON added in v0.29.6

func WithContextTypeJSON() *HTTPClient

WithContextTypeJSON 设置 Context-Type 格式

func WithContextTypeURLEncoded added in v0.29.6

func WithContextTypeURLEncoded() *HTTPClient

WithContextTypeURLEncoded 设置 Context-Type 格式

func WithCookie added in v0.29.6

func WithCookie(cookies ...*http.Cookie) *HTTPClient

WithCookie 设置本次调用的 cookie

func WithDebug added in v0.29.6

func WithDebug(isDebug bool) *HTTPClient

WithDebug 设置调试开关

func WithDefaultCache added in v0.29.6

func WithDefaultCache(isCache bool) *HTTPClient

WithDefaultCache 设置全局缓存开关

func WithDefaultHeaders added in v0.29.6

func WithDefaultHeaders(headers map[string]string) *HTTPClient

WithDefaultHeaders 设置默认消息头

func WithDialTimeout added in v0.29.6

func WithDialTimeout(timeout time.Duration) *HTTPClient

WithDialTimeout 设置拨号的超时时间

func WithHeader added in v0.29.6

func WithHeader(k, v string) *HTTPClient

WithHeader 设置本次调用的消息头

func WithHeaders added in v0.29.6

func WithHeaders(headers map[string]string) *HTTPClient

WithHeaders 设置本次调用的消息头

func WithLock added in v0.29.6

func WithLock() *HTTPClient

WithLock 手动加锁,会在调用结束时自动解锁

func WithLogger added in v0.29.6

func WithLogger(logger zerologger.Logger) *HTTPClient

WithLogger 设置日志

func WithParams added in v0.29.6

func WithParams(params map[string]interface{}) *HTTPClient

WithParams 设置 params

func WithProxy added in v0.29.6

func WithProxy(proxy string) *HTTPClient

WithProxy 设置代理地址

func WithTimeout added in v0.29.6

func WithTimeout(timeout time.Duration) *HTTPClient

WithTimeout 设置每次调用的超时时间

func (*HTTPClient) Body added in v0.30.6

func (client *HTTPClient) Body() map[string]interface{}

Body 获取 body

func (*HTTPClient) Connect

func (client *HTTPClient) Connect(url string) *Context

Connect .

func (*HTTPClient) DelectCache

func (client *HTTPClient) DelectCache(key string)

DelectCache 删除缓存

func (*HTTPClient) Delete

func (client *HTTPClient) Delete(url string) *Context

Delete .

func (*HTTPClient) Get

func (client *HTTPClient) Get(url string) *Context

Get .

func (*HTTPClient) Options

func (client *HTTPClient) Options(url string) *Context

Options .

func (*HTTPClient) Params added in v0.30.6

func (client *HTTPClient) Params() map[string]interface{}

Params 获取参数

func (*HTTPClient) Patch

func (client *HTTPClient) Patch(url string) *Context

Patch .

func (*HTTPClient) Post

func (client *HTTPClient) Post(url string) *Context

Post .

func (*HTTPClient) Put

func (client *HTTPClient) Put(url string) *Context

Put .

func (*HTTPClient) ToURLValues added in v0.34.9

func (client *HTTPClient) ToURLValues(kv map[string]interface{}) (url.Values, error)

ToURLValues 将 map[string]interface{} 解析成 map[string][]string

func (*HTTPClient) Trace

func (client *HTTPClient) Trace(url string) *Context

Trace .

func (*HTTPClient) WithAfters added in v0.30.6

func (client *HTTPClient) WithAfters(handlers ...AfterHandler) *HTTPClient

WithAfters 设置执行后函数

func (*HTTPClient) WithBefores added in v0.30.6

func (client *HTTPClient) WithBefores(handlers ...BeforeHandler) *HTTPClient

WithBefores 设置执行前函数

func (*HTTPClient) WithBody

func (client *HTTPClient) WithBody(body map[string]interface{}) *HTTPClient

WithBody 设置 body,Post, Put, Patch 时使用 body 格式: map[string]string 或者 map[string][]string

func (*HTTPClient) WithCache

func (client *HTTPClient) WithCache(isCache bool) *HTTPClient

WithCache 设置缓存开关

func (*HTTPClient) WithCacheKey added in v0.30.7

func (client *HTTPClient) WithCacheKey(cacheKey string) *HTTPClient

WithCacheKey 设置缓存 key,不设置则使用默认

func (*HTTPClient) WithCacheTTL

func (client *HTTPClient) WithCacheTTL(ttl time.Duration) *HTTPClient

WithCacheTTL 设置缓存时长

func (*HTTPClient) WithContextTypeJSON

func (client *HTTPClient) WithContextTypeJSON() *HTTPClient

WithContextTypeJSON 设置 Context-Type 格式

func (*HTTPClient) WithContextTypeURLEncoded

func (client *HTTPClient) WithContextTypeURLEncoded() *HTTPClient

WithContextTypeURLEncoded 设置 Context-Type 格式

func (*HTTPClient) WithCookie

func (client *HTTPClient) WithCookie(cookies ...*http.Cookie) *HTTPClient

WithCookie 设置本次调用的 cookie

func (*HTTPClient) WithDebug

func (client *HTTPClient) WithDebug(isDebug bool) *HTTPClient

WithDebug 设置调试开关

func (*HTTPClient) WithDefaultAfters added in v0.30.6

func (client *HTTPClient) WithDefaultAfters(handler ...AfterHandler) *HTTPClient

WithDefaultAfters 设置默认执行后函数

func (*HTTPClient) WithDefaultBefores added in v0.30.6

func (client *HTTPClient) WithDefaultBefores(handlers ...BeforeHandler) *HTTPClient

WithDefaultBefores 设置默认执行前函数

func (*HTTPClient) WithDefaultCache

func (client *HTTPClient) WithDefaultCache(isCache bool) *HTTPClient

WithDefaultCache 设置全局缓存开关

func (*HTTPClient) WithDefaultHeaders

func (client *HTTPClient) WithDefaultHeaders(headers map[string]string) *HTTPClient

WithDefaultHeaders 设置默认消息头

func (*HTTPClient) WithDialTimeout

func (client *HTTPClient) WithDialTimeout(timeout time.Duration) *HTTPClient

WithDialTimeout 设置拨号的超时时间

func (*HTTPClient) WithHeader

func (client *HTTPClient) WithHeader(k, v string) *HTTPClient

WithHeader 设置本次调用的消息头

func (*HTTPClient) WithHeaders

func (client *HTTPClient) WithHeaders(headers map[string]string) *HTTPClient

WithHeaders 设置本次调用的消息头

func (*HTTPClient) WithIgnoreTLS added in v0.48.11

func (client *HTTPClient) WithIgnoreTLS(isIgnore bool) *HTTPClient

func (*HTTPClient) WithLock

func (client *HTTPClient) WithLock() *HTTPClient

WithLock 手动加锁,会在调用结束时自动解锁

func (*HTTPClient) WithLogger

func (client *HTTPClient) WithLogger(logger zerologger.Logger) *HTTPClient

WithLogger 设置日志

func (*HTTPClient) WithParams

func (client *HTTPClient) WithParams(params map[string]interface{}) *HTTPClient

WithParams 设置 params, GET, Delete 时使用

func (*HTTPClient) WithProxy

func (client *HTTPClient) WithProxy(proxy string) *HTTPClient

WithProxy 设置代理地址

func (*HTTPClient) WithTimeout

func (client *HTTPClient) WithTimeout(timeout time.Duration) *HTTPClient

WithTimeout 设置每次调用的超时时间

Jump to

Keyboard shortcuts

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