This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contextIO.go
168 lines (150 loc) · 4.45 KB
/
contextIO.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
/*
Package contextio provides a simple way to sign API requests for http://Context.IO.
The simplest usage is to use DoJSON() to return a json byte array that you can use elsewhere in your code.
For more advanced usage, you can use Do() and parse through the http.Response struct yourself. It is not
specific to an API version, so you can use it to make any request you would make through http://console.Context.IO.
*/
package contextio
import (
"bytes"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/garyburd/go-oauth/oauth"
)
const (
// max memory that ParseMultipartForm will use, see https://golang.org/pkg/net/http/#Request.ParseMultipartForm
defaultMaxMemory = 32 << 21 // 64 MB
)
// the default host that the library contacts
const defaultAPIHost = "api.context.io"
// ContextIO is a struct containing the authentication information and a pointer to the oauth client
type ContextIO struct {
key string
secret string
client *oauth.Client
apiHost string
}
// NewContextIO returns a ContextIO struct based on your CIO User and Secret
func NewContextIO(key, secret string) *ContextIO {
c := &oauth.Client{
Credentials: oauth.Credentials{
Token: key,
Secret: secret,
},
}
return &ContextIO{
key: key,
secret: secret,
client: c,
apiHost: defaultAPIHost,
}
}
// SetAPIHost sets the domain (i.e. "api.context.io) for the requests, useful if you are mocking the API for testing
func (c *ContextIO) SetAPIHost(h string) *ContextIO {
c.apiHost = h
return c
}
// NewRequest generates a request and signs it
func (c *ContextIO) NewRequest(method, q string, queryParams url.Values, body *string) (req *http.Request, err error) {
// make sure q has a slash in front of it
if q[0:1] != "/" {
q = "/" + q
}
query := c.apiHost + q
if len(queryParams) > 0 {
query = query + "?" + queryParams.Encode()
}
if body != nil {
// if body is nil, this produces a panic
req, err = http.NewRequest(method, "https://"+query, strings.NewReader(*body))
} else {
req, err = http.NewRequest(method, "https://"+query, nil)
}
if err != nil {
return nil, err
}
req.URL.Opaque = q
req.Header.Set("User-Agent", "GoContextIO Simple Library v. 0.2")
v := url.Values{}
switch method {
case "PUT", "POST", "DELETE":
// need form data here if uploading
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if body != nil {
v, err = url.ParseQuery(*body)
if err != nil {
return nil, err
}
}
}
err = c.client.SetAuthorizationHeader(req.Header, nil, req.Method, req.URL, v)
if err != nil {
return nil, err
}
return req, nil
}
// AttachFile will create a file upload in the request, assumes NewRequest has already been called
func (c *ContextIO) AttachFile(req *http.Request, fieldName, fileName string) error {
f, err := os.Open(fileName)
if err != nil {
return err
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(fieldName, filepath.Base(fileName))
if err != nil {
return err
}
_, err = io.Copy(part, f)
if err != nil {
return err
}
// transfer the existing post vals into the new body
for key, valSlice := range req.PostForm {
for _, val := range valSlice {
err = writer.WriteField(key, val)
if err != nil {
return err
}
}
}
err = writer.Close()
if err != nil {
return err
}
rc := ioutil.NopCloser(body)
req.Body = rc
// update the form
err = req.ParseMultipartForm(defaultMaxMemory)
if err != nil {
return err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
return nil
}
// Do signs the request and returns an *http.Response. The body is a standard response.Body
// and must have defer response.Body.close(). Does not support uploads, use NewRequest and AttachFile for that.
// This is 2 legged authentication, and will not currently work with 3 legged authentication.
func (c *ContextIO) Do(method, q string, params url.Values, body *string) (response *http.Response, err error) {
req, err := c.NewRequest(method, q, params, body)
if err != nil {
return nil, err
}
return http.DefaultClient.Do(req)
}
// DoJSON passes the request to Do and then returns the json in a []byte array
func (c *ContextIO) DoJSON(method, q string, params url.Values, body *string) (json []byte, err error) {
response, err := c.Do(method, q, params, body)
if err != nil {
return nil, err
}
defer func() { _ = response.Body.Close() }()
json, err = ioutil.ReadAll(response.Body)
return json, err
}