-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
153 lines (138 loc) · 3.73 KB
/
http_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
package gdk
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
const (
FORM_MULTIPART = "multipart/form-data"
FORM_ENCODED = "application/x-www-form-urlencoded"
APPLICATION_JSON = "application/json"
CONTENT_TYPE = "Content-Type"
)
// FileUploadInfo upload file info struct
type FileUploadInfo struct {
Name string // form name
Filepath string
FileName string
}
type HttpOptions struct {
C *http.Client
Url string
Params map[string]string
Headers map[string]string
Files []FileUploadInfo // special for HttpPostFile
contentType string
}
const (
ERR_PARAMS_INVALID = 11111
ERR_OPEN_FILE_FAILED = 11112
ERR_WRITER_FAILED = 11113
ERR_WRITE_FIELD_FAILED = 11114
ERR_WRITER_CLOSE_FAILED = 11115
ERR_UNKOWN_TYPE = 11116
)
// HttpGet request to target url
func HttpGet(ho *HttpOptions) (*http.Response, error) {
if ho == nil {
return nil, Errorf("Params invalid").WithCode(ERR_PARAMS_INVALID)
}
if ho.C == nil {
ho.C = http.DefaultClient
}
urlParams := url.Values{}
Url, _ := url.Parse(ho.Url)
for k, v := range ho.Params {
urlParams.Set(k, v)
}
Url.RawQuery = urlParams.Encode()
urlPath := Url.String()
httpReq, _ := http.NewRequest(http.MethodGet, urlPath, nil)
for k, v := range ho.Headers {
httpReq.Header.Add(k, v)
}
return ho.C.Do(httpReq)
}
// HttpPostJSON post json format value to http server
func HttpPostJSON(ho *HttpOptions) (*http.Response, error) {
ho.contentType = APPLICATION_JSON
return doPost(ho)
}
// HttpPostForm post form field value
func HttpPostForm(ho *HttpOptions) (*http.Response, error) {
ho.contentType = FORM_ENCODED
return doPost(ho)
}
// HttpPostFiles post files
func HttpPostFiles(ho *HttpOptions) (*http.Response, error) {
ho.contentType = FORM_MULTIPART
return doPost(ho)
}
func doPost(ho *HttpOptions) (*http.Response, error) {
if ho.C == nil {
ho.C = http.DefaultClient
}
body, realContentType, err := buildRequest(ho)
if err != nil {
return nil, ErrorCause(err)
}
req, _ := http.NewRequest(http.MethodPost, ho.Url, body)
req.Header.Add(CONTENT_TYPE, realContentType)
for k, v := range ho.Headers {
req.Header.Add(k, v)
}
return ho.C.Do(req)
}
func buildRequest(ho *HttpOptions) (io.Reader, string, error) {
switch ho.contentType {
case APPLICATION_JSON:
bytesData, _ := json.Marshal(ho.Params)
return bytes.NewReader(bytesData), ho.contentType, nil
case FORM_ENCODED:
urlValues := url.Values{}
for k, v := range ho.Params {
urlValues.Set(k, v)
}
body := urlValues.Encode()
return strings.NewReader(body), ho.contentType, nil
case FORM_MULTIPART:
if ho.Files == nil || len(ho.Files) == 0 {
return nil, ho.contentType, Errorf("Files invalid").WithCode(ERR_PARAMS_INVALID)
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for _, uploadFile := range ho.Files {
file, err := os.Open(uploadFile.Filepath)
if err != nil {
return nil, "", ErrorCause(err).WithCode(ERR_OPEN_FILE_FAILED)
}
fileName := filepath.Base(uploadFile.Filepath)
if uploadFile.FileName != "" {
fileName = uploadFile.FileName
}
part, err := writer.CreateFormFile(uploadFile.Name, fileName)
if err != nil {
return nil, "", ErrorCause(err).WithCode(ERR_WRITER_FAILED)
}
io.Copy(part, file)
file.Close()
}
for k, v := range ho.Params {
if err := writer.WriteField(k, v); err != nil {
return nil, "", ErrorCause(err).WithCode(ERR_WRITE_FIELD_FAILED)
}
}
if err := writer.Close(); err != nil {
return nil, "", ErrorCause(err).WithCode(ERR_WRITER_CLOSE_FAILED)
}
return body, writer.FormDataContentType(), nil
default:
return nil, "unkownType", Errorf("unkownType").WithCode(ERR_UNKOWN_TYPE)
}
}