-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquareupw.go
130 lines (108 loc) · 2.55 KB
/
squareupw.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
/*
Package squareupw implements a wrapper for squareup.com API for Go (Golang).
*/
package squareupw
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"regexp"
"strings"
)
const (
BaseURL = "https://connect.squareup.com"
MethodGet = "GET"
MethodPost = "POST"
MethodPut = "PUT"
)
type API struct {
token string
}
//Error represents Error response.
type Error struct {
Type string `json:"type"`
Message string `json:"message"`
}
//NewAPI returns new API instance.
func NewAPI(token string) *API {
return &API{
token: token,
}
}
//Send http request.
func (a API) Send(method, url string, reqData []byte) (httpResp *http.Response, body []byte, err error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(reqData))
if err != nil {
return
}
req.Header.Add("Authorization", "Bearer "+a.token)
req.Header.Add("Accept", "application/json")
if method == MethodPost || method == MethodPut {
req.Header.Add("Content-Type", "application/json")
}
client := http.DefaultClient
httpResp, err = client.Do(req)
if err != nil {
return
}
defer httpResp.Body.Close()
body, err = ioutil.ReadAll(httpResp.Body)
if err != nil {
return
}
if httpResp.StatusCode >= 400 {
errorResp := Error{}
err = json.Unmarshal(body, &errorResp)
if err != nil {
return
}
err = errors.New(errorResp.Message)
}
return
}
//ExtractURLFromLinkHeader extracts url from Link header.
func ExtractURLFromLinkHeader(header []string) (url string, err error) {
link := header[0]
pattern := "^<(.+)>;rel='next'$"
re := regexp.MustCompile(pattern)
result := re.FindStringSubmatch(link)
i := 1
if len(result)-1 >= i {
url := result[i]
return url, nil
}
err = errors.New("Invalid value for Link header")
return
}
//GetQueryStringByStruct take struct and build query string.
func GetQueryStringByStruct(s interface{}, tagName string, queryEscape bool) (queryString string, err error) {
queryStringSlice := []string{}
val := reflect.ValueOf(s).Elem()
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
tag := typeField.Tag
tagVal := tag.Get(tagName)
if len(tagVal) < 1 {
continue
}
if valueField.Kind() != reflect.String {
err = errors.New("Values for query string must have string type.")
return
}
if len(valueField.String()) < 1 {
continue
}
v := valueField.String()
if queryEscape {
v = url.QueryEscape(v)
}
queryStringSlice = append(queryStringSlice, tagVal+"="+v)
}
queryString = strings.Join(queryStringSlice, "&")
return
}