gout

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2019 License: Apache-2.0 Imports: 11 Imported by: 180

README

gout

gout 是go写的http 客户端,为提高工作效率而开发

Build Status codecov

内容

安装

env GOPATH=`pwd` go get github.com/guonaihong/gout

技能树

gout.png

迁移

GET POST PUT DELETE PATH HEAD OPTIONS

// 创建一个实例
// 也可以直接调用包里面的GET, POST方法
// 比如gout.GET(url)

g := gout.New(nil)

// 发送GET方法
g.GET(url).Do()

// 发送POST方法
g.POST(url).Do()

// 发送PUT方法
g.PUT(url).Do()

// 发送DELETE方法
g.DELETE(url).Do()

// 发送PATH方法
g.PATCH(url).Do()

// 发送HEAD方法
g.HEAD(url).Do()

// 发送OPTIONS
g.OPTIONS(url).Do()

group

路由组

g := New(nil)

v1 := g.Group(ts.URL + "/v1")
err := v1.POST("/login").Next(). // http://127.0.0.1:80/v1/login
    POST("/submit").Next().      // http://127.0.0.1:80/v1/submit
    POST("/read").Do()           // http://127.0.0.1:80/v1/read

if err != nil {
}

v2 := g.Group(ts.URL + "/v2")
err = v2.POST("/login").Next(). // http://127.0.0.1:80/v2/login
    POST("/submit").Next().     // http://127.0.0.1:80/v2/submit
    POST("/read").Do()          // http://127.0.0.1:80/v2/read

if err != nil {
}

query

  • SetQuery() 设置http 查询字符串
g := gout.New(nil)
code := 0

if err := g.GET(":8080/testquery").SetQuery(/*看下面支持的情况*/).Code(&code).Do(); err != nil {
}

/*
SetQuery支持的类型有
* string
* map[string]interface{},可以使用gout.H别名
* struct
* array, slice(长度必须是偶数)
*/

// string
SetQuery("check_in=2019-06-18&check_out=2018-06-18")

// gout.H 或者 map[string]interface{}
SetQuery(gout.H{
    "check_in":"2019-06-18",
    "check_out":"2019-06-18",
})

// struct
type testQuery struct {
    CheckIn string `query:checkin`
    CheckOut string `query:checkout`
}

SetQuery(&testQuery{CheckIn:2019-06-18, CheckOut:2019-06-18})

