-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
157 lines (138 loc) · 3.85 KB
/
http.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
package pncp
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
//"crypto/tls"
)
// Generic call
func (r *Client) call(method, path, qs string, inBody interface{}) (Future, error) {
var (
client *http.Client
req *http.Request
resp *http.Response
authContext AuthContext
reqBody []byte
url string
err error
)
// Preconditions and sanitize input
// Construct the HTTP Request
if inBody != nil {
reqBody, err = json.Marshal(inBody)
if err != nil {
return nil, err
}
} else {
reqBody = nil
}
// Construct URL and AuthContext
url = fmt.Sprintf("%s%s%s", r.Endpoint, path, qs)
authContext = NewAuthContext(method, path, qs, r.ApplicationKey, r.SharedSecret)
if r.Debug {
log.Printf("CALL\n\tURL: %s\n\tMethod: %s\n\tRequestBody? %t\n", url, method, inBody != nil)
log.Printf("AUTH\n")
log.Printf("\tMethod: %s\n", authContext.Method)
log.Printf("\tResourcePath: %s\n", authContext.ResourcePath)
log.Printf("\tQueryString: %s\n", authContext.QueryString)
log.Printf("\tApplicationKey: %s\n", authContext.ApplicationKey)
log.Printf("\tStringToSign: %s\n", authContext.StringToSign)
log.Printf("\tRequestSignature: %s\n", authContext.RequestSignature)
log.Printf("\tEncodedCredentials: %s\n", authContext.EncodedCredentials)
log.Printf("\tAuthenticator: %s\n", authContext.Authenticator)
if inBody != nil {
log.Printf("BODY:\n\t%s", reqBody)
}
}
switch method {
case `GET`:
fallthrough
case `PUT`:
fallthrough
case `POST`:
fallthrough
case `DELETE`:
req, err = constructRequest(method, url, reqBody)
default:
err = errors.New(`Unknown method`)
}
if err != nil {
return nil, err
}
req.Header.Set("Accept", documentType)
req.Header.Set("Content-Type", documentType)
req.Header.Set("Authorization", authContext.Authenticator)
// temp ignore bad TLS
tr := &http.Transport{
//TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: tr}
resp, err = client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if r.Debug {
log.Printf("Response status: %s code: %d", resp.Status, resp.StatusCode)
}
if resp.StatusCode == 200 {
rawOut, e := ioutil.ReadAll(resp.Body)
if r.Debug {
log.Printf("Response body: %s", rawOut)
}
if e != nil {
return nil, &APIError{error: e, Retriable: true}
}
return &SyncResponse{body: rawOut}, nil
} else if resp.StatusCode == 202 {
rawOut, e := ioutil.ReadAll(resp.Body)
if e != nil {
return nil, &APIError{error: e, Retriable: true}
}
res := &Resource{}
e = json.Unmarshal(rawOut, res)
if e != nil {
return nil, &APIError{error: e, Retriable: true}
}
return &AsyncResponse{api: r, ResourceURL: res.URL}, nil
}
if resp.StatusCode == 500 {
eref, _ := strconv.ParseUint(resp.Header.Get("X-Application-Error-Reference"), 10, 64)
return nil, &APIError{
error: errors.New(resp.Header.Get("X-Application-Error-Description")),
Retriable: true,
Eref: eref,
}
} else if resp.StatusCode == 400 {
eref, _ := strconv.ParseUint(resp.Header.Get("X-Application-Error-Reference"), 10, 64)
return nil, &APIError{
error: errors.New(resp.Header.Get("X-Application-Error-Description")),
Retriable: false,
Eref: eref,
}
} else if resp.StatusCode == 401 {
eref, _ := strconv.ParseUint(resp.Header.Get("X-Application-Error-Reference"), 10, 64)
return nil, &APIError{
error: errors.New(resp.Header.Get("X-Application-Error-Description")),
Retriable: false,
Eref: eref,
}
} else {
return nil, &APIError{
error: errors.New(resp.Status),
Retriable: false,
}
}
}
func constructRequest(method, url string, body []byte) (*http.Request, error) {
if body == nil {
return http.NewRequest(method, url, nil)
} else {
return http.NewRequest(method, url, bytes.NewBuffer(body))
}
}