Skip to content

Commit

Permalink
Merge pull request #22 from mozillazg/develop
Browse files Browse the repository at this point in the history
v0.6.0
  • Loading branch information
mozillazg committed Sep 4, 2016
2 parents 82f729b + 35839cd commit fb2aa53
Show file tree
Hide file tree
Showing 46 changed files with 207 additions and 11,771 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ _testmain.go
test.txt

*~
vendor/
glide.lock
19 changes: 16 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go:
- 1.4
- 1.5
- 1.6
- 1.7
- tip

sudo: false
Expand All @@ -15,15 +16,27 @@ before_install:
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- go get github.com/tools/godep
- curl https://glide.sh/get | sh

install:
- godep restore
- |
version=${TRAVIS_GO_VERSION:0:3} && \
if [[ "$version" == "1.1" \
|| "$version" == "1.2" \
|| "$version" == "1.3" \
|| "$version" == "1.4" \
|| "$version" == "1.5" ]]; then
godep restore
else
glide install
fi
- go build

script:
- go test -v
- if [[ $TRAVIS_GO_VERSION != 'tip' ]]; then $HOME/gopath/bin/goveralls -service=travis-ci; fi
- make test
- if [[ $TRAVIS_GO_VERSION != 'tip' ]]; then $HOME/gopath/bin/goveralls -service=travis-ci -ignore=vendor/; fi

matrix:
allow_failures:
- go: 1.1
- go: tip
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
# Changelog


## 0.6.0 (2016-09-04)

* Remove vendor/, use glide instead

### API Changes

* Add `var DefaultClient = new(http.Client)`
* Support `NewArgs(nil)`
* Support `NewRequest(nil)`
* Support `GET(url, nil), POST(url, nil), ...`


## 0.5.1 (2016-06-05)

* add vendor/
* Add vendor/


## 0.5.0 (2015-11-15)
Expand Down
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
help:
@echo "test run test"
@echo "lint run lint"

.PHONY: test
test:
go test -v -cover

