axios

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2020 License: Apache-2.0 Imports: 15 Imported by: 13

README

go-axios

Build Status

简单易用的HTTP客户端,参考axios的相关实现,支持各类不同的interceptortransform,特性如下:

  • 支持自定义的transform,可根据应用场景指定各类不同的参数格式
  • 支持Requset与Response的interceptror,可针对请求参数与响应数据添加各类的转换处理
  • 默认支持gzip与br两种压缩方式,更节约带宽占用
  • 支持启用请求中的各事件记录,可细化请求的各场景耗时
  • 简单易用的Mock处理
  • Error与Done事件的支持,通过监听事件可快捷收集服务的出错与性能统计
// +build ignore

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"time"

	"github.com/vicanso/go-axios"
)

func main() {
	// 使用默认的配置
	resp, err := axios.Get("https://www.baidu.com")
	if err != nil {
		panic(err)
	}
	fmt.Println(resp.Data)

	// 自定义instance,可指定client、interceptor等
	ins := axios.NewInstance(&axios.InstanceConfig{
		EnableTrace: true,
		Client: &http.Client{
			Transport: &http.Transport{
				Proxy: http.ProxyFromEnvironment,
			},
		},
		Timeout: 10 * time.Second,
	})
	resp, err = ins.Get("https://www.baidu.com/")
	if err != nil {
		panic(err)
	}
	buf, _ := json.Marshal(resp.Config.HTTPTrace.Stats())
	fmt.Println(resp.Config.HTTPTrace.Stats())
	fmt.Println(string(buf))
	fmt.Println(resp.Config.HTTPTrace.Protocol)
	fmt.Println(resp.Status)
	fmt.Println(string(resp.Data))
}

Documentation

Index

Constants

View Source
const (
	// UserAgent client user agent
	UserAgent = "go-axios/0.1.0"
)

Variables

View Source
var (
	// DefaultTransformResponse default transform response
	DefaultTransformResponse []TransformResponse
	// DefaultTransformRequest default transform request
	DefaultTransformRequest []TransformRequest
)

Functions

func SetJSONMarshal added in v0.0.6

func SetJSONMarshal(fn JSONMarshal)

SetJSONMarshal set json marshal function

func SetJSONUnmarshal added in v0.0.6

func SetJSONUnmarshal(fn JSONUnmarshal)

SetJSONUnmarshal set json unmarshal function

Types

type Adapter

type Adapter func(config *Config) (resp *Response, err error)

Adapter adapter function

type Config

type Config struct {
	Request  *http.Request
	Response *Response
	// Route the request route
	Route string
	// URL the request url
	URL string
	// Method http request method, default is `get`
	Method string
	// BaseURL http request base url
	BaseURL string
	// TransformRequest transform requset body
	TransformRequest []TransformRequest
	// TransformResponse transofrm response body
	TransformResponse []TransformResponse
	// Headers  custom headers for request
	Headers http.Header
	// Params params for request route
	Params map[string]string
	// Query query for requset
	Query url.Values

	// Body the request body
	Body interface{}

	// Concurrency current amount handling request of instance
	Concurrency uint32

	// Timeout request timeout
	Timeout time.Duration

	// Context context
	Context context.Context

	// Client http client
	Client *http.Client
	// Adapter custom handling of requset
	Adapter Adapter
	// RequestInterceptors request interceptor list
	RequestInterceptors []RequestInterceptor
	// ResponseInterceptors response interceptor list
	ResponseInterceptors []ResponseInterceptor

	// OnError on error function
	OnError OnError
	// OnDone on done event
	OnDone OnDone

	HTTPTrace *HT.HTTPTrace
	// contains filtered or unexported fields
}

Config http request config

func (*Config) Get

func (conf *Config) Get(key string) interface{}

Get get value from config

func (*Config) GetBool

func (conf *Config) GetBool(key string) bool

GetBool get bool value

func (*Config) GetInt

func (conf *Config) GetInt(key string) int

GetInt get int value

func (*Config) GetString

func (conf *Config) GetString(key string) string

GetString get string value

func (*Config) Set

func (conf *Config) Set(key string, value interface{})

Set set value to config

type Instance

type Instance struct {
	Config *InstanceConfig
	// contains filtered or unexported fields
}

Instance instance of axios

func GetDefaultInstance added in v0.0.5

func GetDefaultInstance() *Instance

GetDefaultInstance get default instanc

