/
simple_client.go
188 lines (162 loc) · 5.02 KB
/
simple_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2019 潘文斌. All rights reserved.
// license that can be found in the LICENSE file.
package ghttpclient
import (
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"github.com/panwenbin/ghttpclient/header"
"golang.org/x/net/html/charset"
"golang.org/x/text/transform"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
)
var Debug bool
// GzipBody encodes the body with gzip
func GzipBody(body io.Reader) *bytes.Buffer{
buf := &bytes.Buffer{}
gz, _ := gzip.NewWriterLevel(buf, gzip.DefaultCompression)
content, _ := ioutil.ReadAll(body)
if _, err := gz.Write(content); err != nil {
gz.Close()
log.Println(err)
return nil
}
gz.Close()
return buf
}
// Send a Request with GET method
func Get(url string, httpHeader header.GHttpHeader) *GHttpClient {
httpHeader = httpHeader.RemoveContentEncoding()
return NewClient().Debug(Debug).Url(url).Headers(httpHeader).Get()
}
// Send a Request with POST method
func Post(url string, body io.Reader, httpHeader header.GHttpHeader) *GHttpClient {
if httpHeader.IsContentEncodingZip() {
body = GzipBody(body)
}
return NewClient().Debug(Debug).Url(url).Headers(httpHeader).Body(body).Post()
}
// Send a Request as a json with POST Method
func PostJson(url string, jsonBytes []byte, httpHeader header.GHttpHeader) *GHttpClient {
var body io.Reader
body = bytes.NewReader(jsonBytes)
if httpHeader.IsContentEncodingZip() {
body = GzipBody(body)
}
return NewClient().Debug(Debug).Url(url).Headers(httpHeader).Body(body).
ContentType(header.CONTENT_TYPE_JSON).Post()
}
// Send a Request as a form with POST method
func PostForm(url string, data url.Values, httpHeader header.GHttpHeader) *GHttpClient {
var body io.Reader
body = strings.NewReader(data.Encode())
if httpHeader.IsContentEncodingZip() {
body = GzipBody(body)
}
return NewClient().Debug(Debug).Url(url).Headers(httpHeader).Body(body).
ContentType(header.CONTENT_TYPE_FORM_URLENCODED).Post()
}
// Send a Request with PUT method
func Put(url string, body io.Reader, httpHeader header.GHttpHeader) *GHttpClient {
if httpHeader.IsContentEncodingZip() {
body = GzipBody(body)
}
return NewClient().Debug(Debug).Url(url).Headers(httpHeader).Body(body).Put()
}
// Send a Request as a json with PUT method
func PutJson(url string, jsonBytes []byte, httpHeader header.GHttpHeader) *GHttpClient {
var body io.Reader
body = bytes.NewReader(jsonBytes)
if httpHeader.IsContentEncodingZip() {
body = GzipBody(body)
}
return NewClient().Debug(Debug).Url(url).Headers(httpHeader).Body(body).
ContentType(header.CONTENT_TYPE_JSON).Put()
}
// Send a Request with PATCH method
func Patch(url string, body io.Reader, httpHeader header.GHttpHeader) *GHttpClient {
if httpHeader.IsContentEncodingZip() {
body = GzipBody(body)
}
return NewClient().Debug(Debug).Url(url).Headers(httpHeader).Body(body).Patch()
}
// Send a Request with DELETE method
func Delete(url string, httpHeader header.GHttpHeader) *GHttpClient {
httpHeader = httpHeader.RemoveContentEncoding()
return NewClient().Debug(Debug).Url(url).Headers(httpHeader).Delete()
}
// Send a Request with OPTIONS method
func Options(url string, httpHeader header.GHttpHeader) *GHttpClient {
httpHeader = httpHeader.RemoveContentEncoding()
return NewClient().Debug(Debug).Url(url).Headers(httpHeader).Options()
}
// ReadBodyClose fetches the response Body, then close the Body
// supports gzip content-type
func ReadBodyClose(response *http.Response) ([]byte, error) {
if response == nil {
return nil, errors.New("response is nil")
}
defer response.Body.Close()
var body []byte
var err error
if response.Header.Get("Content-Encoding") == "gzip" {
gzReader, err := gzip.NewReader(response.Body)
if err != nil {
return nil, err
}
body, err = ioutil.ReadAll(gzReader)
} else {
body, err = ioutil.ReadAll(response.Body)
}
if err != nil {
return nil, err
}
return body, nil
}
// TryUTF8ReadBodyClose tries to transfer the body bytes to utf-8 bytes when the body bytes is not in utf-8 encoding
func TryUTF8ReadBodyClose(response *http.Response) ([]byte, error) {
body, err := ReadBodyClose(response)
if err != nil {
return nil, err
}
e, name, _ := charset.DetermineEncoding(body, response.Header.Get("Content-Type"))
if name == "utf-8" {
return body, nil
}
utf8Body, _, err := transform.Bytes(e.NewDecoder(), body)
if err != nil {
return body, nil
}
return utf8Body, nil
}
// ReadJsonClose fetches the response Body and try to decode as a json, then close the Body
func ReadJsonClose(response *http.Response, v interface{}) error {
contentType := response.Header.Get("Content-Type")
if strings.Index(contentType, header.CONTENT_TYPE_JSON) == -1 {
return errors.New(fmt.Sprintf("content type application/json expected, but %s got", contentType))
}
body, err := ReadBodyClose(response)
if err != nil {
return err
}
err = json.Unmarshal(body, v)
if err != nil {
return err
}
return nil
}
// init inits Debug on/off
func init() {
debug := os.Getenv("DEBUG")
if debug != "" && debug != "false" && debug != "0" {
Debug = true
}
}