-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapi.go
225 lines (199 loc) · 4.49 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package goprismic
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
type work struct {
u string
data map[string]string
res interface{}
ret chan error
}
const (
StatusOK = iota
StatusOverCapacity
)
type Api struct {
URL string
AccessToken string
Data ApiData
Config Config
Status int
client http.Client
queue chan work
retryAt time.Time
}
type ApiData struct {
Forms map[string]Form `json:"forms"`
Refs []Ref `json:"refs"`
Bookmarks map[string]string `json:"bookmarks"`
Tags []string `json:"tags"`
Types map[string]string `json:"types"`
OAuthInitiate string `json:"oauth_initiate"`
OAuthToken string `json:"oauth_token"`
}
type Config struct {
// Number of workers (simultaneous connections)
Workers int
// Timeout for HTTP requests
Timeout time.Duration
// Debug mode
Debug bool
}
// Default configuration
var DefaultConfig = Config{
Workers: 3,
Timeout: 5 * time.Second,
}
// Api entry point
// Use Get(url, accessToken, DefaultConfig) to use the default config
func Get(u, accessToken string, cfg Config) (*Api, error) {
api := &Api{
AccessToken: accessToken,
URL: u,
Config: cfg,
queue: make(chan work),
client: http.Client{Timeout: cfg.Timeout},
}
api.Data.Refs = make([]Ref, 0, 128)
err := api.call(api.URL, map[string]string{}, &api.Data)
if err != nil {
return nil, err
}
if cfg.Workers <= 0 {
panic("Cannot run with no worker")
}
for workers := 0; workers < cfg.Workers; workers++ {
go api.loopWorker()
}
return api, nil
}
// Refreshes the Api data
func (a *Api) Refresh() error {
return a.call(a.URL, map[string]string{}, &a.Data)
}
// Fetches the master ref
func (a *Api) Master() *SearchForm {
for _, r := range a.Data.Refs {
if r.IsMasterRef {
return a.createSearchForm(r)
}
}
return &SearchForm{err: fmt.Errorf("Master ref not found !?!")}
}
// Returns the master ref
func (a *Api) GetMasterRef() string {
for _, r := range a.Data.Refs {
if r.IsMasterRef {
return r.Ref
}
}
return ""
}
// Fetch another ref
func (a *Api) Ref(label string) *SearchForm {
for _, r := range a.Data.Refs {
if r.Label == label {
return a.createSearchForm(r)
}
}
return &SearchForm{err: fmt.Errorf("No ref found with label '%s'", label)}
}
func (a *Api) createSearchForm(r Ref) *SearchForm {
f := &SearchForm{api: a, ref: r}
f.data = make(map[string]string)
return f
}
func (a *Api) work(u string, data map[string]string, res interface{}) error {
w := work{
u: u,
data: data,
res: res,
ret: make(chan error),
}
a.queue <- w
return <-w.ret
}
func (a *Api) loopWorker() {
for {
w := <-a.queue
err := a.call(w.u, w.data, w.res)
w.ret <- err
}
}
func (a *Api) call(u string, data map[string]string, res interface{}) error {
// build query
callurl, errparse := url.Parse(u)
if errparse != nil {
return errparse
}
values := callurl.Query()
for k, v := range data {
values.Set(k, v)
}
if a.AccessToken != "" {
values.Set("access_token", a.AccessToken)
}
callurl.RawQuery = values.Encode()
if a.Status != StatusOK {
if time.Now().Before(a.retryAt) {
if a.Config.Debug {
log.Printf("Prismic - over capacity - ignoring request %s", callurl.String())
}
return errors.New("Prismic - over capacity - aborting request")
} else {
if a.Status == StatusOverCapacity {
a.Status = StatusOK
}
}
}
req, errreq := http.NewRequest("GET", callurl.String(), nil)
if errreq != nil {
return errreq
}
req.Header.Add("Accept", "application/json")
if a.Config.Debug {
log.Printf("Prismic - requesting %s", callurl.String())
}
resp, errdo := a.client.Do(req)
if errdo != nil {
return errdo
}
defer resp.Body.Close()
encoded, errread := ioutil.ReadAll(resp.Body)
if errread != nil {
return errread
}
if resp.StatusCode != 200 {
err := new(PrismicError)
errjson := json.Unmarshal(encoded, err)
if errjson != nil {
return errjson
} else {
if err.IsOverCapacity() {
a.Status = StatusOverCapacity
if err.Until == 0 {
a.retryAt = time.Now().Add(1 * time.Second)
} else {
a.retryAt = time.Unix(err.Until/1000, (err.Until%1000)*100000)
}
if a.Config.Debug {
log.Printf("Prismic - over capacity error, will retry on %s", a.retryAt)
}
} else {
if a.Config.Debug {
log.Printf("Prismic - error %s", err)
}
}
return err
}
}
errjson := json.Unmarshal(encoded, res)
return errjson
}