forked from masci/flickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflickr.go
60 lines (52 loc) · 1.88 KB
/
flickr.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
// Flickr.go is a Go library for accessing Flickr API https://www.flickr.com/services/api
package flickr
import (
"bytes"
"mime/multipart"
)
const (
API_ENDPOINT = "https://api.flickr.com/services/rest"
UPLOAD_ENDPOINT = "https://up.flickr.com/services/upload/"
AUTHORIZE_URL = "https://www.flickr.com/services/oauth/authorize"
REQUEST_TOKEN_URL = "https://www.flickr.com/services/oauth/request_token"
ACCESS_TOKEN_URL = "https://www.flickr.com/services/oauth/access_token"
)
// Perform a GET request to the Flickr API with the configured FlickrClient passed as first
// parameter. Results will be unmarshalled to fill in a FlickrResponse struct passed as
// second parameter.
func DoGet(client *FlickrClient, r FlickrResponse) error {
res, err := client.HTTPClient.Get(client.GetUrl())
if err != nil {
return err
}
return parseApiResponse(res, r)
}
// Perform a POST request to the Flickr API with the configured FlickrClient, the
// request body and the body content type. Results will be unmarshalled in a FlickrResponse
// struct.
func DoPostBody(client *FlickrClient, body *bytes.Buffer, bodyType string, r FlickrResponse) error {
res, err := client.HTTPClient.Post(client.EndpointUrl, bodyType, body)
if err != nil {
return err
}
return parseApiResponse(res, r)
}
// Perform a POST request to the Flickr API with the configured FlickrClient,
// dumping client Args into the request Body.
func DoPost(client *FlickrClient, r FlickrResponse) error {
// instance an empty request body
body := &bytes.Buffer{}
// multipart writer to fill the body
writer := multipart.NewWriter(body)
// dump params
for key, val := range client.Args {
_ = writer.WriteField(key, val[0])
}
err := writer.Close()
if err != nil {
return err
}
// evaluate the content type and the boundary
contentType := writer.FormDataContentType()
return DoPostBody(client, body, contentType, r)
}