-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudsight.go
465 lines (382 loc) · 12 KB
/
cloudsight.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// A simple CloudSight API client library. For full API documentation go to
// https://cloudsight.readme.io/v1.0/docs.
package cloudsight
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
// Base API URL.
const BaseURL = "https://api.cloudsightapi.com"
// Default locale that will be used when none is specified in Params.
const DefaultLocale = "en-US"
const (
requestsURL = BaseURL + "/image_requests"
responsesURL = BaseURL + "/image_responses/"
)
const pollMinWait = time.Second * 4
var (
ErrMissingKey = errors.New("key cannot be empty")
ErrMissingSecret = errors.New("secret cannot be empty")
ErrTimeout = errors.New("poll timed out")
ErrInvalidRepostStatus = errors.New("the job needs to have the timeout status")
)
var userAgent = []string{"cloudsight-go v1.0"}
type apiResponse struct {
Error json.RawMessage `json:"error"`
Name string `json:"name"`
Reason string `json:"reason"`
Status string `json:"status"`
TTL float64 `json:"ttl"`
Token string `json:"token"`
URL string `json:"url"`
}
// Possible values for current job status.
type JobStatus string
const (
// Recognition has not yet been completed for this image. Continue polling
// until response has been marked completed.
StatusNotCompleted JobStatus = "not completed"
// Recognition has been completed. Annotation can be found in Name and
// Categories field of Job structure.
StatusCompleted JobStatus = "completed"
// Token supplied on URL does not match an image.
StatusNotFound JobStatus = "not found"
// Image couldn't be recognized because of a specific reason. Check the
// SkipReason field.
StatusSkipped JobStatus = "skipped"
// Recognition process exceeded the allowed TTL setting.
StatusTimeout JobStatus = "timeout"
)
// Return a detailed description of the job status.
func (s JobStatus) Description() string {
switch s {
case StatusNotCompleted:
return "Recognition has not yet been completed for this image. Continue polling until response has been marked completed."
case StatusCompleted:
return "Recognition has been completed. Annotation can be found in name element of the JSON response."
case StatusNotFound:
return "Token supplied on URL does not match an image."
case StatusSkipped:
return "Image couldn't be recognized because of a specific reason. Check the SkipReason field."
case StatusTimeout:
return "Recognition process exceeded the allowed TTL setting."
default:
return fmt.Sprintf("Unknown status: %d.", s)
}
}
// The API may choose not to return any response for given image. SkipReason
// type includes possible reasons for such behavior.
type SkipReason string
const (
// Offensive image content.
ReasonOffensive SkipReason = "offensive"
// Too blurry to identify.
ReasonBlurry SkipReason = "blurry"
// Too close to identify.
ReasonClose SkipReason = "close"
// Too dark to identify.
ReasonDark SkipReason = "dark"
// Too bright to identify.
ReasonBright SkipReason = "bright"
// Content could not be identified.
ReasonUnsure SkipReason = "unsure"
)
// Return a detailed description of the skip reason.
func (r SkipReason) Description() string {
switch r {
case "":
return "The image hasn't been skipped."
case ReasonOffensive:
return "Offensive image content."
case ReasonBlurry:
return "Too blurry to identify."
case ReasonClose:
return "Too close to identify."
case ReasonDark:
return "Too dark to identify."
case ReasonBright:
return "Too bright to identify."
case ReasonUnsure:
return "Content could not be identified."
default:
return fmt.Sprintf("Unknown reason: %d.", r)
}
}
type Client struct {
key string
secret string
}
// Job is a result of sending an image to CloudSight API.
type Job struct {
// Image description as annotated by the API.
Name string
// Current job status.
Status JobStatus
// Time To Live.
TTL float64
// Token uniquely identifying the job.
Token string
// URL to the image as stored on CloudSight API servers.
URL string
// The reason for the job being skipped, if any.
SkipReason SkipReason
createdAt time.Time
mu *sync.Mutex
}
// Create a new Client instance that will authenticate using OAuth1 protocol.
//
// Error (ErrMissingKey or ErrMissingSecret) will be returned if either key or
// secret is empty.
func NewClientOAuth(key, secret string) (*Client, error) {
if key == "" {
return nil, ErrMissingKey
}
if secret == "" {
return nil, ErrMissingSecret
}
return &Client{
key: key,
secret: secret,
}, nil
}
// Create a new Client instance that will authenticate using simple key-based
// method.
//
// ErrMissingKey will be returned if key is empty.
func NewClientSimple(key string) (*Client, error) {
if key == "" {
return nil, ErrMissingKey
}
return &Client{
key: key,
}, nil
}
func (c *Client) getAuthHeader(method, url string, params Params) (string, error) {
if c.secret == "" {
// Use simple authentication
return fmt.Sprintf("CloudSight %s", c.key), nil
} else {
// Use OAuth1 authentication
return oauthSign(method, url, c.key, c.secret, params)
}
}
func (c *Client) doImageRequest(req *http.Request) (*Job, error) {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var job apiResponse
if err := decoder.Decode(&job); err != nil {
return nil, err
}
if job.Error != nil {
return nil, fmt.Errorf("api error: %s, status code: %d", string(job.Error), resp.StatusCode)
}
if resp.StatusCode != http.StatusOK {
fmt.Println("status code", resp.StatusCode)
}
jobURL := job.URL
if strings.HasPrefix(jobURL, "//") {
jobURL = "https:" + jobURL
}
return &Job{
Status: JobStatus(job.Status),
TTL: job.TTL,
Token: job.Token,
URL: jobURL,
createdAt: time.Now(),
mu: &sync.Mutex{},
}, nil
}
// Send an image for classification. The image may be a os.File instance any
// other object implementing io.Reader interface. The params parameter is
// optional and may be nil.
//
// On success this method will immediately return a Job instance. Its status
// will initially be "not completed" as it usually takes 6-12 seconds for the
// server to process an image. In order to retrieve the annotation data, you
// need to keep updating the job status using the UpdateJob() method until the
// status changes. You may also use the WaitJob() method which does this
// automatically.
func (c *Client) ImageRequest(image io.Reader, filename string, params Params) (*Job, error) {
buf := &bytes.Buffer{}
multi := multipart.NewWriter(buf)
field, err := multi.CreateFormFile("image_request[image]", filename)
if err != nil {
return nil, err
}
if _, err = io.Copy(field, image); err != nil {
return nil, err
}
if params == nil {
params = Params{}
}
if _, ok := params["image_request[locale]"]; !ok {
params["image_request[locale]"] = DefaultLocale
}
for k, v := range params {
multi.WriteField(k, v)
}
if err = multi.Close(); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", requestsURL, buf)
if err != nil {
return nil, err
}
hdr := req.Header
hdr["User-Agent"] = userAgent
hdr["Content-Length"] = []string{strconv.Itoa(buf.Len())}
hdr["Content-Type"] = []string{multi.FormDataContentType()}
auth, err := c.getAuthHeader("POST", requestsURL, params)
if err != nil {
return nil, err
}
hdr["Authorization"] = []string{auth}
return c.doImageRequest(req)
}
// Send an image for classification. The image will be retrieved from the URL
// specified. The params parameter is optional and may be nil.
//
// On success this method will immediately return a Job instance. Its status
// will initially be "not completed" as it usually takes 6-12 seconds for the
// server to process an image. In order to retrieve the annotation data, you
// need to keep updating the job status using the UpdateJob() method until the
// status changes. You may also use the WaitJob() method which does this
// automatically.
func (c *Client) RemoteImageRequest(url string, params Params) (*Job, error) {
if params == nil {
params = Params{}
}
if _, ok := params["image_request[locale]"]; !ok {
params["image_request[locale]"] = DefaultLocale
}
params["image_request[remote_image_url]"] = url
values := params.values()
body := bytes.NewBufferString(values.Encode())
req, err := http.NewRequest("POST", requestsURL, body)
if err != nil {
return nil, err
}
hdr := req.Header
hdr["User-Agent"] = userAgent
hdr["Content-Length"] = []string{strconv.Itoa(body.Len())}
auth, err := c.getAuthHeader("POST", requestsURL, params)
if err != nil {
return nil, err
}
hdr["Authorization"] = []string{auth}
return c.doImageRequest(req)
}
func (c *Client) updateJobFromRequest(job *Job, req *http.Request) error {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("HTTP error: %s", err)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var updatedJob apiResponse
if err := decoder.Decode(&updatedJob); err != nil {
return fmt.Errorf("JSON error: %s", err)
}
if updatedJob.Error != nil {
return fmt.Errorf("api error: %s, status code: %d", string(updatedJob.Error), resp.StatusCode)
}
job.Name = updatedJob.Name
job.Status = JobStatus(updatedJob.Status)
job.TTL = updatedJob.TTL
job.SkipReason = SkipReason(updatedJob.Reason)
return nil
}
// Contact the server and update the job status. This method does nothing if
// the status has already changed from "not completed".
//
// After a request has been submitted, it usually takes 6-12 seconds to receive
// a completed response. We recommend polling for a response every 1 second
// after a 4 second delay from the initial request, while the status is "not
// completed". WaitJob() method does this automatically.
func (c *Client) UpdateJob(job *Job) error {
job.mu.Lock()
defer job.mu.Unlock()
if job.Status != StatusNotCompleted {
return nil
}
url := responsesURL + job.Token
req, _ := http.NewRequest("GET", url, nil)
hdr := req.Header
hdr["User-Agent"] = userAgent
auth, err := c.getAuthHeader("GET", url, nil)
if err != nil {
return err
}
hdr["Authorization"] = []string{auth}
return c.updateJobFromRequest(job, req)
}
// Repost the job if it has timed out (StatusTimeout).
//
// ErrInvalidRepostStatus will be returned if current job status is different
// than StatusTimeout.
func (c *Client) RepostJob(job *Job) error {
if job.Status != StatusTimeout {
return ErrInvalidRepostStatus
}
url := fmt.Sprintf("%s/%s/repost", requestsURL, job.Token)
req, _ := http.NewRequest("POST", url, nil)
hdr := req.Header
hdr["User-Agent"] = userAgent
hdr["Content-Length"] = []string{"0"}
auth, err := c.getAuthHeader("POST", url, nil)
if err != nil {
return err
}
hdr["Authorization"] = []string{auth}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("HTTP error: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("error reposting job: %s, status: %d", string(body), resp.StatusCode)
}
io.Copy(ioutil.Discard, resp.Body)
return c.UpdateJob(job)
}
// Wait for the job until it has been processed. This method will block for up
// to timeout seconds. After that ErrTimeout will be returned. If the timeout
// parameter is set to 0, WaitJob() will wait infinitely.
//
// This method will wait for 4 seconds after the initial request and then will
// call UpdateJob() method every second until the status changes.
func (c *Client) WaitJob(job *Job, timeout time.Duration) error {
timeoutAt := time.Now().Add(timeout)
waitUntil := job.createdAt.Add(pollMinWait)
now := time.Now()
if now.Before(waitUntil) {
time.Sleep(waitUntil.Sub(now))
}
for {
if timeout > 0 && time.Now().After(timeoutAt) {
return ErrTimeout
}
if err := c.UpdateJob(job); err != nil {
return err
}
if job.Status != StatusNotCompleted {
return nil
}
time.Sleep(1 * time.Second)
}
}