// array or slice
// ?active=enable&action=drop
SetQuery([]string{"active", "enable", "action", "drop"})`

http header

  • SetHeader() 设置http header
  • BindHeader() 解析响应http header

对gout来说,既支持客户端发送http header,也支持解码服务端返回的http header

type testHeader struct {
    CheckIn string `header:checkin`
    CheckOut string `header:checkout`
}

t := testheader{}

g := gout.New(nil)
code := 0

if err := g.GET(":8080/testquery").Code(&code).SetHeader(/*看下面支持的类型*/).BindHeader(&t).Do(); err != nil {
}
  • BindHeader支持的类型有
// struct
type testHeader struct {
    CheckIn string `header:checkin`
    CheckOut string `header:checkout`
}

结构体

  • SetHeader支持的类型有
/*
map[string]interface{},可以使用gout.H别名
struct
array, slice(长度必须是偶数)
*/

// gout.H 或者 map[string]interface{}
SetHeader(gout.H{
    "check_in":"2019-06-18",
    "check_out":"2019-06-18",
})

// struct
type testHeader struct {
    CheckIn string `header:checkin`
    CheckOut string `header:checkout`
}

SetHeader(&testHeader{CheckIn:2019-06-18, CheckOut:2019-06-18})

// array or slice
// -H active:enable -H action:drop
SetHeader([]string{"active", "enable", "action", "drop"})

http body

body
  • SetBody 设置string, []byte等类型数据到http body里面
  • BindBody bind body到string, []byte等类型变量里面

err := gout.New(nil).POST(url).SetBody(/*支持的类型如下*/).Do()

var s string
err := gout.New(nil).GET(url).BindBody(&s/*支持的指针类型变量如下*/).Do()

支持的类型有

  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • string
  • []byte
  • float32, float64

明确不支持的类型有

  • struct
  • array, slice
json
  • SetJSON() 设置请求http body为json
  • BindJSON() 解析响应http body里面的json到结构体里面

发送json到服务端,然后把服务端返回的json结果解析到结构体里面

type data struct {
    Id int `json:"id"`
    Data string `json:"data"`
}


var d1, d2 data
var httpCode int

g := gout.New(nil)

err := g.POST(":8080/test.json").SetJSON(&d1).BindJSON(&d2).Code(&httpCode).Do()
if err != nil || httpCode != 200{
    fmt.Printf("send fail:%s\n", err)
}
yaml
  • SetYAML() 设置请求http body为yaml
  • BindYAML() 解析响应http body里面的yaml到结构体里面

发送yaml到服务端,然后把服务端返回的yaml结果解析到结构体里面

type data struct {
    Id int `yaml:"id"`
    Data string `yaml:"data"`
}


var d1, d2 data
var httpCode int 

g := gout.New(nil)

err := g.POST(":8080/test.yaml").SetYAML(&d1).BindYAML(&d2).Code(&httpCode).Do()
if err != nil || httpCode != 200{
    fmt.Printf("send fail:%s\n", err)
}

xml
  • SetXML() 设置请求http body为xml
  • BindXML() 解析响应http body里面的xml到结构体里面

发送xml到服务端,然后把服务端返回的xml结果解析到结构体里面

type data struct {
    Id int `xml:"id"`
    Data string `xml:"data"`
}


var d1, d2 data
var httpCode int 

g := gout.New(nil)

err := g.POST(":8080/test.xml").SetXML(&d1).BindXML(&d2).Code(&httpCode).Do()
if err != nil || httpCode != 200{
    fmt.Printf("send fail:%s\n", err)
}

form
  • SetForm() 设置http body 为multipart/form-data格式数据

客户端发送multipart/form-data到服务端,curl用法等同go代码

curl -F mode=A -F text="good" -F voice=@./test.pcm -f voice2=@./test2.pcm url
  • 使用gout.H
code := 0
err := gout.New(nil).
    POST(":8080/test").
    SetForm(gout.H{"mode": "A",
        "text":   "good",
        "voice":  gout.FormFile("test.pcm"),
        "voice2": gout.FormMem("pcm")}).Code(&code).Do()

if err != nil {
    fmt.Printf("%s\n", err)
}   

if code != 200 {
}   
  • 使用结构体
type testForm struct {
    Mode string `form:"mode"`
    Text string `form:"text"`
    Voice string `form:"voice" form-file:"true"` //从文件中读取 
    Voice2 []byte `form:"voice2" form-mem:"true"`  //从内存中构造
}

type rsp struct{
    ErrMsg string `json:"errmsg"`
    ErrCode int `json:"errcode"`
}

t := testForm{}
r := rsp{}
code := 0

err := gout.New(nil).POST(url).SetForm(&t).ShoudBindJSON(&r).Code(&code).Do()
if err != nil {

}
callback

callback主要用在,服务端会返回多种格式body的场景

g := gout.New(nil)

r , errCode := Result{}, 0

g.GET(url).Callback(func(c *gout.Context) error {

    switch c.Code {
        case 200:
            c.BindJSON(&Result)
        case 500:
            c.BindBody(&errCode)
    }

    return nil
})
proxy
  • SetProxy 设置代理服务地址
package main

import (
	"fmt"
	"github.com/guonaihong/gout"
	"log"
)

func main() {
	var s string
	err := gout.Def().GET("www.qq.com").SetProxy("http://127.0.0.1:7000").BindBody(&s).Do()
	if err != nil {
		log.Println(err)
		return
	}

	fmt.Println(s)
}
  • SetCookies设置cookie, 可以设置一个或者多个cookie
package main

import (
        "fmt"
        "github.com/guonaihong/gout"
        "net/http"
)

func main() {

        // 发送两个cookie
        err := gout.GET(":1234/cookie").SetCookies(&http.Cookie{Name: "test1", Value: "test1"},
                &http.Cookie{Name: "test2", Value: "test2"}).Do()

        // 发送一个cookie
        err = gout.GET(":1234/cookie/one").SetCookies(&http.Cookie{Name: "test3", Value: "test3"}).Do()
        fmt.Println(err)
}

Documentation

Index

Constants

View Source
const (
	Get     = "GET"
	Post    = "POST"
	Put     = "PUT"
	Delete  = "DELETE"
	Patch   = "PATCH"
	Head    = "HEAD"
	Options = "OPTIONS"
)

Variables

View Source
var DefaultClient = http.Client{}

Functions

func DELETE

func DELETE(url string) *routerGroup

func GET

func GET(url string) *routerGroup
func HEAD(url string) *routerGroup

func OPTIONS

func OPTIONS(url string) *routerGroup

func PATCH

func PATCH(url string) *routerGroup

func POST

func POST(url string) *routerGroup

func PUT

func PUT(url string) *routerGroup

Types

type Context

type Context struct {
	Code int //http code
	Resp *http.Response
}

func (*Context) BindBody

func (c *Context) BindBody(obj interface{}) error

func (*Context) BindHeader

func (c *Context) BindHeader(obj interface{}) error

func (*Context) BindJSON

func (c *Context) BindJSON(obj interface{}) error

func (*Context) BindXML

func (c *Context) BindXML(obj interface{}) error

func (*Context) BindYAML

func (c *Context) BindYAML(obj interface{}) error

type Decoder

type Decoder interface {
	Decode(r io.Reader) error
}

type Encoder

type Encoder interface {
	Encode(w io.Writer) error
}

type FormFile

type FormFile = core.FormFile

type FormMem

type FormMem = core.FormMem

type Gout

type Gout struct {
	*http.Client
	// contains filtered or unexported fields
}

func Def

func Def() *Gout

default

func New

func New(c *http.Client) *Gout

func (*Gout) BindBody

func (g *Gout) BindBody(obj interface{}) *routerGroup

func (*Gout) BindHeader

func (g *Gout) BindHeader(obj interface{}) *routerGroup

func (*Gout) BindJSON

func (g *Gout) BindJSON(obj interface{}) *routerGroup

func (*Gout) BindXML

func (g *Gout) BindXML(obj interface{}) *routerGroup

func (*Gout) BindYAML

func (g *Gout) BindYAML(obj interface{}) *routerGroup

func (*Gout) Callback

func (g *Gout) Callback(cb func(*Context) error) *routerGroup

func (*Gout) Code

func (g *Gout) Code(httpCode *int) *routerGroup

func (*Gout) DELETE

func (g *Gout) DELETE(url string) *routerGroup

func (*Gout) Do

func (g *Gout) Do() (err error)

func (*Gout) GET

func (g *Gout) GET(url string) *routerGroup

func (*Gout) Group

func (g *Gout) Group(relativePath string) *routerGroup

func (*Gout) HEAD

func (g *Gout) HEAD(url string) *routerGroup

func (*Gout) Next

func (g *Gout) Next() *routerGroup

func (*Gout) OPTIONS

func (g *Gout) OPTIONS(url string) *routerGroup

func (*Gout) PATCH

func (g *Gout) PATCH(url string) *routerGroup

func (*Gout) POST

func (g *Gout) POST(url string) *routerGroup

func (*Gout) PUT

func (g *Gout) PUT(url string) *routerGroup

func (*Gout) SetBody

func (g *Gout) SetBody(obj interface{}) *routerGroup

func (*Gout) SetCookies

func (g *Gout) SetCookies(c ...*http.Cookie) *routerGroup

func (*Gout) SetForm

func (g *Gout) SetForm(obj interface{}) *routerGroup

func (*Gout) SetHeader

func (g *Gout) SetHeader(obj interface{}) *routerGroup

func (*Gout) SetJSON

func (g *Gout) SetJSON(obj interface{}) *routerGroup

func (*Gout) SetProxy

func (g *Gout) SetProxy(proxyURL string) *routerGroup

func (*Gout) SetQuery

func (g *Gout) SetQuery(obj interface{}) *routerGroup

func (*Gout) SetXML

func (g *Gout) SetXML(obj interface{}) *routerGroup

func (*Gout) SetYAML

func (g *Gout) SetYAML(obj interface{}) *routerGroup

type H

type H map[string]interface{}

type Req

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

func NewReq

func NewReq(method string, url string, g *Gout) *Req

func (*Req) Do

func (r *Req) Do() (err error)

func (*Req) Reset

func (r *Req) Reset()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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