httpx

package
v1.3.28 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 34 Imported by: 5

Documentation

Index

Constants

View Source
const (
	ERR_NONE     = "none"
	ERR_CONNTION = "Connection Error"
	ERR_READ     = "Read Content Error"
	ERR_REQ      = "Request Error"
	ERR_JSON     = "Encode/Decode jsonx Error"
)
View Source
const (
	DefaultB0dyBufferSize = 2000
)

Variables

View Source
var (
	TagHttpX        = "hx_tag"
	TagFiledPlace   = "hx_place"
	TagFieldName    = "hx_query_name"
	TagFieldMust    = "hx_must"
	TagFieldDefault = "hx_default"
	TagFieldRange   = "hx_range"
)
View Source
var (
	NewHTTPError            = echo.NewHTTPError
	MIMEApplicationForm     = echo.MIMEApplicationForm
	HeaderContentType       = echo.HeaderContentType
	MIMEApplicationJSON     = echo.MIMEApplicationJSON
	MIMETextXML             = echo.MIMETextXML
	MIMEApplicationXML      = echo.MIMEApplicationXML
	MIMEMultipartForm       = echo.MIMEMultipartForm
	ErrUnsupportedMediaType = echo.ErrUnsupportedMediaType
)
View Source
var ErrBadParams error = errors.New("Bad input params")
View Source
var StatsTotalReqs int64
View Source
var UserAgent string = "Json Http Client"

var ErrNot200OK error = errors.New("the response is not 200 OK")

Functions

func BindAndValidate

func BindAndValidate(c echo.Context, i interface{}) error

func GetRealIp

func GetRealIp(req *http.Request) string

func HttpGet

func HttpGet(url string, timeout int) (*http.Response, error)

func HttpGetBody

func HttpGetBody(url string, timeout int) (*http.Response, []byte, error)

func HttpPost

func HttpPost(url string, body interface{}, timeout int) (*http.Response, error)

func HttpPostBody

func HttpPostBody(url string, body interface{}, timeout int) (*http.Response, []byte, error)

func IsConnError

func IsConnError(err error) bool

func NewProxyTransport

func NewProxyTransport(proxyHost string, proxyPort int, isHttp bool) (*http.Transport, error)

func QueryMustParam

func QueryMustParam(c echo.Context, key string) (string, error)

func QueryOptionalParam

func QueryOptionalParam(c echo.Context, key string) (string, bool)

func RegisterHandle

func RegisterHandle(funcGetECodeSuccess, funcGetECodeInternalError, funcGetECodeBadRequest func() int,
	funcErrToECode, funcErrToHttpStatus func(error) int,
	funcECodeToStr func(int) string,
	funcNewRequestId func() string)

func ResponseToMap

func ResponseToMap(body []byte) (map[string]interface{}, error)

func ResultResp

func ResultResp(status, code int, result interface{}) error

func SendResp

func SendResp(c echo.Context, resp error) error

func StatusResp

func StatusResp(status int) error

func SuccessResp

func SuccessResp(result interface{}) error

func ValidateMust

func ValidateMust(input interface{}, keys ...string) error

func Wrap

func Wrap(err error) error

Types

type ApiGateway

type ApiGateway struct {
	Ctx       context.Context
	Echo      *echo.Echo
	Logger    *logrus.Logger
	LogConf   *LogConfig
	LogFormat logrus.Formatter
}

func NewApiGateway

func NewApiGateway(pCtx context.Context, lc *LogConfig, logFormat logrus.Formatter) (*ApiGateway, error)

func (*ApiGateway) RoutesToString

func (agw *ApiGateway) RoutesToString() string

func (*ApiGateway) Run

func (agw *ApiGateway) Run(ip, port string) error

func (*ApiGateway) Stop

func (agw *ApiGateway) Stop() error

type AuthJsonClient

type AuthJsonClient struct {
	Client JsonClient
	// contains filtered or unexported fields
}

func (*AuthJsonClient) DelR

func (c *AuthJsonClient) DelR(url string, headers map[string]string) (*resty.Response, error)

func (*AuthJsonClient) Get

func (c *AuthJsonClient) Get(url string, headers map[string]string) (typex.JsonMap, error)

func (*AuthJsonClient) Login

func (c *AuthJsonClient) Login(method, url string, headers map[string]string,
	data interface{}, tokenField string) (string, error)

func (*AuthJsonClient) Post

func (c *AuthJsonClient) Post(url string, headers map[string]string, data interface{}) (typex.JsonMap, error)

func (*AuthJsonClient) Put

func (c *AuthJsonClient) Put(url string, headers map[string]string, data interface{}) (typex.JsonMap, error)

func (*AuthJsonClient) Request