.PHONY: lint
lint:
gofmt -s -w . _example
golint .
golint _example
go vet
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ request
[![Coverage Status](https://coveralls.io/repos/mozillazg/request/badge.png?branch=master)](https://coveralls.io/r/mozillazg/request?branch=master)
[![GoDoc](https://godoc.org/github.com/mozillazg/request?status.svg)](https://godoc.org/github.com/mozillazg/request)

Go HTTP Requests for Humans™. Inspired by [Python-Requests](https://github.com/kennethreitz/requests).
A developer-friendly HTTP request library for Gopher. Inspired by [Python-Requests](https://github.com/kennethreitz/requests).


Installation
Expand Down Expand Up @@ -44,6 +44,7 @@ defer resp.Body.Close() // Don't forget close the response body
**POST**:

```go
req = request.NewRequest(c)
req.Data = map[string]string{
"key": "value",
"a": "123",
Expand All @@ -54,6 +55,7 @@ resp, err := req.Post("http://httpbin.org/post")
**Cookies**:

```go
req = request.NewRequest(c)
req.Cookies = map[string]string{
"key": "value",
"a": "123",
Expand All @@ -64,6 +66,7 @@ resp, err := req.Get("http://httpbin.org/cookies")
**Headers**:

```go
req = request.NewRequest(c)
req.Headers = map[string]string{
"Accept-Encoding": "gzip,deflate,sdch",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
Expand All @@ -74,6 +77,7 @@ resp, err := req.Get("http://httpbin.org/get")
**Files**:

```go
req = request.NewRequest(c)
f, err := os.Open("test.txt")
req.Files = []request.FileField{
request.FileField{"file", "test.txt", f},
Expand All @@ -84,6 +88,7 @@ resp, err := req.Post("http://httpbin.org/post")
**Json**:

```go
req = request.NewRequest(c)
req.Json = map[string]string{
"a": "A",
"b": "B",
Expand All @@ -95,6 +100,7 @@ resp, err = req.Post("http://httpbin.org/post")

**Proxy**:
```go
req = request.NewRequest(c)
req.Proxy = "http://127.0.0.1:8080"
// req.Proxy = "https://127.0.0.1:8080"
// req.Proxy = "socks5://127.0.0.1:57341"
Expand All @@ -104,6 +110,13 @@ or https://github.com/mozillazg/request/tree/develop/_example/proxy

**HTTP Basic Authentication**:
```go
req = request.NewRequest(c)
req.BasicAuth = request.BasicAuth{"user", "passwd"}
resp, err := req.Get("http://httpbin.org/basic-auth/user/passwd")
```


License
---------

Under the MIT License.
11 changes: 10 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Go HTTP Requests for Humans™.
// Package request is a developer-friendly HTTP request library for Gopher.
//
// GET Request:
//
Expand All @@ -10,6 +10,7 @@
//
// POST Request:
//
// req = request.NewRequest(c)
// req.Data = map[string]string{
// "key": "value",
// "a": "123",
Expand All @@ -18,6 +19,7 @@
//
// Custom Cookies:
//
// req = request.NewRequest(c)
// req.Cookies = map[string]string{
// "key": "value",
// "a": "123",
Expand All @@ -27,6 +29,7 @@
//
// Custom Headers:
//
// req = request.NewRequest(c)
// req.Headers = map[string]string{
// "Accept-Encoding": "gzip,deflate,sdch",
// "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
Expand All @@ -35,6 +38,7 @@
//
// Upload Files:
//
// req = request.NewRequest(c)
// f, err := os.Open("test.txt")
// req.Files = []request.FileField{
// request.FileField{"file", "test.txt", f},
Expand All @@ -43,6 +47,7 @@
//
// Json Body:
//
// req = request.NewRequest(c)
// req.Json = map[string]string{
// "a": "A",
// "b": "B",
Expand All @@ -53,11 +58,13 @@
//
// others body:
//
// req = request.NewRequest(c)
// // not set Content-Type
// req.Body = strings.NewReader("<xml><a>abc</a></xml")
// resp, err := req.Post("http://httpbin.org/post")
//
// // form
// req = request.NewRequest(c)
// req.Body = strings.NewReader("a=1&b=2")
// req.Headers = map[string]string{
// "Content-Type": request.DefaultContentType,
Expand All @@ -66,13 +73,15 @@
//
// Proxy:
//
// req = request.NewRequest(c)
// req.Proxy = "http://127.0.0.1:8080"
// // req.Proxy = "https://127.0.0.1:8080"
// // req.Proxy = "socks5://127.0.0.1:57341"
// resp, err := req.Get("http://httpbin.org/get")
//
// HTTP Basic Authentication:
//
// req = request.NewRequest(c)
// req.BasicAuth = request.BasicAuth{"user", "passwd"}
// resp, err := req.Get("http://httpbin.org/basic-auth/user/passwd")
//
Expand Down
42 changes: 34 additions & 8 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/mozillazg/request"
)

func ExampleGet() {
func ExampleRequest_Get() {
c := new(http.Client)
req := request.NewRequest(c)
url := "http://httpbin.org/get"
Expand All @@ -23,7 +23,21 @@ func ExampleGet() {
//http://httpbin.org/get
}

func ExampleGet_params() {
func ExampleGet() {
c := new(http.Client)
args := request.NewArgs(c)
url := "http://httpbin.org/get"
resp, _ := request.Get(url, args)
d, _ := resp.Json()
defer resp.Body.Close()
fmt.Println(resp.Ok())
fmt.Println(d.Get("url").MustString())
// Output:
//true
//http://httpbin.org/get
}

func ExampleRequest_Get_params() {
c := new(http.Client)
req := request.NewRequest(c)
req.Params = map[string]string{
Expand All @@ -39,7 +53,7 @@ func ExampleGet_params() {
//http://httpbin.org/get?a=1&b=2
}

func ExampleGet_customHeaders() {
func ExampleRequest_Get_customHeaders() {
c := new(http.Client)
req := request.NewRequest(c)
req.Headers = map[string]string{
Expand All @@ -57,7 +71,7 @@ func ExampleGet_customHeaders() {
//abc
}

func ExamplePost() {
func ExampleRequest_Post() {
c := new(http.Client)
req := request.NewRequest(c)
req.Data = map[string]string{
Expand All @@ -69,7 +83,19 @@ func ExamplePost() {
defer resp.Body.Close()
}

func ExampleGet_cookies() {
func ExamplePost() {
c := new(http.Client)
args := request.NewArgs(c)
args.Data = map[string]string{
"a": "1",
"b": "2",
}
url := "http://httpbin.org/post"
resp, _ := request.Post(url, args)
defer resp.Body.Close()
}

func ExampleRequest_Get_cookies() {
c := new(http.Client)
req := request.NewRequest(c)
req.Cookies = map[string]string{
Expand All @@ -81,20 +107,20 @@ func ExampleGet_cookies() {
defer resp.Body.Close()
}

func ExamplePost_files() {
func ExampleRequest_Post_files() {
c := new(http.Client)
req := request.NewRequest(c)
f, _ := os.Open("test.txt")
defer f.Close()
req.Files = []request.FileField{
request.FileField{"abc", "abc.txt", f},
{"abc", "abc.txt", f},
}
url := "http://httpbin.org/post"
resp, _ := req.Post(url)
defer resp.Body.Close()
}

func ExamplePostRawBody() {
func ExampleRequest_Post_rawBody() {
c := new(http.Client)
req := request.NewRequest(c)
req.Body = strings.NewReader("a=1&b=2&foo=bar")
Expand Down
2 changes: 1 addition & 1 deletion form.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newMultipartBody(a *Args, vs url.Values) (body io.Reader, contentType strin
return
}

func newJsonBody(a *Args) (body io.Reader, contentType string, err error) {
func newJSONBody(a *Args) (body io.Reader, contentType string, err error) {
b, err := json.Marshal(a.Json)
if err != nil {
return nil, "", err
Expand Down
16 changes: 8 additions & 8 deletions form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func TestPostFiles(t *testing.T) {
"a": "123",
}
req.Files = []FileField{
FileField{"abc", "abc.txt", b},
FileField{"test", "test.txt", f2},
{"abc", "abc.txt", b},
{"test", "test.txt", f2},
}
url := "http://httpbin.org/post"
resp, _ := req.Post(url)
Expand Down Expand Up @@ -161,8 +161,8 @@ func TestPostFormStructB(t *testing.T) {
c := new(http.Client)
req := NewRequest(c)
s := map[string][]string{
"a": []string{"1", "2"},
"b": []string{"2", "3"},
"a": {"1", "2"},
"b": {"2", "3"},
}
url := "http://httpbin.org/post"
resp, _ := req.PostForm(url, s)
Expand All @@ -182,8 +182,8 @@ func TestPostFormStructB(t *testing.T) {
}
assert.Equal(t, form,
map[string][]string{
"a": []string{"1", "2"},
"b": []string{"2", "3"},
"a": {"1", "2"},
"b": {"2", "3"},
}, true)
}

Expand All @@ -201,7 +201,7 @@ func TestPostFormFileA(t *testing.T) {
"a": "123",
}
req.Files = []FileField{
FileField{"abc", "abc.txt", b},
{"abc", "abc.txt", b},
}
url := "http://httpbin.org/post"
resp, _ := req.PostForm(url, nil)
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestPostFormFileB(t *testing.T) {
"a": "123",
}
req.Files = []FileField{
FileField{"abc", "abc.txt", b},
{"abc", "abc.txt", b},
}
url := "http://httpbin.org/post"
resp, _ := req.PostForm(url, data)
Expand Down
Loading

0 comments on commit fb2aa53

Please sign in to comment.