From d3c831d3986cb9c1c744c7d945b1176dfd962441 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 15 Apr 2021 04:44:14 +0800 Subject: [PATCH 01/46] chore: add go test command in makefile --- go.sum | 2 ++ makefile | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 go.sum create mode 100644 makefile diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b5e2922 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/makefile b/makefile new file mode 100644 index 0000000..b80037a --- /dev/null +++ b/makefile @@ -0,0 +1,2 @@ +test: + go test requests_test.go requests.go From 88ccd044cd066f2a2c61a76ddaddfef17eb204ca Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 15 Apr 2021 18:34:59 +0800 Subject: [PATCH 02/46] feat: simple requests(delete unnecessary code) --- README.md | 32 +++--- makefile | 2 +- requests.go | 266 ++++++++++++++--------------------------------- requests_test.go | 95 +++++++++-------- 4 files changed, 144 insertions(+), 251 deletions(-) diff --git a/README.md b/README.md index 098711b..4d85a08 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -[![license](http://dmlc.github.io/img/apache2.svg)](https://raw.githubusercontent.com/asmcos/requests/master/LICENSE) +[![license](http://dmlc.github.io/img/apache2.svg)](https://raw.githubusercontent.com/ahuigo/requests/master/LICENSE) # requests @@ -8,7 +8,7 @@ Requests is an HTTP library , it is easy to use. Similar to Python requests. # Installation ``` -go get -u github.com/asmcos/requests +go get -u github.com/ahuigo/requests ``` # Start @@ -16,7 +16,7 @@ go get -u github.com/asmcos/requests ``` go package main -import "github.com/asmcos/requests" +import "github.com/ahuigo/requests" func main (){ @@ -33,7 +33,7 @@ func main (){ ``` go package main -import "github.com/asmcos/requests" +import "github.com/ahuigo/requests" func main (){ @@ -78,7 +78,7 @@ func main (){ ``` go package main -import "github.com/asmcos/requests" +import "github.com/ahuigo/requests" func main (){ @@ -133,7 +133,7 @@ func main (){ ### example 1 ``` go -req := requests.Requests() +req := requests.Requests("GET") resp,err := req.Get("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"}) if (err == nil){ @@ -144,7 +144,7 @@ if (err == nil){ ### example 2 ``` go -req := requests.Requests() +req := requests.Requests("GET") req.Header.Set("accept-encoding", "gzip, deflate, br") resp,_ := req.Get("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"}) println(resp.Text()) @@ -158,7 +158,7 @@ h := requests.Header{ "Referer": "http://www.jeapedu.com", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", } -resp,_ := req.Get("http://wwww.zhanluejia.net.cn",h) +resp,_ := requests.Get("http://wwww.zhanluejia.net.cn",h) h2 := requests.Header{ ... @@ -178,7 +178,7 @@ p := requests.Params{ "name": "file", "id": "12345", } -resp,_ := req.Get("http://www.cpython.org", p) +resp,_ := requests.Get("http://www.cpython.org", p) ``` @@ -188,23 +188,21 @@ resp,_ := req.Get("http://www.cpython.org", p) Test with the `correct` user information. ``` go -req := requests.Requests() -resp,_ := req.Get("https://api.github.com/user",requests.Auth{"asmcos","password...."}) +resp,_ := requests.Get("https://api.github.com/user",requests.Auth{"ahuigo","password...."}) println(resp.Text()) ``` github return ``` -{"login":"asmcos","id":xxxxx,"node_id":"Mxxxxxxxxx=="..... +{"login":"ahuigo","id":xxxxx,"node_id":"Mxxxxxxxxx=="..... ``` # JSON ``` go -req := requests.Requests() req.Header.Set("Content-Type","application/json") -resp,_ = req.Get("https://httpbin.org/json") +resp,_ = requests.Get("https://httpbin.org/json") var json map[string]interface{} resp.Json(&json) @@ -218,18 +216,18 @@ for k,v := range json{ # SetTimeout ``` -req := Requests() +req := Requests("GET") req.Debug = 1 // 20 Second req.SetTimeout(20) -req.Get("http://golang.org") +req.Run("http://golang.org") ``` # Get Cookies ``` go -resp,_ = req.Get("https://www.httpbin.org") +resp,_ = requests.Get("https://www.httpbin.org") coo := resp.Cookies() // coo is [] *http.Cookies println("********cookies*******") diff --git a/makefile b/makefile index b80037a..87c8628 100644 --- a/makefile +++ b/makefile @@ -1,2 +1,2 @@ test: - go test requests_test.go requests.go + go test -v requests_test.go requests.go diff --git a/requests.go b/requests.go index 5d91b08..ffa0a7a 100644 --- a/requests.go +++ b/requests.go @@ -2,9 +2,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -51,18 +49,22 @@ type Response struct { type Header map[string]string type Params map[string]string -type Datas map[string]string // for post form -type Files map[string]string // name ,filename +type Datas map[string]string // for post form +type Jsons map[string]interface{} // for Json +type AnyData interface{} // for AnyData +type Files map[string]string // name ,filename -// {username,password} +// Auth - {username,password} type Auth []string -func Requests() *Request { +// Requests +// @params method GET|POST|PUT|DELETE|PATCH +func Requests(method string) *Request { req := new(Request) req.httpreq = &http.Request{ - Method: "GET", + Method: strings.ToUpper(method), Header: make(http.Header), Proto: "HTTP/1.1", ProtoMajor: 1, @@ -85,75 +87,11 @@ func Requests() *Request { // Get ,req.Get func Get(origurl string, args ...interface{}) (resp *Response, err error) { - req := Requests() - // call request Get - resp, err = req.Get(origurl, args...) + resp, err = Requests("GET").Run(origurl, args...) return resp, err } -func (req *Request) Get(origurl string, args ...interface{}) (resp *Response, err error) { - - req.httpreq.Method = "GET" - - // set params ?a=b&b=c - //set Header - params := []map[string]string{} - - //reset Cookies, - //Client.Do can copy cookie from client.Jar to req.Header - delete(req.httpreq.Header, "Cookie") - - for _, arg := range args { - switch a := arg.(type) { - // arg is Header , set to request header - case Header: - - for k, v := range a { - req.Header.Set(k, v) - } - // arg is "GET" params - // ?title=website&id=1860&from=login - case Params: - params = append(params, a) - case Auth: - // a{username,password} - req.httpreq.SetBasicAuth(a[0], a[1]) - } - } - - disturl, _ := buildURLParams(origurl, params...) - - //prepare to Do - URL, err := url.Parse(disturl) - if err != nil { - return nil, err - } - req.httpreq.URL = URL - - req.ClientSetCookies() - - req.RequestDebug() - - res, err := req.Client.Do(req.httpreq) - - if err != nil { - fmt.Println(err) - return nil, err - } - - - resp = &Response{} - resp.R = res - resp.req = req - - resp.Content() - defer res.Body.Close() - - resp.ResponseDebug() - return resp, nil -} - // handle URL params func buildURLParams(userURL string, params ...map[string]string) (string, error) { parsedURL, err := url.Parse(userURL) @@ -216,8 +154,8 @@ func (req *Request) ClearCookies() { req.Cookies = req.Cookies[0:0] } +// ClientSetCookies - func (req *Request) ClientSetCookies() { - if len(req.Cookies) > 0 { // 1. Cookies have content, Copy Cookies to Client.jar // 2. Clear Cookies @@ -232,13 +170,11 @@ func (req *Request) SetTimeout(n time.Duration) { req.Client.Timeout = time.Duration(n * time.Second) } - -func (req *Request) Close( ) { - req.httpreq.Close = true +func (req *Request) Close() { + req.httpreq.Close = true } func (req *Request) Proxy(proxyurl string) { - urli := url.URL{} urlproxy, err := urli.Parse(proxyurl) if err != nil { @@ -249,12 +185,10 @@ func (req *Request) Proxy(proxyurl string) { Proxy: http.ProxyURL(urlproxy), TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } - } /**************/ func (resp *Response) ResponseDebug() { - if resp.req.Debug != 1 { return } @@ -271,12 +205,11 @@ func (resp *Response) ResponseDebug() { } func (resp *Response) Content() []byte { - var err error - - if len(resp.content) > 0{ - return resp.content - } + if len(resp.content) > 0 { + return resp.content + } + defer resp.R.Body.Close() var Body = resp.R.Body if resp.R.Header.Get("Content-Encoding") == "gzip" && resp.req.Header.Get("Accept-Encoding") != "" { @@ -340,138 +273,93 @@ func (resp *Response) Cookies() (cookies []*http.Cookie) { /**************post*************************/ // call req.Post ,only for easy func Post(origurl string, args ...interface{}) (resp *Response, err error) { - req := Requests() - - // call request Get - resp, err = req.Post(origurl, args...) - return resp, err + resp, err = Requests("POST").Run(origurl, args...) + return } -func PostJson(origurl string, args ...interface{}) (resp *Response, err error) { - req := Requests() - - // call request Get - resp, err = req.PostJson(origurl, args...) - return resp, err +// Put +func Put(origurl string, args ...interface{}) (resp *Response, err error) { + resp, err = Requests("PUT").Run(origurl, args...) + return } -// POST requests - -func (req *Request) PostJson(origurl string, args ...interface{}) (resp *Response, err error) { - - req.httpreq.Method = "POST" - - req.Header.Set("Content-Type", "application/json") - - //reset Cookies, - //Client.Do can copy cookie from client.Jar to req.Header - delete(req.httpreq.Header, "Cookie") - - for _, arg := range args { - switch a := arg.(type) { - // arg is Header , set to request header - case Header: - - for k, v := range a { - req.Header.Set(k, v) - } - case string: - req.setBodyRawBytes(ioutil.NopCloser(strings.NewReader(arg.(string)))) - case Auth: - // a{username,password} - req.httpreq.SetBasicAuth(a[0], a[1]) - default: - b := new(bytes.Buffer) - err = json.NewEncoder(b).Encode(a) - if err != nil { - return nil, err - } - req.setBodyRawBytes(ioutil.NopCloser(b)) - } - } - - //prepare to Do - URL, err := url.Parse(origurl) - if err != nil { - return nil, err - } - req.httpreq.URL = URL - - req.ClientSetCookies() - - req.RequestDebug() +// Delete +func Delete(origurl string, args ...interface{}) (resp *Response, err error) { + resp, err = Requests("DELETE").Run(origurl, args...) + return +} - res, err := req.Client.Do(req.httpreq) +// Patch +func Patch(origurl string, args ...interface{}) (resp *Response, err error) { + resp, err = Requests("PATCH").Run(origurl, args...) + return +} - // clear post request information +// cleanup +func (req *Request) cleanup() { req.httpreq.Body = nil req.httpreq.GetBody = nil req.httpreq.ContentLength = 0 - - if err != nil { - fmt.Println(err) - return nil, err - } - - - resp = &Response{} - resp.R = res - resp.req = req - - resp.Content() - defer res.Body.Close() - resp.ResponseDebug() - return resp, nil + //reset Cookies, + //Client.Do can copy cookie from client.Jar to req.Header + delete(req.httpreq.Header, "Cookie") + // req.ClearCookies() } -func (req *Request) Post(origurl string, args ...interface{}) (resp *Response, err error) { - - req.httpreq.Method = "POST" +// SetMethod +func (req *Request) SetMethod(method string) *Request { + req.httpreq.Method = strings.ToUpper(method) + return req +} - //set default - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") +// Post - +func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, err error) { + // cleanup + req.cleanup() // set params ?a=b&b=c //set Header + contentType := "application/x-www-form-urlencoded" params := []map[string]string{} - datas := []map[string]string{} // POST - files := []map[string]string{} //post file - - //reset Cookies, - //Client.Do can copy cookie from client.Jar to req.Header - delete(req.httpreq.Header, "Cookie") + datas := []map[string]string{} // form data + files := []map[string]string{} //file data + bodyBytes := []byte{} for _, arg := range args { switch a := arg.(type) { // arg is Header , set to request header case Header: - for k, v := range a { req.Header.Set(k, v) } - // arg is "GET" params - // ?title=website&id=1860&from=login case Params: params = append(params, a) - case Datas: //Post form data,packaged in body. datas = append(datas, a) case Files: files = append(files, a) case Auth: - // a{username,password} req.httpreq.SetBasicAuth(a[0], a[1]) + case string: + contentType = "application/text" + bodyBytes = []byte(a) + default: + contentType = "application/json" + bodyBytes = req.buildJSON(a) } } + req.Header.Add("Content-Type", contentType) disturl, _ := buildURLParams(origurl, params...) if len(files) > 0 { req.buildFilesAndForms(files, datas) - + } else if len(bodyBytes) > 0 { + // fmt.Printf("jsonBytes=%#v\n", string(jsonBytes)) + req.setBodyBytes(bodyBytes) // set forms to body } else { Forms := req.buildForms(datas...) - req.setBodyBytes(Forms) // set forms to body + req.setBodyForms(Forms) // set forms to body } //prepare to Do URL, err := url.Parse(disturl) @@ -484,40 +372,35 @@ func (req *Request) Post(origurl string, args ...interface{}) (resp *Response, e req.RequestDebug() + // fmt.Printf("req:%#v\n", req.httpreq) + // fmt.Printf("req-url:%#v\n", req.httpreq.URL.String()) res, err := req.Client.Do(req.httpreq) - // clear post param - req.httpreq.Body = nil - req.httpreq.GetBody = nil - req.httpreq.ContentLength = 0 - if err != nil { fmt.Println(err) return nil, err } - resp = &Response{} resp.R = res resp.req = req - - resp.Content() - defer res.Body.Close() - resp.ResponseDebug() + resp.Content() return resp, nil } // only set forms -func (req *Request) setBodyBytes(Forms url.Values) { - - // maybe +func (req *Request) setBodyForms(Forms url.Values) { data := Forms.Encode() req.httpreq.Body = ioutil.NopCloser(strings.NewReader(data)) req.httpreq.ContentLength = int64(len(data)) } // only set forms +func (req *Request) setBodyBytes(data []byte) { + req.httpreq.Body = ioutil.NopCloser(bytes.NewReader(data)) + req.httpreq.ContentLength = int64(len(data)) +} func (req *Request) setBodyRawBytes(read io.ReadCloser) { req.httpreq.Body = read } @@ -571,6 +454,13 @@ func (req *Request) buildForms(datas ...map[string]string) (Forms url.Values) { return Forms } +func (req *Request) buildJSON(data interface{}) []byte { + jsonBytes, _ := json.Marshal(data) + + // fmt.Printf("a1=%#v,jsons=%#v\nahui\n", data, string(jsonBytes)) + return jsonBytes +} + // open file for post upload files func openFile(filename string) *os.File { diff --git a/requests_test.go b/requests_test.go index 4c55468..ec66588 100644 --- a/requests_test.go +++ b/requests_test.go @@ -2,19 +2,20 @@ package requests import ( "fmt" - "github.com/davecgh/go-spew/spew" "net/http" "os" "testing" + + "github.com/davecgh/go-spew/spew" ) func TestGet(t *testing.T) { // example 1 println("Get example1") - req := Requests() + req := Requests("get") req.Header.Set("accept-encoding", "gzip, deflate, br") - req.Get("http://www.zhanluejia.net.cn", Header{"Referer": "http://www.jeapedu.com"}, Params{"c": "d", "e": "f"}, Params{"c": "a"}) + req.SetMethod("GET").Run("http://www.zhanluejia.net.cn", Header{"Referer": "http://www.jeapedu.com"}, Params{"c": "d", "e": "f"}, Params{"c": "a"}) // example 2 println("Get example2") @@ -32,7 +33,7 @@ func TestGet(t *testing.T) { "name": "file", "id": "12345", } - resp, err := Requests().Get("http://www.cpython.org", p) + resp, err := Get("http://www.cpython.org", p) if err == nil { resp.Text() @@ -43,8 +44,8 @@ func TestGet(t *testing.T) { println("Get example4") // test authentication usernae,password //documentation https://www.httpwatch.com/httpgallery/authentication/#showExample10 - req = Requests() - resp, err = req.Get("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.45874470316137206", Auth{"httpwatch", "foo"}) + req = Requests("GET") + resp, err = req.Run("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.45874470316137206", Auth{"httpwatch", "foo"}) if err == nil { fmt.Println(resp.R) } @@ -53,9 +54,9 @@ func TestGet(t *testing.T) { //example 5 test Json println("Get example5") - req = Requests() + req = Requests("GET") req.Header.Set("Content-Type", "application/json") - resp, err = req.Get("https://httpbin.org/json") + resp, err = req.SetMethod("GET").Run("https://httpbin.org/json") if err == nil { var json map[string]interface{} @@ -68,35 +69,35 @@ func TestGet(t *testing.T) { // example 6 test gzip println("Get example6") - req = Requests() + req = Requests("get") req.Debug = 1 - resp, err = req.Get("https://httpbin.org/gzip") + resp, err = req.Run("https://httpbin.org/gzip") if err == nil { fmt.Println(resp.Text()) } // example 7 proxy and debug println("Get example7") - req = Requests() + req = Requests("get") req.Debug = 1 // You need open the line //req.Proxy("http://192.168.1.190:8888") - req.Get("https://www.sina.com.cn") + req.Run("https://www.sina.com.cn") //example 8 test auto Cookies println("Get example8") - req = Requests() + req = Requests("get") req.Debug = 1 // req.Proxy("http://192.168.1.190:8888") - req.Get("https://www.httpbin.org/cookies/set?freeform=1234") - req.Get("https://www.httpbin.org") - req.Get("https://www.httpbin.org/cookies/set?a=33d") - req.Get("https://www.httpbin.org") + req.Run("https://www.httpbin.org/cookies/set?freeform=1234") + req.Run("https://www.httpbin.org") + req.Run("https://www.httpbin.org/cookies/set?a=33d") + req.Run("https://www.httpbin.org") // example 9 test AddCookie println("Get example9") - req = Requests() + req = Requests("get") req.Debug = 1 cookie := &http.Cookie{} @@ -108,10 +109,10 @@ func TestGet(t *testing.T) { fmt.Println(req.Cookies) // req.Proxy("http://127.0.0.1:8888") - req.Get("https://www.httpbin.org/cookies/set?freeform=1234") - req.Get("https://www.httpbin.org") - req.Get("https://www.httpbin.org/cookies/set?a=33d") - resp, err = req.Get("https://www.httpbin.org") + req.Run("https://www.httpbin.org/cookies/set?freeform=1234") + req.Run("https://www.httpbin.org") + req.Run("https://www.httpbin.org/cookies/set?a=33d") + resp, err = req.SetMethod("GET").Run("https://www.httpbin.org") if err == nil { coo := resp.Cookies() @@ -126,22 +127,22 @@ func TestGet(t *testing.T) { func TestClose(t *testing.T) { - req := Requests() - fmt.Println("Start 1000 times get test...") - for i:=0;i<1000;i++{ - _,err := req.Post("http://localhost:1337/requests",Datas{"SrcIp":"4312"}) - fmt.Printf("\r%d %v",i,err) - req.Close() - } + req := Requests("GET") + fmt.Println("Start 1000 times get test...") + for i := 0; i < 1000; i++ { + _, err := req.SetMethod("POST").Run("http://localhost:1337/requests", Datas{"SrcIp": "4312"}) + fmt.Printf("\r%d %v", i, err) + req.Close() + } - fmt.Println("1000 times get test end.") + fmt.Println("1000 times get test end.") } func TestPost(t *testing.T) { // example 1 // set post formdata println("Post example1") - req := Requests() + req := Requests("GET") req.Debug = 1 data := Datas{ @@ -154,25 +155,25 @@ func TestPost(t *testing.T) { "topping": "bacon", } - resp, err := req.Post("https://www.httpbin.org/post", data) + resp, err := req.SetMethod("POST").Run("https://www.httpbin.org/post", data) if err == nil { fmt.Println(resp.Text()) } //example 2 upload files println("Post example2") - req = Requests() + req = Requests("GET") req.Debug = 1 path, _ := os.Getwd() path1 := path + "/README.md" path2 := path + "/docs/index.md" - resp, err = req.Post("https://www.httpbin.org/post", data, Files{"a": path1, "b": path2}) + resp, err = req.SetMethod("POST").Run("https://www.httpbin.org/post", data, Files{"a": path1, "b": path2}) if err == nil { fmt.Println(resp.Text()) } - req = Requests() + req = Requests("GET") cookie := &http.Cookie{} cookie.Name = "postcookie" cookie.Value = "20200725" @@ -181,7 +182,7 @@ func TestPost(t *testing.T) { req.SetCookie(cookie) //test post cookies - resp, err = req.Post("https://www.httpbin.org/post", data) + resp, err = req.SetMethod("POST").Run("https://www.httpbin.org/post", data) if err == nil { coo := resp.Cookies() // coo is [] *http.Cookies @@ -195,12 +196,12 @@ func TestPost(t *testing.T) { func TestTimeout(t *testing.T) { println("Timeout example1") - req := Requests() + req := Requests("GET") req.Debug = 1 // 20 Second req.SetTimeout(20) - req.Get("http://golang.org") + req.SetMethod("GET").Run("http://golang.org") } @@ -208,14 +209,18 @@ func TestPostGet(t *testing.T) { println("Test Post and Get") - client := Requests() + client := Requests("GET") client.Debug = 1 - resp, err := client.Post("https://www.httpbin.org/post", Datas{"abc": "123", "ddd": "789"}) + resp, err := Post("https://www.httpbin.org/post", Datas{"abc": "123", "ddd": "789"}) + if err != nil { + fmt.Println(err) + } + fmt.Println(resp.Text()) spew.Dump(client) - resp, err = client.Get("https://www.httpbin.org/get") + resp, err = Get("https://www.httpbin.org/get") if err != nil { fmt.Println(err) } @@ -247,22 +252,22 @@ func TestPostJson(t *testing.T) { println("Test PostJson") - client := Requests() + client := Requests("GET") client.Debug = 1 - resp, err := client.PostJson("https://www.httpbin.org/post", dataStruct) + resp, err := client.Run("https://www.httpbin.org/post", dataStruct) if err != nil { t.Fatalf("post struct json error: %v", err) } fmt.Println(resp.Text()) - resp, err = client.PostJson("https://www.httpbin.org/post", dataMap) + resp, err = client.Run("https://www.httpbin.org/post", dataMap) if err != nil { t.Fatalf("post struct json error: %v", err) } fmt.Println(resp.Text()) - resp, err = client.PostJson("https://www.httpbin.org/post", dataJsonStr) + resp, err = client.Run("https://www.httpbin.org/post", dataJsonStr) if err != nil { t.Fatalf("post struct json error: %v", err) } From c456809dbe4f6ec42a13a3d6de4fe27ed243123f Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 15 Apr 2021 18:36:07 +0800 Subject: [PATCH 03/46] feat: rename package name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 863d801..ade6da6 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/asmcos/requests +module github.com/ahuigo/requests go 1.13 From 284dbe062f830e1074fcc8cb7550cdd1389af46f Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 15 Apr 2021 19:24:17 +0800 Subject: [PATCH 04/46] fix: pakcage path --- README.md | 5 +++++ docs/index.md | 2 +- examples/get1.go | 4 ++-- examples/get_auth.go | 4 ++-- examples/get_set_header.go | 2 +- examples/post1.go | 2 +- requests.go | 2 +- 7 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4d85a08..e09c5fc 100644 --- a/README.md +++ b/README.md @@ -235,3 +235,8 @@ for _, c:= range coo{ fmt.Println(c.Name,c.Value) } ``` + +# Thanks +This project is inspired by [github.com/asmcos/requests](http://github.com/asmcos/requests). + +Great thanks to it :). diff --git a/docs/index.md b/docs/index.md index bd21bd7..0ae298e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -6,7 +6,7 @@ ``` package main -import "github.com/asmcos/requests" +import "github.com/ahuigo/requests" func main (){ diff --git a/examples/get1.go b/examples/get1.go index 12da14f..d4aa3ba 100644 --- a/examples/get1.go +++ b/examples/get1.go @@ -1,10 +1,10 @@ package main -import "github.com/asmcos/requests" +import "github.com/ahuigo/requests" func main (){ - resp,_ := requests.Get("http://go.xiulian.net.cn") + resp,_ := requests.Get("https://www.baidu.com/") println(resp.Text()) } diff --git a/examples/get_auth.go b/examples/get_auth.go index f6b7e1d..3af466a 100644 --- a/examples/get_auth.go +++ b/examples/get_auth.go @@ -1,7 +1,7 @@ package main import ( - "github.com/asmcos/requests" + "github.com/ahuigo/requests" "fmt" ) @@ -9,7 +9,7 @@ func main (){ req := requests.Requests() - resp,_ := req.Get("https://api.github.com/user",requests.Auth{"asmcos","password...."}) + resp,_ := req.Get("https://api.github.com/user",requests.Auth{"ahuigo","password...."}) println(resp.Text()) fmt.Println(resp.R.StatusCode) fmt.Println(resp.R.Header["Content-Type"]) diff --git a/examples/get_set_header.go b/examples/get_set_header.go index 08eb543..b360434 100644 --- a/examples/get_set_header.go +++ b/examples/get_set_header.go @@ -1,6 +1,6 @@ package main -import "github.com/asmcos/requests" +import "github.com/ahuigo/requests" func main (){ diff --git a/examples/post1.go b/examples/post1.go index 5e45208..e300281 100644 --- a/examples/post1.go +++ b/examples/post1.go @@ -1,6 +1,6 @@ package main -import "github.com/asmcos/requests" +import "github.com/ahuigo/requests" func main (){ diff --git a/requests.go b/requests.go index ffa0a7a..d5b0acc 100644 --- a/requests.go +++ b/requests.go @@ -1,4 +1,4 @@ -/* Copyright(2) 2018 by asmcos . +/* Copyright(2) 2018 by asmcos and ahuigo . Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at From 094f0e023c5e118b54d08b7e024e2743d9c26261 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 15 Apr 2021 19:33:25 +0800 Subject: [PATCH 05/46] chore: add make pkg --- makefile | 5 +++++ version | 1 + 2 files changed, 6 insertions(+) create mode 100644 version diff --git a/makefile b/makefile index 87c8628..e051571 100644 --- a/makefile +++ b/makefile @@ -1,2 +1,7 @@ test: go test -v requests_test.go requests.go + +pkg: + newversion.py version + jfrog "rt" "go-publish" "go-pl" $$(cat version) "--url=$$GOPROXY_API" --user=$$GOPROXY_USER --apikey=$$GOPROXY_PASS + diff --git a/version b/version new file mode 100644 index 0000000..f4caccc --- /dev/null +++ b/version @@ -0,0 +1 @@ +v0.1.2 \ No newline at end of file From 7ed17284b3e1d962df7ac2a77ae0ea13566af9d6 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 15 Apr 2021 21:38:39 +0800 Subject: [PATCH 06/46] fix: read cache if resp.Content is nil --- requests.go | 2 +- version | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requests.go b/requests.go index d5b0acc..f4399f2 100644 --- a/requests.go +++ b/requests.go @@ -206,7 +206,7 @@ func (resp *Response) ResponseDebug() { func (resp *Response) Content() []byte { var err error - if len(resp.content) > 0 { + if resp.content!=nil { return resp.content } defer resp.R.Body.Close() diff --git a/version b/version index f4caccc..d34dcac 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.2 \ No newline at end of file +v0.1.3 \ No newline at end of file From 2968cc642575bef8e9089ea9b0a8c180262aaa34 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 15 Apr 2021 23:17:43 +0800 Subject: [PATCH 07/46] doc: get header example --- README.md | 6 ++---- examples/get1.go | 2 ++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e09c5fc..14d1615 100644 --- a/README.md +++ b/README.md @@ -133,9 +133,7 @@ func main (){ ### example 1 ``` go -req := requests.Requests("GET") - -resp,err := req.Get("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"}) +resp,err := requests.Get("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"}) if (err == nil){ println(resp.Text()) } @@ -146,7 +144,7 @@ if (err == nil){ ``` go req := requests.Requests("GET") req.Header.Set("accept-encoding", "gzip, deflate, br") -resp,_ := req.Get("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"}) +resp,_ := req.Run("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"}) println(resp.Text()) ``` diff --git a/examples/get1.go b/examples/get1.go index d4aa3ba..347b0c1 100644 --- a/examples/get1.go +++ b/examples/get1.go @@ -7,4 +7,6 @@ func main (){ resp,_ := requests.Get("https://www.baidu.com/") println(resp.Text()) + println(resp.R.Header.Get("location")) + println(resp.R.Header.Get("Location")) } From 2bc1101ee6d3b5e451424fcd1a2abe4f5091a91c Mon Sep 17 00:00:00 2001 From: ahuigo Date: Sat, 17 Apr 2021 16:55:34 +0800 Subject: [PATCH 08/46] test: add test example cases --- README.md | 142 ++++++----------- docs/index.md | 21 --- examples/auth_test.go | 24 +++ examples/cookie_test.go | 31 ++++ examples/debug_test.go | 18 +++ examples/get1.go | 12 -- examples/get_auth.go | 16 -- examples/get_set_header.go | 14 -- examples/get_test.go | 36 +++++ examples/post1.go | 13 -- examples/post_file_test.go | 26 ++++ examples/post_test.go | 34 +++++ examples/proxy_test.go | 16 ++ examples/timeout_test.go | 30 ++++ go.mod | 5 +- go.sum | 301 +++++++++++++++++++++++++++++++++++++ init/init.go | 17 +++ makefile | 3 +- requests.go | 36 +++-- requests_test.go | 275 --------------------------------- version | 2 +- 21 files changed, 608 insertions(+), 464 deletions(-) delete mode 100644 docs/index.md create mode 100644 examples/auth_test.go create mode 100644 examples/cookie_test.go create mode 100644 examples/debug_test.go delete mode 100644 examples/get1.go delete mode 100644 examples/get_auth.go delete mode 100644 examples/get_set_header.go create mode 100644 examples/get_test.go delete mode 100644 examples/post1.go create mode 100644 examples/post_file_test.go create mode 100644 examples/post_test.go create mode 100644 examples/proxy_test.go create mode 100644 examples/timeout_test.go create mode 100644 init/init.go delete mode 100644 requests_test.go diff --git a/README.md b/README.md index 14d1615..a8216ee 100644 --- a/README.md +++ b/README.md @@ -13,107 +13,55 @@ go get -u github.com/ahuigo/requests # Start -``` go -package main - -import "github.com/ahuigo/requests" - -func main (){ - - resp,err := requests.Get("http://www.zhanluejia.net.cn") - if err != nil{ - return +## Get + + var json map[string]interface{} + resp, err := requests.Get("https://httpbin.org/json") + if err == nil { + resp.Json(&json) + for k, v := range json { + fmt.Println(k, v) } - println(resp.Text()) -} -``` + } ## Post -``` go -package main - -import "github.com/ahuigo/requests" - - -func main (){ - - data := requests.Datas{ - "name":"requests_post_test", - } - resp,_ := requests.Post("https://www.httpbin.org/post",data) - println(resp.Text()) -} - -``` - - Server return data... - -``` json -{ - "args": {}, - "data": "", - "files": {}, - "form": { - "name": "requests_post_test" - }, - "headers": { - "Accept-Encoding": "gzip", - "Connection": "close", - "Content-Length": "23", - "Content-Type": "application/x-www-form-urlencoded", - "Host": "www.httpbin.org", - "User-Agent": "Go-Requests 0.5" - }, - "json": null, - "origin": "114.242.34.110", - "url": "https://www.httpbin.org/post" -} - -``` - - -## PostJson - -``` go -package main - -import "github.com/ahuigo/requests" - - -func main (){ - - jsonStr := "{\"name\":\"requests_post_test\"}" - resp,_ := requests.PostJson("https://www.httpbin.org/post",jsonStr) - println(resp.Text()) -} - -``` - - Server return data... - -``` json -{ - "args": {}, - "data": "", - "files": {}, - "form": { - "name": "requests_post_test" - }, - "headers": { - "Accept-Encoding": "gzip", - "Connection": "close", - "Content-Length": "23", - "Content-Type": "application/x-www-form-urlencoded", - "Host": "www.httpbin.org", - "User-Agent": "Go-Requests 0.5" - }, - "json": null, - "origin": "114.242.34.110", - "url": "https://www.httpbin.org/post" -} - -``` +### PostJson + data := requests.Datas{ + "comments": "ew", + } + // json := requests.Json{ "key": "value"} + json = map[string]interface{}{ + "key": "value", + } + resp, err := requests.Post("https://www.httpbin.org/post", data, json) + if err == nil { + fmt.Println(resp.Text()) + } + +### PostString + + dataStr := "{\"key\":\"This is raw data\"}" + resp, err := requests.Post("https://www.httpbin.org/post", dataStr) + if err == nil { + fmt.Println(resp.Text()) + } + +### PostFiles + + path, _ := os.Getwd() + req := requests.Requests("GET").SetDebug(true) + + resp, err := req.SetMethod("POST").Run( + "https://www.httpbin.org/post", + requests.Files{ + "file1": path + "/README.md", + "file2": path + "/version", + }, + ) + if err == nil { + fmt.Println(resp.Text()) + } # Feature Support - Set headers diff --git a/docs/index.md b/docs/index.md deleted file mode 100644 index 0ae298e..0000000 --- a/docs/index.md +++ /dev/null @@ -1,21 +0,0 @@ -# Installation - - -# example 1 - -``` -package main - -import "github.com/ahuigo/requests" - - -func main (){ - - resp,err := requests.Get("http://go.xiulian.net.cn") - if err != nil { - return - } - println(resp.Text()) -} - -``` diff --git a/examples/auth_test.go b/examples/auth_test.go new file mode 100644 index 0000000..3a43595 --- /dev/null +++ b/examples/auth_test.go @@ -0,0 +1,24 @@ +package examples + +import ( + "fmt" + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +func TestAuth(t *testing.T) { + println("3. Get: Set Auth") + // test authentication usernae,password + //documentation https://www.httpwatch.com/httpgallery/authentication/#showExample10 + resp, err := requests.Get( + "https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.45874470316137206", + requests.Auth{"httpwatch", "foo"}, + ) + if err == nil { + fmt.Println(resp.R) + } + // this save file test PASS + // resp.SaveFile("auth.jpeg") +} diff --git a/examples/cookie_test.go b/examples/cookie_test.go new file mode 100644 index 0000000..b41926f --- /dev/null +++ b/examples/cookie_test.go @@ -0,0 +1,31 @@ +package examples + +import ( + "fmt" + "net/http" + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +func TestSendCookie(t *testing.T) { + // example 9 test AddCookie + println("6. Get: get with cookie") + req := requests.Requests("get").SetDebug(true) + cookie := &http.Cookie{ + Name: "anewcookie", + Value: "20180825", + Path: "/", + } + resp, err := req.SetCookie(cookie).Run("https://www.httpbin.org") + if err == nil { + coo := resp.Cookies() + // coo is [] *http.Cookies + println("********cookies*******") + for _, c := range coo { + fmt.Println(c.Name, c.Value) + } + } + +} diff --git a/examples/debug_test.go b/examples/debug_test.go new file mode 100644 index 0000000..07bbcc9 --- /dev/null +++ b/examples/debug_test.go @@ -0,0 +1,18 @@ +package examples + +import ( + "fmt" + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +func TestGetDebug(t *testing.T) { + println("4. Get: SetDebug") + req := requests.Requests("get").SetDebug(true) + resp, err := req.Run("https://httpbin.org/gzip") + if err == nil { + fmt.Println(resp.Text()) + } +} diff --git a/examples/get1.go b/examples/get1.go deleted file mode 100644 index 347b0c1..0000000 --- a/examples/get1.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import "github.com/ahuigo/requests" - - -func main (){ - - resp,_ := requests.Get("https://www.baidu.com/") - println(resp.Text()) - println(resp.R.Header.Get("location")) - println(resp.R.Header.Get("Location")) -} diff --git a/examples/get_auth.go b/examples/get_auth.go deleted file mode 100644 index 3af466a..0000000 --- a/examples/get_auth.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "github.com/ahuigo/requests" - "fmt" -) - -func main (){ - - req := requests.Requests() - - resp,_ := req.Get("https://api.github.com/user",requests.Auth{"ahuigo","password...."}) - println(resp.Text()) - fmt.Println(resp.R.StatusCode) - fmt.Println(resp.R.Header["Content-Type"]) -} diff --git a/examples/get_set_header.go b/examples/get_set_header.go deleted file mode 100644 index b360434..0000000 --- a/examples/get_set_header.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import "github.com/ahuigo/requests" - - -func main (){ - - req := requests.Requests() - - resp,_ := req.Get("http://go.xiulian.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"}) - - println(resp.Text()) - -} diff --git a/examples/get_test.go b/examples/get_test.go new file mode 100644 index 0000000..58c0849 --- /dev/null +++ b/examples/get_test.go @@ -0,0 +1,36 @@ +package examples + +import ( + "fmt" + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +func TestGetJson(t *testing.T) { + println("Test Get: fetch json response") + resp, err := requests.Get("https://httpbin.org/json") + if err == nil { + var json map[string]interface{} + resp.Json(&json) + for k, v := range json { + fmt.Println(k, v) + } + } +} + +func TestGetParamsHeaders(t *testing.T) { + println("Test Get: custom header and params") + requests.Get("http://www.zhanluejia.net.cn", + requests.Header{"Referer": "http://www.jeapedu.com"}, + requests.Params{"page": "1", "size": "20"}, + requests.Params{"name": "ahuio"}, + ) +} +func TestResponseHeader(t *testing.T) { + resp, _ := requests.Get("https://www.baidu.com/") + println(resp.Text()) + println(resp.R.Header.Get("location")) + println(resp.R.Header.Get("Location")) +} diff --git a/examples/post1.go b/examples/post1.go deleted file mode 100644 index e300281..0000000 --- a/examples/post1.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import "github.com/ahuigo/requests" - - -func main (){ - - data := requests.Datas{ - "name":"requests_post_test", - } - resp,_ := requests.Post("https://www.httpbin.org/post",data) - println(resp.Text()) -} diff --git a/examples/post_file_test.go b/examples/post_file_test.go new file mode 100644 index 0000000..fb20dcd --- /dev/null +++ b/examples/post_file_test.go @@ -0,0 +1,26 @@ +package examples + +import ( + "fmt" + "os" + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +func TestPostFile(t *testing.T) { + req := requests.Requests("GET").SetDebug(true) + path, _ := os.Getwd() + + resp, err := req.SetMethod("POST").Run( + "https://www.httpbin.org/post", + requests.Files{ + "file1": path + "/README.md", + "file2": path + "/version", + }, + ) + if err == nil { + fmt.Println(resp.Text()) + } +} diff --git a/examples/post_test.go b/examples/post_test.go new file mode 100644 index 0000000..d1a5d94 --- /dev/null +++ b/examples/post_test.go @@ -0,0 +1,34 @@ +package examples + +import ( + "fmt" + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +func TestPostJson(t *testing.T) { + println("Test POST: post data and json") + data := requests.Datas{ + "comments": "ew", + } + json := requests.Json{ + "key": "value", + } + json = map[string]interface{}{ + "key": "value", + } + resp, err := requests.Post("https://www.httpbin.org/post", data, json) + if err == nil { + fmt.Println(resp.Text()) + } +} +func TestPostString(t *testing.T) { + println("Test POST: post data and json") + dataStr := "{\"key\":\"This is raw data\"}" + resp, err := requests.Post("https://www.httpbin.org/post", dataStr) + if err == nil { + fmt.Println(resp.Text()) + } +} diff --git a/examples/proxy_test.go b/examples/proxy_test.go new file mode 100644 index 0000000..563bf53 --- /dev/null +++ b/examples/proxy_test.go @@ -0,0 +1,16 @@ +package examples + +import ( + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +func TestProxy(t *testing.T) { + println("5. Get: with proxy") + req := requests.Requests("get") + // req.Proxy("http://192.168.1.190:8888") + req.Run("https://www.httpbin.org/cookies/set?freeform=1234") + req.Run("https://www.httpbin.org") +} diff --git a/examples/timeout_test.go b/examples/timeout_test.go new file mode 100644 index 0000000..a74795a --- /dev/null +++ b/examples/timeout_test.go @@ -0,0 +1,30 @@ +package examples + +import ( + "fmt" + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" + "github.com/davecgh/go-spew/spew" +) + +func TestClose(t *testing.T) { + fmt.Println("Test Close") + req := requests.Requests("POST") + for i := 0; i < 1000; i++ { + _, err := req.Run( + "http://localhost:1337/requests", + requests.Datas{"SrcIp": "4312"}) + fmt.Printf("\r%d %v", i, err) + req.Close() + } + + spew.Dump(req) + fmt.Println("1000 times get test end.") +} +func TestTimeout(t *testing.T) { + println("Test Timeout") + req := requests.Requests("GET").SetTimeout(20) + req.SetMethod("GET").Run("http://golang.org") +} diff --git a/go.mod b/go.mod index ade6da6..0744b87 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/ahuigo/requests go 1.13 -require github.com/davecgh/go-spew v1.1.1 +require ( + github.com/davecgh/go-spew v1.1.1 + gitlab.momenta.works/hdmap-workflow/account-client-go v0.0.18 +) diff --git a/go.sum b/go.sum index b5e2922..0d11d52 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,303 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= +cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU= +cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY= +cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc= +cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0= +cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= +cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= +cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk= +cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= +cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= +dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= +github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= +github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= +github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= +github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= +github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= +github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= +github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= +github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= +github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU= +github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU= +github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= +github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= +github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= +github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= +github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= +github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= +github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= +github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= +github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= +github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +gitlab.momenta.works/hdmap-workflow/account-client-go v0.0.18 h1:7/QZi/oUXCv3NMQ/vm03/MgizDk/BIk8vSv9HbT07B8= +gitlab.momenta.works/hdmap-workflow/account-client-go v0.0.18/go.mod h1:9/rsNo7fcK2JwFYEy5znLxJBKZpJLh1vwbcvvliHfMM= +go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= +go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= +go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +go.uber.org/zap v1.14.1/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= +golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek= +golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= +golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= +golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= +google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= +google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= +google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= +gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/init/init.go b/init/init.go new file mode 100644 index 0000000..91cdda5 --- /dev/null +++ b/init/init.go @@ -0,0 +1,17 @@ +package init + +import ( + "os" + "path" + "runtime" +) + +func init() { + _, filename, _, _ := runtime.Caller(0) + println("filename:" + filename) + dir := path.Join(path.Dir(filename), "..") + err := os.Chdir(dir) + if err != nil { + panic(err) + } +} diff --git a/makefile b/makefile index e051571..e4b40ff 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,5 @@ test: - go test -v requests_test.go requests.go - + go test -v ./examples pkg: newversion.py version jfrog "rt" "go-publish" "go-pl" $$(cat version) "--url=$$GOPROXY_API" --user=$$GOPROXY_USER --apikey=$$GOPROXY_PASS diff --git a/requests.go b/requests.go index f4399f2..df25877 100644 --- a/requests.go +++ b/requests.go @@ -36,7 +36,7 @@ type Request struct { httpreq *http.Request Header *http.Header Client *http.Client - Debug int + debug bool Cookies []*http.Cookie } @@ -49,10 +49,10 @@ type Response struct { type Header map[string]string type Params map[string]string -type Datas map[string]string // for post form -type Jsons map[string]interface{} // for Json -type AnyData interface{} // for AnyData -type Files map[string]string // name ,filename +type Datas map[string]string // for post form +type Json map[string]interface{} // for Json +type Files map[string]string // name ,filename +// type AnyData interface{} // for AnyData // Auth - {username,password} type Auth []string @@ -121,9 +121,14 @@ func addQueryParams(parsedURL *url.URL, parsedQuery url.Values) string { return strings.Replace(parsedURL.String(), "?"+parsedURL.RawQuery, "", -1) } +func (req *Request) SetDebug(debug bool) *Request { + req.debug = debug + return req +} + func (req *Request) RequestDebug() { - if req.Debug != 1 { + if req.debug { return } @@ -146,8 +151,9 @@ func (req *Request) RequestDebug() { // cookies // cookies only save to Client.Jar // req.Cookies is temporary -func (req *Request) SetCookie(cookie *http.Cookie) { +func (req *Request) SetCookie(cookie *http.Cookie) *Request { req.Cookies = append(req.Cookies, cookie) + return req } func (req *Request) ClearCookies() { @@ -166,8 +172,9 @@ func (req *Request) ClientSetCookies() { } // set timeout s = second -func (req *Request) SetTimeout(n time.Duration) { +func (req *Request) SetTimeout(n time.Duration) *Request { req.Client.Timeout = time.Duration(n * time.Second) + return req } func (req *Request) Close() { @@ -189,7 +196,7 @@ func (req *Request) Proxy(proxyurl string) { /**************/ func (resp *Response) ResponseDebug() { - if resp.req.Debug != 1 { + if resp.req.debug { return } @@ -206,7 +213,7 @@ func (resp *Response) ResponseDebug() { func (resp *Response) Content() []byte { var err error - if resp.content!=nil { + if resp.content != nil { return resp.content } defer resp.R.Body.Close() @@ -300,6 +307,7 @@ func (req *Request) cleanup() { req.httpreq.Body = nil req.httpreq.GetBody = nil req.httpreq.ContentLength = 0 + req.Header = &http.Header{} //reset Cookies, //Client.Do can copy cookie from client.Jar to req.Header delete(req.httpreq.Header, "Cookie") @@ -341,14 +349,18 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er case Auth: req.httpreq.SetBasicAuth(a[0], a[1]) case string: - contentType = "application/text" bodyBytes = []byte(a) + case Json: + contentType = "application/json" + bodyBytes = req.buildJSON(a) default: contentType = "application/json" bodyBytes = req.buildJSON(a) } } - req.Header.Add("Content-Type", contentType) + if req.Header.Get("Content-Type") == "" { + req.Header.Set("Content-Type", contentType) + } disturl, _ := buildURLParams(origurl, params...) diff --git a/requests_test.go b/requests_test.go deleted file mode 100644 index ec66588..0000000 --- a/requests_test.go +++ /dev/null @@ -1,275 +0,0 @@ -package requests - -import ( - "fmt" - "net/http" - "os" - "testing" - - "github.com/davecgh/go-spew/spew" -) - -func TestGet(t *testing.T) { - // example 1 - println("Get example1") - req := Requests("get") - - req.Header.Set("accept-encoding", "gzip, deflate, br") - req.SetMethod("GET").Run("http://www.zhanluejia.net.cn", Header{"Referer": "http://www.jeapedu.com"}, Params{"c": "d", "e": "f"}, Params{"c": "a"}) - - // example 2 - println("Get example2") - h := Header{ - "Referer": "http://www.zhanluejia.net.cn", - "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", - } - - Get("http://www.zhanluejia.net.cn", h, Header{"accept-encoding": "gzip, deflate, br"}) - - // example 3 - println("Get example3") - p := Params{ - "title": "The blog", - "name": "file", - "id": "12345", - } - resp, err := Get("http://www.cpython.org", p) - - if err == nil { - resp.Text() - fmt.Println(resp.Text()) - } - - // example 4 - println("Get example4") - // test authentication usernae,password - //documentation https://www.httpwatch.com/httpgallery/authentication/#showExample10 - req = Requests("GET") - resp, err = req.Run("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.45874470316137206", Auth{"httpwatch", "foo"}) - if err == nil { - fmt.Println(resp.R) - } - // this save file test PASS - // resp.SaveFile("auth.jpeg") - - //example 5 test Json - println("Get example5") - req = Requests("GET") - req.Header.Set("Content-Type", "application/json") - resp, err = req.SetMethod("GET").Run("https://httpbin.org/json") - - if err == nil { - var json map[string]interface{} - resp.Json(&json) - - for k, v := range json { - fmt.Println(k, v) - } - } - - // example 6 test gzip - println("Get example6") - req = Requests("get") - req.Debug = 1 - resp, err = req.Run("https://httpbin.org/gzip") - if err == nil { - fmt.Println(resp.Text()) - } - // example 7 proxy and debug - println("Get example7") - req = Requests("get") - req.Debug = 1 - - // You need open the line - //req.Proxy("http://192.168.1.190:8888") - - req.Run("https://www.sina.com.cn") - - //example 8 test auto Cookies - println("Get example8") - req = Requests("get") - req.Debug = 1 - // req.Proxy("http://192.168.1.190:8888") - req.Run("https://www.httpbin.org/cookies/set?freeform=1234") - req.Run("https://www.httpbin.org") - req.Run("https://www.httpbin.org/cookies/set?a=33d") - req.Run("https://www.httpbin.org") - - // example 9 test AddCookie - println("Get example9") - req = Requests("get") - req.Debug = 1 - - cookie := &http.Cookie{} - cookie.Name = "anewcookie" - cookie.Value = "20180825" - cookie.Path = "/" - - req.SetCookie(cookie) - - fmt.Println(req.Cookies) - // req.Proxy("http://127.0.0.1:8888") - req.Run("https://www.httpbin.org/cookies/set?freeform=1234") - req.Run("https://www.httpbin.org") - req.Run("https://www.httpbin.org/cookies/set?a=33d") - resp, err = req.SetMethod("GET").Run("https://www.httpbin.org") - - if err == nil { - coo := resp.Cookies() - // coo is [] *http.Cookies - println("********cookies*******") - for _, c := range coo { - fmt.Println(c.Name, c.Value) - } - } - -} - -func TestClose(t *testing.T) { - - req := Requests("GET") - fmt.Println("Start 1000 times get test...") - for i := 0; i < 1000; i++ { - _, err := req.SetMethod("POST").Run("http://localhost:1337/requests", Datas{"SrcIp": "4312"}) - fmt.Printf("\r%d %v", i, err) - req.Close() - } - - fmt.Println("1000 times get test end.") -} -func TestPost(t *testing.T) { - - // example 1 - // set post formdata - println("Post example1") - req := Requests("GET") - req.Debug = 1 - - data := Datas{ - "comments": "ew", - "custemail": "a@231.com", - "custname": "1", - "custtel": "2", - "delivery": "12:45", - "size": "small", - "topping": "bacon", - } - - resp, err := req.SetMethod("POST").Run("https://www.httpbin.org/post", data) - if err == nil { - fmt.Println(resp.Text()) - } - - //example 2 upload files - println("Post example2") - req = Requests("GET") - req.Debug = 1 - path, _ := os.Getwd() - path1 := path + "/README.md" - path2 := path + "/docs/index.md" - - resp, err = req.SetMethod("POST").Run("https://www.httpbin.org/post", data, Files{"a": path1, "b": path2}) - if err == nil { - fmt.Println(resp.Text()) - } - - req = Requests("GET") - cookie := &http.Cookie{} - cookie.Name = "postcookie" - cookie.Value = "20200725" - cookie.Path = "/" - - req.SetCookie(cookie) - - //test post cookies - resp, err = req.SetMethod("POST").Run("https://www.httpbin.org/post", data) - if err == nil { - coo := resp.Cookies() - // coo is [] *http.Cookies - println("********Post cookies*******") - for _, c := range coo { - fmt.Println(c.Name, c.Value) - } - } - -} - -func TestTimeout(t *testing.T) { - println("Timeout example1") - req := Requests("GET") - req.Debug = 1 - - // 20 Second - req.SetTimeout(20) - req.SetMethod("GET").Run("http://golang.org") - -} - -func TestPostGet(t *testing.T) { - - println("Test Post and Get") - - client := Requests("GET") - client.Debug = 1 - - resp, err := Post("https://www.httpbin.org/post", Datas{"abc": "123", "ddd": "789"}) - if err != nil { - fmt.Println(err) - } - fmt.Println(resp.Text()) - - spew.Dump(client) - - resp, err = Get("https://www.httpbin.org/get") - if err != nil { - fmt.Println(err) - } - fmt.Println(resp.Text()) - -} - -func TestPostJson(t *testing.T) { - - type StructReq struct { - ContainerId string `json:"id"` - Worker string `json:"worker"` - Force bool `json:"force"` - } - - dataStruct := StructReq{ - ContainerId: "123456", - Worker: "worker1", - Force: true, - } - - dataMap := map[string]interface{}{ - "id": "123456", - "worker": "worker1", - "force": true, - } - - dataJsonStr := "{\"id\":\"123456\",\"worker\":\"worker1\",\"force\": true}" - - println("Test PostJson") - - client := Requests("GET") - client.Debug = 1 - - resp, err := client.Run("https://www.httpbin.org/post", dataStruct) - if err != nil { - t.Fatalf("post struct json error: %v", err) - } - fmt.Println(resp.Text()) - - resp, err = client.Run("https://www.httpbin.org/post", dataMap) - if err != nil { - t.Fatalf("post struct json error: %v", err) - } - fmt.Println(resp.Text()) - - resp, err = client.Run("https://www.httpbin.org/post", dataJsonStr) - if err != nil { - t.Fatalf("post struct json error: %v", err) - } - fmt.Println(resp.Text()) -} diff --git a/version b/version index d34dcac..7a5e833 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.3 \ No newline at end of file +v0.1.4 \ No newline at end of file From 57036acf86f0f817e4256c1ab406361c003698f0 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Sat, 17 Apr 2021 17:29:36 +0800 Subject: [PATCH 09/46] doc: update examples --- README.md | 135 +++++++++++-------------------------------- examples/get_test.go | 9 +++ requests.go | 6 ++ 3 files changed, 48 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index a8216ee..ab07269 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ - +# Requests [![license](http://dmlc.github.io/img/apache2.svg)](https://raw.githubusercontent.com/ahuigo/requests/master/LICENSE) # requests - -Requests is an HTTP library , it is easy to use. Similar to Python requests. +Requests is an HTTP library, it is easy to use. Similar to Python requests. # Installation @@ -78,109 +77,41 @@ go get -u github.com/ahuigo/requests # Set header -### example 1 - -``` go -resp,err := requests.Get("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"}) -if (err == nil){ - println(resp.Text()) -} -``` - -### example 2 - -``` go -req := requests.Requests("GET") -req.Header.Set("accept-encoding", "gzip, deflate, br") -resp,_ := req.Run("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"}) -println(resp.Text()) - -``` - -### example 3 - -``` go -h := requests.Header{ - "Referer": "http://www.jeapedu.com", - "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", -} -resp,_ := requests.Get("http://wwww.zhanluejia.net.cn",h) - -h2 := requests.Header{ - ... - ... -} -h3,h4 .... -// two or more headers ... -resp,_ = req.Get("http://www.zhanluejia.net.cn",h,h2,h3,h4) -``` - - -# Set params - -``` go -p := requests.Params{ - "title": "The blog", - "name": "file", - "id": "12345", -} -resp,_ := requests.Get("http://www.cpython.org", p) - -``` - - -# Auth - -Test with the `correct` user information. - -``` go -resp,_ := requests.Get("https://api.github.com/user",requests.Auth{"ahuigo","password...."}) -println(resp.Text()) -``` - -github return - -``` -{"login":"ahuigo","id":xxxxx,"node_id":"Mxxxxxxxxx=="..... -``` - -# JSON - -``` go -req.Header.Set("Content-Type","application/json") -resp,_ = requests.Get("https://httpbin.org/json") - -var json map[string]interface{} -resp.Json(&json) - -for k,v := range json{ - fmt.Println(k,v) -} -``` - - -# SetTimeout + func TestGetParamsHeaders(t *testing.T) { + println("Test Get: custom header and params") + requests.Get("http://www.zhanluejia.net.cn", + requests.Header{"Referer": "http://www.jeapedu.com"}, + requests.Params{"page": "1", "size": "20"}, + requests.Params{"name": "ahuio"}, + ) + } -``` -req := Requests("GET") -req.Debug = 1 + func TestGetParamsHeaders2(t *testing.T) { + req := requests.Requests("get") + req.SetHeader("accept-encoding", "gzip, deflate, br") + req.Run("http://www.zhanluejia.net.cn", + requests.Params{"page": "1", "size": "20"}, + requests.Params{"name": "ahuio"}, + ) + } -// 20 Second -req.SetTimeout(20) -req.Run("http://golang.org") -``` + func TestResponseHeader(t *testing.T) { + resp, _ := requests.Get("https://www.baidu.com/") + println(resp.Text()) + println(resp.R.Header.Get("location")) + println(resp.R.Header.Get("Location")) + } -# Get Cookies +two or more headers ... -``` go -resp,_ = requests.Get("https://www.httpbin.org") -coo := resp.Cookies() -// coo is [] *http.Cookies -println("********cookies*******") -for _, c:= range coo{ - fmt.Println(c.Name,c.Value) -} -``` + headers1 := requests.Header{"Referer": "http://www.jeapedu.com"}, + .... + resp,_ = req.Get( + "http://www.zhanluejia.net.cn", + headers1, + headers2, + headers3, + ) # Thanks This project is inspired by [github.com/asmcos/requests](http://github.com/asmcos/requests). diff --git a/examples/get_test.go b/examples/get_test.go index 58c0849..d0a9918 100644 --- a/examples/get_test.go +++ b/examples/get_test.go @@ -28,6 +28,15 @@ func TestGetParamsHeaders(t *testing.T) { requests.Params{"name": "ahuio"}, ) } +func TestGetParamsHeaders2(t *testing.T) { + req := requests.Requests("get") + req.SetHeader("accept-encoding", "gzip, deflate, br") + req.Run("http://www.zhanluejia.net.cn", + requests.Header{"Referer": "http://www.jeapedu.com"}, + requests.Params{"page": "1", "size": "20"}, + requests.Params{"name": "ahuio"}, + ) +} func TestResponseHeader(t *testing.T) { resp, _ := requests.Get("https://www.baidu.com/") println(resp.Text()) diff --git a/requests.go b/requests.go index df25877..11484c3 100644 --- a/requests.go +++ b/requests.go @@ -320,6 +320,12 @@ func (req *Request) SetMethod(method string) *Request { return req } +// SetHeader +func (req *Request) SetHeader(key, value string) *Request { + req.Header.Set(key, value) + return req +} + // Post - func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, err error) { // cleanup From c4f95166b21d583dc34b9e279729dc59438e0a40 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Mon, 19 Apr 2021 16:03:31 +0800 Subject: [PATCH 10/46] feat: support respHandler --- examples/get_test.go | 1 - helpers.go | 64 +++++++++++++ methods.go | 64 +++++++++++++ requests.go | 220 ++++++------------------------------------- response.go | 109 +++++++++++++++++++++ version | 2 +- 6 files changed, 268 insertions(+), 192 deletions(-) create mode 100644 helpers.go create mode 100644 methods.go create mode 100644 response.go diff --git a/examples/get_test.go b/examples/get_test.go index d0a9918..f2363ea 100644 --- a/examples/get_test.go +++ b/examples/get_test.go @@ -32,7 +32,6 @@ func TestGetParamsHeaders2(t *testing.T) { req := requests.Requests("get") req.SetHeader("accept-encoding", "gzip, deflate, br") req.Run("http://www.zhanluejia.net.cn", - requests.Header{"Referer": "http://www.jeapedu.com"}, requests.Params{"page": "1", "size": "20"}, requests.Params{"name": "ahuio"}, ) diff --git a/helpers.go b/helpers.go new file mode 100644 index 0000000..f9adc2b --- /dev/null +++ b/helpers.go @@ -0,0 +1,64 @@ +package requests + +import ( + "io/ioutil" + "net/url" + "os" + "path" + "runtime" + "strings" +) + +var VERSION string = "v0.0.0" + +func init() { + _, filename, _, _ := runtime.Caller(0) + println("filename:" + filename) + versionFile := path.Dir(filename) + "/version" + version, _ := ioutil.ReadFile(versionFile) + println("version:" + string(version)) + VERSION = string(version) +} + +// open file for post upload files +func openFile(filename string) *os.File { + r, err := os.Open(filename) + if err != nil { + panic(err) + } + return r +} + +// handle URL params +func buildURLParams(userURL string, params ...map[string]string) (string, error) { + parsedURL, err := url.Parse(userURL) + + if err != nil { + return "", err + } + + parsedQuery, err := url.ParseQuery(parsedURL.RawQuery) + + if err != nil { + return "", nil + } + + for _, param := range params { + for key, value := range param { + parsedQuery.Add(key, value) + } + } + return addQueryParams(parsedURL, parsedQuery), nil +} + +func addQueryParams(parsedURL *url.URL, parsedQuery url.Values) string { + if len(parsedQuery) > 0 { + return strings.Join([]string{strings.Replace(parsedURL.String(), "?"+parsedURL.RawQuery, "", -1), parsedQuery.Encode()}, "?") + } + return strings.Replace(parsedURL.String(), "?"+parsedURL.RawQuery, "", -1) +} + +func (req *Request) SetDebug(debug bool) *Request { + req.debug = debug + return req +} diff --git a/methods.go b/methods.go new file mode 100644 index 0000000..29998fd --- /dev/null +++ b/methods.go @@ -0,0 +1,64 @@ +/* Copyright(2) 2018 by asmcos and ahuigo . +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package requests + +/**************post/get/delete/patch*************************/ +func Get(origurl string, args ...interface{}) (resp *Response, err error) { + // call request Get + resp, err = Requests("GET").Run(origurl, args...) + return resp, err +} + +func Post(origurl string, args ...interface{}) (resp *Response, err error) { + resp, err = Requests("POST").Run(origurl, args...) + return +} + +// Put +func Put(origurl string, args ...interface{}) (resp *Response, err error) { + resp, err = Requests("PUT").Run(origurl, args...) + return +} + +// Delete +func Delete(origurl string, args ...interface{}) (resp *Response, err error) { + resp, err = Requests("DELETE").Run(origurl, args...) + return +} + +// Patch +func Patch(origurl string, args ...interface{}) (resp *Response, err error) { + resp, err = Requests("PATCH").Run(origurl, args...) + return +} + +func (req *Request) Get(origurl string, args ...interface{}) (resp *Response, err error) { + req.httpreq.Method = "GET" + resp, err = req.Run(origurl, args...) + return resp, err +} +func (req *Request) Post(origurl string, args ...interface{}) (resp *Response, err error) { + req.httpreq.Method = "POST" + resp, err = req.Run(origurl, args...) + return resp, err +} +func (req *Request) Delete(origurl string, args ...interface{}) (resp *Response, err error) { + req.httpreq.Method = "DELETE" + resp, err = req.Run(origurl, args...) + return resp, err +} +func (req *Request) Patch(origurl string, args ...interface{}) (resp *Response, err error) { + req.httpreq.Method = "PATCH" + resp, err = req.Run(origurl, args...) + return resp, err +} diff --git a/requests.go b/requests.go index 11484c3..0e8fd1e 100644 --- a/requests.go +++ b/requests.go @@ -14,7 +14,6 @@ package requests import ( "bytes" - "compress/gzip" "crypto/tls" "encoding/json" "fmt" @@ -25,26 +24,17 @@ import ( "net/http/cookiejar" "net/http/httputil" "net/url" - "os" "strings" "time" ) -var VERSION string = "0.8" - type Request struct { - httpreq *http.Request - Header *http.Header - Client *http.Client - debug bool - Cookies []*http.Cookie -} - -type Response struct { - R *http.Response - content []byte - text string - req *Request + httpreq *http.Request + Header *http.Header + Client *http.Client + debug bool + respHandler func(*Response) + Cookies []*http.Cookie } type Header map[string]string @@ -62,16 +52,7 @@ type Auth []string func Requests(method string) *Request { req := new(Request) - - req.httpreq = &http.Request{ - Method: strings.ToUpper(method), - Header: make(http.Header), - Proto: "HTTP/1.1", - ProtoMajor: 1, - ProtoMinor: 1, - } - req.Header = &req.httpreq.Header - req.httpreq.Header.Set("User-Agent", "Go-Requests "+VERSION) + req.reset() req.Client = &http.Client{} @@ -84,54 +65,22 @@ func Requests(method string) *Request { return req } -// Get ,req.Get - -func Get(origurl string, args ...interface{}) (resp *Response, err error) { - // call request Get - resp, err = Requests("GET").Run(origurl, args...) - return resp, err -} - -// handle URL params -func buildURLParams(userURL string, params ...map[string]string) (string, error) { - parsedURL, err := url.Parse(userURL) - - if err != nil { - return "", err - } - - parsedQuery, err := url.ParseQuery(parsedURL.RawQuery) - - if err != nil { - return "", nil - } - - for _, param := range params { - for key, value := range param { - parsedQuery.Add(key, value) - } - } - return addQueryParams(parsedURL, parsedQuery), nil -} - -func addQueryParams(parsedURL *url.URL, parsedQuery url.Values) string { - if len(parsedQuery) > 0 { - return strings.Join([]string{strings.Replace(parsedURL.String(), "?"+parsedURL.RawQuery, "", -1), parsedQuery.Encode()}, "?") +func (req *Request) reset() { + req.httpreq = &http.Request{ + Method: "GET", + Header: make(http.Header), + Proto: "HTTP/1.1", + ProtoMajor: 1, + ProtoMinor: 1, } - return strings.Replace(parsedURL.String(), "?"+parsedURL.RawQuery, "", -1) -} - -func (req *Request) SetDebug(debug bool) *Request { - req.debug = debug - return req + req.Header = &req.httpreq.Header + req.httpreq.Header.Set("User-Agent", "Go-Requests "+VERSION) } func (req *Request) RequestDebug() { - if req.debug { return } - fmt.Println("===========Go RequestDebug ============") message, err := httputil.DumpRequestOut(req.httpreq, false) @@ -194,126 +143,26 @@ func (req *Request) Proxy(proxyurl string) { } } -/**************/ -func (resp *Response) ResponseDebug() { - if resp.req.debug { - return - } - - fmt.Println("===========Go ResponseDebug ============") - - message, err := httputil.DumpResponse(resp.R, false) - if err != nil { - return - } - - fmt.Println(string(message)) - -} - -func (resp *Response) Content() []byte { - var err error - if resp.content != nil { - return resp.content - } - defer resp.R.Body.Close() - - var Body = resp.R.Body - if resp.R.Header.Get("Content-Encoding") == "gzip" && resp.req.Header.Get("Accept-Encoding") != "" { - // fmt.Println("gzip") - reader, err := gzip.NewReader(Body) - if err != nil { - return nil - } - Body = reader - } - - resp.content, err = ioutil.ReadAll(Body) - if err != nil { - return nil - } - - return resp.content -} - -func (resp *Response) Text() string { - if resp.content == nil { - resp.Content() - } - resp.text = string(resp.content) - return resp.text -} - -func (resp *Response) SaveFile(filename string) error { - if resp.content == nil { - resp.Content() - } - f, err := os.Create(filename) - if err != nil { - return err - } - defer f.Close() - - _, err = f.Write(resp.content) - f.Sync() - - return err -} - -func (resp *Response) Json(v interface{}) error { - if resp.content == nil { - resp.Content() - } - return json.Unmarshal(resp.content, v) -} - -func (resp *Response) Cookies() (cookies []*http.Cookie) { - httpreq := resp.req.httpreq - client := resp.req.Client - - cookies = client.Jar.Cookies(httpreq.URL) - - return cookies - -} - -/**************post*************************/ -// call req.Post ,only for easy -func Post(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests("POST").Run(origurl, args...) - return -} - -// Put -func Put(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests("PUT").Run(origurl, args...) - return -} - -// Delete -func Delete(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests("DELETE").Run(origurl, args...) - return -} - -// Patch -func Patch(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests("PATCH").Run(origurl, args...) - return -} - // cleanup func (req *Request) cleanup() { req.httpreq.Body = nil req.httpreq.GetBody = nil req.httpreq.ContentLength = 0 - req.Header = &http.Header{} + // req.Header = &http.Header{} + // req.Header = &req.httpreq.Header + //reset Cookies, //Client.Do can copy cookie from client.Jar to req.Header delete(req.httpreq.Header, "Cookie") // req.ClearCookies() } +// SetRespHandler +func (req *Request) SetRespHandler(fn func(*Response)) *Request { + req.respHandler = fn + return req +} + // SetMethod func (req *Request) SetMethod(method string) *Request { req.httpreq.Method = strings.ToUpper(method) @@ -329,7 +178,7 @@ func (req *Request) SetHeader(key, value string) *Request { // Post - func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, err error) { // cleanup - req.cleanup() + // req.cleanup() // set params ?a=b&b=c //set Header @@ -404,6 +253,10 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er resp.req = req resp.ResponseDebug() resp.Content() + req.reset() + if req.respHandler != nil { + req.respHandler(resp) + } return resp, nil } @@ -419,9 +272,6 @@ func (req *Request) setBodyBytes(data []byte) { req.httpreq.Body = ioutil.NopCloser(bytes.NewReader(data)) req.httpreq.ContentLength = int64(len(data)) } -func (req *Request) setBodyRawBytes(read io.ReadCloser) { - req.httpreq.Body = read -} // upload file and form // build to body format @@ -478,13 +328,3 @@ func (req *Request) buildJSON(data interface{}) []byte { // fmt.Printf("a1=%#v,jsons=%#v\nahui\n", data, string(jsonBytes)) return jsonBytes } - -// open file for post upload files - -func openFile(filename string) *os.File { - r, err := os.Open(filename) - if err != nil { - panic(err) - } - return r -} diff --git a/response.go b/response.go new file mode 100644 index 0000000..d637a5a --- /dev/null +++ b/response.go @@ -0,0 +1,109 @@ +/* Copyright(2) 2018 by asmcos and ahuigo . +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package requests + +import ( + "compress/gzip" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/http/httputil" + "os" +) + +type Response struct { + R *http.Response + content []byte + text string + req *Request +} + +func (resp *Response) ResponseDebug() { + if resp.req.debug { + return + } + fmt.Println("===========Go ResponseDebug ============") + + message, err := httputil.DumpResponse(resp.R, false) + if err != nil { + return + } + + fmt.Println(string(message)) +} + +func (resp *Response) Content() []byte { + var err error + if resp.content != nil { + return resp.content + } + defer resp.R.Body.Close() + + var Body = resp.R.Body + if resp.R.Header.Get("Content-Encoding") == "gzip" && resp.req.Header.Get("Accept-Encoding") != "" { + reader, err := gzip.NewReader(Body) + if err != nil { + return nil + } + Body = reader + } + + resp.content, err = ioutil.ReadAll(Body) + if err != nil { + return nil + } + + return resp.content +} + +func (resp *Response) Text() string { + if resp.content == nil { + resp.Content() + } + resp.text = string(resp.content) + return resp.text +} + +func (resp *Response) SaveFile(filename string) error { + if resp.content == nil { + resp.Content() + } + f, err := os.Create(filename) + if err != nil { + return err + } + defer f.Close() + + _, err = f.Write(resp.content) + f.Sync() + + return err +} + +func (resp *Response) Json(v interface{}) error { + if resp.content == nil { + resp.Content() + } + return json.Unmarshal(resp.content, v) +} + +func (resp *Response) Cookies() (cookies []*http.Cookie) { + httpreq := resp.req.httpreq + client := resp.req.Client + + cookies = client.Jar.Cookies(httpreq.URL) + + return cookies + +} diff --git a/version b/version index 7a5e833..610220f 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.4 \ No newline at end of file +v0.1.6 \ No newline at end of file From b8f83f233a0614265ce4b38285b7eea52622720c Mon Sep 17 00:00:00 2001 From: ahuigo Date: Sun, 25 Apr 2021 23:13:17 +0800 Subject: [PATCH 11/46] feat: support global respHandler --- README.md | 38 ++++++++++++++++++++++++++++++++++---- examples/cookie_test.go | 20 +++++++++++--------- examples/debug_test.go | 4 ++-- examples/get_test.go | 9 ++++++--- examples/post_file_test.go | 3 +-- examples/proxy_test.go | 6 +++--- examples/timeout_test.go | 8 ++++---- helpers.go | 5 ----- methods.go | 16 +++++++++++----- requests.go | 34 +++++++++++++++++++++++++--------- 10 files changed, 97 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index ab07269..441d32b 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,7 @@ go get -u github.com/ahuigo/requests data := requests.Datas{ "comments": "ew", } - // json := requests.Json{ "key": "value"} - json = map[string]interface{}{ + json := map[string]interface{}{ "key": "value", } resp, err := requests.Post("https://www.httpbin.org/post", data, json) @@ -38,6 +37,10 @@ go get -u github.com/ahuigo/requests fmt.Println(resp.Text()) } +You can use json builder instead: + + json := requests.Json{ "key": "value"} + ### PostString dataStr := "{\"key\":\"This is raw data\"}" @@ -49,9 +52,9 @@ go get -u github.com/ahuigo/requests ### PostFiles path, _ := os.Getwd() - req := requests.Requests("GET").SetDebug(true) + req := requests.Requests() - resp, err := req.SetMethod("POST").Run( + resp, err := req.SetDebug(true).Post( "https://www.httpbin.org/post", requests.Files{ "file1": path + "/README.md", @@ -62,6 +65,33 @@ go get -u github.com/ahuigo/requests fmt.Println(resp.Text()) } +## Request Options +### SetTimeout and debug + + req := Requests() + req.Debug = 1 + + // 20 Second + req.SetTimeout(20) + req.Get("http://golang.org") + +### Set Authentication + req := requests.Requests() + resp,_ := req.Get("https://api.github.com/user",requests.Auth{"asmcos","password...."}) + +### Set Cookie + cookie1 := http.Cookie{Name: "cookie_name", Value: "cookie_value"} + req.SetCookie(&cookie1) + +## Response +### Get Cookies + + resp,_ = req.Get("https://www.httpbin.org") + coo := resp.Cookies() + for _, c:= range coo{ + fmt.Println(c.Name,c.Value) + } + # Feature Support - Set headers - Set params diff --git a/examples/cookie_test.go b/examples/cookie_test.go index b41926f..88078cf 100644 --- a/examples/cookie_test.go +++ b/examples/cookie_test.go @@ -12,20 +12,22 @@ import ( func TestSendCookie(t *testing.T) { // example 9 test AddCookie println("6. Get: get with cookie") - req := requests.Requests("get").SetDebug(true) + req := requests.Requests().SetDebug(true) cookie := &http.Cookie{ Name: "anewcookie", Value: "20180825", Path: "/", } - resp, err := req.SetCookie(cookie).Run("https://www.httpbin.org") - if err == nil { - coo := resp.Cookies() - // coo is [] *http.Cookies - println("********cookies*******") - for _, c := range coo { - fmt.Println(c.Name, c.Value) - } + resp, err := req.SetCookie(cookie).Get("https://www.httpbin.org") + // resp, err := req.SetCookie(cookie).Get("http://127.0.0.1:8088") + if err != nil { + panic(err) + } + coo := resp.Cookies() + // coo is [] *http.Cookies + println("********cookies*******") + for _, c := range coo { + fmt.Println(c.Name, c.Value) } } diff --git a/examples/debug_test.go b/examples/debug_test.go index 07bbcc9..c3c7c41 100644 --- a/examples/debug_test.go +++ b/examples/debug_test.go @@ -10,8 +10,8 @@ import ( func TestGetDebug(t *testing.T) { println("4. Get: SetDebug") - req := requests.Requests("get").SetDebug(true) - resp, err := req.Run("https://httpbin.org/gzip") + req := requests.Requests().SetDebug(true) + resp, err := req.Get("https://httpbin.org/gzip") if err == nil { fmt.Println(resp.Text()) } diff --git a/examples/get_test.go b/examples/get_test.go index f2363ea..5a5e922 100644 --- a/examples/get_test.go +++ b/examples/get_test.go @@ -13,11 +13,14 @@ func TestGetJson(t *testing.T) { resp, err := requests.Get("https://httpbin.org/json") if err == nil { var json map[string]interface{} - resp.Json(&json) + err = resp.Json(&json) for k, v := range json { fmt.Println(k, v) } } + if err != nil { + t.Fatal(err) + } } func TestGetParamsHeaders(t *testing.T) { @@ -29,9 +32,9 @@ func TestGetParamsHeaders(t *testing.T) { ) } func TestGetParamsHeaders2(t *testing.T) { - req := requests.Requests("get") + req := requests.Requests() req.SetHeader("accept-encoding", "gzip, deflate, br") - req.Run("http://www.zhanluejia.net.cn", + req.Get("http://www.zhanluejia.net.cn", requests.Params{"page": "1", "size": "20"}, requests.Params{"name": "ahuio"}, ) diff --git a/examples/post_file_test.go b/examples/post_file_test.go index fb20dcd..ce9deea 100644 --- a/examples/post_file_test.go +++ b/examples/post_file_test.go @@ -10,10 +10,9 @@ import ( ) func TestPostFile(t *testing.T) { - req := requests.Requests("GET").SetDebug(true) path, _ := os.Getwd() - resp, err := req.SetMethod("POST").Run( + resp, err := requests.Post( "https://www.httpbin.org/post", requests.Files{ "file1": path + "/README.md", diff --git a/examples/proxy_test.go b/examples/proxy_test.go index 563bf53..7359f99 100644 --- a/examples/proxy_test.go +++ b/examples/proxy_test.go @@ -9,8 +9,8 @@ import ( func TestProxy(t *testing.T) { println("5. Get: with proxy") - req := requests.Requests("get") + req := requests.Requests() // req.Proxy("http://192.168.1.190:8888") - req.Run("https://www.httpbin.org/cookies/set?freeform=1234") - req.Run("https://www.httpbin.org") + req.Get("https://www.httpbin.org/cookies/set?freeform=1234") + req.Get("https://www.httpbin.org") } diff --git a/examples/timeout_test.go b/examples/timeout_test.go index a74795a..7e914f2 100644 --- a/examples/timeout_test.go +++ b/examples/timeout_test.go @@ -11,9 +11,9 @@ import ( func TestClose(t *testing.T) { fmt.Println("Test Close") - req := requests.Requests("POST") + req := requests.Requests() for i := 0; i < 1000; i++ { - _, err := req.Run( + _, err := req.Post( "http://localhost:1337/requests", requests.Datas{"SrcIp": "4312"}) fmt.Printf("\r%d %v", i, err) @@ -25,6 +25,6 @@ func TestClose(t *testing.T) { } func TestTimeout(t *testing.T) { println("Test Timeout") - req := requests.Requests("GET").SetTimeout(20) - req.SetMethod("GET").Run("http://golang.org") + req := requests.Requests().SetTimeout(20) + req.Get("http://golang.org") } diff --git a/helpers.go b/helpers.go index f9adc2b..4b41910 100644 --- a/helpers.go +++ b/helpers.go @@ -57,8 +57,3 @@ func addQueryParams(parsedURL *url.URL, parsedQuery url.Values) string { } return strings.Replace(parsedURL.String(), "?"+parsedURL.RawQuery, "", -1) } - -func (req *Request) SetDebug(debug bool) *Request { - req.debug = debug - return req -} diff --git a/methods.go b/methods.go index 29998fd..1d0dc67 100644 --- a/methods.go +++ b/methods.go @@ -15,30 +15,30 @@ package requests /**************post/get/delete/patch*************************/ func Get(origurl string, args ...interface{}) (resp *Response, err error) { // call request Get - resp, err = Requests("GET").Run(origurl, args...) + resp, err = Requests().Get(origurl, args...) return resp, err } func Post(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests("POST").Run(origurl, args...) + resp, err = Requests().Post(origurl, args...) return } // Put func Put(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests("PUT").Run(origurl, args...) + resp, err = Requests().Put(origurl, args...) return } // Delete func Delete(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests("DELETE").Run(origurl, args...) + resp, err = Requests().Delete(origurl, args...) return } // Patch func Patch(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests("PATCH").Run(origurl, args...) + resp, err = Requests().Patch(origurl, args...) return } @@ -57,6 +57,12 @@ func (req *Request) Delete(origurl string, args ...interface{}) (resp *Response, resp, err = req.Run(origurl, args...) return resp, err } +func (req *Request) Put(origurl string, args ...interface{}) (resp *Response, err error) { + req.httpreq.Method = "PUT" + resp, err = req.Run(origurl, args...) + return resp, err +} + func (req *Request) Patch(origurl string, args ...interface{}) (resp *Response, err error) { req.httpreq.Method = "PATCH" resp, err = req.Run(origurl, args...) diff --git a/requests.go b/requests.go index 0e8fd1e..ee0e135 100644 --- a/requests.go +++ b/requests.go @@ -28,13 +28,21 @@ import ( "time" ) +var respHandler func(*Response) + +// SetRespHandler +func SetRespHandler(fn func(*Response)) { + respHandler = fn +} + type Request struct { httpreq *http.Request - Header *http.Header Client *http.Client debug bool respHandler func(*Response) - Cookies []*http.Cookie + // global header + Header *http.Header + Cookies []*http.Cookie } type Header map[string]string @@ -49,14 +57,13 @@ type Auth []string // Requests // @params method GET|POST|PUT|DELETE|PATCH -func Requests(method string) *Request { +func Requests() *Request { req := new(Request) req.reset() req.Client = &http.Client{} - // auto with Cookies // cookiejar.New source code return jar, nil jar, _ := cookiejar.New(nil) @@ -143,7 +150,7 @@ func (req *Request) Proxy(proxyurl string) { } } -// cleanup +// cleanup delete func (req *Request) cleanup() { req.httpreq.Body = nil req.httpreq.GetBody = nil @@ -193,7 +200,7 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er // arg is Header , set to request header case Header: for k, v := range a { - req.Header.Set(k, v) + req.httpreq.Header.Set(k, v) } case Params: params = append(params, a) @@ -213,8 +220,8 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er bodyBytes = req.buildJSON(a) } } - if req.Header.Get("Content-Type") == "" { - req.Header.Set("Content-Type", contentType) + if req.httpreq.Header.Get("Content-Type") == "" { + req.httpreq.Header.Set("Content-Type", contentType) } disturl, _ := buildURLParams(origurl, params...) @@ -250,10 +257,14 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er resp = &Response{} resp.R = res - resp.req = req + req_dup := *req + resp.req = &req_dup resp.ResponseDebug() resp.Content() req.reset() + if respHandler != nil { + respHandler(resp) + } if req.respHandler != nil { req.respHandler(resp) } @@ -328,3 +339,8 @@ func (req *Request) buildJSON(data interface{}) []byte { // fmt.Printf("a1=%#v,jsons=%#v\nahui\n", data, string(jsonBytes)) return jsonBytes } + +func (req *Request) SetDebug(debug bool) *Request { + req.debug = debug + return req +} From ff6e34d1d13902f9ea32f28a593e2a7d372224cd Mon Sep 17 00:00:00 2001 From: ahuigo Date: Mon, 26 Apr 2021 18:03:47 +0800 Subject: [PATCH 12/46] fix: default debug mode is false --- requests.go | 5 ++--- version | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/requests.go b/requests.go index ee0e135..5c3b53c 100644 --- a/requests.go +++ b/requests.go @@ -85,10 +85,10 @@ func (req *Request) reset() { } func (req *Request) RequestDebug() { - if req.debug { + if !req.debug { return } - fmt.Println("===========Go RequestDebug ============") + fmt.Println("===========Go RequestDebug !============") message, err := httputil.DumpRequestOut(req.httpreq, false) if err != nil { @@ -251,7 +251,6 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er res, err := req.Client.Do(req.httpreq) if err != nil { - fmt.Println(err) return nil, err } diff --git a/version b/version index 610220f..678367c 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.6 \ No newline at end of file +v0.1.7 \ No newline at end of file From 124fb958f7fc55f1863ed52328136cb2d5f0eb4b Mon Sep 17 00:00:00 2001 From: ahuigo Date: Mon, 26 Apr 2021 18:18:14 +0800 Subject: [PATCH 13/46] deploy: clean go.mod --- go.mod | 1 - version | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 0744b87..3cd331b 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,4 @@ go 1.13 require ( github.com/davecgh/go-spew v1.1.1 - gitlab.momenta.works/hdmap-workflow/account-client-go v0.0.18 ) diff --git a/version b/version index 678367c..ddd9164 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.7 \ No newline at end of file +v0.1.9 \ No newline at end of file From 0cea942a623cd18da3c914a3a10fd91f55ba8e1b Mon Sep 17 00:00:00 2001 From: ahuigo Date: Mon, 26 Apr 2021 18:22:46 +0800 Subject: [PATCH 14/46] fix: debugResponse fix debug check --- response.go | 2 +- version | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/response.go b/response.go index d637a5a..12e92e8 100644 --- a/response.go +++ b/response.go @@ -30,7 +30,7 @@ type Response struct { } func (resp *Response) ResponseDebug() { - if resp.req.debug { + if !resp.req.debug { return } fmt.Println("===========Go ResponseDebug ============") diff --git a/version b/version index ddd9164..f68b119 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.9 \ No newline at end of file +v0.1.10 \ No newline at end of file From cee2074f884e426f8276432ce66fe4eaad5b53dd Mon Sep 17 00:00:00 2001 From: ahuigo Date: Mon, 26 Apr 2021 18:25:44 +0800 Subject: [PATCH 15/46] clean: debug log in init.go --- helpers.go | 2 -- init/init.go | 1 - version | 2 +- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/helpers.go b/helpers.go index 4b41910..9187a13 100644 --- a/helpers.go +++ b/helpers.go @@ -13,10 +13,8 @@ var VERSION string = "v0.0.0" func init() { _, filename, _, _ := runtime.Caller(0) - println("filename:" + filename) versionFile := path.Dir(filename) + "/version" version, _ := ioutil.ReadFile(versionFile) - println("version:" + string(version)) VERSION = string(version) } diff --git a/init/init.go b/init/init.go index 91cdda5..a7bfcb0 100644 --- a/init/init.go +++ b/init/init.go @@ -8,7 +8,6 @@ import ( func init() { _, filename, _, _ := runtime.Caller(0) - println("filename:" + filename) dir := path.Join(path.Dir(filename), "..") err := os.Chdir(dir) if err != nil { diff --git a/version b/version index f68b119..211cd86 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.10 \ No newline at end of file +v0.1.11 \ No newline at end of file From 6411db7ba019b01bdc8d22c45f22f28911a0894d Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 27 May 2021 22:41:39 +0800 Subject: [PATCH 16/46] doc: add example about respponse and post data --- README.md | 77 +++++++++++++++++++++-------------------- examples/cookie_test.go | 14 ++++---- examples/get_test.go | 11 +++--- examples/post_test.go | 40 ++++++++++++++++++--- examples/resp_test.go | 21 +++++++++++ makefile | 5 +++ 6 files changed, 113 insertions(+), 55 deletions(-) create mode 100644 examples/resp_test.go diff --git a/README.md b/README.md index 441d32b..4c70c4f 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ Requests is an HTTP library, it is easy to use. Similar to Python requests. go get -u github.com/ahuigo/requests ``` -# Start +# Examples +> For more examples, refer to https://github.com/ahuigo/requests/tree/master/examples ## Get @@ -41,14 +42,6 @@ You can use json builder instead: json := requests.Json{ "key": "value"} -### PostString - - dataStr := "{\"key\":\"This is raw data\"}" - resp, err := requests.Post("https://www.httpbin.org/post", dataStr) - if err == nil { - fmt.Println(resp.Text()) - } - ### PostFiles path, _ := os.Getwd() @@ -66,14 +59,16 @@ You can use json builder instead: } ## Request Options -### SetTimeout and debug - req := Requests() - req.Debug = 1 +### SetTimeout - // 20 Second + req := Requests() req.SetTimeout(20) - req.Get("http://golang.org") + + +### Debug Mode + + req.Debug = 1 ### Set Authentication req := requests.Requests() @@ -83,29 +78,7 @@ You can use json builder instead: cookie1 := http.Cookie{Name: "cookie_name", Value: "cookie_value"} req.SetCookie(&cookie1) -## Response -### Get Cookies - - resp,_ = req.Get("https://www.httpbin.org") - coo := resp.Cookies() - for _, c:= range coo{ - fmt.Println(c.Name,c.Value) - } - -# Feature Support - - Set headers - - Set params - - Multipart File Uploads - - Sessions with Cookie Persistence - - Proxy - - Authentication - - JSON - - Chunked Requests - - Debug - - SetTimeout - - -# Set header +### Set header func TestGetParamsHeaders(t *testing.T) { println("Test Get: custom header and params") @@ -143,6 +116,36 @@ two or more headers ... headers3, ) + +## Response +### Fetch Response Body +https://github.com/ahuigo/requests/blob/master/examples/resp_test.go + + fmt.Println(resp.Text()) + fmt.Println(resp.Content()) + +### Fetch Response Cookies +https://github.com/ahuigo/requests/blob/master/examples/cookie_test.go + + resp,_ = req.Get("https://www.httpbin.org") + coo := resp.Cookies() + for _, c:= range coo{ + fmt.Println(c.Name,c.Value) + } + + +# Feature Support + - Set headers + - Set params + - Multipart File Uploads + - Sessions with Cookie Persistence + - Proxy + - Authentication + - JSON + - Chunked Requests + - Debug + - SetTimeout + # Thanks This project is inspired by [github.com/asmcos/requests](http://github.com/asmcos/requests). diff --git a/examples/cookie_test.go b/examples/cookie_test.go index 88078cf..90efe81 100644 --- a/examples/cookie_test.go +++ b/examples/cookie_test.go @@ -9,9 +9,9 @@ import ( _ "github.com/ahuigo/requests/init" ) -func TestSendCookie(t *testing.T) { - // example 9 test AddCookie - println("6. Get: get with cookie") +// Test send/get Cookie +func TestCookie(t *testing.T) { + println("Test: send and get cookie") req := requests.Requests().SetDebug(true) cookie := &http.Cookie{ Name: "anewcookie", @@ -23,10 +23,10 @@ func TestSendCookie(t *testing.T) { if err != nil { panic(err) } - coo := resp.Cookies() - // coo is [] *http.Cookies - println("********cookies*******") - for _, c := range coo { + cookies := resp.Cookies() + // cookies's type is `[]*http.Cookies` + println("********session cookies*******") + for _, c := range cookies { fmt.Println(c.Name, c.Value) } diff --git a/examples/get_test.go b/examples/get_test.go index 5a5e922..36f1fee 100644 --- a/examples/get_test.go +++ b/examples/get_test.go @@ -8,6 +8,7 @@ import ( _ "github.com/ahuigo/requests/init" ) +// Get Json Response func TestGetJson(t *testing.T) { println("Test Get: fetch json response") resp, err := requests.Get("https://httpbin.org/json") @@ -23,6 +24,7 @@ func TestGetJson(t *testing.T) { } } +// Send headers func TestGetParamsHeaders(t *testing.T) { println("Test Get: custom header and params") requests.Get("http://www.zhanluejia.net.cn", @@ -31,6 +33,8 @@ func TestGetParamsHeaders(t *testing.T) { requests.Params{"name": "ahuio"}, ) } + +// Send headers func TestGetParamsHeaders2(t *testing.T) { req := requests.Requests() req.SetHeader("accept-encoding", "gzip, deflate, br") @@ -39,9 +43,4 @@ func TestGetParamsHeaders2(t *testing.T) { requests.Params{"name": "ahuio"}, ) } -func TestResponseHeader(t *testing.T) { - resp, _ := requests.Get("https://www.baidu.com/") - println(resp.Text()) - println(resp.R.Header.Get("location")) - println(resp.R.Header.Get("Location")) -} + diff --git a/examples/post_test.go b/examples/post_test.go index d1a5d94..df72c78 100644 --- a/examples/post_test.go +++ b/examples/post_test.go @@ -8,26 +8,56 @@ import ( _ "github.com/ahuigo/requests/init" ) -func TestPostJson(t *testing.T) { - println("Test POST: post data and json") + +// Post params +func TestPostParams(t *testing.T) { + println("Test POST: post params") + data := requests.Params{ + "name": "ahuigo", + } + resp, err := requests.Post("https://www.httpbin.org/post", data) + if err == nil { + fmt.Println(resp.Text()) + } +} + +// Post Form Request +func TestPostForm(t *testing.T) { + println("Test POST: post form data") data := requests.Datas{ "comments": "ew", } + resp, err := requests.Post("https://www.httpbin.org/post", data) + if err == nil { + fmt.Println(resp.Text()) + } +} + + +// Post Json Request +func TestPostJson(t *testing.T) { + println("Test POST: post json data") json := requests.Json{ "key": "value", } + //it still works! json = map[string]interface{}{ "key": "value", } - resp, err := requests.Post("https://www.httpbin.org/post", data, json) + resp, err := requests.Post("https://www.httpbin.org/post", json) if err == nil { fmt.Println(resp.Text()) } } + + +// Post Raw Text func TestPostString(t *testing.T) { println("Test POST: post data and json") - dataStr := "{\"key\":\"This is raw data\"}" - resp, err := requests.Post("https://www.httpbin.org/post", dataStr) + rawText := "raw data: Hi, Jack!" + resp, err := requests.Post("https://www.httpbin.org/post", rawText, + requests.Header{"Content-Type": "text/plain"}, + ) if err == nil { fmt.Println(resp.Text()) } diff --git a/examples/resp_test.go b/examples/resp_test.go new file mode 100644 index 0000000..0cbc545 --- /dev/null +++ b/examples/resp_test.go @@ -0,0 +1,21 @@ +package examples + +import ( + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +// Test response headers +func TestResponseHeader(t *testing.T) { + resp, _ := requests.Get("https://httpbin.org/get") + println("content-type:", resp.R.Header.Get("content-type")) + //println(resp.Text()) +} + +// Test response body +func TestResponseBody(t *testing.T) { + resp, _ := requests.Get("https://httpbin.org/get") + println(resp.Text()) +} diff --git a/makefile b/makefile index e4b40ff..5840784 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,11 @@ test: go test -v ./examples + +test_local: + go test -run TestResponseHeader -v ./examples + pkg: newversion.py version jfrog "rt" "go-publish" "go-pl" $$(cat version) "--url=$$GOPROXY_API" --user=$$GOPROXY_USER --apikey=$$GOPROXY_PASS + From 747a198d28c45e9fc1b11c39c5a41354b3028cdf Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 3 Jun 2021 16:31:31 +0800 Subject: [PATCH 17/46] doc: add post examples --- README.md | 80 +++++++-- examples/post_test.go | 12 +- go.sum | 303 --------------------------------- init/{init.go => init-test.go} | 0 4 files changed, 76 insertions(+), 319 deletions(-) delete mode 100644 go.sum rename init/{init.go => init-test.go} (100%) diff --git a/README.md b/README.md index 4c70c4f..1fca365 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,22 @@ # requests Requests is an HTTP library, it is easy to use. Similar to Python requests. +Warning: it is not safe in multi goroutine. You can not do as following: + + // Bad! Do not call session in in multi goroutine!!!!! + session := requests.Requests() + + // goroutine 1 + go func(){ + session.Post(url1) + }() + + // goroutine 2 + go func(){ + session.Post(url2) + }() + + # Installation ``` @@ -26,21 +42,63 @@ go get -u github.com/ahuigo/requests ## Post -### PostJson - data := requests.Datas{ - "comments": "ew", - } - json := map[string]interface{}{ - "key": "value", +### Post params + + // Post params + func TestPostParams(t *testing.T) { + println("Test POST: post params") + data := requests.Params{ + "name": "ahuigo", + } + resp, err := requests.Post("https://www.httpbin.org/post", data) + if err == nil { + fmt.Println(resp.Text()) + } } - resp, err := requests.Post("https://www.httpbin.org/post", data, json) - if err == nil { - fmt.Println(resp.Text()) + +### Post Form Data + // Post Form Data + func TestPostForm(t *testing.T) { + println("Test POST: post form data") + data := requests.Datas{ + "comments": "ew", + } + resp, err := requests.Post("https://www.httpbin.org/post", data) + if err == nil { + fmt.Println(resp.Text()) + } } -You can use json builder instead: - json := requests.Json{ "key": "value"} +### Post Json + func TestPostJson(t *testing.T) { + println("Test POST: post json data") + json := requests.Json{ + "key": "value", + } + /* + //it still works! + json = map[string]interface{}{ + "key": "value", + } + */ + resp, err := requests.Post("https://www.httpbin.org/post", json) + if err == nil { + fmt.Println(resp.Text()) + } + } + +### Post Raw Text + func TestPostString(t *testing.T) { + println("Test POST: post data and json") + rawText := "raw data: Hi, Jack!" + resp, err := requests.Post("https://www.httpbin.org/post", rawText, + requests.Header{"Content-Type": "text/plain"}, + ) + if err == nil { + fmt.Println(resp.Text()) + } + } ### PostFiles diff --git a/examples/post_test.go b/examples/post_test.go index df72c78..efb760f 100644 --- a/examples/post_test.go +++ b/examples/post_test.go @@ -40,10 +40,12 @@ func TestPostJson(t *testing.T) { json := requests.Json{ "key": "value", } + /* //it still works! json = map[string]interface{}{ "key": "value", } + */ resp, err := requests.Post("https://www.httpbin.org/post", json) if err == nil { fmt.Println(resp.Text()) @@ -53,12 +55,12 @@ func TestPostJson(t *testing.T) { // Post Raw Text func TestPostString(t *testing.T) { - println("Test POST: post data and json") + println("Test POST: post data and json") rawText := "raw data: Hi, Jack!" - resp, err := requests.Post("https://www.httpbin.org/post", rawText, + resp, err := requests.Post("https://www.httpbin.org/post", rawText, requests.Header{"Content-Type": "text/plain"}, ) - if err == nil { - fmt.Println(resp.Text()) - } + if err == nil { + fmt.Println(resp.Text()) + } } diff --git a/go.sum b/go.sum deleted file mode 100644 index 0d11d52..0000000 --- a/go.sum +++ /dev/null @@ -1,303 +0,0 @@ -cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= -cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU= -cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY= -cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc= -cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0= -cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= -cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= -cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk= -cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= -cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= -dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= -github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= -github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= -github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= -github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= -github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= -github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= -github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= -github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= -github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= -github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= -github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= -github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= -github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= -github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= -github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= -github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= -github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= -github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= -github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= -github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= -github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= -github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= -github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= -github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= -github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= -github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= -github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= -github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= -github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= -github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= -github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= -github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= -github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= -github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU= -github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU= -github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= -github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= -github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= -github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= -github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= -github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= -github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= -github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= -github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= -github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= -github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= -github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= -github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= -github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= -github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= -github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= -github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= -github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= -github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= -github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= -github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= -github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= -github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= -github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= -github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= -github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= -github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= -github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= -github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= -github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= -github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= -github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= -github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= -github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= -github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= -github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= -github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= -github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= -github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= -gitlab.momenta.works/hdmap-workflow/account-client-go v0.0.18 h1:7/QZi/oUXCv3NMQ/vm03/MgizDk/BIk8vSv9HbT07B8= -gitlab.momenta.works/hdmap-workflow/account-client-go v0.0.18/go.mod h1:9/rsNo7fcK2JwFYEy5znLxJBKZpJLh1vwbcvvliHfMM= -go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= -go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= -go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= -go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= -go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= -go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= -go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= -go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= -go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= -go.uber.org/zap v1.14.1/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= -golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= -golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek= -golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= -golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= -golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= -golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= -golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= -golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= -golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= -google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= -google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= -google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= -google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= -google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= -google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= -google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= -google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= -gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= -gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= -gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= -gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= -rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/init/init.go b/init/init-test.go similarity index 100% rename from init/init.go rename to init/init-test.go From a35c883dd8b1ef1cbd33514a619c902ce0ec0660 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 3 Jun 2021 16:31:49 +0800 Subject: [PATCH 18/46] chore: ignore go.sum --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b79d817 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/tmp +/go.sum From ee9cde4b18572b2c4734c37c71b0170c6675578d Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 3 Jun 2021 21:40:38 +0800 Subject: [PATCH 19/46] clean: clean cleanup decrecated --- examples/session_test.go | 29 +++++++++++++++++++++++++++++ requests.go | 21 ++++++--------------- 2 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 examples/session_test.go diff --git a/examples/session_test.go b/examples/session_test.go new file mode 100644 index 0000000..e9dd67a --- /dev/null +++ b/examples/session_test.go @@ -0,0 +1,29 @@ +package examples + +import ( + "testing" + r "github.com/ahuigo/requests" +) + +// Test Session with cookies +func TestSessionWithCookie(t *testing.T) { + var data struct { + Cookies struct { + Count string `json:"count"` + } + } + session := r.Requests() + // set cookies: count=100 + session.Get("https://httpbin.org/cookies/set?count=100") + // get cookies + resp, err := session.Get("https://httpbin.org/cookies") + if err == nil { + resp.Json(&data) + if data.Cookies.Count!="100"{ + t.Fatal("Failed to get valid cookies: "+resp.Text()) + } + } + if err != nil { + t.Fatal(err) + } +} diff --git a/requests.go b/requests.go index 5c3b53c..d51d176 100644 --- a/requests.go +++ b/requests.go @@ -85,6 +85,8 @@ func (req *Request) reset() { } func (req *Request) RequestDebug() { + // fmt.Printf("req:%#v\n", req.httpreq) + // fmt.Printf("req-url:%#v\n", req.httpreq.URL.String()) if !req.debug { return } @@ -104,17 +106,12 @@ func (req *Request) RequestDebug() { } } -// cookies // cookies only save to Client.Jar -// req.Cookies is temporary func (req *Request) SetCookie(cookie *http.Cookie) *Request { req.Cookies = append(req.Cookies, cookie) return req } -func (req *Request) ClearCookies() { - req.Cookies = req.Cookies[0:0] -} // ClientSetCookies - func (req *Request) ClientSetCookies() { @@ -122,7 +119,7 @@ func (req *Request) ClientSetCookies() { // 1. Cookies have content, Copy Cookies to Client.jar // 2. Clear Cookies req.Client.Jar.SetCookies(req.httpreq.URL, req.Cookies) - req.ClearCookies() + req.Cookies = req.Cookies[0:0] } } @@ -150,7 +147,7 @@ func (req *Request) Proxy(proxyurl string) { } } -// cleanup delete +// decrecated cleanup func (req *Request) cleanup() { req.httpreq.Body = nil req.httpreq.GetBody = nil @@ -161,7 +158,6 @@ func (req *Request) cleanup() { //reset Cookies, //Client.Do can copy cookie from client.Jar to req.Header delete(req.httpreq.Header, "Cookie") - // req.ClearCookies() } // SetRespHandler @@ -184,9 +180,6 @@ func (req *Request) SetHeader(key, value string) *Request { // Post - func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, err error) { - // cleanup - // req.cleanup() - // set params ?a=b&b=c //set Header contentType := "application/x-www-form-urlencoded" @@ -246,8 +239,6 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er req.RequestDebug() - // fmt.Printf("req:%#v\n", req.httpreq) - // fmt.Printf("req-url:%#v\n", req.httpreq.URL.String()) res, err := req.Client.Do(req.httpreq) if err != nil { @@ -256,8 +247,8 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er resp = &Response{} resp.R = res - req_dup := *req - resp.req = &req_dup + reqDup := *req + resp.req = &reqDup resp.ResponseDebug() resp.Content() req.reset() From 42c6ed5ca726d2f69e90161c714511391793d449 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 3 Jun 2021 21:41:47 +0800 Subject: [PATCH 20/46] doc: add session support --- README.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1fca365..7883388 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ # requests Requests is an HTTP library, it is easy to use. Similar to Python requests. -Warning: it is not safe in multi goroutine. You can not do as following: +Warning: Session is not safe in multi goroutine. You can not do as following: // Bad! Do not call session in in multi goroutine!!!!! session := requests.Requests() @@ -116,6 +116,26 @@ go get -u github.com/ahuigo/requests fmt.Println(resp.Text()) } +## Session Support + + var data struct { + Cookies struct { + Count string `json:"count"` + } + } + session := r.Requests() + // set cookies: count=100 + session.Get("https://httpbin.org/cookies/set?count=100") + + // get cookies + resp, err := session.Get("https://httpbin.org/cookies") + if err == nil { + resp.Json(&data) + if data.Cookies.Count!="100"{ + t.Fatal("Failed to get valid cookies: "+resp.Text()) + } + } + ## Request Options ### SetTimeout From 0f276d589349641d53aa6453d596babe29f31e38 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 3 Jun 2021 22:28:55 +0800 Subject: [PATCH 21/46] clean: clean build query code --- helpers.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/helpers.go b/helpers.go index 9187a13..cc914dc 100644 --- a/helpers.go +++ b/helpers.go @@ -6,7 +6,6 @@ import ( "os" "path" "runtime" - "strings" ) var VERSION string = "v0.0.0" @@ -35,23 +34,13 @@ func buildURLParams(userURL string, params ...map[string]string) (string, error) return "", err } - parsedQuery, err := url.ParseQuery(parsedURL.RawQuery) - - if err != nil { - return "", nil - } + parsedQuery := parsedURL.Query() for _, param := range params { for key, value := range param { parsedQuery.Add(key, value) } } - return addQueryParams(parsedURL, parsedQuery), nil + return parsedURL.String(), nil } -func addQueryParams(parsedURL *url.URL, parsedQuery url.Values) string { - if len(parsedQuery) > 0 { - return strings.Join([]string{strings.Replace(parsedURL.String(), "?"+parsedURL.RawQuery, "", -1), parsedQuery.Encode()}, "?") - } - return strings.Replace(parsedURL.String(), "?"+parsedURL.RawQuery, "", -1) -} From 01efd292f68fdfd899a8565488ea18c9c8ffa980 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Sat, 5 Jun 2021 17:20:21 +0800 Subject: [PATCH 22/46] chore: replace requests with session and support buildRequest --- README.md | 52 +++++++---- examples/cookie_test.go | 2 +- examples/debug_test.go | 4 +- examples/get_test.go | 7 +- examples/proxy_test.go | 8 +- examples/req_test.go | 23 +++++ examples/session_test.go | 15 +-- examples/timeout_test.go | 4 +- methods.go | 50 ++++++---- response.go | 10 +- requests.go => sessions.go | 186 ++++++++++++++++++------------------- 11 files changed, 204 insertions(+), 157 deletions(-) create mode 100644 examples/req_test.go rename requests.go => sessions.go (53%) diff --git a/README.md b/README.md index 7883388..f35eb38 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # Requests [![license](http://dmlc.github.io/img/apache2.svg)](https://raw.githubusercontent.com/ahuigo/requests/master/LICENSE) -# requests +# Requests Requests is an HTTP library, it is easy to use. Similar to Python requests. Warning: Session is not safe in multi goroutine. You can not do as following: // Bad! Do not call session in in multi goroutine!!!!! - session := requests.Requests() + session := requests.Sessions() // goroutine 1 go func(){ @@ -103,9 +103,9 @@ go get -u github.com/ahuigo/requests ### PostFiles path, _ := os.Getwd() - req := requests.Requests() + session := requests.Sessions() - resp, err := req.SetDebug(true).Post( + resp, err := session.SetDebug(true).Post( "https://www.httpbin.org/post", requests.Files{ "file1": path + "/README.md", @@ -117,17 +117,18 @@ go get -u github.com/ahuigo/requests } ## Session Support + // 0. Make a session + session := r.Sessions() + // 1. First, set cookies: count=100 var data struct { Cookies struct { Count string `json:"count"` } } - session := r.Requests() - // set cookies: count=100 session.Get("https://httpbin.org/cookies/set?count=100") - // get cookies + // 2. Second, get cookies resp, err := session.Get("https://httpbin.org/cookies") if err == nil { resp.Json(&data) @@ -136,25 +137,40 @@ go get -u github.com/ahuigo/requests } } +Warning: Session is not safe in multi goroutine. You can not do as following: + + // Bad! Do not call session in in multi goroutine!!!!! + session := requests.Sessions() + + // goroutine 1 + go func(){ + session.Post(url1) + }() + + // goroutine 2 + go func(){ + session.Post(url2) + }() + ## Request Options ### SetTimeout - req := Requests() - req.SetTimeout(20) + session := Requests.Sessions() + session.SetTimeout(20) ### Debug Mode - req.Debug = 1 + session.Debug = 1 ### Set Authentication - req := requests.Requests() - resp,_ := req.Get("https://api.github.com/user",requests.Auth{"asmcos","password...."}) + session := requests.Sessions() + resp,_ := session.Get("https://api.github.com/user",requests.Auth{"asmcos","password...."}) ### Set Cookie cookie1 := http.Cookie{Name: "cookie_name", Value: "cookie_value"} - req.SetCookie(&cookie1) + session.SetCookie(&cookie1) ### Set header @@ -168,9 +184,9 @@ go get -u github.com/ahuigo/requests } func TestGetParamsHeaders2(t *testing.T) { - req := requests.Requests("get") - req.SetHeader("accept-encoding", "gzip, deflate, br") - req.Run("http://www.zhanluejia.net.cn", + session := requests.Sessions() + session.SetHeader("accept-encoding", "gzip, deflate, br") + session.Run("http://www.zhanluejia.net.cn", requests.Params{"page": "1", "size": "20"}, requests.Params{"name": "ahuio"}, ) @@ -187,7 +203,7 @@ two or more headers ... headers1 := requests.Header{"Referer": "http://www.jeapedu.com"}, .... - resp,_ = req.Get( + resp,_ = session.Get( "http://www.zhanluejia.net.cn", headers1, headers2, @@ -205,7 +221,7 @@ https://github.com/ahuigo/requests/blob/master/examples/resp_test.go ### Fetch Response Cookies https://github.com/ahuigo/requests/blob/master/examples/cookie_test.go - resp,_ = req.Get("https://www.httpbin.org") + resp,_ = session.Get("https://www.httpbin.org") coo := resp.Cookies() for _, c:= range coo{ fmt.Println(c.Name,c.Value) diff --git a/examples/cookie_test.go b/examples/cookie_test.go index 90efe81..6a86d01 100644 --- a/examples/cookie_test.go +++ b/examples/cookie_test.go @@ -12,7 +12,7 @@ import ( // Test send/get Cookie func TestCookie(t *testing.T) { println("Test: send and get cookie") - req := requests.Requests().SetDebug(true) + req := requests.Sessions().SetDebug(true) cookie := &http.Cookie{ Name: "anewcookie", Value: "20180825", diff --git a/examples/debug_test.go b/examples/debug_test.go index c3c7c41..1f55c74 100644 --- a/examples/debug_test.go +++ b/examples/debug_test.go @@ -10,8 +10,8 @@ import ( func TestGetDebug(t *testing.T) { println("4. Get: SetDebug") - req := requests.Requests().SetDebug(true) - resp, err := req.Get("https://httpbin.org/gzip") + session := requests.Sessions().SetDebug(true) + resp, err := session.Get("https://httpbin.org/gzip") if err == nil { fmt.Println(resp.Text()) } diff --git a/examples/get_test.go b/examples/get_test.go index 36f1fee..d18ac07 100644 --- a/examples/get_test.go +++ b/examples/get_test.go @@ -36,11 +36,10 @@ func TestGetParamsHeaders(t *testing.T) { // Send headers func TestGetParamsHeaders2(t *testing.T) { - req := requests.Requests() - req.SetHeader("accept-encoding", "gzip, deflate, br") - req.Get("http://www.zhanluejia.net.cn", + session := requests.Sessions() + session.SetHeader("accept-encoding", "gzip, deflate, br") + session.Get("http://www.zhanluejia.net.cn", requests.Params{"page": "1", "size": "20"}, requests.Params{"name": "ahuio"}, ) } - diff --git a/examples/proxy_test.go b/examples/proxy_test.go index 7359f99..0713c74 100644 --- a/examples/proxy_test.go +++ b/examples/proxy_test.go @@ -9,8 +9,8 @@ import ( func TestProxy(t *testing.T) { println("5. Get: with proxy") - req := requests.Requests() - // req.Proxy("http://192.168.1.190:8888") - req.Get("https://www.httpbin.org/cookies/set?freeform=1234") - req.Get("https://www.httpbin.org") + session := requests.Sessions() + // session.Proxy("http://192.168.1.190:8888") + session.Get("https://www.httpbin.org/cookies/set?freeform=1234") + session.Get("https://www.httpbin.org") } diff --git a/examples/req_test.go b/examples/req_test.go new file mode 100644 index 0000000..c2aee80 --- /dev/null +++ b/examples/req_test.go @@ -0,0 +1,23 @@ +package examples + +import ( + "io/ioutil" + "testing" + + r "github.com/ahuigo/requests" +) + +// TestBuildRequest +func TestBuildRequest(t *testing.T) { + req, err := r.BuildRequest("post", "http://baidu.com/a/b/c", r.Json{ + "age": 1, + }) + if err != nil { + t.Fatal(err) + } + body, _ := ioutil.ReadAll(req.Body) + expectedBody := `{"age":1}` + if string(body) != expectedBody { + t.Fatal("Failed to build request") + } +} diff --git a/examples/session_test.go b/examples/session_test.go index e9dd67a..a5e0f62 100644 --- a/examples/session_test.go +++ b/examples/session_test.go @@ -2,6 +2,7 @@ package examples import ( "testing" + r "github.com/ahuigo/requests" ) @@ -12,18 +13,18 @@ func TestSessionWithCookie(t *testing.T) { Count string `json:"count"` } } - session := r.Requests() + session := r.Sessions() // set cookies: count=100 session.Get("https://httpbin.org/cookies/set?count=100") // get cookies resp, err := session.Get("https://httpbin.org/cookies") if err == nil { resp.Json(&data) - if data.Cookies.Count!="100"{ - t.Fatal("Failed to get valid cookies: "+resp.Text()) - } + if data.Cookies.Count != "100" { + t.Fatal("Failed to get valid cookies: " + resp.Text()) + } + } + if err != nil { + t.Fatal(err) } - if err != nil { - t.Fatal(err) - } } diff --git a/examples/timeout_test.go b/examples/timeout_test.go index 7e914f2..ceaa757 100644 --- a/examples/timeout_test.go +++ b/examples/timeout_test.go @@ -11,7 +11,7 @@ import ( func TestClose(t *testing.T) { fmt.Println("Test Close") - req := requests.Requests() + req := requests.Sessions() for i := 0; i < 1000; i++ { _, err := req.Post( "http://localhost:1337/requests", @@ -25,6 +25,6 @@ func TestClose(t *testing.T) { } func TestTimeout(t *testing.T) { println("Test Timeout") - req := requests.Requests().SetTimeout(20) + req := requests.Sessions().SetTimeout(20) req.Get("http://golang.org") } diff --git a/methods.go b/methods.go index 1d0dc67..113d2e8 100644 --- a/methods.go +++ b/methods.go @@ -12,59 +12,69 @@ Licensed under the Apache License, Version 2.0 (the "License"); package requests +import "net/http" + +// BuildRequest - +func BuildRequest(method string, origurl string, args ...interface{}) (req *http.Request, err error) { + // call request Get + args = append(args, Method(method)) + req, err = Sessions().BuildRequest(origurl, args...) + return +} + /**************post/get/delete/patch*************************/ func Get(origurl string, args ...interface{}) (resp *Response, err error) { // call request Get - resp, err = Requests().Get(origurl, args...) + resp, err = Sessions().Get(origurl, args...) return resp, err } func Post(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests().Post(origurl, args...) + resp, err = Sessions().Post(origurl, args...) return } // Put func Put(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests().Put(origurl, args...) + resp, err = Sessions().Put(origurl, args...) return } // Delete func Delete(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests().Delete(origurl, args...) + resp, err = Sessions().Delete(origurl, args...) return } // Patch func Patch(origurl string, args ...interface{}) (resp *Response, err error) { - resp, err = Requests().Patch(origurl, args...) + resp, err = Sessions().Patch(origurl, args...) return } -func (req *Request) Get(origurl string, args ...interface{}) (resp *Response, err error) { - req.httpreq.Method = "GET" - resp, err = req.Run(origurl, args...) +func (session *Session) Get(origurl string, args ...interface{}) (resp *Response, err error) { + session.httpreq.Method = "GET" + resp, err = session.Run(origurl, args...) return resp, err } -func (req *Request) Post(origurl string, args ...interface{}) (resp *Response, err error) { - req.httpreq.Method = "POST" - resp, err = req.Run(origurl, args...) +func (session *Session) Post(origurl string, args ...interface{}) (resp *Response, err error) { + session.httpreq.Method = "POST" + resp, err = session.Run(origurl, args...) return resp, err } -func (req *Request) Delete(origurl string, args ...interface{}) (resp *Response, err error) { - req.httpreq.Method = "DELETE" - resp, err = req.Run(origurl, args...) +func (session *Session) Delete(origurl string, args ...interface{}) (resp *Response, err error) { + session.httpreq.Method = "DELETE" + resp, err = session.Run(origurl, args...) return resp, err } -func (req *Request) Put(origurl string, args ...interface{}) (resp *Response, err error) { - req.httpreq.Method = "PUT" - resp, err = req.Run(origurl, args...) +func (session *Session) Put(origurl string, args ...interface{}) (resp *Response, err error) { + session.httpreq.Method = "PUT" + resp, err = session.Run(origurl, args...) return resp, err } -func (req *Request) Patch(origurl string, args ...interface{}) (resp *Response, err error) { - req.httpreq.Method = "PATCH" - resp, err = req.Run(origurl, args...) +func (session *Session) Patch(origurl string, args ...interface{}) (resp *Response, err error) { + session.httpreq.Method = "PATCH" + resp, err = session.Run(origurl, args...) return resp, err } diff --git a/response.go b/response.go index 12e92e8..793ad83 100644 --- a/response.go +++ b/response.go @@ -26,11 +26,11 @@ type Response struct { R *http.Response content []byte text string - req *Request + session *Session } func (resp *Response) ResponseDebug() { - if !resp.req.debug { + if !resp.session.debug { return } fmt.Println("===========Go ResponseDebug ============") @@ -51,7 +51,7 @@ func (resp *Response) Content() []byte { defer resp.R.Body.Close() var Body = resp.R.Body - if resp.R.Header.Get("Content-Encoding") == "gzip" && resp.req.Header.Get("Accept-Encoding") != "" { + if resp.R.Header.Get("Content-Encoding") == "gzip" && resp.session.Header.Get("Accept-Encoding") != "" { reader, err := gzip.NewReader(Body) if err != nil { return nil @@ -99,8 +99,8 @@ func (resp *Response) Json(v interface{}) error { } func (resp *Response) Cookies() (cookies []*http.Cookie) { - httpreq := resp.req.httpreq - client := resp.req.Client + httpreq := resp.session.httpreq + client := resp.session.Client cookies = client.Jar.Cookies(httpreq.URL) diff --git a/requests.go b/sessions.go similarity index 53% rename from requests.go rename to sessions.go index d51d176..ed3448f 100644 --- a/requests.go +++ b/sessions.go @@ -35,7 +35,7 @@ func SetRespHandler(fn func(*Response)) { respHandler = fn } -type Request struct { +type Session struct { httpreq *http.Request Client *http.Client debug bool @@ -54,134 +54,123 @@ type Files map[string]string // name ,filename // Auth - {username,password} type Auth []string +type Method string -// Requests +// Sessions // @params method GET|POST|PUT|DELETE|PATCH -func Requests() *Request { +func Sessions() *Session { - req := new(Request) - req.reset() + session := new(Session) + session.reset() - req.Client = &http.Client{} + session.Client = &http.Client{} // cookiejar.New source code return jar, nil jar, _ := cookiejar.New(nil) - req.Client.Jar = jar + session.Client.Jar = jar - return req + return session } -func (req *Request) reset() { - req.httpreq = &http.Request{ +func (session *Session) reset() { + session.httpreq = &http.Request{ Method: "GET", Header: make(http.Header), Proto: "HTTP/1.1", ProtoMajor: 1, ProtoMinor: 1, } - req.Header = &req.httpreq.Header - req.httpreq.Header.Set("User-Agent", "Go-Requests "+VERSION) + session.Header = &session.httpreq.Header + session.httpreq.Header.Set("User-Agent", "Go-Sessions "+VERSION) } -func (req *Request) RequestDebug() { - // fmt.Printf("req:%#v\n", req.httpreq) - // fmt.Printf("req-url:%#v\n", req.httpreq.URL.String()) - if !req.debug { +func (session *Session) RequestDebug() { + if !session.debug { return } fmt.Println("===========Go RequestDebug !============") - message, err := httputil.DumpRequestOut(req.httpreq, false) + message, err := httputil.DumpRequestOut(session.httpreq, false) if err != nil { return } fmt.Println(string(message)) - if len(req.Client.Jar.Cookies(req.httpreq.URL)) > 0 { + if len(session.Client.Jar.Cookies(session.httpreq.URL)) > 0 { fmt.Println("Cookies:") - for _, cookie := range req.Client.Jar.Cookies(req.httpreq.URL) { + for _, cookie := range session.Client.Jar.Cookies(session.httpreq.URL) { fmt.Println(cookie) } } } +// cookies // cookies only save to Client.Jar -func (req *Request) SetCookie(cookie *http.Cookie) *Request { - req.Cookies = append(req.Cookies, cookie) - return req +// session.Cookies is temporary +func (session *Session) SetCookie(cookie *http.Cookie) *Session { + session.Cookies = append(session.Cookies, cookie) + return session } +func (session *Session) ClearCookies() { + session.Cookies = session.Cookies[0:0] +} // ClientSetCookies - -func (req *Request) ClientSetCookies() { - if len(req.Cookies) > 0 { +func (session *Session) ClientSetCookies() { + if len(session.Cookies) > 0 { // 1. Cookies have content, Copy Cookies to Client.jar // 2. Clear Cookies - req.Client.Jar.SetCookies(req.httpreq.URL, req.Cookies) - req.Cookies = req.Cookies[0:0] + session.Client.Jar.SetCookies(session.httpreq.URL, session.Cookies) + session.ClearCookies() } } // set timeout s = second -func (req *Request) SetTimeout(n time.Duration) *Request { - req.Client.Timeout = time.Duration(n * time.Second) - return req +func (session *Session) SetTimeout(n time.Duration) *Session { + session.Client.Timeout = time.Duration(n * time.Second) + return session } -func (req *Request) Close() { - req.httpreq.Close = true +func (session *Session) Close() { + session.httpreq.Close = true } -func (req *Request) Proxy(proxyurl string) { +func (session *Session) Proxy(proxyurl string) { urli := url.URL{} urlproxy, err := urli.Parse(proxyurl) if err != nil { fmt.Println("Set proxy failed") return } - req.Client.Transport = &http.Transport{ + session.Client.Transport = &http.Transport{ Proxy: http.ProxyURL(urlproxy), TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } } -// decrecated cleanup -func (req *Request) cleanup() { - req.httpreq.Body = nil - req.httpreq.GetBody = nil - req.httpreq.ContentLength = 0 - // req.Header = &http.Header{} - // req.Header = &req.httpreq.Header - - //reset Cookies, - //Client.Do can copy cookie from client.Jar to req.Header - delete(req.httpreq.Header, "Cookie") -} - -// SetRespHandler -func (req *Request) SetRespHandler(fn func(*Response)) *Request { - req.respHandler = fn - return req +// SetRespHandler - +func (session *Session) SetRespHandler(fn func(*Response)) *Session { + session.respHandler = fn + return session } // SetMethod -func (req *Request) SetMethod(method string) *Request { - req.httpreq.Method = strings.ToUpper(method) - return req +func (session *Session) SetMethod(method string) *Session { + session.httpreq.Method = strings.ToUpper(method) + return session } // SetHeader -func (req *Request) SetHeader(key, value string) *Request { - req.Header.Set(key, value) - return req +func (session *Session) SetHeader(key, value string) *Session { + session.Header.Set(key, value) + return session } -// Post - -func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, err error) { - // set params ?a=b&b=c - //set Header +// BuildRequest +func (session *Session) BuildRequest(origurl string, args ...interface{}) (*http.Request, error) { contentType := "application/x-www-form-urlencoded" params := []map[string]string{} datas := []map[string]string{} // form data @@ -191,9 +180,11 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er for _, arg := range args { switch a := arg.(type) { // arg is Header , set to request header + case Method: + session.httpreq.Method = strings.ToUpper(string(a)) case Header: for k, v := range a { - req.httpreq.Header.Set(k, v) + session.httpreq.Header.Set(k, v) } case Params: params = append(params, a) @@ -202,44 +193,51 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er case Files: files = append(files, a) case Auth: - req.httpreq.SetBasicAuth(a[0], a[1]) + session.httpreq.SetBasicAuth(a[0], a[1]) case string: bodyBytes = []byte(a) case Json: contentType = "application/json" - bodyBytes = req.buildJSON(a) + bodyBytes = session.buildJSON(a) default: contentType = "application/json" - bodyBytes = req.buildJSON(a) + bodyBytes = session.buildJSON(a) } } - if req.httpreq.Header.Get("Content-Type") == "" { - req.httpreq.Header.Set("Content-Type", contentType) + if session.httpreq.Header.Get("Content-Type") == "" { + session.httpreq.Header.Set("Content-Type", contentType) } disturl, _ := buildURLParams(origurl, params...) if len(files) > 0 { - req.buildFilesAndForms(files, datas) + session.buildFilesAndForms(files, datas) } else if len(bodyBytes) > 0 { // fmt.Printf("jsonBytes=%#v\n", string(jsonBytes)) - req.setBodyBytes(bodyBytes) // set forms to body + session.setBodyBytes(bodyBytes) // set forms to body } else { - Forms := req.buildForms(datas...) - req.setBodyForms(Forms) // set forms to body + Forms := session.buildForms(datas...) + session.setBodyForms(Forms) // set forms to body } //prepare to Do URL, err := url.Parse(disturl) if err != nil { return nil, err } - req.httpreq.URL = URL + session.httpreq.URL = URL - req.ClientSetCookies() + session.ClientSetCookies() + // fmt.Printf("session:%#v\n", session.httpreq) + // fmt.Printf("session-url:%#v\n", session.httpreq.URL.String()) + return session.httpreq, nil - req.RequestDebug() +} - res, err := req.Client.Do(req.httpreq) +// Post - +func (session *Session) Run(origurl string, args ...interface{}) (resp *Response, err error) { + session.BuildRequest(origurl, args...) + session.RequestDebug() + res, err := session.Client.Do(session.httpreq) if err != nil { return nil, err @@ -247,36 +245,36 @@ func (req *Request) Run(origurl string, args ...interface{}) (resp *Response, er resp = &Response{} resp.R = res - reqDup := *req - resp.req = &reqDup + req_dup := *session + resp.session = &req_dup resp.ResponseDebug() resp.Content() - req.reset() + session.reset() if respHandler != nil { respHandler(resp) } - if req.respHandler != nil { - req.respHandler(resp) + if session.respHandler != nil { + session.respHandler(resp) } return resp, nil } // only set forms -func (req *Request) setBodyForms(Forms url.Values) { +func (session *Session) setBodyForms(Forms url.Values) { data := Forms.Encode() - req.httpreq.Body = ioutil.NopCloser(strings.NewReader(data)) - req.httpreq.ContentLength = int64(len(data)) + session.httpreq.Body = ioutil.NopCloser(strings.NewReader(data)) + session.httpreq.ContentLength = int64(len(data)) } // only set forms -func (req *Request) setBodyBytes(data []byte) { - req.httpreq.Body = ioutil.NopCloser(bytes.NewReader(data)) - req.httpreq.ContentLength = int64(len(data)) +func (session *Session) setBodyBytes(data []byte) { + session.httpreq.Body = ioutil.NopCloser(bytes.NewReader(data)) + session.httpreq.ContentLength = int64(len(data)) } // upload file and form // build to body format -func (req *Request) buildFilesAndForms(files []map[string]string, datas []map[string]string) { +func (session *Session) buildFilesAndForms(files []map[string]string, datas []map[string]string) { //handle file multipart @@ -307,13 +305,13 @@ func (req *Request) buildFilesAndForms(files []map[string]string, datas []map[st w.Close() // set file header example: // "Content-Type": "multipart/form-data; boundary=------------------------7d87eceb5520850c", - req.httpreq.Body = ioutil.NopCloser(bytes.NewReader(b.Bytes())) - req.httpreq.ContentLength = int64(b.Len()) - req.Header.Set("Content-Type", w.FormDataContentType()) + session.httpreq.Body = ioutil.NopCloser(bytes.NewReader(b.Bytes())) + session.httpreq.ContentLength = int64(b.Len()) + session.Header.Set("Content-Type", w.FormDataContentType()) } // build post Form data -func (req *Request) buildForms(datas ...map[string]string) (Forms url.Values) { +func (session *Session) buildForms(datas ...map[string]string) (Forms url.Values) { Forms = url.Values{} for _, data := range datas { for key, value := range data { @@ -323,14 +321,14 @@ func (req *Request) buildForms(datas ...map[string]string) (Forms url.Values) { return Forms } -func (req *Request) buildJSON(data interface{}) []byte { +func (session *Session) buildJSON(data interface{}) []byte { jsonBytes, _ := json.Marshal(data) // fmt.Printf("a1=%#v,jsons=%#v\nahui\n", data, string(jsonBytes)) return jsonBytes } -func (req *Request) SetDebug(debug bool) *Request { - req.debug = debug - return req +func (session *Session) SetDebug(debug bool) *Session { + session.debug = debug + return session } From 38b9a1e723e7a88086163a8625641f27b1d36f81 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Sat, 5 Jun 2021 17:22:43 +0800 Subject: [PATCH 23/46] doc: support buildRequest --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f35eb38..91a35d5 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,6 @@ Warning: Session is not safe in multi goroutine. You can not do as following: session := Requests.Sessions() session.SetTimeout(20) - ### Debug Mode session.Debug = 1 @@ -227,6 +226,20 @@ https://github.com/ahuigo/requests/blob/master/examples/cookie_test.go fmt.Println(c.Name,c.Value) } +## Build Request + + req, err := r.BuildRequest("post", "http://baidu.com/a/b/c", r.Json{ + "age": 1, + }) + if err != nil { + t.Fatal(err) + } + body, _ := ioutil.ReadAll(req.Body) + expectedBody := `{"age":1}` + if string(body) != expectedBody { + t.Fatal("Failed to build request") + } + # Feature Support - Set headers From d8bd8a4e8c091358ce6a1c2989938f045c8edd96 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Tue, 22 Jun 2021 22:52:06 +0800 Subject: [PATCH 24/46] feat: support send raw bytes data --- examples/post_test.go | 22 +++++++++++++++++++++- sessions.go | 2 ++ version | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/examples/post_test.go b/examples/post_test.go index efb760f..139b499 100644 --- a/examples/post_test.go +++ b/examples/post_test.go @@ -55,7 +55,7 @@ func TestPostJson(t *testing.T) { // Post Raw Text func TestPostString(t *testing.T) { - println("Test POST: post data and json") + println("Test POST: raw post data ") rawText := "raw data: Hi, Jack!" resp, err := requests.Post("https://www.httpbin.org/post", rawText, requests.Header{"Content-Type": "text/plain"}, @@ -64,3 +64,23 @@ func TestPostString(t *testing.T) { fmt.Println(resp.Text()) } } + +// Post Raw Text +func TestPostBytes(t *testing.T) { + println("Test POST: post bytes data") + rawText := "raw data: Hi, Jack!" + resp, err := requests.Post("https://www.httpbin.org/post", []byte(rawText), + requests.Header{"Content-Type": "text/plain"}, + ) + if err != nil { + t.Error(err) + } + var data = struct{ + Data string + }{} + err = resp.Json(&data) + if data.Data != rawText { + t.Error(err) + } + +} diff --git a/sessions.go b/sessions.go index ed3448f..3400683 100644 --- a/sessions.go +++ b/sessions.go @@ -196,6 +196,8 @@ func (session *Session) BuildRequest(origurl string, args ...interface{}) (*http session.httpreq.SetBasicAuth(a[0], a[1]) case string: bodyBytes = []byte(a) + case []byte: + bodyBytes = a case Json: contentType = "application/json" bodyBytes = session.buildJSON(a) diff --git a/version b/version index 211cd86..30a48ae 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.11 \ No newline at end of file +v0.1.13 \ No newline at end of file From a751ba3d272cee914696bbc2bf416da856ff8e59 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Wed, 21 Jul 2021 23:13:57 +0800 Subject: [PATCH 25/46] fix: fix missing params when make GET request --- examples/get_test.go | 37 +++++++++++++++++++------ examples/post_test.go | 63 +++++++++++++++++++++---------------------- helpers.go | 8 +++--- version | 2 +- 4 files changed, 64 insertions(+), 46 deletions(-) diff --git a/examples/get_test.go b/examples/get_test.go index d18ac07..9762bf6 100644 --- a/examples/get_test.go +++ b/examples/get_test.go @@ -24,22 +24,43 @@ func TestGetJson(t *testing.T) { } } +type HbResponse struct { + Args map[string]string `json:"args"` +} + +// Get with params +func TestGetParams(t *testing.T) { + params := requests.Params{"name": "ahuigo"} + resp, err := requests.Get("https://httpbin.org/get", params) + if err == nil { + json := &HbResponse{} + if err := resp.Json(&json); err != nil { + t.Fatal(err) + } + if json.Args["name"] != "ahuigo" { + t.Fatal("Invalid response: " + resp.Text()) + } + } + if err != nil { + t.Fatal(err) + } +} + // Send headers -func TestGetParamsHeaders(t *testing.T) { - println("Test Get: custom header and params") - requests.Get("http://www.zhanluejia.net.cn", +func TestGetHeaders(t *testing.T) { + println("Test Get: custom header") + requests.Get( + "http://www.zhanluejia.net.cn", requests.Header{"Referer": "http://www.jeapedu.com"}, - requests.Params{"page": "1", "size": "20"}, - requests.Params{"name": "ahuio"}, ) } // Send headers -func TestGetParamsHeaders2(t *testing.T) { +func TestGetHeaderParams(t *testing.T) { session := requests.Sessions() session.SetHeader("accept-encoding", "gzip, deflate, br") - session.Get("http://www.zhanluejia.net.cn", - requests.Params{"page": "1", "size": "20"}, + session.Get( + "http://www.zhanluejia.net.cn", requests.Params{"name": "ahuio"}, ) } diff --git a/examples/post_test.go b/examples/post_test.go index 139b499..dafe109 100644 --- a/examples/post_test.go +++ b/examples/post_test.go @@ -8,7 +8,6 @@ import ( _ "github.com/ahuigo/requests/init" ) - // Post params func TestPostParams(t *testing.T) { println("Test POST: post params") @@ -23,7 +22,7 @@ func TestPostParams(t *testing.T) { // Post Form Request func TestPostForm(t *testing.T) { - println("Test POST: post form data") + println("Test POST: post form data(x-wwww-form-urlencoded)") data := requests.Datas{ "comments": "ew", } @@ -33,54 +32,52 @@ func TestPostForm(t *testing.T) { } } - // Post Json Request func TestPostJson(t *testing.T) { println("Test POST: post json data") json := requests.Json{ "key": "value", } - /* - //it still works! - json = map[string]interface{}{ - "key": "value", - } - */ + /* + //it still works! + json = map[string]interface{}{ + "key": "value", + } + */ resp, err := requests.Post("https://www.httpbin.org/post", json) if err == nil { fmt.Println(resp.Text()) } } - // Post Raw Text func TestPostString(t *testing.T) { - println("Test POST: raw post data ") - rawText := "raw data: Hi, Jack!" - resp, err := requests.Post("https://www.httpbin.org/post", rawText, - requests.Header{"Content-Type": "text/plain"}, - ) - if err == nil { - fmt.Println(resp.Text()) - } + println("Test POST: raw post data ") + rawText := "raw data: Hi, Jack!" + resp, err := requests.Post("https://www.httpbin.org/post", rawText, + requests.Header{"Content-Type": "text/plain"}, + ) + if err == nil { + fmt.Println(resp.Text()) + } } // Post Raw Text func TestPostBytes(t *testing.T) { - println("Test POST: post bytes data") - rawText := "raw data: Hi, Jack!" - resp, err := requests.Post("https://www.httpbin.org/post", []byte(rawText), - requests.Header{"Content-Type": "text/plain"}, - ) - if err != nil { - t.Error(err) - } - var data = struct{ - Data string - }{} - err = resp.Json(&data) - if data.Data != rawText { - t.Error(err) - } + println("Test POST: post bytes data") + rawText := "raw data: Hi, Jack!" + resp, err := requests.Post("https://www.httpbin.org/post", []byte(rawText), + requests.Header{"Content-Type": "text/plain"}, + ) + if err != nil { + t.Error(err) + } + var data = struct { + Data string + }{} + err = resp.Json(&data) + if data.Data != rawText { + t.Error(err) + } } diff --git a/helpers.go b/helpers.go index cc914dc..6d8aa5b 100644 --- a/helpers.go +++ b/helpers.go @@ -34,13 +34,13 @@ func buildURLParams(userURL string, params ...map[string]string) (string, error) return "", err } - parsedQuery := parsedURL.Query() + values := parsedURL.Query() for _, param := range params { for key, value := range param { - parsedQuery.Add(key, value) + values.Set(key, value) } } - return parsedURL.String(), nil + parsedURL.RawQuery = values.Encode() + return parsedURL.String(), nil } - diff --git a/version b/version index 30a48ae..26452c5 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.13 \ No newline at end of file +v0.1.14 \ No newline at end of file From 80dae234f597c8123fc952ed6336b0d44ecf3a54 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Wed, 21 Jul 2021 23:37:37 +0800 Subject: [PATCH 26/46] example: add delete method example --- examples/delete_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 examples/delete_test.go diff --git a/examples/delete_test.go b/examples/delete_test.go new file mode 100644 index 0000000..e36392f --- /dev/null +++ b/examples/delete_test.go @@ -0,0 +1,23 @@ +package examples + +import ( + "fmt" + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +// Delete Form Request +func TestDeleteForm(t *testing.T) { + println("Test DELETE method: form data(x-wwww-form-urlencoded)") + data := requests.Datas{ + "comments": "ew", + } + session := requests.Sessions() //.SetDebug(true) + resp, err := session.Delete("https://www.httpbin.org/delete", data) + if err == nil { + fmt.Println(resp.Text()) + } +} + From c11362d395dfbae46a1214aca1e2ac1bdf909c10 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 22 Jul 2021 15:28:05 +0800 Subject: [PATCH 27/46] test: add send cookie test --- examples/cookie_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/examples/cookie_test.go b/examples/cookie_test.go index 6a86d01..eab2a20 100644 --- a/examples/cookie_test.go +++ b/examples/cookie_test.go @@ -9,8 +9,22 @@ import ( _ "github.com/ahuigo/requests/init" ) -// Test send/get Cookie -func TestCookie(t *testing.T) { +func TestSendCookie(t *testing.T) { + resp, err := requests.Get("https://www.httpbin.org/cookies", + requests.Header{"Cookie": "id_token=1234"}, + requests.Json{"workflow_id": "wfid1234"}, + ) + if err != nil { + panic(err) + } + data := map[string]interface{}{} + resp.Json(&data) + fmt.Println(data) + +} + +// Test session Cookie +func TestSessionCookie(t *testing.T) { println("Test: send and get cookie") req := requests.Sessions().SetDebug(true) cookie := &http.Cookie{ From 8d926fb9e7d7ded23f7a5d691503c7348ace78ea Mon Sep 17 00:00:00 2001 From: ahuigo Date: Tue, 27 Jul 2021 15:49:34 +0800 Subject: [PATCH 28/46] feat: return request url within error if request failed --- sessions.go | 3 ++- version | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sessions.go b/sessions.go index 3400683..a0021ee 100644 --- a/sessions.go +++ b/sessions.go @@ -25,6 +25,7 @@ import ( "net/http/httputil" "net/url" "strings" + "errors" "time" ) @@ -242,7 +243,7 @@ func (session *Session) Run(origurl string, args ...interface{}) (resp *Response res, err := session.Client.Do(session.httpreq) if err != nil { - return nil, err + return nil, errors.New(session.httpreq.Method+" "+origurl+" "+err.Error()) } resp = &Response{} diff --git a/version b/version index 26452c5..bb86fac 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.14 \ No newline at end of file +v0.1.15 \ No newline at end of file From b876700805a511fda10a340c3168e129921a5fa0 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Fri, 13 Aug 2021 11:38:56 +0800 Subject: [PATCH 29/46] doc: readme.md add an example about get with params --- README.md | 3 ++- examples/get_test.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 91a35d5..f872153 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ go get -u github.com/ahuigo/requests ## Get var json map[string]interface{} - resp, err := requests.Get("https://httpbin.org/json") + params := requests.Params{"name": "ahuigo", "page":"1"} + resp, err := requests.Get("https://httpbin.org/json", params) if err == nil { resp.Json(&json) for k, v := range json { diff --git a/examples/get_test.go b/examples/get_test.go index 9762bf6..e271120 100644 --- a/examples/get_test.go +++ b/examples/get_test.go @@ -30,7 +30,7 @@ type HbResponse struct { // Get with params func TestGetParams(t *testing.T) { - params := requests.Params{"name": "ahuigo"} + params := requests.Params{"name": "ahuigo", "page":"1"} resp, err := requests.Get("https://httpbin.org/get", params) if err == nil { json := &HbResponse{} From 554ffbd8ea055f72ad1517e958a86577280b0e4e Mon Sep 17 00:00:00 2001 From: ahuigo Date: Sat, 9 Oct 2021 15:19:05 +0800 Subject: [PATCH 30/46] chore: add tag in makefile --- makefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/makefile b/makefile index 5840784..a176b1b 100644 --- a/makefile +++ b/makefile @@ -4,8 +4,15 @@ test: test_local: go test -run TestResponseHeader -v ./examples +.ONESHELL: +t: + v=`cat version` && echoraw $$v + v=`cat version` && git tag $$v && git push origin $$v + pkg: newversion.py version jfrog "rt" "go-publish" "go-pl" $$(cat version) "--url=$$GOPROXY_API" --user=$$GOPROXY_USER --apikey=$$GOPROXY_PASS + v=`cat version` && git tag $$v && git push origin $$v + From d971e1237b62bffb59318d63dc2f00932b0b4df6 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 14 Oct 2021 20:13:11 +0800 Subject: [PATCH 31/46] doc: add main get example --- README.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f872153..35ed0c0 100644 --- a/README.md +++ b/README.md @@ -30,17 +30,24 @@ go get -u github.com/ahuigo/requests > For more examples, refer to https://github.com/ahuigo/requests/tree/master/examples ## Get + package main + import ( + "github.com/ahuigo/requests" + ) - var json map[string]interface{} - params := requests.Params{"name": "ahuigo", "page":"1"} - resp, err := requests.Get("https://httpbin.org/json", params) - if err == nil { - resp.Json(&json) - for k, v := range json { - fmt.Println(k, v) + func main(){ + var json map[string]interface{} + params := requests.Params{"name": "ahuigo", "page":"1"} + resp, err := requests.Get("https://httpbin.org/json", params) + if err == nil { + resp.Json(&json) + for k, v := range json { + fmt.Println(k, v) + } } } + ## Post ### Post params From e5ac318a22d625ce216682b3c5745b92891d093a Mon Sep 17 00:00:00 2001 From: ahuigo Date: Fri, 15 Oct 2021 22:38:25 +0800 Subject: [PATCH 32/46] feat: support custom global header --- README.md | 8 +++++ examples/get_test.go | 22 ++---------- examples/{req_test.go => req_build_test.go} | 0 examples/req_header_test.go | 37 +++++++++++++++++++++ sessions.go | 20 +++++++++-- version | 2 +- 6 files changed, 65 insertions(+), 24 deletions(-) rename examples/{req_test.go => req_build_test.go} (100%) create mode 100644 examples/req_header_test.go diff --git a/README.md b/README.md index 35ed0c0..58aa2d3 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ go get -u github.com/ahuigo/requests package main import ( "github.com/ahuigo/requests" + "fmt" ) func main(){ @@ -248,6 +249,13 @@ https://github.com/ahuigo/requests/blob/master/examples/cookie_test.go t.Fatal("Failed to build request") } +## Custom + +### Custom User Agent + + headerK := "User-Agent" + headerV := "Custom-Test-Go-User-Agent" + requests.SetHeader(headerK, headerV) # Feature Support - Set headers diff --git a/examples/get_test.go b/examples/get_test.go index e271120..195fc50 100644 --- a/examples/get_test.go +++ b/examples/get_test.go @@ -30,8 +30,9 @@ type HbResponse struct { // Get with params func TestGetParams(t *testing.T) { - params := requests.Params{"name": "ahuigo", "page":"1"} + params := requests.Params{"name": "ahuigo", "page": "1"} resp, err := requests.Get("https://httpbin.org/get", params) + if err == nil { json := &HbResponse{} if err := resp.Json(&json); err != nil { @@ -45,22 +46,3 @@ func TestGetParams(t *testing.T) { t.Fatal(err) } } - -// Send headers -func TestGetHeaders(t *testing.T) { - println("Test Get: custom header") - requests.Get( - "http://www.zhanluejia.net.cn", - requests.Header{"Referer": "http://www.jeapedu.com"}, - ) -} - -// Send headers -func TestGetHeaderParams(t *testing.T) { - session := requests.Sessions() - session.SetHeader("accept-encoding", "gzip, deflate, br") - session.Get( - "http://www.zhanluejia.net.cn", - requests.Params{"name": "ahuio"}, - ) -} diff --git a/examples/req_test.go b/examples/req_build_test.go similarity index 100% rename from examples/req_test.go rename to examples/req_build_test.go diff --git a/examples/req_header_test.go b/examples/req_header_test.go new file mode 100644 index 0000000..925bb77 --- /dev/null +++ b/examples/req_header_test.go @@ -0,0 +1,37 @@ +package examples + +import ( + "testing" + + "github.com/ahuigo/requests" +) + +// Send headers +func TestSendHeaders(t *testing.T) { + println("Test Get: send header") + requests.Get( + "http://www.zhanluejia.net.cn", + requests.Header{"Referer": "http://www.jeapedu.com"}, + ) +} + +// Set session headers +func TestSendSessionHeader(t *testing.T) { + session := requests.Sessions() + session.SetHeader("accept-encoding", "gzip, deflate, br") + session.Get("http://www.zhanluejia.net.cn") +} + +// Test send header +func TestSetGlobalHeader(t *testing.T) { + headerK := "User-Agent" + headerV := "Custom-Test-Go-User-Agent" + requests.SetHeader(headerK, headerV) + req, err := requests.BuildRequest("post", "http://baidu.com/a/b/c") + if err != nil { + t.Fatal(err) + } + if req.Header.Get(headerK) != headerV { + t.Fatalf("Expected header %s is %s", headerK, headerV) + } +} diff --git a/sessions.go b/sessions.go index a0021ee..06e9ac1 100644 --- a/sessions.go +++ b/sessions.go @@ -16,6 +16,7 @@ import ( "bytes" "crypto/tls" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -25,11 +26,13 @@ import ( "net/http/httputil" "net/url" "strings" - "errors" "time" ) var respHandler func(*Response) +var gHeader = map[string]string{ + "User-Agent": "Go-requests-" + VERSION, +} // SetRespHandler func SetRespHandler(fn func(*Response)) { @@ -74,6 +77,15 @@ func Sessions() *Session { return session } +// Set global header +func SetHeader(key, value string) { + if value == "" { + delete(gHeader, key) + return + } + gHeader[key] = value +} + func (session *Session) reset() { session.httpreq = &http.Request{ Method: "GET", @@ -83,7 +95,9 @@ func (session *Session) reset() { ProtoMinor: 1, } session.Header = &session.httpreq.Header - session.httpreq.Header.Set("User-Agent", "Go-Sessions "+VERSION) + for key, value := range gHeader { + session.httpreq.Header.Set(key, value) + } } func (session *Session) RequestDebug() { @@ -243,7 +257,7 @@ func (session *Session) Run(origurl string, args ...interface{}) (resp *Response res, err := session.Client.Do(session.httpreq) if err != nil { - return nil, errors.New(session.httpreq.Method+" "+origurl+" "+err.Error()) + return nil, errors.New(session.httpreq.Method + " " + origurl + " " + err.Error()) } resp = &Response{} diff --git a/version b/version index bb86fac..a33ca21 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.15 \ No newline at end of file +v0.1.16 \ No newline at end of file From a41933b9a7a791569bd363f1dc4bf9eb40eecee5 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Fri, 29 Oct 2021 03:14:49 +0800 Subject: [PATCH 33/46] feat: add curl generator 0.1 --- README.md | 4 ++-- examples/req_build_test.go | 13 +++++++++++++ methods.go | 10 ---------- utils.go | 16 ++++++++++++++++ 4 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 utils.go diff --git a/README.md b/README.md index 58aa2d3..7f37cbb 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ # Requests Requests is an HTTP library, it is easy to use. Similar to Python requests. -Warning: Session is not safe in multi goroutine. You can not do as following: +Warning: Session is not safe in multiple goroutines. You can not do as following: - // Bad! Do not call session in in multi goroutine!!!!! + // Bad! Do not call session in in multiple goroutines!!!!! session := requests.Sessions() // goroutine 1 diff --git a/examples/req_build_test.go b/examples/req_build_test.go index c2aee80..eaf4d4e 100644 --- a/examples/req_build_test.go +++ b/examples/req_build_test.go @@ -21,3 +21,16 @@ func TestBuildRequest(t *testing.T) { t.Fatal("Failed to build request") } } + +func TestBuildCurlRequest(t *testing.T) { + // req, err := r.BuildRequest("post", "http://baidu.com/a/b/c", r.Json{ + // "age": 1, + // }) + // if err != nil { + // t.Fatal(err) + // } + // cmd:=r.BuildCurlRequest(req) + // if !regexp.MustCompile(``).MatchString(cmd){ + // t.Fatal(`bad curl cmd`) + // } +} diff --git a/methods.go b/methods.go index 113d2e8..4a64019 100644 --- a/methods.go +++ b/methods.go @@ -12,16 +12,6 @@ Licensed under the Apache License, Version 2.0 (the "License"); package requests -import "net/http" - -// BuildRequest - -func BuildRequest(method string, origurl string, args ...interface{}) (req *http.Request, err error) { - // call request Get - args = append(args, Method(method)) - req, err = Sessions().BuildRequest(origurl, args...) - return -} - /**************post/get/delete/patch*************************/ func Get(origurl string, args ...interface{}) (resp *Response, err error) { // call request Get diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..756746d --- /dev/null +++ b/utils.go @@ -0,0 +1,16 @@ +package requests + +import "net/http" + +// BuildRequest - +func BuildRequest(method string, origurl string, args ...interface{}) (req *http.Request, err error) { + // call request Get + args = append(args, Method(method)) + req, err = Sessions().BuildRequest(origurl, args...) + return +} + +func BuildCurlRequest(req *http.Request) (curl string) { + // call request Get + return +} From 6e30b812329eaabd9f4cb6a286e3d9386541ddf7 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Tue, 2 Nov 2021 16:51:20 +0800 Subject: [PATCH 34/46] feat: support curl shell command generator --- README.md | 23 +++++++++++++++----- examples/req_build_test.go | 23 ++++++++++---------- go.mod | 1 + utils.go | 44 +++++++++++++++++++++++++++++++++++--- 4 files changed, 71 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7f37cbb..de06b11 100644 --- a/README.md +++ b/README.md @@ -235,6 +235,16 @@ https://github.com/ahuigo/requests/blob/master/examples/cookie_test.go fmt.Println(c.Name,c.Value) } +## Custom + +### Custom User Agent + + headerK := "User-Agent" + headerV := "Custom-Test-Go-User-Agent" + requests.SetHeader(headerK, headerV) + +# Utils + ## Build Request req, err := r.BuildRequest("post", "http://baidu.com/a/b/c", r.Json{ @@ -249,13 +259,16 @@ https://github.com/ahuigo/requests/blob/master/examples/cookie_test.go t.Fatal("Failed to build request") } -## Custom +## Generate curl shell command -### Custom User Agent + req, _ := requests.BuildRequest("post", "https://baidu.com/path?q=curl&v=1", requests.Json{ + "age": 1, + }) + curl := requests.BuildCurlRequest(req) + if !regexp.MustCompile(`^curl -X POST .+ 'https://baidu.com/path\?q=curl&v=1'`).MatchString(curl) { + t.Fatal(`bad curl cmd: ` + curl) + } - headerK := "User-Agent" - headerV := "Custom-Test-Go-User-Agent" - requests.SetHeader(headerK, headerV) # Feature Support - Set headers diff --git a/examples/req_build_test.go b/examples/req_build_test.go index eaf4d4e..d3d3087 100644 --- a/examples/req_build_test.go +++ b/examples/req_build_test.go @@ -2,14 +2,15 @@ package examples import ( "io/ioutil" + "regexp" "testing" - r "github.com/ahuigo/requests" + "github.com/ahuigo/requests" ) // TestBuildRequest func TestBuildRequest(t *testing.T) { - req, err := r.BuildRequest("post", "http://baidu.com/a/b/c", r.Json{ + req, err := requests.BuildRequest("post", "http://baidu.com/a/b/c", requests.Json{ "age": 1, }) if err != nil { @@ -23,14 +24,12 @@ func TestBuildRequest(t *testing.T) { } func TestBuildCurlRequest(t *testing.T) { - // req, err := r.BuildRequest("post", "http://baidu.com/a/b/c", r.Json{ - // "age": 1, - // }) - // if err != nil { - // t.Fatal(err) - // } - // cmd:=r.BuildCurlRequest(req) - // if !regexp.MustCompile(``).MatchString(cmd){ - // t.Fatal(`bad curl cmd`) - // } + req, _ := requests.BuildRequest("post", "https://baidu.com/path?q=curl&v=1", requests.Json{ + "age": 1, + }) + curl := requests.BuildCurlRequest(req) + if !regexp.MustCompile(`^curl -X POST .+ 'https://baidu.com/path\?q=curl&v=1'`).MatchString(curl) { + t.Fatal(`bad curl cmd: ` + curl) + } + t.Log(curl) } diff --git a/go.mod b/go.mod index 3cd331b..d0c22c0 100644 --- a/go.mod +++ b/go.mod @@ -3,5 +3,6 @@ module github.com/ahuigo/requests go 1.13 require ( + github.com/alessio/shellescape v1.4.1 github.com/davecgh/go-spew v1.1.1 ) diff --git a/utils.go b/utils.go index 756746d..a947a82 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,12 @@ package requests -import "net/http" +import ( + "bytes" + "io/ioutil" + "net/http" + + "github.com/alessio/shellescape" +) // BuildRequest - func BuildRequest(method string, origurl string, args ...interface{}) (req *http.Request, err error) { @@ -11,6 +17,38 @@ func BuildRequest(method string, origurl string, args ...interface{}) (req *http } func BuildCurlRequest(req *http.Request) (curl string) { - // call request Get - return + curl = "curl -X " + req.Method + " " + // req.Host + req.URL.Path + "?" + req.URL.RawQuery + " " + req.Proto + " " + headers := getHeaders(req) + for _, kv := range *headers { + curl += `-H ` + shellescape.Quote(kv[0]+": "+kv[1]) + ` ` + } + // body + buf, _ := ioutil.ReadAll(req.Body) + req.Body = ioutil.NopCloser(bytes.NewBuffer(buf)) // important!! + curl += `-d ` + shellescape.Quote(string(buf)) + + curl += " " + shellescape.Quote(req.URL.String()) + return curl +} + +// getHeaders +func getHeaders(req *http.Request) *[][2]string { + headers := [][2]string{} + for k, vs := range req.Header { + for _, v := range vs { + headers = append(headers, [2]string{k, v}) + } + } + n := len(headers) + for i := 0; i < n; i++ { + for j := n - 1; j > i; j-- { + jj := j - 1 + h1, h2 := headers[j], headers[jj] + if h1[0] < h2[0] { + headers[jj], headers[j] = headers[j], headers[jj] + } + } + } + return &headers } From ab90cf43b1aaecdf3aa4212aec8a66ec714f0508 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Wed, 3 Nov 2021 11:57:10 +0800 Subject: [PATCH 35/46] feat: support curl command generator --- examples/resp_handler_test.go | 15 +++++++++++++++ examples/timeout_test.go | 4 ++-- sessions.go | 3 ++- utils.go | 6 +++++- version | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 examples/resp_handler_test.go diff --git a/examples/resp_handler_test.go b/examples/resp_handler_test.go new file mode 100644 index 0000000..334a3d4 --- /dev/null +++ b/examples/resp_handler_test.go @@ -0,0 +1,15 @@ +package examples + +import ( + "testing" + + "github.com/ahuigo/requests" + _ "github.com/ahuigo/requests/init" +) + +// Test response headers +func TestResponseHeader1(t *testing.T) { + resp, _ := requests.Get("https://httpbin.org/get") + println("content-type:", resp.R.Header.Get("content-type")) + //println(resp.Text()) +} diff --git a/examples/timeout_test.go b/examples/timeout_test.go index ceaa757..4a0827c 100644 --- a/examples/timeout_test.go +++ b/examples/timeout_test.go @@ -12,7 +12,7 @@ import ( func TestClose(t *testing.T) { fmt.Println("Test Close") req := requests.Sessions() - for i := 0; i < 1000; i++ { + for i := 0; i < 10; i++ { _, err := req.Post( "http://localhost:1337/requests", requests.Datas{"SrcIp": "4312"}) @@ -21,7 +21,7 @@ func TestClose(t *testing.T) { } spew.Dump(req) - fmt.Println("1000 times get test end.") + fmt.Println("10 times get test end.") } func TestTimeout(t *testing.T) { println("Test Timeout") diff --git a/sessions.go b/sessions.go index 06e9ac1..da65a1b 100644 --- a/sessions.go +++ b/sessions.go @@ -105,7 +105,8 @@ func (session *Session) RequestDebug() { return } fmt.Println("===========Go RequestDebug !============") - + curl := BuildCurlRequest(session.httpreq) + fmt.Println(curl) message, err := httputil.DumpRequestOut(session.httpreq, false) if err != nil { return diff --git a/utils.go b/utils.go index a947a82..5da21b6 100644 --- a/utils.go +++ b/utils.go @@ -26,7 +26,9 @@ func BuildCurlRequest(req *http.Request) (curl string) { // body buf, _ := ioutil.ReadAll(req.Body) req.Body = ioutil.NopCloser(bytes.NewBuffer(buf)) // important!! - curl += `-d ` + shellescape.Quote(string(buf)) + if len(buf) > 0 { + curl += `-d ` + shellescape.Quote(string(buf)) + } curl += " " + shellescape.Quote(req.URL.String()) return curl @@ -41,6 +43,8 @@ func getHeaders(req *http.Request) *[][2]string { } } n := len(headers) + // fmt.Printf("%#v\n", headers) + // sort headers for i := 0; i < n; i++ { for j := n - 1; j > i; j-- { jj := j - 1 diff --git a/version b/version index a33ca21..1afd364 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.16 \ No newline at end of file +v0.1.17 \ No newline at end of file From 95cd32de48812da48a376689249b184263aed7dc Mon Sep 17 00:00:00 2001 From: ahuigo Date: Wed, 3 Nov 2021 15:19:10 +0800 Subject: [PATCH 36/46] test: add cookie test for sesseion cookie --- examples/cookie_test.go | 24 ++++++++++++++++++------ examples/debug_test.go | 13 +++++++++++-- makefile | 4 ++++ sessions.go | 9 +++++++-- utils.go | 4 ++++ version | 2 +- 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/examples/cookie_test.go b/examples/cookie_test.go index eab2a20..84fe210 100644 --- a/examples/cookie_test.go +++ b/examples/cookie_test.go @@ -28,20 +28,32 @@ func TestSessionCookie(t *testing.T) { println("Test: send and get cookie") req := requests.Sessions().SetDebug(true) cookie := &http.Cookie{ - Name: "anewcookie", - Value: "20180825", + Name: "name1", + Value: "value1", Path: "/", } - resp, err := req.SetCookie(cookie).Get("https://www.httpbin.org") + req.SetCookie(cookie).Get("https://www.httpbin.org") + resp, err := req.Get("https://www.httpbin.org", + &http.Cookie{ + Name: "name2", + Value: "value2", + }, + ) // resp, err := req.SetCookie(cookie).Get("http://127.0.0.1:8088") if err != nil { panic(err) } - cookies := resp.Cookies() + cookies := map[string]string{} // cookies's type is `[]*http.Cookies` println("********session cookies*******") - for _, c := range cookies { - fmt.Println(c.Name, c.Value) + for _, c := range resp.Cookies() { + if _, exists := cookies[c.Name]; exists { + t.Fatal("duplicated cookie:", c.Name, c.Value) + } + cookies[c.Name] = c.Value + } + if cookies["name1"] != "value1" || cookies["name2"] != "value2" { + t.Fatal("Failed to send valid cookie") } } diff --git a/examples/debug_test.go b/examples/debug_test.go index 1f55c74..38e01f7 100644 --- a/examples/debug_test.go +++ b/examples/debug_test.go @@ -2,6 +2,7 @@ package examples import ( "fmt" + "net/http" "testing" "github.com/ahuigo/requests" @@ -11,8 +12,16 @@ import ( func TestGetDebug(t *testing.T) { println("4. Get: SetDebug") session := requests.Sessions().SetDebug(true) - resp, err := session.Get("https://httpbin.org/gzip") + resp, err := session.Post("https://httpbin.org/post", + requests.Json{ + "name": "ahuigo", + }, + &http.Cookie{ + Name: "count", + Value: "1", + }, + ) if err == nil { - fmt.Println(resp.Text()) + fmt.Println("response text:", resp.Text()) } } diff --git a/makefile b/makefile index a176b1b..5c1d631 100644 --- a/makefile +++ b/makefile @@ -1,3 +1,4 @@ +msg?='' test: go test -v ./examples @@ -10,7 +11,10 @@ t: v=`cat version` && git tag $$v && git push origin $$v pkg: + echoraw "$$msg" + exit newversion.py version + git commit -am "$$msg" jfrog "rt" "go-publish" "go-pl" $$(cat version) "--url=$$GOPROXY_API" --user=$$GOPROXY_USER --apikey=$$GOPROXY_PASS v=`cat version` && git tag $$v && git push origin $$v diff --git a/sessions.go b/sessions.go index da65a1b..32264b7 100644 --- a/sessions.go +++ b/sessions.go @@ -137,8 +137,11 @@ func (session *Session) ClearCookies() { func (session *Session) ClientSetCookies() { if len(session.Cookies) > 0 { // 1. Cookies have content, Copy Cookies to Client.jar - // 2. Clear Cookies + // for _, cookie := range session.Cookies { + // session.httpreq.AddCookie(cookie) + // } session.Client.Jar.SetCookies(session.httpreq.URL, session.Cookies) + // 2. Clear Cookies session.ClearCookies() } @@ -214,6 +217,8 @@ func (session *Session) BuildRequest(origurl string, args ...interface{}) (*http bodyBytes = []byte(a) case []byte: bodyBytes = a + case *http.Cookie: + session.SetCookie(a) case Json: contentType = "application/json" bodyBytes = session.buildJSON(a) @@ -254,8 +259,8 @@ func (session *Session) BuildRequest(origurl string, args ...interface{}) (*http // Post - func (session *Session) Run(origurl string, args ...interface{}) (resp *Response, err error) { session.BuildRequest(origurl, args...) - session.RequestDebug() res, err := session.Client.Do(session.httpreq) + session.RequestDebug() if err != nil { return nil, errors.New(session.httpreq.Method + " " + origurl + " " + err.Error()) diff --git a/utils.go b/utils.go index 5da21b6..4132417 100644 --- a/utils.go +++ b/utils.go @@ -23,6 +23,10 @@ func BuildCurlRequest(req *http.Request) (curl string) { for _, kv := range *headers { curl += `-H ` + shellescape.Quote(kv[0]+": "+kv[1]) + ` ` } + // // cookies + // for _, cookie := range req.Cookies() { + // fmt.Printf("cookie:%#v\n", cookie) + // } // body buf, _ := ioutil.ReadAll(req.Body) req.Body = ioutil.NopCloser(bytes.NewBuffer(buf)) // important!! diff --git a/version b/version index 1afd364..92e284b 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.17 \ No newline at end of file +v0.1.20 \ No newline at end of file From 751cdae09d4a4454f68c32bd977e38ae16b92f7c Mon Sep 17 00:00:00 2001 From: ahuigo Date: Wed, 3 Nov 2021 15:34:20 +0800 Subject: [PATCH 37/46] chore: update test --- makefile | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/makefile b/makefile index 5c1d631..21bccaa 100644 --- a/makefile +++ b/makefile @@ -10,13 +10,8 @@ t: v=`cat version` && echoraw $$v v=`cat version` && git tag $$v && git push origin $$v -pkg: - echoraw "$$msg" - exit +pkg: test newversion.py version git commit -am "$$msg" jfrog "rt" "go-publish" "go-pl" $$(cat version) "--url=$$GOPROXY_API" --user=$$GOPROXY_USER --apikey=$$GOPROXY_PASS v=`cat version` && git tag $$v && git push origin $$v - - - From a6dd492535b50c7c617cb0ea2bbddec071214cc1 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Thu, 4 Nov 2021 11:14:45 +0800 Subject: [PATCH 38/46] chore: add git msg check when push code --- makefile | 15 +++++++++------ version | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/makefile b/makefile index 21bccaa..55ab266 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -msg?='' +msg ?= '' test: go test -v ./examples @@ -7,11 +7,14 @@ test_local: .ONESHELL: t: - v=`cat version` && echoraw $$v - v=`cat version` && git tag $$v && git push origin $$v + if [[ "$(msg)" = "" ]] ; then echo "Usage: make pkg msg='commit msg'";exit 20; fi -pkg: test +gitcheck: + if [[ "$(msg)" = "" ]] ; then echo "Usage: make pkg msg='commit msg'";exit 20; fi + +.ONESHELL: +pkg: gitcheck test newversion.py version - git commit -am "$$msg" + git commit -am "$(msg)" jfrog "rt" "go-publish" "go-pl" $$(cat version) "--url=$$GOPROXY_API" --user=$$GOPROXY_USER --apikey=$$GOPROXY_PASS - v=`cat version` && git tag $$v && git push origin $$v + v=`cat version` && git tag "$$v" && git push origin "$$v" diff --git a/version b/version index 92e284b..d6af0a9 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.20 \ No newline at end of file +v0.1.22 \ No newline at end of file From 7bdbca1706d32d1e19dcc8e5774c287dce1f12ff Mon Sep 17 00:00:00 2001 From: ahuigo Date: Fri, 19 Nov 2021 16:39:10 +0800 Subject: [PATCH 39/46] doc: add doc about Post query string --- README.md | 29 +++++++++-- examples/post_file_test.go | 15 ++++-- examples/post_test.go | 102 +++++++++++++++++++++++++++++-------- 3 files changed, 118 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index de06b11..c94ee77 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,27 @@ go get -u github.com/ahuigo/requests fmt.Println(resp.Text()) } } +#### Post query string + + // Post QueryString: application/x-www-form-urlencoded + func TestPostQueryString(t *testing.T) { + queryString := "name=Alex&age=29" + resp, err := requests.Post("https://www.httpbin.org/post", queryString) + if err != nil { + t.Fatal(err) + } + var data = struct { + Form struct{ + Name string + Age string + } + }{} + err = resp.Json(&data) + if data.Form.Age != "29"{ + t.Error("invalid response body:", resp.Text()) + } + } + ### Post Form Data // Post Form Data @@ -97,11 +118,13 @@ go get -u github.com/ahuigo/requests } } -### Post Raw Text - func TestPostString(t *testing.T) { +### Post Raw text/plain + func TestRawString(t *testing.T) { println("Test POST: post data and json") rawText := "raw data: Hi, Jack!" - resp, err := requests.Post("https://www.httpbin.org/post", rawText, + resp, err := requests.Post( + "https://www.httpbin.org/post", + rawText, requests.Header{"Content-Type": "text/plain"}, ) if err == nil { diff --git a/examples/post_file_test.go b/examples/post_file_test.go index ce9deea..42bde6c 100644 --- a/examples/post_file_test.go +++ b/examples/post_file_test.go @@ -1,7 +1,6 @@ package examples import ( - "fmt" "os" "testing" @@ -19,7 +18,17 @@ func TestPostFile(t *testing.T) { "file2": path + "/version", }, ) - if err == nil { - fmt.Println(resp.Text()) + if err !=nil { + t.Error(err) } + var data = struct { + Files struct{ + File2 string + } + }{} + err = resp.Json(&data) + if data.Files.File2== ""{ + t.Error("invalid response body:", resp.Text()) + } + } diff --git a/examples/post_test.go b/examples/post_test.go index dafe109..0123a21 100644 --- a/examples/post_test.go +++ b/examples/post_test.go @@ -1,8 +1,8 @@ package examples import ( - "fmt" "testing" + ejson "encoding/json" "github.com/ahuigo/requests" _ "github.com/ahuigo/requests/init" @@ -11,24 +11,46 @@ import ( // Post params func TestPostParams(t *testing.T) { println("Test POST: post params") - data := requests.Params{ - "name": "ahuigo", + resp, err := requests.Post( + "https://www.httpbin.org/post", + requests.Params{ + "name": "ahuigo", + }, + ) + if err !=nil { + t.Error(err) } - resp, err := requests.Post("https://www.httpbin.org/post", data) - if err == nil { - fmt.Println(resp.Text()) + var data = struct { + Args struct{ + Name string + } + }{} + err = resp.Json(&data) + if data.Args.Name!= "ahuigo"{ + t.Error("invalid response body:", resp.Text()) } } -// Post Form Request +// Post Form Data func TestPostForm(t *testing.T) { println("Test POST: post form data(x-wwww-form-urlencoded)") - data := requests.Datas{ - "comments": "ew", + resp, err := requests.Post( + "https://www.httpbin.org/post", + requests.Datas{ + "name": "ahuigo", + }, + ) + if err !=nil { + t.Error(err) } - resp, err := requests.Post("https://www.httpbin.org/post", data) - if err == nil { - fmt.Println(resp.Text()) + var data = struct { + Form struct{ + Name string + } + }{} + err = resp.Json(&data) + if data.Form.Name!= "ahuigo"{ + t.Error("invalid response body:", resp.Text()) } } @@ -36,7 +58,7 @@ func TestPostForm(t *testing.T) { func TestPostJson(t *testing.T) { println("Test POST: post json data") json := requests.Json{ - "key": "value", + "name": "Alex", } /* //it still works! @@ -45,25 +67,61 @@ func TestPostJson(t *testing.T) { } */ resp, err := requests.Post("https://www.httpbin.org/post", json) - if err == nil { - fmt.Println(resp.Text()) + if err !=nil { + t.Error(err) + } + + // parse data + var data = struct { + Data string + }{} + resp.Json(&data) + + // is expected results + jsonData,_ := ejson.Marshal(json) // if data.Data!= "{\"name\":\"Alex\"}"{ + if data.Data!= string(jsonData){ + t.Error("invalid response body:", resp.Text()) + } +} +// Post QueryString: application/x-www-form-urlencoded +func TestPostQueryString(t *testing.T) { + println("Test POST: raw post data ") + queryString := "name=Alex&age=29" + resp, err := requests.Post("https://www.httpbin.org/post", queryString) + if err != nil { + t.Fatal(err) + } + var data = struct { + Form struct{ + Name string + Age string + } + }{} + err = resp.Json(&data) + if data.Form.Age != "29"{ + t.Error("invalid response body:", resp.Text()) } } -// Post Raw Text -func TestPostString(t *testing.T) { +// Post Raw text/plain +func TestRawString(t *testing.T) { println("Test POST: raw post data ") rawText := "raw data: Hi, Jack!" resp, err := requests.Post("https://www.httpbin.org/post", rawText, requests.Header{"Content-Type": "text/plain"}, ) - if err == nil { - fmt.Println(resp.Text()) + if err != nil { + t.Fatal(err) + } + var data interface{} + err = resp.Json(&data) + if data.(map[string]interface{})["data"].(string) != rawText { + t.Error("invalid response body:", resp.Text()) } } -// Post Raw Text -func TestPostBytes(t *testing.T) { +// Post Raw text/plain (with bytes) +func TestRawBytes(t *testing.T) { println("Test POST: post bytes data") rawText := "raw data: Hi, Jack!" resp, err := requests.Post("https://www.httpbin.org/post", []byte(rawText), @@ -77,7 +135,7 @@ func TestPostBytes(t *testing.T) { }{} err = resp.Json(&data) if data.Data != rawText { - t.Error(err) + t.Error("invalid response body:", resp.Text()) } } From 9622a45ff74735ffb63ab6c6ae0b82980512b9a2 Mon Sep 17 00:00:00 2001 From: ahuigo Date: Tue, 7 Dec 2021 15:32:49 +0800 Subject: [PATCH 40/46] fix: debug curl missing body --- sessions.go | 2 +- version | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sessions.go b/sessions.go index 32264b7..ea62248 100644 --- a/sessions.go +++ b/sessions.go @@ -259,8 +259,8 @@ func (session *Session) BuildRequest(origurl string, args ...interface{}) (*http // Post - func (session *Session) Run(origurl string, args ...interface{}) (resp *Response, err error) { session.BuildRequest(origurl, args...) - res, err := session.Client.Do(session.httpreq) session.RequestDebug() + res, err := session.Client.Do(session.httpreq) if err != nil { return nil, errors.New(session.httpreq.Method + " " + origurl + " " + err.Error()) diff --git a/version b/version index d6af0a9..c78733c 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.22 \ No newline at end of file +v0.1.23 \ No newline at end of file From 527889aff16000bc3d2976d1d197e03fc83505d5 Mon Sep 17 00:00:00 2001 From: yexuehui Date: Fri, 17 Dec 2021 14:28:19 +0800 Subject: [PATCH 41/46] style: tidy code --- README.md | 6 ++- examples/delete_test.go | 3 +- examples/post_test.go | 89 +++++++++++++++++++++-------------------- examples/resp_test.go | 2 +- 4 files changed, 51 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index c94ee77..014ea99 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,9 @@ go get -u github.com/ahuigo/requests var json map[string]interface{} params := requests.Params{"name": "ahuigo", "page":"1"} resp, err := requests.Get("https://httpbin.org/json", params) - if err == nil { + if err != nil { + panic(err) + }else{ resp.Json(&json) for k, v := range json { fmt.Println(k, v) @@ -100,7 +102,7 @@ go get -u github.com/ahuigo/requests } -### Post Json +### Post Json (default) func TestPostJson(t *testing.T) { println("Test POST: post json data") json := requests.Json{ diff --git a/examples/delete_test.go b/examples/delete_test.go index e36392f..5d4e9b3 100644 --- a/examples/delete_test.go +++ b/examples/delete_test.go @@ -14,10 +14,9 @@ func TestDeleteForm(t *testing.T) { data := requests.Datas{ "comments": "ew", } - session := requests.Sessions() //.SetDebug(true) + session := requests.Sessions() //.SetDebug(true) resp, err := session.Delete("https://www.httpbin.org/delete", data) if err == nil { fmt.Println(resp.Text()) } } - diff --git a/examples/post_test.go b/examples/post_test.go index 0123a21..30376e6 100644 --- a/examples/post_test.go +++ b/examples/post_test.go @@ -1,8 +1,8 @@ package examples import ( + ejson "encoding/json" "testing" - ejson "encoding/json" "github.com/ahuigo/requests" _ "github.com/ahuigo/requests/init" @@ -12,22 +12,22 @@ import ( func TestPostParams(t *testing.T) { println("Test POST: post params") resp, err := requests.Post( - "https://www.httpbin.org/post", - requests.Params{ - "name": "ahuigo", - }, - ) - if err !=nil { - t.Error(err) + "https://www.httpbin.org/post", + requests.Params{ + "name": "ahuigo", + }, + ) + if err != nil { + t.Error(err) } var data = struct { - Args struct{ - Name string - } + Args struct { + Name string + } }{} - err = resp.Json(&data) - if data.Args.Name!= "ahuigo"{ - t.Error("invalid response body:", resp.Text()) + _ = resp.Json(&data) + if data.Args.Name != "ahuigo" { + t.Error("invalid response body:", resp.Text()) } } @@ -35,22 +35,22 @@ func TestPostParams(t *testing.T) { func TestPostForm(t *testing.T) { println("Test POST: post form data(x-wwww-form-urlencoded)") resp, err := requests.Post( - "https://www.httpbin.org/post", - requests.Datas{ - "name": "ahuigo", - }, - ) - if err !=nil { - t.Error(err) + "https://www.httpbin.org/post", + requests.Datas{ + "name": "ahuigo", + }, + ) + if err != nil { + t.Error(err) } var data = struct { - Form struct{ - Name string - } + Form struct { + Name string + } }{} err = resp.Json(&data) - if data.Form.Name!= "ahuigo"{ - t.Error("invalid response body:", resp.Text()) + if data.Form.Name != "ahuigo" { + t.Error("invalid response body:", resp.Text()) } } @@ -67,39 +67,40 @@ func TestPostJson(t *testing.T) { } */ resp, err := requests.Post("https://www.httpbin.org/post", json) - if err !=nil { + if err != nil { t.Error(err) } - // parse data + // parse data var data = struct { Data string - }{} + }{} resp.Json(&data) - // is expected results - jsonData,_ := ejson.Marshal(json) // if data.Data!= "{\"name\":\"Alex\"}"{ - if data.Data!= string(jsonData){ - t.Error("invalid response body:", resp.Text()) + // is expected results + jsonData, _ := ejson.Marshal(json) // if data.Data!= "{\"name\":\"Alex\"}"{ + if data.Data != string(jsonData) { + t.Error("invalid response body:", resp.Text()) } } + // Post QueryString: application/x-www-form-urlencoded func TestPostQueryString(t *testing.T) { println("Test POST: raw post data ") queryString := "name=Alex&age=29" resp, err := requests.Post("https://www.httpbin.org/post", queryString) if err != nil { - t.Fatal(err) + t.Fatal(err) } var data = struct { - Form struct{ - Name string - Age string - } + Form struct { + Name string + Age string + } }{} err = resp.Json(&data) - if data.Form.Age != "29"{ - t.Error("invalid response body:", resp.Text()) + if data.Form.Age != "29" { + t.Error("invalid response body:", resp.Text()) } } @@ -111,12 +112,12 @@ func TestRawString(t *testing.T) { requests.Header{"Content-Type": "text/plain"}, ) if err != nil { - t.Fatal(err) + t.Fatal(err) } - var data interface{} + var data interface{} err = resp.Json(&data) if data.(map[string]interface{})["data"].(string) != rawText { - t.Error("invalid response body:", resp.Text()) + t.Error("invalid response body:", resp.Text()) } } @@ -135,7 +136,7 @@ func TestRawBytes(t *testing.T) { }{} err = resp.Json(&data) if data.Data != rawText { - t.Error("invalid response body:", resp.Text()) + t.Error("invalid response body:", resp.Text()) } } diff --git a/examples/resp_test.go b/examples/resp_test.go index 0cbc545..835a2f9 100644 --- a/examples/resp_test.go +++ b/examples/resp_test.go @@ -10,7 +10,7 @@ import ( // Test response headers func TestResponseHeader(t *testing.T) { resp, _ := requests.Get("https://httpbin.org/get") - println("content-type:", resp.R.Header.Get("content-type")) + println("content-type:", resp.R.Header.Get("content-type")) //println(resp.Text()) } From f6f0d4a9b327ee2043ad26d0af4489ef2356b46c Mon Sep 17 00:00:00 2001 From: yexuehui Date: Wed, 26 Jan 2022 15:38:46 +0800 Subject: [PATCH 42/46] fix: fix version --- helpers.go | 8 +++++++- sessions.go | 2 +- version | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/helpers.go b/helpers.go index 6d8aa5b..593e513 100644 --- a/helpers.go +++ b/helpers.go @@ -10,11 +10,17 @@ import ( var VERSION string = "v0.0.0" -func init() { +func getVersion() string{ _, filename, _, _ := runtime.Caller(0) versionFile := path.Dir(filename) + "/version" version, _ := ioutil.ReadFile(versionFile) VERSION = string(version) + return VERSION + +} + +func init() { + getVersion() } // open file for post upload files diff --git a/sessions.go b/sessions.go index ea62248..adb25cd 100644 --- a/sessions.go +++ b/sessions.go @@ -31,7 +31,7 @@ import ( var respHandler func(*Response) var gHeader = map[string]string{ - "User-Agent": "Go-requests-" + VERSION, + "User-Agent": "Go-requests-" + getVersion(), } // SetRespHandler diff --git a/version b/version index c78733c..0f8d34d 100644 --- a/version +++ b/version @@ -1 +1 @@ -v0.1.23 \ No newline at end of file +v0.1.24 \ No newline at end of file From 715a45c1d7c011e388d7eae1da15a44b9ff62ab6 Mon Sep 17 00:00:00 2001 From: yexuehui Date: Wed, 26 Jan 2022 18:51:23 +0800 Subject: [PATCH 43/46] comment: add global heaer --- examples/req_header_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/req_header_test.go b/examples/req_header_test.go index 925bb77..cce809e 100644 --- a/examples/req_header_test.go +++ b/examples/req_header_test.go @@ -22,7 +22,7 @@ func TestSendSessionHeader(t *testing.T) { session.Get("http://www.zhanluejia.net.cn") } -// Test send header +// Set global header(user-agent) func TestSetGlobalHeader(t *testing.T) { headerK := "User-Agent" headerV := "Custom-Test-Go-User-Agent" From a9a8b5521cbf96452d62e8045e99685285210195 Mon Sep 17 00:00:00 2001 From: yexuehui Date: Wed, 26 Jan 2022 19:05:02 +0800 Subject: [PATCH 44/46] clean: clean makefile --- makefile | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/makefile b/makefile index 55ab266..aec50d2 100644 --- a/makefile +++ b/makefile @@ -2,19 +2,14 @@ msg ?= '' test: go test -v ./examples -test_local: - go test -run TestResponseHeader -v ./examples - .ONESHELL: -t: - if [[ "$(msg)" = "" ]] ; then echo "Usage: make pkg msg='commit msg'";exit 20; fi - gitcheck: if [[ "$(msg)" = "" ]] ; then echo "Usage: make pkg msg='commit msg'";exit 20; fi + .ONESHELL: pkg: gitcheck test - newversion.py version + hash newversion.py && newversion.py version git commit -am "$(msg)" jfrog "rt" "go-publish" "go-pl" $$(cat version) "--url=$$GOPROXY_API" --user=$$GOPROXY_USER --apikey=$$GOPROXY_PASS v=`cat version` && git tag "$$v" && git push origin "$$v" From 362372a4c6521bc5bee27cfa463c763dc0665a20 Mon Sep 17 00:00:00 2001 From: yexuehui Date: Wed, 26 Jan 2022 19:31:20 +0800 Subject: [PATCH 45/46] fix: fix gitcheck --- makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/makefile b/makefile index aec50d2..06106fb 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -msg ?= '' +msg?= test: go test -v ./examples @@ -6,7 +6,6 @@ test: gitcheck: if [[ "$(msg)" = "" ]] ; then echo "Usage: make pkg msg='commit msg'";exit 20; fi - .ONESHELL: pkg: gitcheck test hash newversion.py && newversion.py version From 56d9c202ebcb0cc5bf3f27228b654b2af7c9e5ef Mon Sep 17 00:00:00 2001 From: yexuehui Date: Fri, 28 Jan 2022 21:57:41 +0800 Subject: [PATCH 46/46] =?UTF-8?q?chore=EF=BC=9Achange=20repo's=20path=20fr?= =?UTF-8?q?om=20ahuigo=20to=20asmcos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 ++++++++-------- examples/auth_test.go | 4 ++-- examples/cookie_test.go | 4 ++-- examples/debug_test.go | 6 +++--- examples/delete_test.go | 4 ++-- examples/get_test.go | 8 ++++---- examples/post_file_test.go | 4 ++-- examples/post_test.go | 12 ++++++------ examples/proxy_test.go | 4 ++-- examples/req_build_test.go | 2 +- examples/req_header_test.go | 2 +- examples/resp_handler_test.go | 4 ++-- examples/resp_test.go | 4 ++-- examples/session_test.go | 2 +- examples/timeout_test.go | 4 ++-- go.mod | 2 +- methods.go | 2 +- response.go | 2 +- sessions.go | 2 +- 19 files changed, 44 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 014ea99..1cbac30 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Requests -[![license](http://dmlc.github.io/img/apache2.svg)](https://raw.githubusercontent.com/ahuigo/requests/master/LICENSE) +[![license](http://dmlc.github.io/img/apache2.svg)](https://raw.githubusercontent.com/asmcos/requests/master/LICENSE) # Requests Requests is an HTTP library, it is easy to use. Similar to Python requests. @@ -23,22 +23,22 @@ Warning: Session is not safe in multiple goroutines. You can not do as following # Installation ``` -go get -u github.com/ahuigo/requests +go get -u github.com/asmcos/requests ``` # Examples -> For more examples, refer to https://github.com/ahuigo/requests/tree/master/examples +> For more examples, refer to https://github.com/asmcos/requests/tree/master/examples ## Get package main import ( - "github.com/ahuigo/requests" + "github.com/asmcos/requests" "fmt" ) func main(){ var json map[string]interface{} - params := requests.Params{"name": "ahuigo", "page":"1"} + params := requests.Params{"name": "asmcos", "page":"1"} resp, err := requests.Get("https://httpbin.org/json", params) if err != nil { panic(err) @@ -59,7 +59,7 @@ go get -u github.com/ahuigo/requests func TestPostParams(t *testing.T) { println("Test POST: post params") data := requests.Params{ - "name": "ahuigo", + "name": "asmcos", } resp, err := requests.Post("https://www.httpbin.org/post", data) if err == nil { @@ -246,13 +246,13 @@ two or more headers ... ## Response ### Fetch Response Body -https://github.com/ahuigo/requests/blob/master/examples/resp_test.go +https://github.com/asmcos/requests/blob/master/examples/resp_test.go fmt.Println(resp.Text()) fmt.Println(resp.Content()) ### Fetch Response Cookies -https://github.com/ahuigo/requests/blob/master/examples/cookie_test.go +https://github.com/asmcos/requests/blob/master/examples/cookie_test.go resp,_ = session.Get("https://www.httpbin.org") coo := resp.Cookies() diff --git a/examples/auth_test.go b/examples/auth_test.go index 3a43595..360e18e 100644 --- a/examples/auth_test.go +++ b/examples/auth_test.go @@ -4,8 +4,8 @@ import ( "fmt" "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) func TestAuth(t *testing.T) { diff --git a/examples/cookie_test.go b/examples/cookie_test.go index 84fe210..0a6954d 100644 --- a/examples/cookie_test.go +++ b/examples/cookie_test.go @@ -5,8 +5,8 @@ import ( "net/http" "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) func TestSendCookie(t *testing.T) { diff --git a/examples/debug_test.go b/examples/debug_test.go index 38e01f7..6c6a507 100644 --- a/examples/debug_test.go +++ b/examples/debug_test.go @@ -5,8 +5,8 @@ import ( "net/http" "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) func TestGetDebug(t *testing.T) { @@ -14,7 +14,7 @@ func TestGetDebug(t *testing.T) { session := requests.Sessions().SetDebug(true) resp, err := session.Post("https://httpbin.org/post", requests.Json{ - "name": "ahuigo", + "name": "asmcos", }, &http.Cookie{ Name: "count", diff --git a/examples/delete_test.go b/examples/delete_test.go index 5d4e9b3..17247c4 100644 --- a/examples/delete_test.go +++ b/examples/delete_test.go @@ -4,8 +4,8 @@ import ( "fmt" "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) // Delete Form Request diff --git a/examples/get_test.go b/examples/get_test.go index 195fc50..c6c3b77 100644 --- a/examples/get_test.go +++ b/examples/get_test.go @@ -4,8 +4,8 @@ import ( "fmt" "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) // Get Json Response @@ -30,7 +30,7 @@ type HbResponse struct { // Get with params func TestGetParams(t *testing.T) { - params := requests.Params{"name": "ahuigo", "page": "1"} + params := requests.Params{"name": "asmcos", "page": "1"} resp, err := requests.Get("https://httpbin.org/get", params) if err == nil { @@ -38,7 +38,7 @@ func TestGetParams(t *testing.T) { if err := resp.Json(&json); err != nil { t.Fatal(err) } - if json.Args["name"] != "ahuigo" { + if json.Args["name"] != "asmcos" { t.Fatal("Invalid response: " + resp.Text()) } } diff --git a/examples/post_file_test.go b/examples/post_file_test.go index 42bde6c..c6781e8 100644 --- a/examples/post_file_test.go +++ b/examples/post_file_test.go @@ -4,8 +4,8 @@ import ( "os" "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) func TestPostFile(t *testing.T) { diff --git a/examples/post_test.go b/examples/post_test.go index 30376e6..0c2cc67 100644 --- a/examples/post_test.go +++ b/examples/post_test.go @@ -4,8 +4,8 @@ import ( ejson "encoding/json" "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) // Post params @@ -14,7 +14,7 @@ func TestPostParams(t *testing.T) { resp, err := requests.Post( "https://www.httpbin.org/post", requests.Params{ - "name": "ahuigo", + "name": "asmcos", }, ) if err != nil { @@ -26,7 +26,7 @@ func TestPostParams(t *testing.T) { } }{} _ = resp.Json(&data) - if data.Args.Name != "ahuigo" { + if data.Args.Name != "asmcos" { t.Error("invalid response body:", resp.Text()) } } @@ -37,7 +37,7 @@ func TestPostForm(t *testing.T) { resp, err := requests.Post( "https://www.httpbin.org/post", requests.Datas{ - "name": "ahuigo", + "name": "asmcos", }, ) if err != nil { @@ -49,7 +49,7 @@ func TestPostForm(t *testing.T) { } }{} err = resp.Json(&data) - if data.Form.Name != "ahuigo" { + if data.Form.Name != "asmcos" { t.Error("invalid response body:", resp.Text()) } } diff --git a/examples/proxy_test.go b/examples/proxy_test.go index 0713c74..9a43124 100644 --- a/examples/proxy_test.go +++ b/examples/proxy_test.go @@ -3,8 +3,8 @@ package examples import ( "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) func TestProxy(t *testing.T) { diff --git a/examples/req_build_test.go b/examples/req_build_test.go index d3d3087..14c4de2 100644 --- a/examples/req_build_test.go +++ b/examples/req_build_test.go @@ -5,7 +5,7 @@ import ( "regexp" "testing" - "github.com/ahuigo/requests" + "github.com/asmcos/requests" ) // TestBuildRequest diff --git a/examples/req_header_test.go b/examples/req_header_test.go index cce809e..f2b949c 100644 --- a/examples/req_header_test.go +++ b/examples/req_header_test.go @@ -3,7 +3,7 @@ package examples import ( "testing" - "github.com/ahuigo/requests" + "github.com/asmcos/requests" ) // Send headers diff --git a/examples/resp_handler_test.go b/examples/resp_handler_test.go index 334a3d4..036d5d7 100644 --- a/examples/resp_handler_test.go +++ b/examples/resp_handler_test.go @@ -3,8 +3,8 @@ package examples import ( "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) // Test response headers diff --git a/examples/resp_test.go b/examples/resp_test.go index 835a2f9..a0bc9d6 100644 --- a/examples/resp_test.go +++ b/examples/resp_test.go @@ -3,8 +3,8 @@ package examples import ( "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" ) // Test response headers diff --git a/examples/session_test.go b/examples/session_test.go index a5e0f62..f5b7f13 100644 --- a/examples/session_test.go +++ b/examples/session_test.go @@ -3,7 +3,7 @@ package examples import ( "testing" - r "github.com/ahuigo/requests" + r "github.com/asmcos/requests" ) // Test Session with cookies diff --git a/examples/timeout_test.go b/examples/timeout_test.go index 4a0827c..efb3592 100644 --- a/examples/timeout_test.go +++ b/examples/timeout_test.go @@ -4,8 +4,8 @@ import ( "fmt" "testing" - "github.com/ahuigo/requests" - _ "github.com/ahuigo/requests/init" + "github.com/asmcos/requests" + _ "github.com/asmcos/requests/init" "github.com/davecgh/go-spew/spew" ) diff --git a/go.mod b/go.mod index d0c22c0..c6856fe 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/ahuigo/requests +module github.com/asmcos/requests go 1.13 diff --git a/methods.go b/methods.go index 4a64019..1a83926 100644 --- a/methods.go +++ b/methods.go @@ -1,4 +1,4 @@ -/* Copyright(2) 2018 by asmcos and ahuigo . +/* Copyright(2) 2018 by asmcos and asmcos . Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at diff --git a/response.go b/response.go index 793ad83..fd72769 100644 --- a/response.go +++ b/response.go @@ -1,4 +1,4 @@ -/* Copyright(2) 2018 by asmcos and ahuigo . +/* Copyright(2) 2018 by asmcos and asmcos . Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at diff --git a/sessions.go b/sessions.go index adb25cd..3167607 100644 --- a/sessions.go +++ b/sessions.go @@ -1,4 +1,4 @@ -/* Copyright(2) 2018 by asmcos and ahuigo . +/* Copyright(2) 2018 by asmcos and asmcos . Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at