HttpClient

package module
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2023 License: Apache-2.0 Imports: 14 Imported by: 1

README

golang http客户端工具

支持功能:

  1. 重试机制
  2. 请求唯一标识
  3. 通过回调完成结果处理
  4. 自定义header头,同时处理并发时header头问题
  5. 支持post/get 请求

下载方式

go get -u gitee.com/tym_hmm/go-httpclient

使用方式

/**
自定义超时时间请求
@param timeOut int 超时时间
**/

[使用方法]
client := HttpClient.NewHttpClient()
//第一个参数为超时时间(精到秒)
client.SetOutTime(5, 5)
//注意 使用常用header 尽量保持首字符大写 如:Content-Type
headerMap := map[string]string{
     "Content-Type": "application/x-www-form-urlencoded",
}
httpClient.AddHeader(headerMap)
//设置传参
dataParam := make(map[string]string)
dataParam["notifyIdentifier"] = "21321"
dataParam["userIdentifier"] = "21321"
dataParam["extraInfo"] = "21321"
dataParam["orderId"] = "21321"

httpClient.Init(url, dataParam)
httpClient.SetSuccessListener(func(identifier string, response *Request.HttpResponse) {
    //content := response.Content
})
httpClient.SetErrorListener(func(s string, httpError *Request.HttpError) {
    
})
//发送post请求
httpClient.HttpPostReTryTimeOut()
//发送get请求
client.HttpGetReTryTimeOut()

接口说明

  • 设置cookie
//设置cookie
SetCookie(cookie []*http.Cookie)

  • 设置超时时间
SetOutTime(time int, typeTime TimeOutType) 
名称 类型 说明
time int 时间值,根据typeTime 指定秒还是毫秒
typeTime TimeOutType 时间类型
TIME_OUT_TYPE_SECOND(秒)
TIME_OUT_TYPE_MILLISECOND (毫秒)
  • 设置最大重试次数(默认为5次)
SetMaxRetryTime(retryTime int)
  • 错误回调
SetErrorListener(fn FnError)
  • 成功回调
 SetSuccessListener(fn FnSuccess)

Documentation

Index

Constants

View Source
const (
	HTTP_ERROR_TIME_OUT = 122 //超时
	HTTP_SUCCESS        = 200 //正常
	HTTP_ERROR          = 101 //错误

	HTTP_TIME_OUT     = 5 //默认超时时间(秒)
	HTTP_MAX_TRY_TIME = 5 //默认最大重试次数

	TIME_OUT_TYPE_SECOND      TimeOutType = 11
	TIME_OUT_TYPE_MILLISECOND TimeOutType = 12
)

Variables

View Source
var (
	DEFAULT_NODE_ID int64 = 1
)

Functions

func HttpGet

func HttpGet(httpClient *httpClient, httpUrl string, data interface{}, header map[string]string, cookie []*http.Cookie, timeOut int) (*HttpResponse, *HttpError)

* 发送get请求 * @param httpUrl string 请求地址 * @param header map[string]string 请求头 * @param timeOut 超时时间(秒)

func HttpGetWithCtx added in v1.0.17

func HttpGetWithCtx(ctx context.Context, httpClient *httpClient, httpUrl string, data interface{}, header map[string]string, cookie []*http.Cookie, timeOut int) (*HttpResponse, *HttpError)

func HttpPost

func HttpPost(httpClient *httpClient, httpUrl string, data interface{}, header map[string]string, cookie []*http.Cookie, timeOut int) (*HttpResponse, *HttpError)

* 发送post请求 * @param httpUrl string 请求地址 * @param data interface 发送数据 * @param header map[string]string 请求头 * @param timeOut 超时时间(秒)

func HttpPostJson

func HttpPostJson(httpUrl string, data []byte, timeOut int) (*HttpResponse, *HttpError)

* 发送post json

func HttpPostWithCtx added in v1.0.17

func HttpPostWithCtx(ctx context.Context, httpClient *httpClient, httpUrl string, data interface{}, header map[string]string, cookie []*http.Cookie, timeOut int) (*HttpResponse, *HttpError)

func NewHttpClient

func NewHttpClient() *httpClient

* 默认请求

func NewHttpClientNodeId

func NewHttpClientNodeId(nodeId int64) *httpClient

func NewHttpClientNodeIdWithCtx added in v1.0.17

func NewHttpClientNodeIdWithCtx(ctx context.Context, nodeId int64) *httpClient

func NewHttpClientTimeOut

func NewHttpClientTimeOut(timeOut int, nodeId int64) *httpClient

func NewHttpClientTimeOutCtx added in v1.0.17

func NewHttpClientTimeOutCtx(ctx context.Context, timeOut int, nodeId int64) *httpClient

func NewHttpClientWithCtx added in v1.0.17

func NewHttpClientWithCtx(ctx context.Context) *httpClient

Types

type FnError

type FnError func(string, *HttpError)

type FnSuccess

type FnSuccess func(string, *HttpResponse)

type HttpError

type HttpError struct {
	Code    int
	Error   error
	TryTime int
}

type HttpResponse

type HttpResponse struct {
	Resp         *http.Response
	StatusCode   int    //请求状态
	Content      string //内容
	ContentBytes []byte //内容数组
	Cookie       []*http.Cookie
	Header       http.Header
	TryTime      int
}

* 客户端访问请求 @param url string 发送地址 @param data interface{} 发送数据 @param contentType string 数据类型 @param timeout int 超时时间(秒)

type TimeOutType

type TimeOutType int

type Worker

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

定义一个woker工作节点所需要的基本参数

func NewWorker

func NewWorker(workerId int64) (*Worker, error)

func (*Worker) GetId

func (w *Worker) GetId() int64

接下来我们开始生成id 生成方法一定要挂载在某个woker下,这样逻辑会比较清晰 指定某个节点生成id

Jump to

Keyboard shortcuts

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