-
Notifications
You must be signed in to change notification settings - Fork 53
/
form.go
150 lines (128 loc) · 3.2 KB
/
form.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
package api
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
"strconv"
)
type httpClient interface {
PostForm(string, url.Values) (*http.Response, error)
}
// FormResponse is the parsed "www-form-urlencoded" response from the server.
type FormResponse struct {
StatusCode int
requestURI string
values map[string]string
}
// Get the response value named k.
func (f FormResponse) Get(k string) string {
v, ok := f.values[k]
if !ok {
return ""
}
return v
}
// Err returns an Error object extracted from the response.
func (f FormResponse) Err() error {
return &Error{
RequestURI: f.requestURI,
ResponseCode: f.StatusCode,
Code: f.Get("error"),
message: f.Get("error_description"),
}
}
// Error is the result of an unexpected HTTP response from the server.
type Error struct {
Code string
ResponseCode int
RequestURI string
message string
}
func (e Error) Error() string {
if e.message != "" {
return fmt.Sprintf("%s (%s)", e.message, e.Code)
}
if e.Code != "" {
return e.Code
}
return fmt.Sprintf("HTTP %d", e.ResponseCode)
}
// PostForm makes an POST request by serializing input parameters as a form and parsing the response
// of the same type.
func PostForm(c httpClient, u string, params url.Values) (*FormResponse, error) {
resp, err := c.PostForm(u, params)
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
r := &FormResponse{
StatusCode: resp.StatusCode,
requestURI: u,
values: make(map[string]string),
}
// since mine.ParseMediaType always returns the media type regardless of error, and
// that we don't use the params - we can just ignore any errors.
mediaType, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type"))
if mediaType == formType {
var bb []byte
bb, err = ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}
values, err := url.ParseQuery(string(bb))
if err != nil {
return r, err
}
readFormValues(r, values)
} else if mediaType == jsonType {
var bb []byte
bb, err = ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}
var values map[string]interface{}
err = json.Unmarshal(bb, &values)
if err != nil {
return r, err
}
readJsonValues(r, values)
} else {
_, err = io.Copy(ioutil.Discard, resp.Body)
if err != nil {
return r, err
}
}
return r, nil
}
const formType = "application/x-www-form-urlencoded"
const jsonType = "application/json"
// readFormValues feeds information from a url.Values into a FormResponse's values
// map.
func readFormValues(r *FormResponse, values url.Values) {
for key, value := range values {
if len(value) < 1 {
continue
}
r.values[key] = value[0]
}
}
// readJsonValues feeds information from an un-marshaled JSON response into a
// FormResponse's values map.
func readJsonValues(r *FormResponse, values map[string]interface{}) {
for key, value := range values {
// Go's JSON library uses float64 and int64 for numbers.
if v, ok := value.(string); ok {
r.values[key] = v
} else if v, ok := value.(int64); ok {
r.values[key] = strconv.FormatInt(v, 10)
} else if v, ok := value.(float64); ok {
r.values[key] = strconv.FormatFloat(v, 'f', -1, 64)
}
}
}