goHttp

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GetRequest  requestMethod = "GET"
	PostRequest requestMethod = "POST"
)

Variables

This section is empty.

Functions

func Init

func Init(client http.Client, logName string, options ...ClientOption)

Types

type ApiOption

type ApiOption func(apiServer *ApiServer)

func WithLogName

func WithLogName(logName string) ApiOption

自定义日志名

func WithReply

func WithReply(reply IReply) ApiOption

自定义业务响应结构 对 ApiServer.GET、ApiServer.POST 有效

type ApiServer

type ApiServer struct {
	Host string
	// contains filtered or unexported fields
}

func NewApiServer

func NewApiServer(host string, options ...ApiOption) ApiServer

func (*ApiServer) Get

func (server *ApiServer) Get(c context.Context, uri string, dataResult interface{}, options ...RequestOption) (int, error)

GET请求api,解析响应数据的data字段数据 @param uri 请求uri @param dataResult 响应结果data数据绑定 @param options 请求可省参数 @return int 响应码 @return error 错误

func (*ApiServer) GetParams

func (server *ApiServer) GetParams(c context.Context, uri string, params map[string]string, dataResult interface{}) (int, error)

GET请求api,解析响应数据的data字段数据 @param uri 请求uri @param params 请求参数 @param dataResult 响应结果data数据绑定 @return int 响应码 @return error 错误

func (*ApiServer) Post

func (server *ApiServer) Post(c context.Context, uri string, dataResult interface{}, options ...RequestOption) (int, error)

POST请求api,解析响应数据的data字段数据 @param uri 请求uri @param dataResult 响应结果data数据绑定 @param options 请求可省参数 @return int 响应码 @return error 错误

func (*ApiServer) PostJSON

func (server *ApiServer) PostJSON(c context.Context, uri string, body map[string]interface{}, dataResult interface{}) (int, error)

POST请求api,解析响应数据的data字段数据 @param uri 请求uri @param body 请求体 @param dataResult 响应结果data数据绑定 @return int 响应码 @return error 错误

func (*ApiServer) SimpleGet

func (server *ApiServer) SimpleGet(c context.Context, uri string, result interface{}, options ...RequestOption) error

GET请求api,解析整个响应数据 @param uri 请求uri @param result 响应结果数据绑定 @param options 请求可省参数 @return error 错误

func (*ApiServer) SimpleGetParams

func (server *ApiServer) SimpleGetParams(c context.Context, uri string, params map[string]string, result interface{}) error

GET请求api,解析整个响应数据 @param uri 请求uri @param params 请求参数 @param result 响应结果数据绑定 @return error

func (*ApiServer) SimplePost

func (server *ApiServer) SimplePost(c context.Context, uri string, result interface{}, options ...RequestOption) error

POST请求api,解析整个响应数据 @param uri 请求uri @param result 响应结果数据绑定 @param options 请求可省参数 @return error

func (*ApiServer) SimplePostJSON

func (server *ApiServer) SimplePostJSON(c context.Context, uri string, body map[string]interface{}, result interface{}) error

POST请求api,解析整个响应数据 @param uri 请求uri @param body 请求体 @param result 响应结果数据绑定 @return error

type ClientOption

type ClientOption func(client *globalClient)

func WithHeaderB3TraceFromCtx

func WithHeaderB3TraceFromCtx(key string) ClientOption

是否开启zipkin-B3追踪,将会在头部注入追踪数据,数据从上下文获取 key为从上下文获取b3.SpanContext数据的键名

type IReply

type IReply interface {
	GetCode() int
	GetUnknownCode() int
	GetSuccessCode() int
	GetMsg() string
	IsSuccess() bool
	UnmarshalData(dataResult interface{}) error
}

func DefaultReply

func DefaultReply() IReply

type Reply

type Reply struct {
	Msg  string      `json:"msg"`
	Code int         `json:"code"`
	Data interface{} `json:"data"`
}

func (*Reply) GetCode

func (r *Reply) GetCode() int

func (*Reply) GetMsg

func (r *Reply) GetMsg() string

func (*Reply) GetSuccessCode

func (r *Reply) GetSuccessCode() int

func (*Reply) GetUnknownCode

func (r *Reply) GetUnknownCode() int

func (*Reply) IsSuccess

func (r *Reply) IsSuccess() bool

func (*Reply) UnmarshalData

func (r *Reply) UnmarshalData(dataResult interface{}) error

type Request

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

func (*Request) Request

func (ro *Request) Request() *http.Request

type RequestOption

type RequestOption func(request *Request)

func WithAddHeaders

func WithAddHeaders(headers map[string]string) RequestOption

添加请求头

func WithFormParams

func WithFormParams(params map[string]string) RequestOption

添加请求表单

func WithJSONData

func WithJSONData(data interface{}) RequestOption

添加JSON请求体

Jump to

Keyboard shortcuts

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