xhttp

package
v0.0.0-...-1369677 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2022 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MethodGet 请求方法:GET
	MethodGet = http.MethodGet
	// MethodHead 请求方法:HEAD
	MethodHead = http.MethodHead
	// MethodPost 请求方法:POST
	MethodPost = http.MethodPost
	// MethodPut 请求方法:PUT
	MethodPut = http.MethodPut
	// MethodPatch 请求方法:PATCH
	MethodPatch = http.MethodPatch
	// MethodDelete 请求方法:DELETE
	MethodDelete = http.MethodDelete
	// MethodConnect 请求方法:CONNECT
	MethodConnect = http.MethodConnect
	// MethodOptions 请求方法:OPTIONS
	MethodOptions = http.MethodOptions
	// MethodTrace 请求方法:TRACE
	MethodTrace = http.MethodTrace

	// HeaderAccept 请求头:Accept
	HeaderAccept = "Accept"
	// HeaderContentType 请求头:Content-Type
	HeaderContentType = "Content-Type"
	// HeaderDate 请求头:Date
	HeaderDate = "Date"
	// HeaderUserAgent 请求头:User-Agent
	HeaderUserAgent = "User-Agent"
	// HeaderAuthorization 请求头:Authorization
	HeaderAuthorization = "Authorization"
	// HeaderLocation 请求头:Location
	HeaderLocation = "Location"
	// HeaderContentDisposition 请求头:Content-Disposition
	HeaderContentDisposition = "Content-Disposition"

	// HeaderGWErrorCode 自定义网关请求头:X-GW-Error-Code
	HeaderGWErrorCode = "X-GW-Error-Code"
	// HeaderGWErrorMessage 自定义网关请求头:X-GW-Error-Message
	HeaderGWErrorMessage = "X-GW-Error-Message"

	// ApplicationForm  应用类型:x-www-form-urlencoded
	ApplicationForm = "application/x-www-form-urlencoded"
	// ApplicationStream  应用类型:octet-stream
	ApplicationStream = "application/octet-stream"
	// ApplicationJSON  应用类型:json
	ApplicationJSON = "application/json"
	// ApplicationXML  应用类型:xml
	ApplicationXML = "application/xml"
	// ApplicationText  应用类型:text
	ApplicationText = "application/text"
	// ApplicationZip  应用类型:zip
	ApplicationZip = "application/zip"
)

Variables

This section is empty.

Functions

func CopyHttpRequest

func CopyHttpRequest(r *http.Request) (*http.Request, error)

CopyHttpRequest 复制请求体

func Error

func Error(w http.ResponseWriter, r *http.Request, err error)

Error 错误响应返回

func FromFile

func FromFile(r *http.Request, name string) (*multipart.FileHeader, error)

FromFile 请求表单文件获取

func GetClientIP

func GetClientIP(r *http.Request) string

GetClientIP 获取客户端的IP

func GetExternalIP

func GetExternalIP() (string, error)

GetExternalIP 通过API获取服务端的外部IP

func GetInternalIP

func GetInternalIP() string

GetInternalIP 获取服务端的内部IP

func GetQuery

func GetQuery(r *http.Request, key string) (string, bool)

GetQuery 返回给定请求查询参数键的字符串值并判断其是否存在

func GetQueryArray

func GetQueryArray(r *http.Request, key string) ([]string, bool)

GetQueryArray 返回给定请求查询参数键的字符串切片值并判断其是否存在

func GetTraceId

func GetTraceId(ctx context.Context) string

GetTraceId 获取链路追踪id

func NewDefaultHTTPClient

func NewDefaultHTTPClient() *http.Client

NewDefaultHTTPClient 新建默认HTTP客户端

func NewHTTPClient

func NewHTTPClient(c *Config) *http.Client

NewHTTPClient 新建HTTP客户端

func OkJson

func OkJson(w http.ResponseWriter, r *http.Request, v interface{})

OkJson 成功json响应返回

func Params

func Params(params ...interface{}) interface{}

Params 构建请求参数

func Parse

func Parse(r *http.Request, v interface{}) error

Parse 请求体解析

func ParseForm

func ParseForm(r *http.Request, v interface{}) error