func NewInstance

func NewInstance(config *InstanceConfig) *Instance

NewInstance create a new instance

func (*Instance) Delete

func (ins *Instance) Delete(url string, query ...url.Values) (resp *Response, err error)

Delete http delete request

func (*Instance) Get

func (ins *Instance) Get(url string, query ...url.Values) (resp *Response, err error)

Get http get request

func (*Instance) Head

func (ins *Instance) Head(url string, query ...url.Values) (resp *Response, err error)

Head http head request

func (*Instance) Mock added in v0.0.4

func (ins *Instance) Mock(resp *Response) (done func())

Mock mock response

func (*Instance) MultiMock added in v0.1.0

func (ins *Instance) MultiMock(multi map[string]*Response) (done func())

MultiMock multi mock response

func (*Instance) Options

func (ins *Instance) Options(url string, query ...url.Values) (resp *Response, err error)

Options http options request

func (*Instance) Patch

func (ins *Instance) Patch(url string, data interface{}, query ...url.Values) (resp *Response, err error)

Patch http patch request

func (*Instance) Post

func (ins *Instance) Post(url string, data interface{}, query ...url.Values) (resp *Response, err error)

Post http post request

func (*Instance) Put

func (ins *Instance) Put(url string, data interface{}, query ...url.Values) (resp *Response, err error)

Put http put request

func (*Instance) Request

func (ins *Instance) Request(config *Config) (resp *Response, err error)

Request http request

type InstanceConfig

type InstanceConfig struct {
	// BaseURL http request base url
	BaseURL string
	// TransformRequest transform requset body
	TransformRequest []TransformRequest
	// TransformResponse transofrm response body
	TransformResponse []TransformResponse
	// Headers  custom headers for request
	Headers http.Header
	// Timeout request timeout
	Timeout time.Duration

	// Client http client
	Client *http.Client
	// Adapter custom adapter
	Adapter Adapter

	// RequestInterceptors request interceptor list
	RequestInterceptors []RequestInterceptor
	// ResponseInterceptors response interceptor list
	ResponseInterceptors []ResponseInterceptor

	// EnableTrace enable http trace
	EnableTrace bool
	// OnError on error function
	OnError OnError
	// OnDone on done event
	OnDone OnDone
}

InstanceConfig config of instance

type JSONMarshal added in v0.0.6

type JSONMarshal func(interface{}) ([]byte, error)

JSONMarshal json marshal function type

type JSONUnmarshal added in v0.0.6

type JSONUnmarshal func([]byte, interface{}) error

JSONUnmarshal json unmarshal function type

type OnDone added in v0.0.6

type OnDone func(config *Config, resp *Response, err error)

OnDone on done event

type OnError

type OnError func(err error, config *Config) (newErr error)

OnError on error function

type RequestInterceptor

type RequestInterceptor func(config *Config) (err error)

RequestInterceptor requset interceptor

type Response

type Response struct {
	Data    []byte
	Status  int
	Headers http.Header
	Config  *Config
	Request *http.Request
}

Response http response

func Delete added in v0.0.5

func Delete(url string) (resp *Response, err error)

Delete http delete request by default instance

func Get added in v0.0.5

func Get(url string) (resp *Response, err error)

Get http get request by default instance

func Head(url string) (resp *Response, err error)

Head http head request by default instance

func Options added in v0.0.5

func Options(url string) (resp *Response, err error)

Options http options request by default instance

func Patch added in v0.0.5

func Patch(url string, data interface{}) (resp *Response, err error)

Patch http patch request by default instance

func Post added in v0.0.5

func Post(url string, data interface{}) (resp *Response, err error)

Post http post request by default instance

func Put added in v0.0.5

func Put(url string, data interface{}) (resp *Response, err error)

Put http put request by default instance

func Request added in v0.0.5

func Request(config *Config) (resp *Response, err error)

Request http request by default instance

func (*Response) JSON

func (resp *Response) JSON(v interface{}) (err error)

JSON convert json data

type ResponseInterceptor

type ResponseInterceptor func(resp *Response) (err error)

ResponseInterceptor response interceptor

type TransformRequest

type TransformRequest func(body interface{}, headers http.Header) (data interface{}, err error)

TransformRequest transform function for http request

type TransformResponse

type TransformResponse func(body []byte, headers http.Header) (data []byte, err error)

TransformResponse transform function for http response

Jump to

Keyboard shortcuts

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