func (c *AuthJsonClient) Request(method, url string, headers map[string]string,
	data interface{}) (typex.JsonMap, error)

func (*AuthJsonClient) RequestR

func (c *AuthJsonClient) RequestR(result interface{}, method, url string, headers map[string]string,
	data interface{}) (*resty.Response, error)

type BindUnmarshaler

type BindUnmarshaler interface {
	// UnmarshalParam decodes and assigns a value from an form or query param.
	UnmarshalParam(param string) error
}

BindUnmarshaler is the interface used to wrap the UnmarshalParam method.

type Binder

type Binder interface {
	Bind(i interface{}, c echo.Context) error
}

Binder is the interface that wraps the Bind method.

type DefaultBinder

type DefaultBinder struct{}

DefaultBinder is the default implementation of the Binder interface.

func (*DefaultBinder) Bind

func (b *DefaultBinder) Bind(i interface{}, c echo.Context) (err error)

Bind implements the `Binder#Bind` function.

type DropWriter

type DropWriter struct {
}

func (*DropWriter) Write

func (dw *DropWriter) Write(p []byte) (int, error)

type HttpClient

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

func NewHttpClient

func NewHttpClient(transport http.RoundTripper) *HttpClient

func NewProxyClient

func NewProxyClient(proxyHost string, proxyPort int, isHttp bool) (*HttpClient, error)

func (*HttpClient) Get

func (pc *HttpClient) Get(url string, out io.Writer) (*Stats, error)

func (*HttpClient) GetAndDrop

func (pc *HttpClient) GetAndDrop(url string) (*Stats, error)

func (*HttpClient) PostJson

func (pc *HttpClient) PostJson(url string, obj interface{}) (*Stats, error)

func (*HttpClient) Purge

func (pc *HttpClient) Purge(url string) (*Stats, error)

type JsonClient

type JsonClient struct {
	IsHttps bool
	Host    string
	Port    int
	Timeout int // in milliseconds
	// contains filtered or unexported fields
}

func NewJsonClient

func NewJsonClient(host string, port int, timeout int) *JsonClient

func NewJsonClientWithRetry

func NewJsonClientWithRetry(host string, port, timeout, retryCount, retryWaitTime int) *JsonClient

func (*JsonClient) Client

func (c *JsonClient) Client() *resty.Client

func (*JsonClient) Del

func (c *JsonClient) Del(url string, headers map[string]string, data interface{}) (typex.JsonMap, error)

func (*JsonClient) DelR

func (c *JsonClient) DelR(url string, headers map[string]string) (*resty.Response, error)

func (*JsonClient) Get

func (c *JsonClient) Get(url string, headers map[string]string) (typex.JsonMap, error)

func (*JsonClient) GetR

func (c *JsonClient) GetR(url string, headers map[string]string) (*resty.Response, error)

func (*JsonClient) Post

func (c *JsonClient) Post(url string, headers map[string]string, data interface{}) (typex.JsonMap, error)

func (*JsonClient) PostR

func (c *JsonClient) PostR(url string, headers map[string]string, data interface{}) (*resty.Response, error)

func (*JsonClient) Put

func (c *JsonClient) Put(url string, headers map[string]string, data interface{}) (typex.JsonMap, error)

func (*JsonClient) PutR

func (c *JsonClient) PutR(url string, headers map[string]string, data interface{}) (*resty.Response, error)

func (*JsonClient) Request

func (c *JsonClient) Request(method, url string, headers map[string]string,
	data interface{}) (typex.JsonMap, error)

func (*JsonClient) RequestR

func (c *JsonClient) RequestR(result interface{}, method, url string, headers map[string]string,
	data interface{}) (*resty.Response, error)

func (*JsonClient) RequestTimeout

func (c *JsonClient) RequestTimeout(method, url string, headers map[string]string,
	data interface{}, timeout int) (typex.JsonMap, error)

func (*JsonClient) SetInsecure

func (c *JsonClient) SetInsecure()

func (*JsonClient) SetReuseConnection

func (c *JsonClient) SetReuseConnection()

func (*JsonClient) SetStatsChan

func (c *JsonClient) SetStatsChan(ch chan<- *RequestStats)

func (*JsonClient) SetTransport

func (c *JsonClient) SetTransport(maxIdleConns, maxIdleConnsPerHost int)

func (*JsonClient) StatusError

func (c *JsonClient) StatusError(method, url string, rsp *resty.Response) *StatusError

func (*JsonClient) Transport

func (c *JsonClient) Transport() http.RoundTripper

func (*JsonClient) Url

func (c *JsonClient) Url(api string) string

type JsonResponse

