forked from hanzoai/gochimp3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
157 lines (130 loc) Β· 3.04 KB
/
api.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 gochimp3
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"reflect"
"regexp"
"time"
)
// URIFormat defines the endpoint for a single app
const URIFormat string = "%s.api.mailchimp.com"
// Version the latest API version
const Version string = "/3.0"
// DatacenterRegex defines which datacenter to hit
var DatacenterRegex = regexp.MustCompile("[^-]\\w+$")
// API represents the origin of the API
type API struct {
Key string
Timeout time.Duration
Transport http.RoundTripper
User string
Debug bool
endpoint string
}
// New creates a API
func New(apiKey string) *API {
u := url.URL{}
u.Scheme = "https"
u.Host = fmt.Sprintf(URIFormat, DatacenterRegex.FindString(apiKey))
u.Path = Version
return &API{
User: "gochimp3",
Key: apiKey,
endpoint: u.String(),
}
}
// Request will make a call to the actual API.
func (api *API) Request(method, path string, params QueryParams, body, response interface{}) error {
client := &http.Client{Transport: api.Transport}
if api.Timeout > 0 {
client.Timeout = api.Timeout
}
requestURL := fmt.Sprintf("%s%s", api.endpoint, path)
if api.Debug {
log.Printf("Requesting %s: %s\n", method, requestURL)
}
var bodyBytes io.Reader
var err error
var data []byte
if body != nil {
data, err = json.Marshal(body)
if err != nil {
return err
}
bodyBytes = bytes.NewBuffer(data)
if api.Debug {
log.Printf("Adding body: %+v\n", body)
}
}
req, err := http.NewRequest(method, requestURL, bodyBytes)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(api.User, api.Key)
if params != nil && !reflect.ValueOf(params).IsNil() {
queryParams := req.URL.Query()
for k, v := range params.Params() {
if v != "" {
queryParams.Set(k, v)
}
}
req.URL.RawQuery = queryParams.Encode()
if api.Debug {
log.Printf("Adding query params: %q\n", req.URL.Query())
}
}
if api.Debug {
dump, _ := httputil.DumpRequestOut(req, true)
log.Printf("%s", string(dump))
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if api.Debug {
dump, _ := httputil.DumpResponse(resp, true)
log.Printf("%s", string(dump))
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
// Do not unmarshall response is nil
if response == nil || reflect.ValueOf(response).IsNil() || len(data) == 0 {
return nil
}
err = json.Unmarshal(data, response)
if err != nil {
return err
}
return nil
}
// This is an API Error
return parseAPIError(data)
}
// RequestOk Make Request ignoring body and return true if HTTP status code is 2xx.
func (api *API) RequestOk(method, path string) (bool, error) {
err := api.Request(method, path, nil, nil, nil)
if err != nil {
return false, err
}
return true, nil
}
func parseAPIError(data []byte) error {
apiError := new(APIError)
err := json.Unmarshal(data, apiError)
if err != nil {
return err
}
return apiError
}