impossiblefxv1 1.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HOWTO-PUBLISH.txt +15 -0
- package/README.md +21 -0
- package/apis/edit-2016-06-02.json +15 -0
- package/apis/metadata.json +31 -0
- package/apis/project-2016-06-01.json +56 -0
- package/apis/project-2016-06-02.json +767 -0
- package/apis/project-2023-03-07.json +767 -0
- package/apis/project-2023-12-11.json +767 -0
- package/apis/render-2016-06-02.json +764 -0
- package/apis/render-2023-12-11.json +764 -0
- package/dist/fx-sdk-latest.js +22494 -0
- package/dist-tools/build-browser.js +109 -0
- package/dist-tools/build-sdk.sh +6 -0
- package/dist-tools/es6-promise.js +957 -0
- package/dist-tools/foo.sh +4 -0
- package/dist-tools/publish.sh +6 -0
- package/lib/browser.js +58 -0
- package/lib/config.js +125 -0
- package/lib/core.js +15 -0
- package/lib/credentials/chain.js +54 -0
- package/lib/credentials/environment.js +69 -0
- package/lib/credentials/inifile.js +57 -0
- package/lib/credentials/token.js +38 -0
- package/lib/credentials.js +53 -0
- package/lib/encoding.js +36 -0
- package/lib/endpoint.js +18 -0
- package/lib/fx.js +50 -0
- package/lib/http/node.js +69 -0
- package/lib/http/xhr.js +12 -0
- package/lib/http.js +57 -0
- package/lib/proto.js +38 -0
- package/lib/protocol.js +56 -0
- package/lib/request.js +252 -0
- package/lib/response.js +24 -0
- package/lib/service.js +182 -0
- package/lib/services/batch.js +5 -0
- package/lib/services/edit.js +9 -0
- package/lib/services/project.js +102 -0
- package/lib/services/render.js +63 -0
- package/lib/services/story.js +5 -0
- package/lib/services.js +30 -0
- package/lib/util.js +126 -0
- package/package.json +37 -0
- package/package.json.save +36 -0
- package/proto/Movie.proto +4081 -0
- package/proto/fx.proto +43 -0
- package/templates/config.html +91 -0
- package/templates/examples.html +69 -0
- package/templates/getstarted.html +30 -0
- package/templates/index.html +6 -0
- package/templates/makeexample.py +57 -0
- package/templates/makerequests.html +210 -0
- package/templates/operation.html +36 -0
- package/templates/service.html +9 -0
- package/templates/services.html +19 -0
- package/templates/version.html +12 -0
- package/templates/versions.html +10 -0
- package/templates/workservice.html +68 -0
- package/test/circles.mp4 +0 -0
- package/test/config.js +131 -0
- package/test/index.html +35 -0
- package/test/mocha.opts +4 -0
- package/test/project.js +148 -0
- package/test/render.js +136 -0
- package/test/retry.js +53 -0
- package/test/sdktests.js +62 -0
- package/test/sdl.js +125 -0
- package/test/servertest/simple.js +20 -0
package/lib/http.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
var FX = require('./core');
|
|
2
|
+
|
|
3
|
+
FX.HttpRequest = FX.util.inherit({
|
|
4
|
+
constructor: function HttpRequest(service, operation) {
|
|
5
|
+
this.service = service
|
|
6
|
+
this.operation = operation
|
|
7
|
+
|
|
8
|
+
// TODO: change to ssl or httpProtocol to avoid conflict with "protocol"
|
|
9
|
+
this.protocol = service.config.protocol
|
|
10
|
+
this.data = null
|
|
11
|
+
this.auth = undefined
|
|
12
|
+
this.headers = {}
|
|
13
|
+
|
|
14
|
+
if(!FX.util.isBrowser) {
|
|
15
|
+
this.headers["x-fx-apiversion"] = service.apiVersion
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
this.hostname = service.config.endpoint
|
|
19
|
+
this.path = operation.http.path
|
|
20
|
+
this.method = operation.http.method
|
|
21
|
+
this.port = service.config.protocol == "https" ? 443 : 80
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
authenticate: function(callback) {
|
|
25
|
+
var self = this;
|
|
26
|
+
if(self.service.config.requiresAuth) {
|
|
27
|
+
|
|
28
|
+
self.service.config.getCredentials(function(err, creds){
|
|
29
|
+
if(err){
|
|
30
|
+
callback(err)
|
|
31
|
+
} else {
|
|
32
|
+
if(creds.apikey && creds.apisecret) {
|
|
33
|
+
self.auth = creds.apikey + ':' + creds.apisecret
|
|
34
|
+
callback(null)
|
|
35
|
+
} else if(creds.apitoken) {
|
|
36
|
+
self.headers["Authorization"] = "Bearer " + creds.apitoken
|
|
37
|
+
callback(null)
|
|
38
|
+
} else {
|
|
39
|
+
callback(new Error("No valid credentials found"))
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
} else {
|
|
44
|
+
callback(null)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
FX.HttpClient = FX.util.inherit({});
|
|
51
|
+
|
|
52
|
+
FX.HttpClient.getInstance = function getInstance() {
|
|
53
|
+
if (this.singleton === undefined) {
|
|
54
|
+
this.singleton = new this();
|
|
55
|
+
}
|
|
56
|
+
return this.singleton;
|
|
57
|
+
};
|
package/lib/proto.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
var FX = require('./core')
|
|
2
|
+
var ProtoBuf = require("protobufjs");
|
|
3
|
+
|
|
4
|
+
var getProtoURL = function() {
|
|
5
|
+
var url = "";
|
|
6
|
+
if(!FX.util.isBrowser()) {
|
|
7
|
+
var path = require('path')
|
|
8
|
+
url = path.join(__dirname, '..', 'proto', 'fx.proto');
|
|
9
|
+
} else {
|
|
10
|
+
//url = "https://console.impossible.io/proto/fx.proto"
|
|
11
|
+
|
|
12
|
+
if (FX.config.protoURLPrefix){
|
|
13
|
+
url = FX.config.protoURLPrefix + "/fx.proto";
|
|
14
|
+
}else{
|
|
15
|
+
url = "https://is-console.s3.amazonaws.com/proto/fx.proto";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return url;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
FX.SDL = FX.util.inherit({
|
|
24
|
+
|
|
25
|
+
constructor: function(options) {
|
|
26
|
+
var config = FX.util.copy(FX.config.SDL)
|
|
27
|
+
config.url = getProtoURL()
|
|
28
|
+
FX.util.update(config, options || {})
|
|
29
|
+
|
|
30
|
+
ProtoBuf.populateDefaults = config.populateDefaults;
|
|
31
|
+
console.log("Protobuf url is", config.url)
|
|
32
|
+
|
|
33
|
+
var builder = ProtoBuf.loadProtoFile(config.url)
|
|
34
|
+
|
|
35
|
+
return builder.build(config.domain);
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
});
|
package/lib/protocol.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
var FX = require('./core')
|
|
2
|
+
|
|
3
|
+
var inherit = FX.util.inherit
|
|
4
|
+
|
|
5
|
+
FX.Protocol = {
|
|
6
|
+
|
|
7
|
+
jsonbody: {
|
|
8
|
+
build: function(request, name, value, description) {
|
|
9
|
+
body = request.data || {}
|
|
10
|
+
value = value || description.locationValue
|
|
11
|
+
|
|
12
|
+
if(typeof value == 'object') {
|
|
13
|
+
value = JSON.parse(JSON.stringify(value))
|
|
14
|
+
}
|
|
15
|
+
var dest = body
|
|
16
|
+
var parts = description.locationName.split('.')
|
|
17
|
+
if(parts.length > 1) {
|
|
18
|
+
for(var p=0; p < parts.length - 1; p++) {
|
|
19
|
+
dest[parts[p]] = dest[parts[p]] || {}
|
|
20
|
+
dest = dest[parts[p]]
|
|
21
|
+
}
|
|
22
|
+
dest[parts[parts.length - 1]] = value
|
|
23
|
+
} else {
|
|
24
|
+
dest[parts[0]] = value
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
request.data = body
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
body: {
|
|
32
|
+
build: function(request, name, value, description) {
|
|
33
|
+
request.data = value
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
uri: {
|
|
38
|
+
build: function(request, name, value, description) {
|
|
39
|
+
var regex = new RegExp('\{' + description.locationName + '\}', 'g')
|
|
40
|
+
request.path = request.path.replace(regex, value);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
json: {
|
|
45
|
+
build: function(params, members) {
|
|
46
|
+
var result = {}
|
|
47
|
+
FX.util.arrayEach(members, function(m){
|
|
48
|
+
result[m] = params[m]
|
|
49
|
+
})
|
|
50
|
+
return result
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
}
|
package/lib/request.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
var FX = require('./core')
|
|
2
|
+
var inherit = FX.util.inherit
|
|
3
|
+
|
|
4
|
+
FX.Request = inherit({
|
|
5
|
+
|
|
6
|
+
constructor: function Request(service, method, operation, params) {
|
|
7
|
+
this.startTime = FX.util.date.getDate();
|
|
8
|
+
|
|
9
|
+
this.service = service;
|
|
10
|
+
this.operation = operation;
|
|
11
|
+
this.method = method;
|
|
12
|
+
this.data = null;
|
|
13
|
+
this.params = {};
|
|
14
|
+
this.eventhandlers = {};
|
|
15
|
+
|
|
16
|
+
FX.util.update(this.params, service.config.params || {})
|
|
17
|
+
FX.util.update(this.params, params || {})
|
|
18
|
+
|
|
19
|
+
this.httpRequest = this.buildHttpRequest()
|
|
20
|
+
this.response = new FX.Response(this);
|
|
21
|
+
this.retryCount = 0;
|
|
22
|
+
this.startTime = new Date();
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
buildHttpRequest: function() {
|
|
26
|
+
var self = this;
|
|
27
|
+
var request = new FX.HttpRequest(self.service, self.operation)
|
|
28
|
+
|
|
29
|
+
if(this.operation.input) {
|
|
30
|
+
var inputtype = this.operation.input.type
|
|
31
|
+
if(inputtype== "structure") {
|
|
32
|
+
var inputparams = this.operation.input.members || {}
|
|
33
|
+
FX.util.each(inputparams, function(name, description) {
|
|
34
|
+
|
|
35
|
+
// var translatedName = FX.util.string.lowerFirst(name)
|
|
36
|
+
var translatedName = name
|
|
37
|
+
var value = self.params[translatedName]
|
|
38
|
+
|
|
39
|
+
// encode parameter
|
|
40
|
+
if(description.encoding) {
|
|
41
|
+
value = FX.Encoding[description.encoding].encode(value)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// build body, fill uri and querystring parameters
|
|
45
|
+
if(FX.Protocol.hasOwnProperty(description.location)) {
|
|
46
|
+
FX.Protocol[description.location].build(request, name, value, description)
|
|
47
|
+
} else {
|
|
48
|
+
//console.log("Request Builder: no protocol for", name)
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if(this.operation.input.encoding) {
|
|
54
|
+
request.data = FX.Encoding[this.operation.input.encoding].encode(request.data)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return request
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
handleResponse: function(callback, body) {
|
|
61
|
+
var self = this;
|
|
62
|
+
|
|
63
|
+
if(self.response.error) {
|
|
64
|
+
|
|
65
|
+
if(body) {
|
|
66
|
+
errorMessage = body.toString()
|
|
67
|
+
try {
|
|
68
|
+
self.response.error.message = JSON.parse(errorMessage)
|
|
69
|
+
} catch(error) {
|
|
70
|
+
self.response.error.message = errorMessage
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if(callback) {
|
|
75
|
+
callback(self.response.error, null)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
self.invokeHandlers('error', self.response)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
} else {
|
|
82
|
+
|
|
83
|
+
// transform response, usually a JSON transform
|
|
84
|
+
var protocol = self.service.api.metadata.protocol
|
|
85
|
+
if(self.operation.output && self.operation.output.protocol)
|
|
86
|
+
protocol = self.operation.output.protocol
|
|
87
|
+
|
|
88
|
+
if(protocol && body)
|
|
89
|
+
self.response.data = FX.Encoding[protocol].decode(body)
|
|
90
|
+
|
|
91
|
+
// operation specific response transformation
|
|
92
|
+
if(self.operation.transform) {
|
|
93
|
+
if(self.operation.transform.output) {
|
|
94
|
+
self.service[self.operation.transform.output.method](self)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if(callback) {
|
|
99
|
+
callback(self.response.error, self.response.data)
|
|
100
|
+
} else {
|
|
101
|
+
self.invokeHandlers('success', self.response)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
var logger = self.service.config.logger;
|
|
107
|
+
if(logger) {
|
|
108
|
+
var line = this.buildMessage(logger.isTTY);
|
|
109
|
+
if(typeof logger.log === 'function') {
|
|
110
|
+
logger.log(line);
|
|
111
|
+
} else if(typeof logger.write === 'function') {
|
|
112
|
+
logger.write(line + '\n');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
self.invokeHandlers('complete', self.response)
|
|
117
|
+
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
send: function send(callback) {
|
|
121
|
+
var self = this;
|
|
122
|
+
FX.HttpClient.getInstance().exec(self.httpRequest, function(err, stream) {
|
|
123
|
+
self.response.stream = stream;
|
|
124
|
+
|
|
125
|
+
if(err) {
|
|
126
|
+
self.response.error = {
|
|
127
|
+
code: 999,
|
|
128
|
+
message: err
|
|
129
|
+
}
|
|
130
|
+
self.handleResponse(callback)
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
stream.on('aborted', function (err) {
|
|
135
|
+
self.response.error = {
|
|
136
|
+
code: 999,
|
|
137
|
+
message: err
|
|
138
|
+
}
|
|
139
|
+
self.handleResponse(callback)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
stream.on('error', function (err) {
|
|
143
|
+
self.response.stream.abort()
|
|
144
|
+
|
|
145
|
+
self.response.error = {
|
|
146
|
+
code: 555,
|
|
147
|
+
message: err
|
|
148
|
+
}
|
|
149
|
+
self.handleResponse(callback)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
stream.on('response', function (response) {
|
|
154
|
+
if(response.setTimeout)
|
|
155
|
+
response.setTimeout(self.service.config.responseTimeout || 600000)
|
|
156
|
+
|
|
157
|
+
self.response.httpResponse = response
|
|
158
|
+
|
|
159
|
+
if(response.statusCode >= 500) {
|
|
160
|
+
if(self.retryCount < (self.service.config.maxRetries||10) ){
|
|
161
|
+
self.retryCount++;
|
|
162
|
+
var base = self.service.config.retryDelayBase || 100;
|
|
163
|
+
var delay = Math.random() * (Math.pow(2, self.retryCount) * base);
|
|
164
|
+
setTimeout(self.send.bind(self, callback), delay)
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if(response.statusCode >= 400) {
|
|
170
|
+
self.response.error = {
|
|
171
|
+
code: response.statusCode
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if(!self.operation.output.payload) {
|
|
176
|
+
var data = [];
|
|
177
|
+
response.on('data', function (chunk) {
|
|
178
|
+
data.push(chunk)
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
response.on('end', function () {
|
|
182
|
+
var buffer = Buffer.concat(data)
|
|
183
|
+
self.handleResponse(callback, buffer)
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
} else {
|
|
187
|
+
//Streamable response?
|
|
188
|
+
self.handleResponse(callback)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
});
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
return self;
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
promise: function() {
|
|
198
|
+
var self = this;
|
|
199
|
+
var PromiseDependency = self.service.config.promiseDependency || Promise
|
|
200
|
+
|
|
201
|
+
if(typeof PromiseDependency !== 'function') {
|
|
202
|
+
throw new Error("No PromiseDependency defined")
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return new PromiseDependency(function(resolve, reject) {
|
|
206
|
+
self.on('success', function(response) {
|
|
207
|
+
resolve(response.data);
|
|
208
|
+
});
|
|
209
|
+
self.on('error', function(response) {
|
|
210
|
+
reject(response.error);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
self.send();
|
|
214
|
+
});
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
on: function(event, callback) {
|
|
218
|
+
var handlers = this.eventhandlers[event] || []
|
|
219
|
+
handlers.push(callback)
|
|
220
|
+
this.eventhandlers[event] = handlers
|
|
221
|
+
return this;
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
invokeHandlers: function(event) {
|
|
225
|
+
var args = Array.prototype.slice.call(arguments, 1);
|
|
226
|
+
var handlers = this.eventhandlers[event] || []
|
|
227
|
+
FX.util.arrayEach(handlers, function(handler){
|
|
228
|
+
handler.apply(null, args)
|
|
229
|
+
})
|
|
230
|
+
},
|
|
231
|
+
|
|
232
|
+
buildMessage: function(ansi) {
|
|
233
|
+
var time = new Date();
|
|
234
|
+
var delta = (time - this.startTime) / 1000;
|
|
235
|
+
var status = this.response.httpResponse.statusCode;
|
|
236
|
+
var params = JSON.stringify(this.params)
|
|
237
|
+
|
|
238
|
+
var message = '';
|
|
239
|
+
if (ansi) message += '\x1B[34m';
|
|
240
|
+
message += '[FX ' + this.service.identifier + ' ' + status;
|
|
241
|
+
message += ' ' + delta.toString() + 's ' + this.retryCount + ' retries]';
|
|
242
|
+
if (ansi) message += '\x1B[0;1m';
|
|
243
|
+
message += ' ' + FX.util.string.lowerFirst(this.method);
|
|
244
|
+
message += '(' + params + ')';
|
|
245
|
+
if (ansi) message += '\x1B[0m';
|
|
246
|
+
return message;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
package/lib/response.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var FX = require('./core')
|
|
2
|
+
var inherit = FX.util.inherit
|
|
3
|
+
|
|
4
|
+
FX.Response = inherit({
|
|
5
|
+
constructor: function Response(request) {
|
|
6
|
+
this.request = request;
|
|
7
|
+
this.data = null;
|
|
8
|
+
this.error = null;
|
|
9
|
+
this.stream = null
|
|
10
|
+
this.httpResponse = null;
|
|
11
|
+
|
|
12
|
+
this.retryCount = 0;
|
|
13
|
+
this.redirectCount = 0;
|
|
14
|
+
// if (request) {
|
|
15
|
+
// this.maxRetries = request.service.numRetries();
|
|
16
|
+
// this.maxRedirects = request.service.config.maxRedirects;
|
|
17
|
+
// }
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
package/lib/service.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
var FX = require('./core');
|
|
2
|
+
var configureEndpoint = require('./endpoint');
|
|
3
|
+
|
|
4
|
+
//var Api = require('./model/api');
|
|
5
|
+
var path = require('path');
|
|
6
|
+
var inherit = FX.util.inherit;
|
|
7
|
+
var apiRoot = path.join(__dirname, '..', 'apis');
|
|
8
|
+
|
|
9
|
+
FX.Service = inherit({
|
|
10
|
+
|
|
11
|
+
constructor: function Service(config) {
|
|
12
|
+
|
|
13
|
+
if(!this.loadServiceClass) {
|
|
14
|
+
throw Error('Service must be constructed with `new\' operator');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var ServiceClass = this.loadServiceClass(config || {});
|
|
18
|
+
|
|
19
|
+
if (ServiceClass) {
|
|
20
|
+
var svc = new ServiceClass(config);
|
|
21
|
+
return svc;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
this.initialize(config);
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
initialize: function(config) {
|
|
28
|
+
var svcConfig = FX.config[this.identifier];
|
|
29
|
+
|
|
30
|
+
this.config = new FX.Config(FX.config);
|
|
31
|
+
|
|
32
|
+
if (svcConfig)
|
|
33
|
+
this.config.update(svcConfig, true);
|
|
34
|
+
|
|
35
|
+
if (config) {
|
|
36
|
+
this.config.update(config, true);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!this.config.endpoint)
|
|
40
|
+
configureEndpoint(this);
|
|
41
|
+
|
|
42
|
+
if(!this.config.region && !this.api.globalEndpoint)
|
|
43
|
+
throw new Error("No region specified")
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
loadServiceClass: function(serviceConfig) {
|
|
47
|
+
var config = serviceConfig;
|
|
48
|
+
|
|
49
|
+
if (!FX.util.isEmpty(this.api)) {
|
|
50
|
+
return null;
|
|
51
|
+
} else if (config.apiConfig) {
|
|
52
|
+
return FX.Service.defineServiceApi(this.constructor, config.apiConfig);
|
|
53
|
+
} else if (!this.constructor.services) {
|
|
54
|
+
return null;
|
|
55
|
+
} else {
|
|
56
|
+
config = new FX.Config(FX.config);
|
|
57
|
+
config.update(serviceConfig, true);
|
|
58
|
+
var version = config.apiVersions[this.constructor.identifier]
|
|
59
|
+
version = FX.config.apiVersion || version
|
|
60
|
+
version = config.apiVersion || version;
|
|
61
|
+
|
|
62
|
+
return this.getLatestServiceClass(version);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
getLatestServiceClass: function(version) {
|
|
67
|
+
version = this.getLatestServiceVersion(version);
|
|
68
|
+
|
|
69
|
+
// serviceApi does not exist for this version. create one
|
|
70
|
+
if(!this.constructor.services[version]) {
|
|
71
|
+
FX.Service.defineServiceApi(this.constructor, version);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return this.constructor.services[version];
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
getLatestServiceVersion: function(version) {
|
|
79
|
+
if (Object.hasOwnProperty(this.constructor.versions, version)) {
|
|
80
|
+
return version;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
var versions = this.constructor.versions
|
|
84
|
+
if(versions == undefined)
|
|
85
|
+
return
|
|
86
|
+
|
|
87
|
+
versions.sort();
|
|
88
|
+
for(var i = versions.length - 1; i >= 0; i--) {
|
|
89
|
+
if(versions[i] <= version) {
|
|
90
|
+
return versions[i];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
throw new Error('Could not find a version for service')
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
makeRequest: function(method, params, callback) {
|
|
99
|
+
if(typeof params == 'function') {
|
|
100
|
+
callback = params
|
|
101
|
+
params = {}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
var operation = this.api.operations[method]
|
|
105
|
+
var transform = operation.transform || {}
|
|
106
|
+
|
|
107
|
+
// Copy parameters bound to service
|
|
108
|
+
params = params || {};
|
|
109
|
+
if(this.config.params && operation.input) {
|
|
110
|
+
FX.util.each(this.config.params, function(key, value) {
|
|
111
|
+
if(operation.input.members && operation.input.members[key]) {
|
|
112
|
+
if (params[key] === undefined || params[key] === null) {
|
|
113
|
+
params[key] = value;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if(transform.input) {
|
|
120
|
+
this[transform.input.method]()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
var request = new FX.Request(this, method, operation, params)
|
|
124
|
+
if(callback) {
|
|
125
|
+
request.send(callback)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return request
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
FX.util.update(FX.Service, {
|
|
135
|
+
|
|
136
|
+
defineServiceApi: function(superclass, version) {
|
|
137
|
+
var svc = inherit(superclass, {
|
|
138
|
+
identifier: superclass.identifier,
|
|
139
|
+
apiVersion: version,
|
|
140
|
+
version: version
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
var api = FX.apiLoader(superclass.identifier, version);
|
|
145
|
+
svc.prototype.api = api;
|
|
146
|
+
superclass.services[version] = svc;
|
|
147
|
+
|
|
148
|
+
FX.Service.defineMethods(svc);
|
|
149
|
+
FX.config[superclass.identifier] = api.config || {}
|
|
150
|
+
|
|
151
|
+
return svc;
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
serviceFile: function(svcname, version) {
|
|
156
|
+
return path.join(apiRoot, svcname + '-' + version + '.json')
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
defineMethods: function(svc) {
|
|
160
|
+
FX.util.each(svc.prototype.api.operations, function iterator(method) {
|
|
161
|
+
var jsMethodName = FX.util.string.lowerFirst(method)
|
|
162
|
+
|
|
163
|
+
svc.prototype[jsMethodName] = function (params, callback) {
|
|
164
|
+
return this.makeRequest(method, params, callback)
|
|
165
|
+
};
|
|
166
|
+
})
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
defineService: function(name, versions) {
|
|
170
|
+
var svc = inherit(FX.Service, {})
|
|
171
|
+
svc.versions = versions
|
|
172
|
+
svc.services = {}
|
|
173
|
+
svc.identifier = name
|
|
174
|
+
|
|
175
|
+
return svc
|
|
176
|
+
},
|
|
177
|
+
// var util = require('util');
|
|
178
|
+
// console.log(util.inspect(svc, {showHidden: false, depth: null}));
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
})
|
|
182
|
+
|