type JsonResponse struct {
	RequestId *string `jsonx:"RequestId,omitempty"`
	Status    int     `jsonx:"-"`
	Code      *string `jsonx:"Code,omitempty"`
	CodeInt   *int    `jsonx:"CodeInt,omitempty"`
	Message   *string `jsonx:"Message,omitempty"`
	Result    any     `jsonx:"Result,omitempty"`
	// contains filtered or unexported fields
}

func ErrStrResp

func ErrStrResp(status, code int, msg string) *JsonResponse

func ErrorResp

func ErrorResp(status, code int, err error) *JsonResponse

func (*JsonResponse) Error

func (e *JsonResponse) Error() string

func (*JsonResponse) IsNoContent

func (e *JsonResponse) IsNoContent() bool

func (*JsonResponse) ToString

func (e *JsonResponse) ToString() string

func (*JsonResponse) Unwrap

func (e *JsonResponse) Unwrap() error

type LogConfig

type LogConfig struct {
	FileConfig     log.FileConfig
	Level          string
	BodyBufferSize int `vx_default:"2000"`
}

type RequestStats

type RequestStats struct {
	Scheme   string
	Host     string
	Port     int
	Uri      string
	Method   string
	ReqTime  time.Time
	SendTime time.Time
	RspTime  time.Time
	Latency  int64
	PreTime  int64
	Status   int
}

format := "${time_rfc3339} ${time_unix} ${status} ${method} ${latency} ${remote_ip} ${bytes_in} ${bytes_out} ${uri}\n"

func (*RequestStats) LogLine

func (s *RequestStats) LogLine() string

type RoundrobinClient

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

func NewRoundrobinClient

func NewRoundrobinClient(service string, https bool, hosts []string, port int,
	sign *auth.SignCfg, apiTimeout, retryTime int) *RoundrobinClient

func NewRoundrobinClient2

func NewRoundrobinClient2(service string, cl []*JsonClient, sign *auth.SignCfg, retryTime int) *RoundrobinClient

func (*RoundrobinClient) Client

func (c *RoundrobinClient) Client() *JsonClient

func (*RoundrobinClient) DelR

func (c *RoundrobinClient) DelR(url string, headers map[string]string) (*resty.Response, error)

func (*RoundrobinClient) Get

func (c *RoundrobinClient) Get(url string, headers map[string]string) (typex.JsonMap, error)

func (*RoundrobinClient) GetR

func (c *RoundrobinClient) GetR(url string, headers map[string]string) (*resty.Response, error)

func (*RoundrobinClient) Post

func (c *RoundrobinClient) Post(url string, headers map[string]string, data interface{}) (typex.JsonMap, error)

func (*RoundrobinClient) PostR

func (c *RoundrobinClient) PostR(url string, headers map[string]string, data interface{}) (*resty.Response, error)

func (*RoundrobinClient) Put

func (c *RoundrobinClient) Put(url string, headers map[string]string, data interface{}) (typex.JsonMap, error)

func (*RoundrobinClient) PutR

func (c *RoundrobinClient) PutR(url string, headers map[string]string, data interface{}) (*resty.Response, error)

func (*RoundrobinClient) Request

func (c *RoundrobinClient) Request(method, url string, headers map[string]string,
	data interface{}) (typex.JsonMap, error)

func (*RoundrobinClient) RequestR

func (c *RoundrobinClient) RequestR(result interface{}, method, url string, headers map[string]string, data interface{}) (*resty.Response, error)

light wrapper of RoundrobinClient.RequestR

func (*RoundrobinClient) SetInsecure

func (c *RoundrobinClient) SetInsecure()

func (*RoundrobinClient) SetReuseConnection

func (c *RoundrobinClient) SetReuseConnection()

must called before any request

func (*RoundrobinClient) SetTransport

func (c *RoundrobinClient) SetTransport(maxIdleConns, maxIdleConnsPerHost int)

must called before any request

func (*RoundrobinClient) StatusError

func (c *RoundrobinClient) StatusError(method, api string, rsp *resty.Response) *StatusError

func (*RoundrobinClient) Url

func (c *RoundrobinClient) Url(api string) string

type RrServer

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

type Stats

type Stats struct {
	Error         string
	Url           string
	Status        int
	Proto         string
	ContentLength int64
	DownloadSize  int64
	TimeToServe   float64 // in seconds
	RespHeader    http.Header
	Resp          *http.Response
}

func NewStats

func NewStats() *Stats

type StatusError

type StatusError struct {
	Err    string
	Status int
	Rsp    *resty.Response
}

func NewStatusError

func NewStatusError(method, host, api string, rsp *resty.Response) *StatusError

func NewStatusError2

func NewStatusError2(method, url string, rsp *resty.Response) *StatusError

func (*StatusError) Body

func (e *StatusError) Body() string

func (*StatusError) Error

func (e *StatusError) Error() string

Jump to

Keyboard shortcuts

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