-
Notifications
You must be signed in to change notification settings - Fork 15
/
sendsafely.go
76 lines (64 loc) · 1.88 KB
/
sendsafely.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
package sendsafelyuploader
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
type Uploader struct {
URL string
APIKey string
RequestAPITarget string
ClientSecret string
Client *http.Client
}
func CreateUploader(SSUrl, SSAPIKey, SSRequestTarget string) *Uploader {
u := Uploader{URL: SSUrl,
APIKey: SSAPIKey,
RequestAPITarget: SSRequestTarget,
ClientSecret: createClientSecret(),
Client: &http.Client{},
}
return &u
}
type uploadReqOption func(*http.Request) error
func withReqJSONHeader() uploadReqOption {
return func(r *http.Request) error {
r.Header.Set("content-type", "application/json;charset=utf-8")
return nil
}
}
func withReqFormHeader() uploadReqOption {
return func(r *http.Request) error {
r.Header.Set("content-type", "application/x-www-form-urlencoded")
return nil
}
}
// uses uploader credentials to send the provided body to the provided enpoint
// returns the response body as an array of bytes and any errors
func (u *Uploader) sendRequest(method, endpoint string, body []byte, reqOptions ...uploadReqOption) ([]byte, error) {
req, err := http.NewRequest(method, u.URL+endpoint, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("ss-api-key", u.APIKey)
req.Header.Set("ss-request-api", u.RequestAPITarget)
for _, option := range reqOptions {
if err := option(req); err != nil {
return nil, err
}
}
resp, err := u.Client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Invalid HTTP Status Code: %d; Unable to read response body", resp.StatusCode)
}
return nil, fmt.Errorf("Invalid HTTP status response; HTTP Error code %d; request body %s", resp.StatusCode, string(respBody))
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}