ParseForm 请求表单解析

func Query

func Query(r *http.Request, key string) string

Query 返回给定请求查询参数键的字符串值

func QueryArray

func QueryArray(r *http.Request, key string) []string

QueryArray 返回给定请求查询参数键的字符串切片值

func WriteHeader

func WriteHeader(w http.ResponseWriter, err ...error)

WriteHeader 写入自定义响应header

Types

type ChainReq

type ChainReq struct {
	Id     int         `json:"id"`
	Method string      `json:"method"`
	Params interface{} `json:"params"`
}

ChainReq 区块链HTTP调用请求

type ChainResp

type ChainResp struct {
	Id     int             `json:"id"`
	Result json.RawMessage `json:"result"`
	Error  string          `json:"error"`
}

ChainResp 区块链HTTP调用响应

type Client

type Client struct {
	*http.Client
}

Client HTTP拓展客户端结构详情

func NewClient

func NewClient(c *Config) *Client

NewClient 新建HTTP拓展客户端

func NewClientWithHTTPClient

func NewClientWithHTTPClient(client *http.Client) *Client

NewClientWithHTTPClient 使用HTTP客户端新建HTTP拓展客户端

func NewDefaultClient

func NewDefaultClient() *Client

NewDefaultClient 新建默认HTTP拓展客户端

func (*Client) Call

func (c *Client) Call(method, rawurl string, header map[string]string, data io.Reader, resp interface{}) error

Call HTTP调用

func (*Client) CallChain

func (c *Client) CallChain(method, url string, header map[string]string, params, resp interface{}) error

CallChain 区块链HTTP调用

func (*Client) CallWithRequest

func (c *Client) CallWithRequest(req *http.Request, resp interface{}) (*http.Response, error)

CallWithRequest 利用HTTP请求进行HTTP调用

func (*Client) GetRequest

func (c *Client) GetRequest(method, rawurl string, header map[string]string, data io.Reader) (*http.Request, error)

GetRequest 获取HTTP请求

func (*Client) GetResponse

func (c *Client) GetResponse(req *http.Request) (*http.Response, []byte, error)

GetResponse 获取HTTP响应及其响应体内容

type Config

type Config struct {
	HTTPTimeout           time.Duration // HTTP请求超时时间
	DialTimeout           time.Duration // 拨号超时时间
	DialKeepAlive         time.Duration // 拨号保持连接时间
	MaxIdleConns          int           // 最大空闲连接数
	MaxIdleConnsPerHost   int           // 每个主机最大空闲连接数
	MaxConnsPerHost       int           // 每个主机最大连接数
	IdleConnTimeout       time.Duration // 空闲连接超时时间
	ResponseHeaderTimeout time.Duration // 读取响应头超时时间
	ExpectContinueTimeout time.Duration // 期望继续超时时间
	TLSHandshakeTimeout   time.Duration // TLS握手超时时间
	ForceAttemptHTTP2     bool          // 允许尝试启用HTTP/2
}

Config HTTP客户端相关配置

func GetDefaultConfig

func GetDefaultConfig() *Config

GetDefaultConfig 获取默认HTTP客户端相关配置

type DetailLoggedResponseWriter

type DetailLoggedResponseWriter struct {
	Writer *LoggedResponseWriter
	Buf    *bytes.Buffer
}

DetailLoggedResponseWriter 详细日志记录响应写入器

func NewDetailLoggedResponseWriter

func NewDetailLoggedResponseWriter(w http.ResponseWriter, r *http.Request) *DetailLoggedResponseWriter

NewDetailLoggedResponseWriter 新建详细日志记录响应写入器

func (*DetailLoggedResponseWriter) Flush

func (w *DetailLoggedResponseWriter) Flush()

Flush 实现Flush方法

func (*DetailLoggedResponseWriter) Header

Header 实现Header方法

func (*DetailLoggedResponseWriter) Hijack

Hijack 实现Hijack方法

func (*DetailLoggedResponseWriter) Write

func (w *DetailLoggedResponseWriter) Write(bs []byte) (int, error)

Write 实现Write方法

func (*DetailLoggedResponseWriter) WriteHeader

func (w *DetailLoggedResponseWriter) WriteHeader(code int)

