Giter Site home page Giter Site logo

go-axios's Introduction

go-axios

Build Status

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

  • 支持自定义的transform,可根据应用场景指定各类不同的参数格式
  • 支持Request与Response的interceptor,可针对请求参数与响应数据添加各类的转换处理
  • 默认支持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{
		BaseURL:     "https://www.baidu.com",
		EnableTrace: true,
		Client: &http.Client{
			Transport: &http.Transport{
				Proxy: http.ProxyFromEnvironment,
			},
		},
		// 超时设置,建议设置此字段避免无接口无超时处理
		Timeout: 10 * time.Second,
		// OnDone 无论成功或失败均会调用,可在此处添加统计
		OnDone: func(config *axios.Config, resp *axios.Response, err error) {
			fmt.Println(config)
			fmt.Println(resp)
			fmt.Println(err)
		},
	})
	resp, err = ins.Get("/")
	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))
}

go-axios's People

Contributors

vicanso avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.