This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monzgo.go
109 lines (87 loc) · 2.32 KB
/
monzgo.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
package monzgo
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// Monzgo client
type Monzgo struct {
APIKey string
BaseURL string
}
// MonzoError - API error definition
type MonzoError struct {
StatusCode int
Code string
Message string
}
func (e *MonzoError) Error() string {
return fmt.Sprintf("%s: %s", e.Code, e.Message)
}
func (m *Monzgo) request(requestMethod string, path string, responseHolder interface{}, requestData map[string]string) error {
var request *http.Request
var err error
if requestMethod == "GET" {
request, err = m.createRequestWithQuery(path, requestData)
} else {
request, err = m.createRequestWithFormData(requestMethod, path, requestData)
}
if err != nil {
return err
}
return m.performRequest(request, responseHolder)
}
func (m *Monzgo) createRequestWithQuery(path string, queryParams map[string]string) (*http.Request, error) {
request, err := http.NewRequest("GET", m.BaseURL+path, nil)
if err != nil {
return nil, err
}
if len(queryParams) > 0 {
q := request.URL.Query()
for key, val := range queryParams {
q.Add(key, val)
}
request.URL.RawQuery = q.Encode()
}
return request, nil
}
func (m *Monzgo) createRequestWithFormData(requestMethod string, path string, parameters map[string]string) (*http.Request, error) {
formData := url.Values{}
for key, val := range parameters {
formData.Set(key, val)
}
request, err := http.NewRequest(requestMethod, m.BaseURL+path, strings.NewReader(formData.Encode()))
if err != nil {
return nil, err
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
return request, nil
}
func (m *Monzgo) performRequest(request *http.Request, responseHolder interface{}) error {
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", m.APIKey))
res, err := http.DefaultClient.Do(request)
if err != nil {
return err
}
defer res.Body.Close()
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != 200 {
monzoErr := &MonzoError{
StatusCode: res.StatusCode,
}
if err := json.Unmarshal(bytes, monzoErr); err != nil {
return fmt.Errorf("Error during JSON umarshalling %d error: %v", res.StatusCode, err)
}
return monzoErr
}
if err := json.Unmarshal(bytes, responseHolder); err != nil {
return err
}
return nil
}