WriteHeader 实现WriteHeader方法

type LoggedResponseWriter

type LoggedResponseWriter struct {
	W    http.ResponseWriter
	R    *http.Request
	Code int
}

LoggedResponseWriter 日志记录响应写入器

func NewLoggedResponseWriter

func NewLoggedResponseWriter(w http.ResponseWriter, r *http.Request) *LoggedResponseWriter

NewLoggedResponseWriter 新建日志记录响应写入器

func (*LoggedResponseWriter) Flush

func (w *LoggedResponseWriter) Flush()

Flush 实现Flush方法

func (*LoggedResponseWriter) Header

func (w *LoggedResponseWriter) Header() http.Header

Header 实现Header方法

func (*LoggedResponseWriter) Hijack

Hijack 实现Hijack方法

func (*LoggedResponseWriter) Write

func (w *LoggedResponseWriter) Write(bytes []byte) (int, error)

Write 实现Write方法

func (*LoggedResponseWriter) WriteHeader

func (w *LoggedResponseWriter) WriteHeader(code int)

WriteHeader 实现WriteHeader方法

type RPCClient

type RPCClient interface {
	// Call 进行 JSON-RPC 调用
	Call(method string, params ...interface{}) (*RPCResponse, error)
	// CallRaw 基于所给请求体进行 JSON-RPC 调用
	CallRaw(request *RPCRequest) (*RPCResponse, error)
	// CallFor 进行 JSON-RPC 调用并将响应结果反序列化到所给类型对象中
	CallFor(out interface{}, method string, params ...interface{}) error
}

RPCClient 通用 JSON-RPC 客户端接口

func NewRPCClient

func NewRPCClient(endpoint string, opts ...RPCOption) RPCClient

NewRPCClient 新建通用 JSON-RPC 客户端

type RPCOption

type RPCOption func(server *rpcClient)

RPCOption JSON-RPC 客户端可选配置

func WithCustomHeaders

func WithCustomHeaders(m map[string]string) RPCOption

WithCustomHeaders 使用配置的 HTTP 请求头

func WithHTTPClient

func WithHTTPClient(hc *http.Client) RPCOption

WithHTTPClient 使用配置的 HTTP 客户端

type RPCRequest

type RPCRequest struct {
	JSONRPC string      `json:"jsonrpc"`
	ID      int         `json:"id"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
}

RPCRequest 通用 JSON-RPC 请求体

func NewRPCRequest

func NewRPCRequest(method string, params ...interface{}) *RPCRequest

NewRPCRequest 新建通用 JSON-RPC 请求体

type RPCResponse

type RPCResponse struct {
	JSONRPC string      `json:"jsonrpc"`
	ID      int         `json:"id"`
	Result  interface{} `json:"result,omitempty"`
	Error   interface{} `json:"error,omitempty"`
}

RPCResponse 通用 JSON-RPC 响应体

func (*RPCResponse) GetBool

func (resp *RPCResponse) GetBool() (bool, error)

GetBool 获取响应结果的 bool 类型值

func (*RPCResponse) GetFloat64

func (resp *RPCResponse) GetFloat64() (float64, error)

GetFloat64 获取响应结果的 float64 类型值

func (*RPCResponse) GetInt64

func (resp *RPCResponse) GetInt64() (int64, error)

GetInt64 获取响应结果的 int64 类型值

func (*RPCResponse) GetString

func (resp *RPCResponse) GetString() (string, error)

GetString 获取响应结果的 string 类型值

func (*RPCResponse) ReadToObject

func (resp *RPCResponse) ReadToObject(to interface{}) error

ReadToObject 将响应结果反序列化到所给类型对象中

type Response

type Response struct {
	TraceId string      `json:"trace_id" example:"a1b2c3d4e5f6g7h8" extensions:"x-order=000"` // 链路追踪id
	Code    uint32      `json:"code" example:"200" extensions:"x-order=001"`                  // 状态码
	Msg     string      `json:"msg" example:"OK" extensions:"x-order=002"`                    // 消息
	Data    interface{} `json:"data" extensions:"x-order=003"`                                // 数据
}

Response 业务通用响应体

Jump to

Keyboard shortcuts

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