Skip to content

Commit

Permalink
Url support template (#351)
Browse files Browse the repository at this point in the history
* url支持模板

* 更新文档
  • Loading branch information
guonaihong authored Jan 18, 2023
1 parent faf5d11 commit 13ff215
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.13
- name: Set up Go 1.14
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.14
id: go

- name: Check out code into the Go module directory
Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ gout 是go写的http 客户端,为提高工作效率而开发
- [quick start](#quick-start)
- [API Examples](#api-examples)
- [GET POST PUT DELETE PATH HEAD OPTIONS](#get-post-put-delete-path-head-options)
- [GET POST PUT DELETE PATH HEAD OPTIONS template](#get-post-put-delete-path-head-options-template)
- [Query Parameters](#Query-Parameters)
- [http header](#http-header)
- [Set request header](#set-request-header)
Expand Down Expand Up @@ -239,6 +240,44 @@ func main() {
gout.OPTIONS(url).Do()
}
```
## GET POST PUT DELETE PATH HEAD OPTIONS template
```go
package main
import (
"github.com/guonaihong/gout"
)
type testURLTemplateCase struct {
Host string
}
func main() {
url := "https://{{.Host}}"
// 发送GET方法
gout.GET(url, testURLTemplateCase{Host:"www.qq.com"}).Do()
// 发送POST方法
gout.POST(url, testURLTemplateCase{Host:"www.github.com"}).Do()
// 发送PUT方法
gout.PUT(url, testURLTemplateCase{Host:"www.baidu.com"}).Do()
// 发送DELETE方法
gout.DELETE(url, testURLTemplateCase{Host:"www.google.com"}).Do()
// 发送PATH方法
gout.PATCH(url, testURLTemplateCase{Host:"www.google.com"}).Do()
// 发送HEAD方法
gout.HEAD(url, testURLTemplateCase{Host:"www.google.com"}).Do()
// 发送OPTIONS
gout.OPTIONS(url, testURLTemplateCase{Host:"www.google.com"}).Do()
}
```
## Query Parameters
Expand Down
34 changes: 17 additions & 17 deletions dataflow/dataflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,44 @@ type DataFlow struct {
}

// GET send HTTP GET method
func (df *DataFlow) GET(url string) *DataFlow {
df.Req = reqDef(get, cleanPaths(url), df.out)
func (df *DataFlow) GET(url string, urlStruct ...interface{}) *DataFlow {
df.Req = reqDef(get, cleanPaths(url), df.out, urlStruct...)
return df
}

// POST send HTTP POST method
func (df *DataFlow) POST(url string) *DataFlow {
df.Req = reqDef(post, cleanPaths(url), df.out)
func (df *DataFlow) POST(url string, urlStruct ...interface{}) *DataFlow {
df.Req = reqDef(post, cleanPaths(url), df.out, urlStruct...)
return df
}

// PUT send HTTP PUT method
func (df *DataFlow) PUT(url string) *DataFlow {
df.Req = reqDef(put, cleanPaths(url), df.out)
func (df *DataFlow) PUT(url string, urlStruct ...interface{}) *DataFlow {
df.Req = reqDef(put, cleanPaths(url), df.out, urlStruct...)
return df
}

// DELETE send HTTP DELETE method
func (df *DataFlow) DELETE(url string) *DataFlow {
df.Req = reqDef(delete2, cleanPaths(url), df.out)
func (df *DataFlow) DELETE(url string, urlStruct ...interface{}) *DataFlow {
df.Req = reqDef(delete2, cleanPaths(url), df.out, urlStruct...)
return df
}

// PATCH send HTTP PATCH method
func (df *DataFlow) PATCH(url string) *DataFlow {
df.Req = reqDef(patch, cleanPaths(url), df.out)
func (df *DataFlow) PATCH(url string, urlStruct ...interface{}) *DataFlow {
df.Req = reqDef(patch, cleanPaths(url), df.out, urlStruct...)
return df
}

// HEAD send HTTP HEAD method
func (df *DataFlow) HEAD(url string) *DataFlow {
df.Req = reqDef(head, cleanPaths(url), df.out)
func (df *DataFlow) HEAD(url string, urlStruct ...interface{}) *DataFlow {
df.Req = reqDef(head, cleanPaths(url), df.out, urlStruct...)
return df
}

// OPTIONS send HTTP OPTIONS method
func (df *DataFlow) OPTIONS(url string) *DataFlow {
df.Req = reqDef(options, cleanPaths(url), df.out)
func (df *DataFlow) OPTIONS(url string, urlStruct ...interface{}) *DataFlow {
df.Req = reqDef(options, cleanPaths(url), df.out, urlStruct...)
return df
}

Expand Down Expand Up @@ -129,13 +129,13 @@ func (df *DataFlow) SetMethod(method string) *DataFlow {
}

// SetURL set url
func (df *DataFlow) SetURL(url string) *DataFlow {
func (df *DataFlow) SetURL(url string, urlStruct ...interface{}) *DataFlow {
if df.Err != nil {
return df
}

if df.Req.url == "" && df.Req.req == nil && df.Req.method == "" {
df.Req = reqDef("", cleanPaths(url), df.out)
if df.Req.url == "" && df.Req.req == nil {
df.Req = reqDef(df.method, cleanPaths(url), df.out, urlStruct...)
return df
}

Expand Down
7 changes: 7 additions & 0 deletions dataflow/gout.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,44 @@ func New(c ...*http.Client) *Gout {
}

// TODO 这一层可以直接删除
// v0.3.3版本开始算起, v0.3.7版本将会删除
// GET send HTTP GET method
func GET(url string) *DataFlow {
return New().GET(url)
}

// POST send HTTP POST method
// v0.3.3版本开始算起, v0.3.7版本将会删除
func POST(url string) *DataFlow {
return New().POST(url)
}

// PUT send HTTP PUT method
// v0.3.3版本开始算起, v0.3.7版本将会删除
func PUT(url string) *DataFlow {
return New().PUT(url)
}

// DELETE send HTTP DELETE method
// v0.3.3版本开始算起, v0.3.7版本将会删除
func DELETE(url string) *DataFlow {
return New().DELETE(url)
}

// PATCH send HTTP PATCH method
// v0.3.3版本开始算起, v0.3.7版本将会删除
func PATCH(url string) *DataFlow {
return New().PATCH(url)
}

// HEAD send HTTP HEAD method
// v0.3.3版本开始算起, v0.3.7版本将会删除
func HEAD(url string) *DataFlow {
return New().HEAD(url)
}

// OPTIONS send HTTP OPTIONS method
// v0.3.3版本开始算起, v0.3.7版本将会删除
func OPTIONS(url string) *DataFlow {
return New().OPTIONS(url)
}
19 changes: 10 additions & 9 deletions dataflow/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"reflect"
"strings"
"text/template"

"github.com/guonaihong/gout/core"
"github.com/guonaihong/gout/debug"
Expand Down Expand Up @@ -583,17 +584,17 @@ func modifyURL(url string) string {
return fmt.Sprintf("http://%s", url)
}

func reqDef(method string, url string, g *Gout) Req {
func reqDef(method string, url string, g *Gout, urlStruct ...interface{}) Req {
if len(urlStruct) > 0 {
var out strings.Builder
tpl := template.Must(template.New(url).Parse(url))
tpl.Execute(&out, urlStruct[0])
url = out.String()
}

r := Req{method: method, url: modifyURL(url), g: g}
//后面收敛GlobalSetting, 计划删除这个变量
//先这么写, 控制影响的范围

r.Setting = GlobalSetting
/*
r.Setting.NotIgnoreEmpty = GlobalSetting.NotIgnoreEmpty
r.Setting.Timeout = GlobalSetting.Timeout
r.Setting.Index = GlobalSetting.Index
r.TimeoutIndex = GlobalSetting.TimeoutIndex
*/

return r
}
17 changes: 17 additions & 0 deletions dataflow/req_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dataflow

import "testing"

func Benchmark_URL_Template(b *testing.B) {

ts := createMethodEcho()
tc := testURLTemplateCase{Host: ts.URL, Method: "get"}

for n := 0; n < b.N; n++ {
code := 0
New().GET("{{.Host}}/{{.Method}}", tc).Code(&code).Do()
if code != 200 {
panic("code != 200")
}
}
}
91 changes: 91 additions & 0 deletions dataflow/req_url_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package dataflow

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

type testURLTemplateCase struct {
Host string
Method string
}

func createMethodEcho() *httptest.Server {
router := func() *gin.Engine {
router := gin.New()

router.GET("/get", func(c *gin.Context) {
c.String(200, "get")
})

router.POST("/post", func(c *gin.Context) {
c.String(200, "post")
})

router.PUT("/put", func(c *gin.Context) {
c.String(200, "put")
})

router.PATCH("/patch", func(c *gin.Context) {
c.String(200, "patch")
})

router.OPTIONS("/options", func(c *gin.Context) {
c.String(200, "options")
})

router.HEAD("/head", func(c *gin.Context) {
c.String(200, "head")
})

return router
}()

return httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
}

func Test_URL_Template(t *testing.T) {
ts := createMethodEcho()
for _, tc := range []testURLTemplateCase{
{Host: ts.URL, Method: "get"},
{Host: ts.URL, Method: "post"},
{Host: ts.URL, Method: "put"},
{Host: ts.URL, Method: "patch"},
{Host: ts.URL, Method: "options"},
{Host: ts.URL, Method: "head"},
} {
body := ""
body2 := ""
switch tc.Method {
case "get":
New().GET("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
case "post":
New().POST("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
case "put":
New().PUT("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
case "patch":
New().PATCH("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
case "options":
New().OPTIONS("{{.Host}}/{{.Method}}", tc).BindBody(&body).Do()
case "head":
code := 0
New().HEAD("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do()
New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Code(&code).Do()
assert.Equal(t, code, 200)
continue
}

New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Do()
assert.Equal(t, body, tc.Method)
b := assert.Equal(t, body2, tc.Method)
if !b {
return
}
}

}
39 changes: 25 additions & 14 deletions gout.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,49 @@ func New(c ...*http.Client) *dataflow.Gout {
}

// GET send HTTP GET method
func GET(url string) *dataflow.DataFlow {
return dataflow.GET(url)
// 第一种情况
// gout.GET("wwww.demo.xx/test-appkey")
//
// 第二种情况
//
// type host struct {
// Host string
// AppKey string
// }
//
// gout.GET("http://{{.Host}/{{.AppKey}}}", &host{Host:"www.demo.xx", AppKey:"test-appkey"})
func GET(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().GET(url, urlStruct...)
}

// POST send HTTP POST method
func POST(url string) *dataflow.DataFlow {
return dataflow.POST(url)
func POST(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().POST(url, urlStruct)
}

// PUT send HTTP PUT method
func PUT(url string) *dataflow.DataFlow {
return dataflow.PUT(url)
func PUT(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().PUT(url, urlStruct)
}

// DELETE send HTTP DELETE method
func DELETE(url string) *dataflow.DataFlow {
return dataflow.DELETE(url)
func DELETE(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().DELETE(url, urlStruct)
}

// PATCH send HTTP PATCH method
func PATCH(url string) *dataflow.DataFlow {
return dataflow.PATCH(url)
func PATCH(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().PATCH(url, urlStruct)
}

// HEAD send HTTP HEAD method
func HEAD(url string) *dataflow.DataFlow {
return dataflow.HEAD(url)
func HEAD(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().HEAD(url, urlStruct)
}

// OPTIONS send HTTP OPTIONS method
func OPTIONS(url string) *dataflow.DataFlow {
return dataflow.OPTIONS(url)
func OPTIONS(url string, urlStruct ...interface{}) *dataflow.DataFlow {
return dataflow.New().OPTIONS(url, urlStruct)
}

// 设置不忽略空值
Expand Down

0 comments on commit 13ff215

Please sign in to comment.