gresty

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2021 License: MIT Imports: 9 Imported by: 0

README

gresty package

    go http client library,base on go-resty.
    go-resty: https://github.com/go-resty/resty

usage

    go version 1.11+

    import "github.com/daheige/tigago/gresty"

    s := &gresty.Service{
        BaseUri: "http://localhost:1338/",
        Timeout: 2 * time.Second,
    }

    opt := &gresty.RequestOption{
        Data: map[string]interface{}{
            "id": "1234",
        },
        RetryCount:2,
    }

    res := s.Do("post", "v1/data", opt)
    log.Println("err: ", res.Err)
    log.Println("body:", res.Text())

    For other usage, please see the method in the gresty source package.

Documentation

Overview

Package gresty for go http client support get,post,delete,patch,put,head,file method. go-resty/resty: https:// github.com/go-resty/resty

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiStdRes

type ApiStdRes struct {
	Code    int
	Message string
	Data    interface{}
}

ApiStdRes 标准的api返回格式

type Option

type Option func(s *Service)

Option service option.

func WithBaseUri

func WithBaseUri(uri string) Option

WithBaseUri 设置请求地址url的前缀

func WithEnableKeepAlive

func WithEnableKeepAlive(b bool) Option

WithEnableKeepAlive 是否长连接模式,默认短连接

func WithProxy

func WithProxy(proxy string) Option

WithProxy 设置请求proxy

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout 请求超时时间

type Reply

type Reply struct {
	StatusCode int    // http request 返回status code
	Err        error  // 请求过程中,发生的error
	Body       []byte // 返回的body内容
}

Reply 请求后的结果 statusCode,body,error.

func (*Reply) Json

func (r *Reply) Json(data interface{}) error

Json 将响应的结果Reply解析到data 对返回的Reply.Body做json反序列化处理

func (*Reply) Text

func (r *Reply) Text() string

Text 返回Reply.Body文本格式

type RequestOption

type RequestOption struct {
	Method string // 请求的方法
	Url    string // 请求url

	RetryCount       int                        // 重试次数
	RetryWaitTime    time.Duration              // 重试间隔,默认100ms
	RetryMaxWaitTime time.Duration              // 重试最大等待间隔,默认2s
	RetryConditions  []resty.RetryConditionFunc // 重试条件,是一个函数切片

	Params  map[string]interface{} // get,delete的Params参数
	Data    map[string]interface{} // post请求form data表单数据
	Headers map[string]interface{} // header头信息

	// cookie参数设置
	Cookies        map[string]interface{} // cookie信息
	CookiePath     string                 // 可选参数
	CookieDomain   string                 // cookie domain可选
	CookieMaxAge   int                    // cookie MaxAge
	CookieHttpOnly bool                   // cookie httpOnly

	// 支持post,put,patch以json格式传递,[]int{1, 2, 3},map[string]string{"a":"b"}格式
	// json支持[],{}数据格式,主要是golang的基本数据类型,就可以
	// 直接调用SetBody方法,自动添加header头"Content-Type":"application/json"
	Json interface{}

	// 支持文件上传的参数
	FileName      string // 文件名称
	FileParamName string // 文件上传的表单file参数名称
}

RequestOption 请求参数设置

type Service

type Service struct {
	BaseUri         string        // 请求地址url的前缀
	Timeout         time.Duration // 请求超时限制
	Proxy           string        // 请求设置的http_proxy代理
	EnableKeepAlive bool          // 是否允许长连接方式请求接口,默认短连接方式
}

Service 请求句柄设置

func New

func New(opts ...Option) *Service

New 创建一个service实例

func (*Service) Do

func (s *Service) Do(method string, reqUrl string, opt *RequestOption) *Reply

Do 请求方法 method string 请求的方法get,post,put,patch,delete,head等 uri string 请求的相对地址,如果BaseUri为空,就必须是完整的url地址 opt *RequestOption 请求参数ReqOpt 短连接的形式请求api 关于如何关闭http connection https:// www.cnblogs.com/cobbliu/p/4517598.html

func (*Service) GetResult

func (s *Service) GetResult(resp *resty.Response, err error) *Reply

GetResult 处理请求的结果statusCode,body,error. 首先判断是否出错,然后判断http resp是否请求成功或有错误产生

func (*Service) NewRestyClient

func (s *Service) NewRestyClient() *resty.Client

NewRestyClient 创建一个resty client 创建一个resty客户端,支持post,get,delete,head,put,patch,file文件上传等 可以快速使用go-resty/resty上面的方法 参考文档: https:// github.com/go-resty/resty

func (*Service) ParseData

func (s *Service) ParseData(d map[string]interface{}) map[string]string

ParseData 解析ReqOpt Params和Data

func (*Service) Request

func (s *Service) Request(reqOpt *RequestOption, client *resty.Client) *Reply

Request 请求方法 resty.setBody: for struct and map data type defaults to 'application/json' SetBody method sets the request body for the request. It supports various realtime needs as easy. We can say its quite handy or powerful. Supported request body data types is `string`, `[]byte`, `struct`, `map`, `slice` and `io.Reader`. Body value can be pointer or non-pointer. Automatic marshalling for JSON and XML content type, if it is `struct`, `map`, or `slice`.

client.R().SetFormData method sets Form parameters and their values in the current request. It's applicable only HTTP method `POST` and `PUT` and requests content type would be set as `application/x-www-form-urlencoded`.

Jump to

Keyboard shortcuts

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