-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
166 lines (144 loc) · 3.61 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
158
159
160
161
162
163
164
165
166
package makeplans
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
var DefaultURL = "http://%s.test.makeplans.net/api/v1"
// DefaultResolver replaces the API url with the account name specified.
// This can be overridden to use a different mechanism
var DefaultResolver = func(urlTmpl string, accountName string) string {
return fmt.Sprintf(urlTmpl, accountName)
}
type Client struct {
URL string
AccountName string
Token string
// annoying patch for appengine
Client *http.Client
Resolver func(string, string) string
}
func New(account string, token string) *Client {
return &Client{
URL: DefaultURL,
Token: token,
AccountName: account,
Resolver: DefaultResolver,
}
}
var showResponse = false
func (c *Client) do(method string, path string, body io.Reader) (*http.Response, error) {
var httpCli *http.Client
if c.Client != nil {
httpCli = c.Client
} else {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpCli = &http.Client{Transport: tr}
}
u := c.Resolver(c.URL, c.AccountName) + path
req, err := http.NewRequest(method, u, body)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "https://github.com/drewwells/makeplans")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.SetBasicAuth(c.Token, "")
return httpCli.Do(req)
}
func (c *Client) Do(method string, path string, body io.Reader) ([]byte, error) {
r, err := c.do(method, path, body)
if err != nil {
return nil, err
}
if r.Body == nil {
return nil, nil
}
defer r.Body.Close()
bs, err := ioutil.ReadAll(r.Body)
// fmt.Println(string(bs))
if err != nil {
return nil, err
}
// FIXME: parseError should happen AFTER endpoints attempt to unmarshal
return bs, parseError(bs)
}
type FieldError map[string][]string
func (fe FieldError) Error() string {
var msg string
virgin := true
for field, errors := range fe {
if !virgin {
msg += "\n"
}
virgin = false
msg += "error " + field + ": " + strings.Join(errors, ", ")
}
return msg
}
type E struct {
Error struct {
Description string
}
}
var (
// ErrNotFound is a generic error returned by Makeplans. It sometimes
// indicates an ID is invalid.
ErrNotFound = errors.New("Not found")
ErrEmptyResponse = errors.New("empty response")
// ErrEmailTaken indicates the person already exists
ErrEmailTaken = errors.New("error email: has already been taken")
)
func parseError(bs []byte) error {
if len(bs) == 0 {
return ErrEmptyResponse
}
e := E{}
err := json.Unmarshal(bs, &e)
if err != nil {
return nil
}
if len(e.Error.Description) > 0 {
// Produce real errors for known http errors
desc := e.Error.Description
switch desc {
case ErrEmailTaken.Error():
return ErrEmailTaken
case ErrBookingCapacityLimit.Error():
return ErrBookingCapacityLimit
case ErrNotFound.Error():
return ErrNotFound
default:
fmt.Println("default error", e.Error.Description)
return errors.New(e.Error.Description)
}
}
// False positive error
// if fal := `{"resource_id":["can't be blank"],"count":["More than maximum count per booking"]}`; fal == string(bs) {
// log.Println("bypassed false error")
// return nil
// }
// Try again with FieldError
var fe FieldError
err = json.Unmarshal(bs, &fe)
if err != nil {
return nil
}
// FIXME: this is weird, there's a better way to report up errors
if msgs, ok := fe["email"]; ok {
if len(msgs) == 1 && "error email: "+msgs[0] == ErrEmailTaken.Error() {
return ErrEmailTaken
}
}
if len(fe) > 0 {
return fe
}
return nil
}