forked from FacePlusPlus/facepp-javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facepp-sdk.coffee
138 lines (114 loc) · 3.77 KB
/
facepp-sdk.coffee
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
class @FacePP
@RE_TRIM: /^\/+|\/+$/g
constructor: (@apiKey, @apiSecret, options={}) ->
defaults =
apiURL: 'https://apicn.faceplusplus.com/v2'
sessionInterval: 500
requestTimeout: 10 * 1000
ajaxAdapter: if ('FormData' of window) then 'XMLHttpRequest' else 'jQuery'
concurrency: 2
for k of defaults
if not options[k]?
options[k] = defaults[k]
@apiURL = options.apiURL.replace FacePP.RE_TRIM, ''
{@sessionInterval, @requestTimeout} = options
@requestAdapter = FacePP.adapter[options.ajaxAdapter]
if (requestCapacity = options.concurrency) > 0
# wrapper for limiting concurrency
queue = []
scheduleRequest = =>
if requestCapacity > 0 and queue.length > 0
--requestCapacity
[apiMethod, data, callback] = queue.shift()
FacePP::request.call this, apiMethod, data, (err, resp) ->
++requestCapacity
setTimeout scheduleRequest, 0
callback err, resp
return
return
@request = (apiMethod, data, callback) ->
queue.push [apiMethod, data, callback]
scheduleRequest()
return
request: (apiMethod, data, callback) ->
data['api_key'] = @apiKey
data['api_secret'] = @apiSecret
url = @apiURL + '/' + (apiMethod.replace FacePP.RE_TRIM, '')
@requestAdapter url, data, timeout: @requestTimeout, callback
return
sessionCheck: (session_id, callback) =>
@request 'info/get_session', session_id: session_id, (err, result) =>
if err
callback err, result
else if result.status == 'FAILED'
callback (result.result.error_code or -1), result.result
else if result.status == 'INQUEUE'
setTimeout @sessionCheck, @sessionInterval, session_id, callback
else
callback null, result.result
return
return
requestAsync: (apiMethod, data, callback) ->
data['async'] = 'true'
@request apiMethod, data, (err, result) =>
if err
callback err, result
else
setTimeout @sessionCheck, @sessionInterval, result.session_id, callback
return
return
# Adapters for ajax library
@adapter:
jQuery: (url, data, options, callback) ->
valueLengthEst = 0
for k of data
valueLengthEst += data[k].length or 0
jQuery.ajax
url: url
dataType: 'jsonp'
crossDomain: true
data: data
method: if valueLengthEst < 1024 then 'GET' else 'POST'
timeout: options.timeout
error: (xhr) ->
if (response = xhr.responseText)
try
response = JSON.parse response
callback (response?.error_code or -1), response
return
success: (data) ->
callback null, data
return
return
XMLHttpRequest: (url, data, options, callback) ->
hasBlob = false
for k of data
if data[k] instanceof Blob
hasBlob = true
break
xhr = new XMLHttpRequest
xhr.onreadystatechange = ->
if @readyState == 4
@onreadystatechange = null
if (response = @responseText)
try
response = JSON.parse response
if @status == 200
callback null, response
else
callback (response.error_code or -1), response
return
xhr.open 'POST', url, true
if 'timeout' of xhr
xhr.timeout = options.timeout
if hasBlob
form = new FormData
form.append k, data[k] for k of data
xhr.send form
else
xhr.setRequestHeader 'Content-type', 'application/x-www-form-urlencoded'
encode = encodeURIComponent
tmp = []
tmp.push "#{encode k}=#{encode data[k]}" for k of data
xhr.send (tmp.join '&')
return