rack-swagger 0.0.1 → 0.0.6
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.
- checksums.yaml +13 -5
- data/.gitignore +1 -1
- data/README.md +85 -1
- data/Rakefile +30 -0
- data/lib/rack/swagger.rb +35 -1
- data/lib/rack/swagger/asset_server.rb +22 -0
- data/lib/rack/swagger/index_page_server.rb +32 -0
- data/lib/rack/swagger/json_server.rb +30 -0
- data/lib/rack/swagger/rentpath/logo_small.png +0 -0
- data/lib/rack/swagger/rentpath/rentpath.diff +47 -0
- data/lib/rack/swagger/routes_to_models.rb +111 -0
- data/lib/rack/swagger/server_helpers.rb +55 -0
- data/lib/rack/swagger/sinatra_helpers.rb +78 -0
- data/lib/rack/swagger/version.rb +1 -1
- data/rack-swagger.gemspec +8 -2
- data/spec/lib/rack/swagger/asset_server_spec.rb +18 -0
- data/spec/lib/rack/swagger/index_page_server_spec.rb +21 -0
- data/spec/lib/rack/swagger/routes_to_models_spec.rb +81 -0
- data/spec/lib/rack/swagger/server_helpers_spec.rb +44 -0
- data/spec/lib/rack/swagger/sinatra_helpers_spec.rb +85 -0
- data/spec/spec_helper.rb +25 -0
- data/swagger-ui/dist/css/reset.css +125 -0
- data/swagger-ui/dist/css/screen.css +1224 -0
- data/swagger-ui/dist/css/screen.css.orig +1224 -0
- data/swagger-ui/dist/images/explorer_icons.png +0 -0
- data/swagger-ui/dist/images/logo_small.png +0 -0
- data/swagger-ui/dist/images/pet_store_api.png +0 -0
- data/swagger-ui/dist/images/throbber.gif +0 -0
- data/swagger-ui/dist/images/wordnik_api.png +0 -0
- data/swagger-ui/dist/index.html +105 -0
- data/swagger-ui/dist/index.html.orig +105 -0
- data/swagger-ui/dist/lib/backbone-min.js +38 -0
- data/swagger-ui/dist/lib/handlebars-1.0.0.js +2278 -0
- data/swagger-ui/dist/lib/highlight.7.3.pack.js +1 -0
- data/swagger-ui/dist/lib/jquery-1.8.0.min.js +2 -0
- data/swagger-ui/dist/lib/jquery.ba-bbq.min.js +18 -0
- data/swagger-ui/dist/lib/jquery.slideto.min.js +1 -0
- data/swagger-ui/dist/lib/jquery.wiggle.min.js +8 -0
- data/swagger-ui/dist/lib/shred.bundle.js +2765 -0
- data/swagger-ui/dist/lib/shred/content.js +193 -0
- data/swagger-ui/dist/lib/swagger-client.js +1477 -0
- data/swagger-ui/dist/lib/swagger-oauth.js +211 -0
- data/swagger-ui/dist/lib/swagger.js +1678 -0
- data/swagger-ui/dist/lib/underscore-min.js +32 -0
- data/swagger-ui/dist/o2c.html +15 -0
- data/swagger-ui/dist/swagger-ui.js +2477 -0
- data/swagger-ui/dist/swagger-ui.min.js +1 -0
- data/swagger-ui/master.tar.gz +0 -0
- data/swagger-ui/swagger-ui-v2.0.22.tar +0 -0
- data/swagger_ui_version.yml +4 -0
- data/templates/swagger_ui_version.yml +4 -0
- metadata +132 -12
@@ -0,0 +1,193 @@
|
|
1
|
+
|
2
|
+
// The purpose of the `Content` object is to abstract away the data conversions
|
3
|
+
// to and from raw content entities as strings. For example, you want to be able
|
4
|
+
// to pass in a Javascript object and have it be automatically converted into a
|
5
|
+
// JSON string if the `content-type` is set to a JSON-based media type.
|
6
|
+
// Conversely, you want to be able to transparently get back a Javascript object
|
7
|
+
// in the response if the `content-type` is a JSON-based media-type.
|
8
|
+
|
9
|
+
// One limitation of the current implementation is that it [assumes the `charset` is UTF-8](https://github.com/spire-io/shred/issues/5).
|
10
|
+
|
11
|
+
// The `Content` constructor takes an options object, which *must* have either a
|
12
|
+
// `body` or `data` property and *may* have a `type` property indicating the
|
13
|
+
// media type. If there is no `type` attribute, a default will be inferred.
|
14
|
+
var Content = function(options) {
|
15
|
+
this.body = options.body;
|
16
|
+
this.data = options.data;
|
17
|
+
this.type = options.type;
|
18
|
+
};
|
19
|
+
|
20
|
+
Content.prototype = {
|
21
|
+
// Treat `toString()` as asking for the `content.body`. That is, the raw content entity.
|
22
|
+
//
|
23
|
+
// toString: function() { return this.body; }
|
24
|
+
//
|
25
|
+
// Commented out, but I've forgotten why. :/
|
26
|
+
};
|
27
|
+
|
28
|
+
|
29
|
+
// `Content` objects have the following attributes:
|
30
|
+
Object.defineProperties(Content.prototype,{
|
31
|
+
|
32
|
+
// - **type**. Typically accessed as `content.type`, reflects the `content-type`
|
33
|
+
// header associated with the request or response. If not passed as an options
|
34
|
+
// to the constructor or set explicitly, it will infer the type the `data`
|
35
|
+
// attribute, if possible, and, failing that, will default to `text/plain`.
|
36
|
+
type: {
|
37
|
+
get: function() {
|
38
|
+
if (this._type) {
|
39
|
+
return this._type;
|
40
|
+
} else {
|
41
|
+
if (this._data) {
|
42
|
+
switch(typeof this._data) {
|
43
|
+
case "string": return "text/plain";
|
44
|
+
case "object": return "application/json";
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
return "text/plain";
|
49
|
+
},
|
50
|
+
set: function(value) {
|
51
|
+
this._type = value;
|
52
|
+
return this;
|
53
|
+
},
|
54
|
+
enumerable: true
|
55
|
+
},
|
56
|
+
|
57
|
+
// - **data**. Typically accessed as `content.data`, reflects the content entity
|
58
|
+
// converted into Javascript data. This can be a string, if the `type` is, say,
|
59
|
+
// `text/plain`, but can also be a Javascript object. The conversion applied is
|
60
|
+
// based on the `processor` attribute. The `data` attribute can also be set
|
61
|
+
// directly, in which case the conversion will be done the other way, to infer
|
62
|
+
// the `body` attribute.
|
63
|
+
data: {
|
64
|
+
get: function() {
|
65
|
+
if (this._body) {
|
66
|
+
return this.processor.parser(this._body);
|
67
|
+
} else {
|
68
|
+
return this._data;
|
69
|
+
}
|
70
|
+
},
|
71
|
+
set: function(data) {
|
72
|
+
if (this._body&&data) Errors.setDataWithBody(this);
|
73
|
+
this._data = data;
|
74
|
+
return this;
|
75
|
+
},
|
76
|
+
enumerable: true
|
77
|
+
},
|
78
|
+
|
79
|
+
// - **body**. Typically accessed as `content.body`, reflects the content entity
|
80
|
+
// as a UTF-8 string. It is the mirror of the `data` attribute. If you set the
|
81
|
+
// `data` attribute, the `body` attribute will be inferred and vice-versa. If
|
82
|
+
// you attempt to set both, an exception is raised.
|
83
|
+
body: {
|
84
|
+
get: function() {
|
85
|
+
if (this._data) {
|
86
|
+
return this.processor.stringify(this._data);
|
87
|
+
} else {
|
88
|
+
return this._body.toString();
|
89
|
+
}
|
90
|
+
},
|
91
|
+
set: function(body) {
|
92
|
+
if (this._data&&body) Errors.setBodyWithData(this);
|
93
|
+
this._body = body;
|
94
|
+
return this;
|
95
|
+
},
|
96
|
+
enumerable: true
|
97
|
+
},
|
98
|
+
|
99
|
+
// - **processor**. The functions that will be used to convert to/from `data` and
|
100
|
+
// `body` attributes. You can add processors. The two that are built-in are for
|
101
|
+
// `text/plain`, which is basically an identity transformation and
|
102
|
+
// `application/json` and other JSON-based media types (including custom media
|
103
|
+
// types with `+json`). You can add your own processors. See below.
|
104
|
+
processor: {
|
105
|
+
get: function() {
|
106
|
+
var processor = Content.processors[this.type];
|
107
|
+
if (processor) {
|
108
|
+
return processor;
|
109
|
+
} else {
|
110
|
+
// Return the first processor that matches any part of the
|
111
|
+
// content type. ex: application/vnd.foobar.baz+json will match json.
|
112
|
+
var main = this.type.split(";")[0];
|
113
|
+
var parts = main.split(/\+|\//);
|
114
|
+
for (var i=0, l=parts.length; i < l; i++) {
|
115
|
+
processor = Content.processors[parts[i]]
|
116
|
+
}
|
117
|
+
return processor || {parser:identity,stringify:toString};
|
118
|
+
}
|
119
|
+
},
|
120
|
+
enumerable: true
|
121
|
+
},
|
122
|
+
|
123
|
+
// - **length**. Typically accessed as `content.length`, returns the length in
|
124
|
+
// bytes of the raw content entity.
|
125
|
+
length: {
|
126
|
+
get: function() {
|
127
|
+
if (typeof Buffer !== 'undefined') {
|
128
|
+
return Buffer.byteLength(this.body);
|
129
|
+
}
|
130
|
+
return this.body.length;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
});
|
134
|
+
|
135
|
+
Content.processors = {};
|
136
|
+
|
137
|
+
// The `registerProcessor` function allows you to add your own processors to
|
138
|
+
// convert content entities. Each processor consists of a Javascript object with
|
139
|
+
// two properties:
|
140
|
+
// - **parser**. The function used to parse a raw content entity and convert it
|
141
|
+
// into a Javascript data type.
|
142
|
+
// - **stringify**. The function used to convert a Javascript data type into a
|
143
|
+
// raw content entity.
|
144
|
+
Content.registerProcessor = function(types,processor) {
|
145
|
+
|
146
|
+
// You can pass an array of types that will trigger this processor, or just one.
|
147
|
+
// We determine the array via duck-typing here.
|
148
|
+
if (types.forEach) {
|
149
|
+
types.forEach(function(type) {
|
150
|
+
Content.processors[type] = processor;
|
151
|
+
});
|
152
|
+
} else {
|
153
|
+
// If you didn't pass an array, we just use what you pass in.
|
154
|
+
Content.processors[types] = processor;
|
155
|
+
}
|
156
|
+
};
|
157
|
+
|
158
|
+
// Register the identity processor, which is used for text-based media types.
|
159
|
+
var identity = function(x) { return x; }
|
160
|
+
, toString = function(x) { return x.toString(); }
|
161
|
+
Content.registerProcessor(
|
162
|
+
["text/html","text/plain","text"],
|
163
|
+
{ parser: identity, stringify: toString });
|
164
|
+
|
165
|
+
// Register the JSON processor, which is used for JSON-based media types.
|
166
|
+
Content.registerProcessor(
|
167
|
+
["application/json; charset=utf-8","application/json","json"],
|
168
|
+
{
|
169
|
+
parser: function(string) {
|
170
|
+
return JSON.parse(string);
|
171
|
+
},
|
172
|
+
stringify: function(data) {
|
173
|
+
return JSON.stringify(data); }});
|
174
|
+
|
175
|
+
var qs = require('querystring');
|
176
|
+
// Register the post processor, which is used for JSON-based media types.
|
177
|
+
Content.registerProcessor(
|
178
|
+
["application/x-www-form-urlencoded"],
|
179
|
+
{ parser : qs.parse, stringify : qs.stringify });
|
180
|
+
|
181
|
+
// Error functions are defined separately here in an attempt to make the code
|
182
|
+
// easier to read.
|
183
|
+
var Errors = {
|
184
|
+
setDataWithBody: function(object) {
|
185
|
+
throw new Error("Attempt to set data attribute of a content object " +
|
186
|
+
"when the body attributes was already set.");
|
187
|
+
},
|
188
|
+
setBodyWithData: function(object) {
|
189
|
+
throw new Error("Attempt to set body attribute of a content object " +
|
190
|
+
"when the data attributes was already set.");
|
191
|
+
}
|
192
|
+
}
|
193
|
+
module.exports = Content;
|
@@ -0,0 +1,1477 @@
|
|
1
|
+
// swagger-client.js
|
2
|
+
// version 2.1.0-alpha.2
|
3
|
+
/**
|
4
|
+
* Array Model
|
5
|
+
**/
|
6
|
+
var ArrayModel = function(definition) {
|
7
|
+
this.name = "name";
|
8
|
+
this.definition = definition || {};
|
9
|
+
this.properties = [];
|
10
|
+
this.type;
|
11
|
+
this.ref;
|
12
|
+
|
13
|
+
var requiredFields = definition.enum || [];
|
14
|
+
var items = definition.items;
|
15
|
+
if(items) {
|
16
|
+
var type = items.type;
|
17
|
+
if(items.type) {
|
18
|
+
this.type = typeFromJsonSchema(type.type, type.format);
|
19
|
+
}
|
20
|
+
else {
|
21
|
+
this.ref = items['$ref'];
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
ArrayModel.prototype.createJSONSample = function(modelsToIgnore) {
|
27
|
+
var result;
|
28
|
+
modelsToIgnore = (modelsToIgnore||{})
|
29
|
+
if(this.type) {
|
30
|
+
result = type;
|
31
|
+
}
|
32
|
+
else if (this.ref) {
|
33
|
+
var name = simpleRef(this.ref);
|
34
|
+
result = models[name].createJSONSample();
|
35
|
+
}
|
36
|
+
return [ result ];
|
37
|
+
};
|
38
|
+
|
39
|
+
ArrayModel.prototype.getSampleValue = function(modelsToIgnore) {
|
40
|
+
var result;
|
41
|
+
modelsToIgnore = (modelsToIgnore || {})
|
42
|
+
if(this.type) {
|
43
|
+
result = type;
|
44
|
+
}
|
45
|
+
else if (this.ref) {
|
46
|
+
var name = simpleRef(this.ref);
|
47
|
+
result = models[name].getSampleValue(modelsToIgnore);
|
48
|
+
}
|
49
|
+
return [ result ];
|
50
|
+
}
|
51
|
+
|
52
|
+
ArrayModel.prototype.getMockSignature = function(modelsToIgnore) {
|
53
|
+
var propertiesStr = [];
|
54
|
+
|
55
|
+
if(this.ref) {
|
56
|
+
return models[simpleRef(this.ref)].getMockSignature();
|
57
|
+
}
|
58
|
+
};
|
59
|
+
|
60
|
+
/**
|
61
|
+
* SwaggerAuthorizations applys the correct authorization to an operation being executed
|
62
|
+
*/
|
63
|
+
var SwaggerAuthorizations = function() {
|
64
|
+
this.authz = {};
|
65
|
+
};
|
66
|
+
|
67
|
+
SwaggerAuthorizations.prototype.add = function(name, auth) {
|
68
|
+
this.authz[name] = auth;
|
69
|
+
return auth;
|
70
|
+
};
|
71
|
+
|
72
|
+
SwaggerAuthorizations.prototype.remove = function(name) {
|
73
|
+
return delete this.authz[name];
|
74
|
+
};
|
75
|
+
|
76
|
+
SwaggerAuthorizations.prototype.apply = function(obj, authorizations) {
|
77
|
+
var status = null;
|
78
|
+
var key;
|
79
|
+
|
80
|
+
// if the "authorizations" key is undefined, or has an empty array, add all keys
|
81
|
+
if(typeof authorizations === 'undefined' || Object.keys(authorizations).length == 0) {
|
82
|
+
for (key in this.authz) {
|
83
|
+
value = this.authz[key];
|
84
|
+
result = value.apply(obj, authorizations);
|
85
|
+
if (result === true)
|
86
|
+
status = true;
|
87
|
+
}
|
88
|
+
}
|
89
|
+
else {
|
90
|
+
for(name in authorizations) {
|
91
|
+
for (key in this.authz) {
|
92
|
+
if(key == name) {
|
93
|
+
value = this.authz[key];
|
94
|
+
result = value.apply(obj, authorizations);
|
95
|
+
if (result === true)
|
96
|
+
status = true;
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
return status;
|
103
|
+
};
|
104
|
+
|
105
|
+
/**
|
106
|
+
* ApiKeyAuthorization allows a query param or header to be injected
|
107
|
+
*/
|
108
|
+
var ApiKeyAuthorization = function(name, value, type) {
|
109
|
+
this.name = name;
|
110
|
+
this.value = value;
|
111
|
+
this.type = type;
|
112
|
+
};
|
113
|
+
|
114
|
+
ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
|
115
|
+
if (this.type === "query") {
|
116
|
+
if (obj.url.indexOf('?') > 0)
|
117
|
+
obj.url = obj.url + "&" + this.name + "=" + this.value;
|
118
|
+
else
|
119
|
+
obj.url = obj.url + "?" + this.name + "=" + this.value;
|
120
|
+
return true;
|
121
|
+
} else if (this.type === "header") {
|
122
|
+
obj.headers[this.name] = this.value;
|
123
|
+
return true;
|
124
|
+
}
|
125
|
+
};
|
126
|
+
|
127
|
+
var CookieAuthorization = function(cookie) {
|
128
|
+
this.cookie = cookie;
|
129
|
+
}
|
130
|
+
|
131
|
+
CookieAuthorization.prototype.apply = function(obj, authorizations) {
|
132
|
+
obj.cookieJar = obj.cookieJar || CookieJar();
|
133
|
+
obj.cookieJar.setCookie(this.cookie);
|
134
|
+
return true;
|
135
|
+
}
|
136
|
+
|
137
|
+
/**
|
138
|
+
* Password Authorization is a basic auth implementation
|
139
|
+
*/
|
140
|
+
var PasswordAuthorization = function(name, username, password) {
|
141
|
+
this.name = name;
|
142
|
+
this.username = username;
|
143
|
+
this.password = password;
|
144
|
+
this._btoa = null;
|
145
|
+
if (typeof window !== 'undefined')
|
146
|
+
this._btoa = btoa;
|
147
|
+
else
|
148
|
+
this._btoa = require("btoa");
|
149
|
+
};
|
150
|
+
|
151
|
+
PasswordAuthorization.prototype.apply = function(obj, authorizations) {
|
152
|
+
var base64encoder = this._btoa;
|
153
|
+
obj.headers["Authorization"] = "Basic " + base64encoder(this.username + ":" + this.password);
|
154
|
+
return true;
|
155
|
+
};var __bind = function(fn, me){
|
156
|
+
return function(){
|
157
|
+
return fn.apply(me, arguments);
|
158
|
+
};
|
159
|
+
};
|
160
|
+
|
161
|
+
fail = function(message) {
|
162
|
+
log(message);
|
163
|
+
}
|
164
|
+
|
165
|
+
log = function(){
|
166
|
+
log.history = log.history || [];
|
167
|
+
log.history.push(arguments);
|
168
|
+
if(this.console){
|
169
|
+
console.log( Array.prototype.slice.call(arguments)[0] );
|
170
|
+
}
|
171
|
+
};
|
172
|
+
|
173
|
+
if (!Array.prototype.indexOf) {
|
174
|
+
Array.prototype.indexOf = function(obj, start) {
|
175
|
+
for (var i = (start || 0), j = this.length; i < j; i++) {
|
176
|
+
if (this[i] === obj) { return i; }
|
177
|
+
}
|
178
|
+
return -1;
|
179
|
+
}
|
180
|
+
}
|
181
|
+
|
182
|
+
if (!('filter' in Array.prototype)) {
|
183
|
+
Array.prototype.filter= function(filter, that /*opt*/) {
|
184
|
+
var other= [], v;
|
185
|
+
for (var i=0, n= this.length; i<n; i++)
|
186
|
+
if (i in this && filter.call(that, v= this[i], i, this))
|
187
|
+
other.push(v);
|
188
|
+
return other;
|
189
|
+
};
|
190
|
+
}
|
191
|
+
|
192
|
+
if (!('map' in Array.prototype)) {
|
193
|
+
Array.prototype.map= function(mapper, that /*opt*/) {
|
194
|
+
var other= new Array(this.length);
|
195
|
+
for (var i= 0, n= this.length; i<n; i++)
|
196
|
+
if (i in this)
|
197
|
+
other[i]= mapper.call(that, this[i], i, this);
|
198
|
+
return other;
|
199
|
+
};
|
200
|
+
}
|
201
|
+
|
202
|
+
Object.keys = Object.keys || (function () {
|
203
|
+
var hasOwnProperty = Object.prototype.hasOwnProperty,
|
204
|
+
hasDontEnumBug = !{toString:null}.propertyIsEnumerable("toString"),
|
205
|
+
DontEnums = [
|
206
|
+
'toString',
|
207
|
+
'toLocaleString',
|
208
|
+
'valueOf',
|
209
|
+
'hasOwnProperty',
|
210
|
+
'isPrototypeOf',
|
211
|
+
'propertyIsEnumerable',
|
212
|
+
'constructor'
|
213
|
+
],
|
214
|
+
DontEnumsLength = DontEnums.length;
|
215
|
+
|
216
|
+
return function (o) {
|
217
|
+
if (typeof o != "object" && typeof o != "function" || o === null)
|
218
|
+
throw new TypeError("Object.keys called on a non-object");
|
219
|
+
|
220
|
+
var result = [];
|
221
|
+
for (var name in o) {
|
222
|
+
if (hasOwnProperty.call(o, name))
|
223
|
+
result.push(name);
|
224
|
+
}
|
225
|
+
|
226
|
+
if (hasDontEnumBug) {
|
227
|
+
for (var i = 0; i < DontEnumsLength; i++) {
|
228
|
+
if (hasOwnProperty.call(o, DontEnums[i]))
|
229
|
+
result.push(DontEnums[i]);
|
230
|
+
}
|
231
|
+
}
|
232
|
+
|
233
|
+
return result;
|
234
|
+
};
|
235
|
+
})();
|
236
|
+
/**
|
237
|
+
* PrimitiveModel
|
238
|
+
**/
|
239
|
+
var PrimitiveModel = function(definition) {
|
240
|
+
this.name = "name";
|
241
|
+
this.definition = definition || {};
|
242
|
+
this.properties = [];
|
243
|
+
this.type;
|
244
|
+
|
245
|
+
var requiredFields = definition.enum || [];
|
246
|
+
this.type = typeFromJsonSchema(definition.type, definition.format);
|
247
|
+
}
|
248
|
+
|
249
|
+
PrimitiveModel.prototype.createJSONSample = function(modelsToIgnore) {
|
250
|
+
var result = this.type;
|
251
|
+
return result;
|
252
|
+
};
|
253
|
+
|
254
|
+
PrimitiveModel.prototype.getSampleValue = function() {
|
255
|
+
var result = this.type;
|
256
|
+
return null;
|
257
|
+
}
|
258
|
+
|
259
|
+
PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
|
260
|
+
var propertiesStr = [];
|
261
|
+
var i;
|
262
|
+
for (i = 0; i < this.properties.length; i++) {
|
263
|
+
var prop = this.properties[i];
|
264
|
+
propertiesStr.push(prop.toString());
|
265
|
+
}
|
266
|
+
|
267
|
+
var strong = '<span class="strong">';
|
268
|
+
var stronger = '<span class="stronger">';
|
269
|
+
var strongClose = '</span>';
|
270
|
+
var classOpen = strong + this.name + ' {' + strongClose;
|
271
|
+
var classClose = strong + '}' + strongClose;
|
272
|
+
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
|
273
|
+
|
274
|
+
if (!modelsToIgnore)
|
275
|
+
modelsToIgnore = {};
|
276
|
+
modelsToIgnore[this.name] = this;
|
277
|
+
var i;
|
278
|
+
for (i = 0; i < this.properties.length; i++) {
|
279
|
+
var prop = this.properties[i];
|
280
|
+
var ref = prop['$ref'];
|
281
|
+
var model = models[ref];
|
282
|
+
if (model && typeof modelsToIgnore[ref] === 'undefined') {
|
283
|
+
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
|
284
|
+
}
|
285
|
+
}
|
286
|
+
return returnVal;
|
287
|
+
};var SwaggerClient = function(url, options) {
|
288
|
+
this.isBuilt = false;
|
289
|
+
this.url = null;
|
290
|
+
this.debug = false;
|
291
|
+
this.basePath = null;
|
292
|
+
this.authorizations = null;
|
293
|
+
this.authorizationScheme = null;
|
294
|
+
this.isValid = false;
|
295
|
+
this.info = null;
|
296
|
+
this.useJQuery = false;
|
297
|
+
this.models = models;
|
298
|
+
|
299
|
+
options = (options||{});
|
300
|
+
if (url)
|
301
|
+
if (url.url) options = url;
|
302
|
+
else this.url = url;
|
303
|
+
else options = url;
|
304
|
+
|
305
|
+
if (options.url != null)
|
306
|
+
this.url = options.url;
|
307
|
+
|
308
|
+
if (options.success != null)
|
309
|
+
this.success = options.success;
|
310
|
+
|
311
|
+
if (typeof options.useJQuery === 'boolean')
|
312
|
+
this.useJQuery = options.useJQuery;
|
313
|
+
|
314
|
+
this.failure = options.failure != null ? options.failure : function() {};
|
315
|
+
this.progress = options.progress != null ? options.progress : function() {};
|
316
|
+
this.spec = options.spec;
|
317
|
+
|
318
|
+
if (options.success != null)
|
319
|
+
this.build();
|
320
|
+
}
|
321
|
+
|
322
|
+
SwaggerClient.prototype.build = function() {
|
323
|
+
var self = this;
|
324
|
+
this.progress('fetching resource list: ' + this.url);
|
325
|
+
var obj = {
|
326
|
+
useJQuery: this.useJQuery,
|
327
|
+
url: this.url,
|
328
|
+
method: "get",
|
329
|
+
headers: {
|
330
|
+
accept: "application/json, */*"
|
331
|
+
},
|
332
|
+
on: {
|
333
|
+
error: function(response) {
|
334
|
+
if (self.url.substring(0, 4) !== 'http')
|
335
|
+
return self.fail('Please specify the protocol for ' + self.url);
|
336
|
+
else if (response.status === 0)
|
337
|
+
return self.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.');
|
338
|
+
else if (response.status === 404)
|
339
|
+
return self.fail('Can\'t read swagger JSON from ' + self.url);
|
340
|
+
else
|
341
|
+
return self.fail(response.status + ' : ' + response.statusText + ' ' + self.url);
|
342
|
+
},
|
343
|
+
response: function(resp) {
|
344
|
+
var responseObj = resp.obj || JSON.parse(resp.data);
|
345
|
+
self.swaggerVersion = responseObj.swaggerVersion;
|
346
|
+
|
347
|
+
if(responseObj.swagger && parseInt(responseObj.swagger) === 2) {
|
348
|
+
self.swaggerVersion = responseObj.swagger;
|
349
|
+
self.buildFromSpec(responseObj);
|
350
|
+
self.isValid = true;
|
351
|
+
}
|
352
|
+
else {
|
353
|
+
self.isValid = false;
|
354
|
+
self.failure();
|
355
|
+
}
|
356
|
+
}
|
357
|
+
}
|
358
|
+
};
|
359
|
+
if(this.spec) {
|
360
|
+
var self = this;
|
361
|
+
setTimeout(function() { self.buildFromSpec(self.spec); }, 10);
|
362
|
+
}
|
363
|
+
else {
|
364
|
+
var e = (typeof window !== 'undefined' ? window : exports);
|
365
|
+
var status = e.authorizations.apply(obj);
|
366
|
+
new SwaggerHttp().execute(obj);
|
367
|
+
}
|
368
|
+
|
369
|
+
return this;
|
370
|
+
};
|
371
|
+
|
372
|
+
SwaggerClient.prototype.buildFromSpec = function(response) {
|
373
|
+
if(this.isBuilt) return this;
|
374
|
+
|
375
|
+
this.info = response.info || {};
|
376
|
+
this.title = response.title || '';
|
377
|
+
this.host = response.host || '';
|
378
|
+
this.schemes = response.schemes || [];
|
379
|
+
this.scheme;
|
380
|
+
this.basePath = response.basePath || '';
|
381
|
+
this.apis = {};
|
382
|
+
this.apisArray = [];
|
383
|
+
this.consumes = response.consumes;
|
384
|
+
this.produces = response.produces;
|
385
|
+
this.authSchemes = response.authorizations;
|
386
|
+
|
387
|
+
var location = this.parseUri(this.url);
|
388
|
+
if(typeof this.schemes === 'undefined' || this.schemes.length === 0) {
|
389
|
+
this.scheme = location.scheme;
|
390
|
+
}
|
391
|
+
else {
|
392
|
+
this.scheme = this.schemes[0];
|
393
|
+
}
|
394
|
+
|
395
|
+
if(typeof this.host === 'undefined' || this.host === '') {
|
396
|
+
this.host = location.host;
|
397
|
+
if (location.port) {
|
398
|
+
this.host = this.host + ':' + location.port;
|
399
|
+
}
|
400
|
+
}
|
401
|
+
|
402
|
+
this.definitions = response.definitions;
|
403
|
+
var key;
|
404
|
+
for(key in this.definitions) {
|
405
|
+
var model = new Model(key, this.definitions[key]);
|
406
|
+
if(model) {
|
407
|
+
models[key] = model;
|
408
|
+
}
|
409
|
+
}
|
410
|
+
|
411
|
+
// get paths, create functions for each operationId
|
412
|
+
var path;
|
413
|
+
var operations = [];
|
414
|
+
for(path in response.paths) {
|
415
|
+
if(typeof response.paths[path] === 'object') {
|
416
|
+
var httpMethod;
|
417
|
+
for(httpMethod in response.paths[path]) {
|
418
|
+
var operation = response.paths[path][httpMethod];
|
419
|
+
var tags = operation.tags;
|
420
|
+
if(typeof tags === 'undefined') {
|
421
|
+
operation.tags = [ 'default' ];
|
422
|
+
tags = operation.tags;
|
423
|
+
}
|
424
|
+
var operationId = this.idFromOp(path, httpMethod, operation);
|
425
|
+
var operationObject = new Operation (
|
426
|
+
this,
|
427
|
+
operationId,
|
428
|
+
httpMethod,
|
429
|
+
path,
|
430
|
+
operation,
|
431
|
+
this.definitions
|
432
|
+
);
|
433
|
+
// bind this operation's execute command to the api
|
434
|
+
if(tags.length > 0) {
|
435
|
+
var i;
|
436
|
+
for(i = 0; i < tags.length; i++) {
|
437
|
+
var tag = this.tagFromLabel(tags[i]);
|
438
|
+
var operationGroup = this[tag];
|
439
|
+
if(typeof operationGroup === 'undefined') {
|
440
|
+
this[tag] = [];
|
441
|
+
operationGroup = this[tag];
|
442
|
+
operationGroup.label = tag;
|
443
|
+
operationGroup.apis = [];
|
444
|
+
this[tag].help = this.help.bind(operationGroup);
|
445
|
+
this.apisArray.push(new OperationGroup(tag, operationObject));
|
446
|
+
}
|
447
|
+
operationGroup[operationId] = operationObject.execute.bind(operationObject);
|
448
|
+
operationGroup[operationId].help = operationObject.help.bind(operationObject);
|
449
|
+
operationGroup.apis.push(operationObject);
|
450
|
+
|
451
|
+
// legacy UI feature
|
452
|
+
var j;
|
453
|
+
var api;
|
454
|
+
for(j = 0; j < this.apisArray.length; j++) {
|
455
|
+
if(this.apisArray[j].tag === tag) {
|
456
|
+
api = this.apisArray[j];
|
457
|
+
}
|
458
|
+
}
|
459
|
+
if(api) {
|
460
|
+
api.operationsArray.push(operationObject);
|
461
|
+
}
|
462
|
+
}
|
463
|
+
}
|
464
|
+
else {
|
465
|
+
log('no group to bind to');
|
466
|
+
}
|
467
|
+
}
|
468
|
+
}
|
469
|
+
}
|
470
|
+
this.isBuilt = true;
|
471
|
+
if (this.success)
|
472
|
+
this.success();
|
473
|
+
return this;
|
474
|
+
}
|
475
|
+
|
476
|
+
SwaggerClient.prototype.parseUri = function(uri) {
|
477
|
+
var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
|
478
|
+
var parts = urlParseRE.exec(uri);
|
479
|
+
return {
|
480
|
+
scheme: parts[4].replace(':',''),
|
481
|
+
host: parts[11],
|
482
|
+
port: parts[12],
|
483
|
+
path: parts[15]
|
484
|
+
};
|
485
|
+
}
|
486
|
+
|
487
|
+
SwaggerClient.prototype.help = function() {
|
488
|
+
var i;
|
489
|
+
log('operations for the "' + this.label + '" tag');
|
490
|
+
for(i = 0; i < this.apis.length; i++) {
|
491
|
+
var api = this.apis[i];
|
492
|
+
log(' * ' + api.nickname + ': ' + api.operation.summary);
|
493
|
+
}
|
494
|
+
}
|
495
|
+
|
496
|
+
SwaggerClient.prototype.tagFromLabel = function(label) {
|
497
|
+
return label;
|
498
|
+
}
|
499
|
+
|
500
|
+
SwaggerClient.prototype.idFromOp = function(path, httpMethod, op) {
|
501
|
+
if(typeof op.operationId !== 'undefined') {
|
502
|
+
return (op.operationId);
|
503
|
+
}
|
504
|
+
else {
|
505
|
+
return path.substring(1).replace(/\//g, "_").replace(/\{/g, "").replace(/\}/g, "") + "_" + httpMethod;
|
506
|
+
}
|
507
|
+
}
|
508
|
+
|
509
|
+
SwaggerClient.prototype.fail = function(message) {
|
510
|
+
this.failure(message);
|
511
|
+
throw message;
|
512
|
+
};
|
513
|
+
|
514
|
+
var OperationGroup = function(tag, operation) {
|
515
|
+
this.tag = tag;
|
516
|
+
this.path = tag;
|
517
|
+
this.name = tag;
|
518
|
+
this.operation = operation;
|
519
|
+
this.operationsArray = [];
|
520
|
+
|
521
|
+
this.description = operation.description || "";
|
522
|
+
}
|
523
|
+
|
524
|
+
var Operation = function(parent, operationId, httpMethod, path, args, definitions) {
|
525
|
+
var errors = [];
|
526
|
+
this.operation = args;
|
527
|
+
this.deprecated = args.deprecated;
|
528
|
+
this.consumes = args.consumes;
|
529
|
+
this.produces = args.produces;
|
530
|
+
this.parent = parent;
|
531
|
+
this.host = parent.host;
|
532
|
+
this.schemes = parent.schemes;
|
533
|
+
this.scheme = parent.scheme || 'http';
|
534
|
+
this.basePath = parent.basePath;
|
535
|
+
this.nickname = (operationId||errors.push('Operations must have a nickname.'));
|
536
|
+
this.method = (httpMethod||errors.push('Operation ' + operationId + ' is missing method.'));
|
537
|
+
this.path = (path||errors.push('Operation ' + nickname + ' is missing path.'));
|
538
|
+
this.parameters = args != null ? (args.parameters||[]) : {};
|
539
|
+
this.summary = args.summary || '';
|
540
|
+
this.responses = (args.responses||{});
|
541
|
+
this.type = null;
|
542
|
+
this.security = args.security;
|
543
|
+
this.description = args.description;
|
544
|
+
|
545
|
+
var i;
|
546
|
+
for(i = 0; i < this.parameters.length; i++) {
|
547
|
+
var param = this.parameters[i];
|
548
|
+
if(param.type === 'array') {
|
549
|
+
param.isList = true;
|
550
|
+
param.allowMultiple = true;
|
551
|
+
}
|
552
|
+
var innerType = this.getType(param);
|
553
|
+
if(innerType.toString().toLowerCase() === 'boolean') {
|
554
|
+
param.allowableValues = {};
|
555
|
+
param.isList = true;
|
556
|
+
param.enum = ["true", "false"];
|
557
|
+
}
|
558
|
+
if(typeof param.enum !== 'undefined') {
|
559
|
+
var id;
|
560
|
+
param.allowableValues = {};
|
561
|
+
param.allowableValues.values = [];
|
562
|
+
param.allowableValues.descriptiveValues = [];
|
563
|
+
for(id = 0; id < param.enum.length; id++) {
|
564
|
+
var value = param.enum[id];
|
565
|
+
var isDefault = (value === param.default) ? true : false;
|
566
|
+
param.allowableValues.values.push(value);
|
567
|
+
param.allowableValues.descriptiveValues.push({value : value, isDefault: isDefault});
|
568
|
+
}
|
569
|
+
}
|
570
|
+
if(param.type === 'array' && typeof param.allowableValues === 'undefined') {
|
571
|
+
// can't show as a list if no values to select from
|
572
|
+
delete param.isList;
|
573
|
+
delete param.allowMultiple;
|
574
|
+
}
|
575
|
+
param.signature = this.getSignature(innerType, models);
|
576
|
+
param.sampleJSON = this.getSampleJSON(innerType, models);
|
577
|
+
param.responseClassSignature = param.signature;
|
578
|
+
}
|
579
|
+
|
580
|
+
var response;
|
581
|
+
var model;
|
582
|
+
var responses = this.responses;
|
583
|
+
|
584
|
+
if(responses['200']) {
|
585
|
+
response = responses['200'];
|
586
|
+
defaultResponseCode = '200';
|
587
|
+
}
|
588
|
+
else if(responses['201']) {
|
589
|
+
response = responses['201'];
|
590
|
+
defaultResponseCode = '201';
|
591
|
+
}
|
592
|
+
else if(responses['202']) {
|
593
|
+
response = responses['202'];
|
594
|
+
defaultResponseCode = '202';
|
595
|
+
}
|
596
|
+
else if(responses['203']) {
|
597
|
+
response = responses['203'];
|
598
|
+
defaultResponseCode = '203';
|
599
|
+
}
|
600
|
+
else if(responses['204']) {
|
601
|
+
response = responses['204'];
|
602
|
+
defaultResponseCode = '204';
|
603
|
+
}
|
604
|
+
else if(responses['205']) {
|
605
|
+
response = responses['205'];
|
606
|
+
defaultResponseCode = '205';
|
607
|
+
}
|
608
|
+
else if(responses['206']) {
|
609
|
+
response = responses['206'];
|
610
|
+
defaultResponseCode = '206';
|
611
|
+
}
|
612
|
+
else if(responses['default']) {
|
613
|
+
response = responses['default'];
|
614
|
+
defaultResponseCode = 'default';
|
615
|
+
}
|
616
|
+
|
617
|
+
if(response && response.schema) {
|
618
|
+
var resolvedModel = this.resolveModel(response.schema, definitions);
|
619
|
+
if(resolvedModel) {
|
620
|
+
this.type = resolvedModel.name;
|
621
|
+
this.responseSampleJSON = JSON.stringify(resolvedModel.getSampleValue(), null, 2);
|
622
|
+
this.responseClassSignature = resolvedModel.getMockSignature();
|
623
|
+
delete responses[defaultResponseCode];
|
624
|
+
}
|
625
|
+
else {
|
626
|
+
this.type = response.schema.type;
|
627
|
+
}
|
628
|
+
}
|
629
|
+
|
630
|
+
if (errors.length > 0)
|
631
|
+
this.resource.api.fail(errors);
|
632
|
+
|
633
|
+
return this;
|
634
|
+
}
|
635
|
+
|
636
|
+
OperationGroup.prototype.sort = function(sorter) {
|
637
|
+
|
638
|
+
}
|
639
|
+
|
640
|
+
Operation.prototype.getType = function (param) {
|
641
|
+
var type = param.type;
|
642
|
+
var format = param.format;
|
643
|
+
var isArray = false;
|
644
|
+
var str;
|
645
|
+
if(type === 'integer' && format === 'int32')
|
646
|
+
str = 'integer';
|
647
|
+
else if(type === 'integer' && format === 'int64')
|
648
|
+
str = 'long';
|
649
|
+
else if(type === 'integer' && typeof format === 'undefined')
|
650
|
+
str = 'long';
|
651
|
+
else if(type === 'string' && format === 'date-time')
|
652
|
+
str = 'date-time';
|
653
|
+
else if(type === 'string' && format === 'date')
|
654
|
+
str = 'date';
|
655
|
+
else if(type === 'number' && format === 'float')
|
656
|
+
str = 'float';
|
657
|
+
else if(type === 'number' && format === 'double')
|
658
|
+
str = 'double';
|
659
|
+
else if(type === 'number' && typeof format === 'undefined')
|
660
|
+
str = 'double';
|
661
|
+
else if(type === 'boolean')
|
662
|
+
str = 'boolean';
|
663
|
+
else if(type === 'string')
|
664
|
+
str = 'string';
|
665
|
+
else if(type === 'array') {
|
666
|
+
isArray = true;
|
667
|
+
if(param.items)
|
668
|
+
str = this.getType(param.items);
|
669
|
+
}
|
670
|
+
if(param['$ref'])
|
671
|
+
str = param['$ref'];
|
672
|
+
|
673
|
+
var schema = param.schema;
|
674
|
+
if(schema) {
|
675
|
+
var ref = schema['$ref'];
|
676
|
+
if(ref) {
|
677
|
+
ref = simpleRef(ref);
|
678
|
+
if(isArray)
|
679
|
+
return [ ref ];
|
680
|
+
else
|
681
|
+
return ref;
|
682
|
+
}
|
683
|
+
else
|
684
|
+
return this.getType(schema);
|
685
|
+
}
|
686
|
+
if(isArray)
|
687
|
+
return [ str ];
|
688
|
+
else
|
689
|
+
return str;
|
690
|
+
}
|
691
|
+
|
692
|
+
Operation.prototype.resolveModel = function (schema, definitions) {
|
693
|
+
if(typeof schema['$ref'] !== 'undefined') {
|
694
|
+
var ref = schema['$ref'];
|
695
|
+
if(ref.indexOf('#/definitions/') == 0)
|
696
|
+
ref = ref.substring('#/definitions/'.length);
|
697
|
+
if(definitions[ref])
|
698
|
+
return new Model(ref, definitions[ref]);
|
699
|
+
}
|
700
|
+
if(schema.type === 'array')
|
701
|
+
return new ArrayModel(schema);
|
702
|
+
else
|
703
|
+
return null;
|
704
|
+
}
|
705
|
+
|
706
|
+
Operation.prototype.help = function() {
|
707
|
+
log(this.nickname + ': ' + this.operation.summary);
|
708
|
+
for(var i = 0; i < this.parameters.length; i++) {
|
709
|
+
var param = this.parameters[i];
|
710
|
+
log(' * ' + param.name + ': ' + param.description);
|
711
|
+
}
|
712
|
+
}
|
713
|
+
|
714
|
+
Operation.prototype.getSignature = function(type, models) {
|
715
|
+
var isPrimitive, listType;
|
716
|
+
|
717
|
+
if(type instanceof Array) {
|
718
|
+
listType = true;
|
719
|
+
type = type[0];
|
720
|
+
}
|
721
|
+
|
722
|
+
if(type === 'string')
|
723
|
+
isPrimitive = true
|
724
|
+
else
|
725
|
+
isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true;
|
726
|
+
if (isPrimitive) {
|
727
|
+
return type;
|
728
|
+
} else {
|
729
|
+
if (listType != null)
|
730
|
+
return models[type].getMockSignature();
|
731
|
+
else
|
732
|
+
return models[type].getMockSignature();
|
733
|
+
}
|
734
|
+
};
|
735
|
+
|
736
|
+
/**
|
737
|
+
* gets sample response for a single operation
|
738
|
+
**/
|
739
|
+
Operation.prototype.getSampleJSON = function(type, models) {
|
740
|
+
var isPrimitive, listType, sampleJson;
|
741
|
+
|
742
|
+
listType = (type instanceof Array);
|
743
|
+
isPrimitive = (models[type] != null) ? false : true;
|
744
|
+
sampleJson = isPrimitive ? void 0 : models[type].createJSONSample();
|
745
|
+
|
746
|
+
if (sampleJson) {
|
747
|
+
sampleJson = listType ? [sampleJson] : sampleJson;
|
748
|
+
if(typeof sampleJson == 'string')
|
749
|
+
return sampleJson;
|
750
|
+
else if(typeof sampleJson === 'object') {
|
751
|
+
var t = sampleJson;
|
752
|
+
if(sampleJson instanceof Array && sampleJson.length > 0) {
|
753
|
+
t = sampleJson[0];
|
754
|
+
}
|
755
|
+
if(t.nodeName) {
|
756
|
+
var xmlString = new XMLSerializer().serializeToString(t);
|
757
|
+
return this.formatXml(xmlString);
|
758
|
+
}
|
759
|
+
else
|
760
|
+
return JSON.stringify(sampleJson, null, 2);
|
761
|
+
}
|
762
|
+
else
|
763
|
+
return sampleJson;
|
764
|
+
}
|
765
|
+
};
|
766
|
+
|
767
|
+
/**
|
768
|
+
* legacy binding
|
769
|
+
**/
|
770
|
+
Operation.prototype["do"] = function(args, opts, callback, error, parent) {
|
771
|
+
return this.execute(args, opts, callback, error, parent);
|
772
|
+
}
|
773
|
+
|
774
|
+
/**
|
775
|
+
* executes an operation
|
776
|
+
**/
|
777
|
+
Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
|
778
|
+
var args = (arg1||{});
|
779
|
+
var opts = {}, success, error;
|
780
|
+
if(typeof arg2 === 'object') {
|
781
|
+
opts = arg2;
|
782
|
+
success = arg3;
|
783
|
+
error = arg4;
|
784
|
+
}
|
785
|
+
if(typeof arg2 === 'function') {
|
786
|
+
success = arg2;
|
787
|
+
error = arg3;
|
788
|
+
}
|
789
|
+
|
790
|
+
var formParams = {};
|
791
|
+
var headers = {};
|
792
|
+
var requestUrl = this.path;
|
793
|
+
|
794
|
+
success = (success||log)
|
795
|
+
error = (error||log)
|
796
|
+
|
797
|
+
var requiredParams = [];
|
798
|
+
var missingParams = [];
|
799
|
+
// check required params, track the ones that are missing
|
800
|
+
var i;
|
801
|
+
for(i = 0; i < this.parameters.length; i++) {
|
802
|
+
var param = this.parameters[i];
|
803
|
+
if(param.required === true) {
|
804
|
+
requiredParams.push(param.name);
|
805
|
+
if(typeof args[param.name] === 'undefined')
|
806
|
+
missingParams = param.name;
|
807
|
+
}
|
808
|
+
}
|
809
|
+
|
810
|
+
if(missingParams.length > 0) {
|
811
|
+
var message = 'missing required params: ' + missingParams;
|
812
|
+
fail(message);
|
813
|
+
return;
|
814
|
+
}
|
815
|
+
|
816
|
+
// set content type negotiation
|
817
|
+
var consumes = this.consumes || this.parent.consumes || [ 'application/json' ];
|
818
|
+
var produces = this.produces || this.parent.produces || [ 'application/json' ];
|
819
|
+
|
820
|
+
headers = this.setContentTypes(args, opts);
|
821
|
+
|
822
|
+
// grab params from the args, build the querystring along the way
|
823
|
+
var querystring = "";
|
824
|
+
for(var i = 0; i < this.parameters.length; i++) {
|
825
|
+
var param = this.parameters[i];
|
826
|
+
if(typeof args[param.name] !== 'undefined') {
|
827
|
+
if(param.in === 'path') {
|
828
|
+
var reg = new RegExp('\{' + param.name + '[^\}]*\}', 'gi');
|
829
|
+
requestUrl = requestUrl.replace(reg, this.encodePathParam(args[param.name]));
|
830
|
+
}
|
831
|
+
else if (param.in === 'query') {
|
832
|
+
if(querystring === '')
|
833
|
+
querystring += '?';
|
834
|
+
else
|
835
|
+
querystring += '&';
|
836
|
+
if(typeof param.collectionFormat !== 'undefined') {
|
837
|
+
var qp = args[param.name];
|
838
|
+
if(Array.isArray(qp))
|
839
|
+
querystring += this.encodeCollection(param.collectionFormat, param.name, qp);
|
840
|
+
else
|
841
|
+
querystring += this.encodeQueryParam(param.name) + '=' + this.encodeQueryParam(args[param.name]);
|
842
|
+
}
|
843
|
+
else
|
844
|
+
querystring += this.encodeQueryParam(param.name) + '=' + this.encodeQueryParam(args[param.name]);
|
845
|
+
}
|
846
|
+
else if (param.in === 'header')
|
847
|
+
headers[param.name] = args[param.name];
|
848
|
+
else if (param.in === 'formData')
|
849
|
+
formParams[param.name] = args[param.name];
|
850
|
+
else if (param.in === 'body')
|
851
|
+
args.body = args[param.name];
|
852
|
+
}
|
853
|
+
}
|
854
|
+
// handle form params
|
855
|
+
if(headers['Content-Type'] === 'application/x-www-form-urlencoded') {
|
856
|
+
var encoded = "";
|
857
|
+
var key;
|
858
|
+
for(key in formParams) {
|
859
|
+
value = formParams[key];
|
860
|
+
if(typeof value !== 'undefined'){
|
861
|
+
if(encoded !== "")
|
862
|
+
encoded += "&";
|
863
|
+
encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
|
864
|
+
}
|
865
|
+
}
|
866
|
+
// todo append?
|
867
|
+
args.body = encoded;
|
868
|
+
}
|
869
|
+
var url = this.scheme + '://' + this.host + this.basePath + requestUrl + querystring;
|
870
|
+
|
871
|
+
var obj = {
|
872
|
+
url: url,
|
873
|
+
method: this.method,
|
874
|
+
body: args.body,
|
875
|
+
useJQuery: this.useJQuery,
|
876
|
+
headers: headers,
|
877
|
+
on: {
|
878
|
+
response: function(response) {
|
879
|
+
return success(response, parent);
|
880
|
+
},
|
881
|
+
error: function(response) {
|
882
|
+
return error(response, parent);
|
883
|
+
}
|
884
|
+
}
|
885
|
+
};
|
886
|
+
var status = e.authorizations.apply(obj, this.operation.security);
|
887
|
+
new SwaggerHttp().execute(obj);
|
888
|
+
}
|
889
|
+
|
890
|
+
Operation.prototype.setContentTypes = function(args, opts) {
|
891
|
+
// default type
|
892
|
+
var accepts = 'application/json';
|
893
|
+
var consumes = 'application/json';
|
894
|
+
|
895
|
+
var allDefinedParams = this.parameters;
|
896
|
+
var definedFormParams = [];
|
897
|
+
var definedFileParams = [];
|
898
|
+
var body = args.body;
|
899
|
+
var headers = {};
|
900
|
+
|
901
|
+
// get params from the operation and set them in definedFileParams, definedFormParams, headers
|
902
|
+
var i;
|
903
|
+
for(i = 0; i < allDefinedParams.length; i++) {
|
904
|
+
var param = allDefinedParams[i];
|
905
|
+
if(param.in === 'formData')
|
906
|
+
definedFormParams.push(param);
|
907
|
+
else if(param.in === 'file')
|
908
|
+
definedFileParams.push(param);
|
909
|
+
else if(param.in === 'header' && this.headers) {
|
910
|
+
var key = param.name;
|
911
|
+
var headerValue = this.headers[param.name];
|
912
|
+
if(typeof this.headers[param.name] !== 'undefined')
|
913
|
+
headers[key] = headerValue;
|
914
|
+
}
|
915
|
+
}
|
916
|
+
|
917
|
+
// if there's a body, need to set the accepts header via requestContentType
|
918
|
+
if (body && (this.type === 'post' || this.type === 'put' || this.type === 'patch' || this.type === 'delete')) {
|
919
|
+
if (opts.requestContentType)
|
920
|
+
consumes = opts.requestContentType;
|
921
|
+
} else {
|
922
|
+
// if any form params, content type must be set
|
923
|
+
if(definedFormParams.length > 0) {
|
924
|
+
if(definedFileParams.length > 0)
|
925
|
+
consumes = 'multipart/form-data';
|
926
|
+
else
|
927
|
+
consumes = 'application/x-www-form-urlencoded';
|
928
|
+
}
|
929
|
+
else if (this.type == 'DELETE')
|
930
|
+
body = '{}';
|
931
|
+
else if (this.type != 'DELETE')
|
932
|
+
accepts = null;
|
933
|
+
}
|
934
|
+
|
935
|
+
if (consumes && this.consumes) {
|
936
|
+
if (this.consumes.indexOf(consumes) === -1) {
|
937
|
+
log('server doesn\'t consume ' + consumes + ', try ' + JSON.stringify(this.consumes));
|
938
|
+
consumes = this.operation.consumes[0];
|
939
|
+
}
|
940
|
+
}
|
941
|
+
|
942
|
+
if (opts.responseContentType) {
|
943
|
+
accepts = opts.responseContentType;
|
944
|
+
} else {
|
945
|
+
accepts = 'application/json';
|
946
|
+
}
|
947
|
+
if (accepts && this.produces) {
|
948
|
+
if (this.produces.indexOf(accepts) === -1) {
|
949
|
+
log('server can\'t produce ' + accepts);
|
950
|
+
accepts = this.produces[0];
|
951
|
+
}
|
952
|
+
}
|
953
|
+
|
954
|
+
if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded'))
|
955
|
+
headers['Content-Type'] = consumes;
|
956
|
+
if (accepts)
|
957
|
+
headers['Accept'] = accepts;
|
958
|
+
return headers;
|
959
|
+
}
|
960
|
+
|
961
|
+
Operation.prototype.encodeCollection = function(type, name, value) {
|
962
|
+
var encoded = '';
|
963
|
+
var i;
|
964
|
+
if(type === 'default' || type === 'multi') {
|
965
|
+
for(i = 0; i < value.length; i++) {
|
966
|
+
if(i > 0) encoded += '&'
|
967
|
+
encoded += this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
|
968
|
+
}
|
969
|
+
}
|
970
|
+
else {
|
971
|
+
var separator = '';
|
972
|
+
if(type === 'csv')
|
973
|
+
separator = ',';
|
974
|
+
else if(type === 'ssv')
|
975
|
+
separator = '%20';
|
976
|
+
else if(type === 'tsv')
|
977
|
+
separator = '\\t';
|
978
|
+
else if(type === 'pipes')
|
979
|
+
separator = '|';
|
980
|
+
if(separator !== '') {
|
981
|
+
for(i = 0; i < value.length; i++) {
|
982
|
+
if(i == 0)
|
983
|
+
encoded = this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
|
984
|
+
else
|
985
|
+
encoded += separator + this.encodeQueryParam(value[i]);
|
986
|
+
}
|
987
|
+
}
|
988
|
+
}
|
989
|
+
// TODO: support the different encoding schemes here
|
990
|
+
return encoded;
|
991
|
+
}
|
992
|
+
|
993
|
+
/**
|
994
|
+
* TODO this encoding needs to be changed
|
995
|
+
**/
|
996
|
+
Operation.prototype.encodeQueryParam = function(arg) {
|
997
|
+
return escape(arg);
|
998
|
+
}
|
999
|
+
|
1000
|
+
/**
|
1001
|
+
* TODO revisit, might not want to leave '/'
|
1002
|
+
**/
|
1003
|
+
Operation.prototype.encodePathParam = function(pathParam) {
|
1004
|
+
var encParts, part, parts, _i, _len;
|
1005
|
+
pathParam = pathParam.toString();
|
1006
|
+
if (pathParam.indexOf('/') === -1) {
|
1007
|
+
return encodeURIComponent(pathParam);
|
1008
|
+
} else {
|
1009
|
+
parts = pathParam.split('/');
|
1010
|
+
encParts = [];
|
1011
|
+
for (_i = 0, _len = parts.length; _i < _len; _i++) {
|
1012
|
+
part = parts[_i];
|
1013
|
+
encParts.push(encodeURIComponent(part));
|
1014
|
+
}
|
1015
|
+
return encParts.join('/');
|
1016
|
+
}
|
1017
|
+
};
|
1018
|
+
|
1019
|
+
var Model = function(name, definition) {
|
1020
|
+
this.name = name;
|
1021
|
+
this.definition = definition || {};
|
1022
|
+
this.properties = [];
|
1023
|
+
var requiredFields = definition.required || [];
|
1024
|
+
|
1025
|
+
var key;
|
1026
|
+
var props = definition.properties;
|
1027
|
+
if(props) {
|
1028
|
+
for(key in props) {
|
1029
|
+
var required = false;
|
1030
|
+
var property = props[key];
|
1031
|
+
if(requiredFields.indexOf(key) >= 0)
|
1032
|
+
required = true;
|
1033
|
+
this.properties.push(new Property(key, property, required));
|
1034
|
+
}
|
1035
|
+
}
|
1036
|
+
}
|
1037
|
+
|
1038
|
+
Model.prototype.createJSONSample = function(modelsToIgnore) {
|
1039
|
+
var result = {};
|
1040
|
+
modelsToIgnore = (modelsToIgnore||{})
|
1041
|
+
modelsToIgnore[this.name] = this;
|
1042
|
+
var i;
|
1043
|
+
for (i = 0; i < this.properties.length; i++) {
|
1044
|
+
prop = this.properties[i];
|
1045
|
+
result[prop.name] = prop.getSampleValue(modelsToIgnore);
|
1046
|
+
}
|
1047
|
+
delete modelsToIgnore[this.name];
|
1048
|
+
return result;
|
1049
|
+
};
|
1050
|
+
|
1051
|
+
Model.prototype.getSampleValue = function(modelsToIgnore) {
|
1052
|
+
var i;
|
1053
|
+
var obj = {};
|
1054
|
+
for(i = 0; i < this.properties.length; i++ ) {
|
1055
|
+
var property = this.properties[i];
|
1056
|
+
obj[property.name] = property.sampleValue(false, modelsToIgnore);
|
1057
|
+
}
|
1058
|
+
return obj;
|
1059
|
+
}
|
1060
|
+
|
1061
|
+
Model.prototype.getMockSignature = function(modelsToIgnore) {
|
1062
|
+
var propertiesStr = [];
|
1063
|
+
var i;
|
1064
|
+
for (i = 0; i < this.properties.length; i++) {
|
1065
|
+
var prop = this.properties[i];
|
1066
|
+
propertiesStr.push(prop.toString());
|
1067
|
+
}
|
1068
|
+
|
1069
|
+
var strong = '<span class="strong">';
|
1070
|
+
var stronger = '<span class="stronger">';
|
1071
|
+
var strongClose = '</span>';
|
1072
|
+
var classOpen = strong + this.name + ' {' + strongClose;
|
1073
|
+
var classClose = strong + '}' + strongClose;
|
1074
|
+
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
|
1075
|
+
if (!modelsToIgnore)
|
1076
|
+
modelsToIgnore = {};
|
1077
|
+
|
1078
|
+
modelsToIgnore[this.name] = this;
|
1079
|
+
var i;
|
1080
|
+
for (i = 0; i < this.properties.length; i++) {
|
1081
|
+
var prop = this.properties[i];
|
1082
|
+
var ref = prop['$ref'];
|
1083
|
+
var model = models[ref];
|
1084
|
+
if (model && typeof modelsToIgnore[model.name] === 'undefined') {
|
1085
|
+
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
|
1086
|
+
}
|
1087
|
+
}
|
1088
|
+
return returnVal;
|
1089
|
+
};
|
1090
|
+
|
1091
|
+
var Property = function(name, obj, required) {
|
1092
|
+
this.schema = obj;
|
1093
|
+
this.required = required;
|
1094
|
+
if(obj['$ref']) {
|
1095
|
+
var refType = obj['$ref'];
|
1096
|
+
refType = refType.indexOf('#/definitions') === -1 ? refType : refType.substring('#/definitions').length;
|
1097
|
+
this['$ref'] = refType;
|
1098
|
+
}
|
1099
|
+
else if (obj.type === 'array') {
|
1100
|
+
if(obj.items['$ref'])
|
1101
|
+
this['$ref'] = obj.items['$ref'];
|
1102
|
+
else
|
1103
|
+
obj = obj.items;
|
1104
|
+
}
|
1105
|
+
this.name = name;
|
1106
|
+
this.description = obj.description;
|
1107
|
+
this.obj = obj;
|
1108
|
+
this.optional = true;
|
1109
|
+
this.default = obj.default || null;
|
1110
|
+
this.example = obj.example || null;
|
1111
|
+
}
|
1112
|
+
|
1113
|
+
Property.prototype.getSampleValue = function (modelsToIgnore) {
|
1114
|
+
return this.sampleValue(false, modelsToIgnore);
|
1115
|
+
}
|
1116
|
+
|
1117
|
+
Property.prototype.isArray = function () {
|
1118
|
+
var schema = this.schema;
|
1119
|
+
if(schema.type === 'array')
|
1120
|
+
return true;
|
1121
|
+
else
|
1122
|
+
return false;
|
1123
|
+
}
|
1124
|
+
|
1125
|
+
Property.prototype.sampleValue = function(isArray, ignoredModels) {
|
1126
|
+
isArray = (isArray || this.isArray());
|
1127
|
+
ignoredModels = (ignoredModels || {});
|
1128
|
+
var type = getStringSignature(this.obj);
|
1129
|
+
var output;
|
1130
|
+
|
1131
|
+
if(this['$ref']) {
|
1132
|
+
var refModel = models[this['$ref']];
|
1133
|
+
if(refModel && typeof ignoredModels[type] === 'undefined') {
|
1134
|
+
ignoredModels[type] = this;
|
1135
|
+
output = refModel.getSampleValue(ignoredModels);
|
1136
|
+
}
|
1137
|
+
else
|
1138
|
+
type = refModel;
|
1139
|
+
}
|
1140
|
+
else if(this.example)
|
1141
|
+
output = this.example;
|
1142
|
+
else if(this.default)
|
1143
|
+
output = this.default;
|
1144
|
+
else if(type === 'date-time')
|
1145
|
+
output = new Date().toISOString();
|
1146
|
+
else if(type === 'string')
|
1147
|
+
output = 'string';
|
1148
|
+
else if(type === 'integer')
|
1149
|
+
output = 0;
|
1150
|
+
else if(type === 'long')
|
1151
|
+
output = 0;
|
1152
|
+
else if(type === 'float')
|
1153
|
+
output = 0.0;
|
1154
|
+
else if(type === 'double')
|
1155
|
+
output = 0.0;
|
1156
|
+
else if(type === 'boolean')
|
1157
|
+
output = true;
|
1158
|
+
else
|
1159
|
+
output = {};
|
1160
|
+
ignoredModels[type] = output;
|
1161
|
+
if(isArray)
|
1162
|
+
return [output];
|
1163
|
+
else
|
1164
|
+
return output;
|
1165
|
+
}
|
1166
|
+
|
1167
|
+
getStringSignature = function(obj) {
|
1168
|
+
var str = '';
|
1169
|
+
if(obj.type === 'array') {
|
1170
|
+
obj = (obj.items || obj['$ref'] || {});
|
1171
|
+
str += 'Array[';
|
1172
|
+
}
|
1173
|
+
if(obj.type === 'integer' && obj.format === 'int32')
|
1174
|
+
str += 'integer';
|
1175
|
+
else if(obj.type === 'integer' && obj.format === 'int64')
|
1176
|
+
str += 'long';
|
1177
|
+
else if(obj.type === 'integer' && typeof obj.format === 'undefined')
|
1178
|
+
str += 'long';
|
1179
|
+
else if(obj.type === 'string' && obj.format === 'date-time')
|
1180
|
+
str += 'date-time';
|
1181
|
+
else if(obj.type === 'string' && obj.format === 'date')
|
1182
|
+
str += 'date';
|
1183
|
+
else if(obj.type === 'number' && obj.format === 'float')
|
1184
|
+
str += 'float';
|
1185
|
+
else if(obj.type === 'number' && obj.format === 'double')
|
1186
|
+
str += 'double';
|
1187
|
+
else if(obj.type === 'number' && typeof obj.format === 'undefined')
|
1188
|
+
str += 'double';
|
1189
|
+
else if(obj.type === 'boolean')
|
1190
|
+
str += 'boolean';
|
1191
|
+
else
|
1192
|
+
str += obj.type || obj['$ref'];
|
1193
|
+
if(obj.type === 'array')
|
1194
|
+
str += ']';
|
1195
|
+
return str;
|
1196
|
+
}
|
1197
|
+
|
1198
|
+
simpleRef = function(name) {
|
1199
|
+
if(name.indexOf("#/definitions/") === 0)
|
1200
|
+
return name.substring('#/definitions/'.length)
|
1201
|
+
else
|
1202
|
+
return name;
|
1203
|
+
}
|
1204
|
+
|
1205
|
+
Property.prototype.toString = function() {
|
1206
|
+
var str = getStringSignature(this.obj);
|
1207
|
+
if(str !== '') {
|
1208
|
+
str = '<span class="propName ' + this.required + '">' + this.name + '</span> (<span class="propType">' + str + '</span>';
|
1209
|
+
if(!this.required)
|
1210
|
+
str += ', <span class="propOptKey">optional</span>';
|
1211
|
+
str += ')';
|
1212
|
+
}
|
1213
|
+
else
|
1214
|
+
str = this.name + ' (' + JSON.stringify(this.obj) + ')';
|
1215
|
+
|
1216
|
+
if(typeof this.description !== 'undefined')
|
1217
|
+
str += ': ' + this.description;
|
1218
|
+
return str;
|
1219
|
+
}
|
1220
|
+
|
1221
|
+
typeFromJsonSchema = function(type, format) {
|
1222
|
+
var str;
|
1223
|
+
if(type === 'integer' && format === 'int32')
|
1224
|
+
str = 'integer';
|
1225
|
+
else if(type === 'integer' && format === 'int64')
|
1226
|
+
str = 'long';
|
1227
|
+
else if(type === 'integer' && typeof format === 'undefined')
|
1228
|
+
str = 'long';
|
1229
|
+
else if(type === 'string' && format === 'date-time')
|
1230
|
+
str = 'date-time';
|
1231
|
+
else if(type === 'string' && format === 'date')
|
1232
|
+
str = 'date';
|
1233
|
+
else if(type === 'number' && format === 'float')
|
1234
|
+
str = 'float';
|
1235
|
+
else if(type === 'number' && format === 'double')
|
1236
|
+
str = 'double';
|
1237
|
+
else if(type === 'number' && typeof format === 'undefined')
|
1238
|
+
str = 'double';
|
1239
|
+
else if(type === 'boolean')
|
1240
|
+
str = 'boolean';
|
1241
|
+
else if(type === 'string')
|
1242
|
+
str = 'string';
|
1243
|
+
|
1244
|
+
return str;
|
1245
|
+
}
|
1246
|
+
|
1247
|
+
var e = (typeof window !== 'undefined' ? window : exports);
|
1248
|
+
|
1249
|
+
var sampleModels = {};
|
1250
|
+
var cookies = {};
|
1251
|
+
var models = {};
|
1252
|
+
|
1253
|
+
e.authorizations = new SwaggerAuthorizations();
|
1254
|
+
e.ApiKeyAuthorization = ApiKeyAuthorization;
|
1255
|
+
e.PasswordAuthorization = PasswordAuthorization;
|
1256
|
+
e.CookieAuthorization = CookieAuthorization;
|
1257
|
+
e.SwaggerClient = SwaggerClient;
|
1258
|
+
|
1259
|
+
/**
|
1260
|
+
* SwaggerHttp is a wrapper for executing requests
|
1261
|
+
*/
|
1262
|
+
var SwaggerHttp = function() {};
|
1263
|
+
|
1264
|
+
SwaggerHttp.prototype.execute = function(obj) {
|
1265
|
+
if(obj && (typeof obj.useJQuery === 'boolean'))
|
1266
|
+
this.useJQuery = obj.useJQuery;
|
1267
|
+
else
|
1268
|
+
this.useJQuery = this.isIE8();
|
1269
|
+
|
1270
|
+
if(this.useJQuery)
|
1271
|
+
return new JQueryHttpClient().execute(obj);
|
1272
|
+
else
|
1273
|
+
return new ShredHttpClient().execute(obj);
|
1274
|
+
}
|
1275
|
+
|
1276
|
+
SwaggerHttp.prototype.isIE8 = function() {
|
1277
|
+
var detectedIE = false;
|
1278
|
+
if (typeof navigator !== 'undefined' && navigator.userAgent) {
|
1279
|
+
nav = navigator.userAgent.toLowerCase();
|
1280
|
+
if (nav.indexOf('msie') !== -1) {
|
1281
|
+
var version = parseInt(nav.split('msie')[1]);
|
1282
|
+
if (version <= 8) {
|
1283
|
+
detectedIE = true;
|
1284
|
+
}
|
1285
|
+
}
|
1286
|
+
}
|
1287
|
+
return detectedIE;
|
1288
|
+
};
|
1289
|
+
|
1290
|
+
/*
|
1291
|
+
* JQueryHttpClient lets a browser take advantage of JQuery's cross-browser magic.
|
1292
|
+
* NOTE: when jQuery is available it will export both '$' and 'jQuery' to the global space.
|
1293
|
+
* Since we are using closures here we need to alias it for internal use.
|
1294
|
+
*/
|
1295
|
+
var JQueryHttpClient = function(options) {
|
1296
|
+
"use strict";
|
1297
|
+
if(!jQuery){
|
1298
|
+
var jQuery = window.jQuery;
|
1299
|
+
}
|
1300
|
+
}
|
1301
|
+
|
1302
|
+
JQueryHttpClient.prototype.execute = function(obj) {
|
1303
|
+
var cb = obj.on;
|
1304
|
+
var request = obj;
|
1305
|
+
|
1306
|
+
obj.type = obj.method;
|
1307
|
+
obj.cache = false;
|
1308
|
+
|
1309
|
+
obj.beforeSend = function(xhr) {
|
1310
|
+
var key, results;
|
1311
|
+
if (obj.headers) {
|
1312
|
+
results = [];
|
1313
|
+
var key;
|
1314
|
+
for (key in obj.headers) {
|
1315
|
+
if (key.toLowerCase() === "content-type") {
|
1316
|
+
results.push(obj.contentType = obj.headers[key]);
|
1317
|
+
} else if (key.toLowerCase() === "accept") {
|
1318
|
+
results.push(obj.accepts = obj.headers[key]);
|
1319
|
+
} else {
|
1320
|
+
results.push(xhr.setRequestHeader(key, obj.headers[key]));
|
1321
|
+
}
|
1322
|
+
}
|
1323
|
+
return results;
|
1324
|
+
}
|
1325
|
+
};
|
1326
|
+
|
1327
|
+
obj.data = obj.body;
|
1328
|
+
obj.complete = function(response, textStatus, opts) {
|
1329
|
+
var headers = {},
|
1330
|
+
headerArray = response.getAllResponseHeaders().split("\n");
|
1331
|
+
|
1332
|
+
for(var i = 0; i < headerArray.length; i++) {
|
1333
|
+
var toSplit = headerArray[i].trim();
|
1334
|
+
if(toSplit.length === 0)
|
1335
|
+
continue;
|
1336
|
+
var separator = toSplit.indexOf(":");
|
1337
|
+
if(separator === -1) {
|
1338
|
+
// Name but no value in the header
|
1339
|
+
headers[toSplit] = null;
|
1340
|
+
continue;
|
1341
|
+
}
|
1342
|
+
var name = toSplit.substring(0, separator).trim(),
|
1343
|
+
value = toSplit.substring(separator + 1).trim();
|
1344
|
+
headers[name] = value;
|
1345
|
+
}
|
1346
|
+
|
1347
|
+
var out = {
|
1348
|
+
url: request.url,
|
1349
|
+
method: request.method,
|
1350
|
+
status: response.status,
|
1351
|
+
data: response.responseText,
|
1352
|
+
headers: headers
|
1353
|
+
};
|
1354
|
+
|
1355
|
+
var contentType = (headers["content-type"]||headers["Content-Type"]||null)
|
1356
|
+
|
1357
|
+
if(contentType != null) {
|
1358
|
+
if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) {
|
1359
|
+
if(response.responseText && response.responseText !== "")
|
1360
|
+
out.obj = JSON.parse(response.responseText);
|
1361
|
+
else
|
1362
|
+
out.obj = {}
|
1363
|
+
}
|
1364
|
+
}
|
1365
|
+
|
1366
|
+
if(response.status >= 200 && response.status < 300)
|
1367
|
+
cb.response(out);
|
1368
|
+
else if(response.status === 0 || (response.status >= 400 && response.status < 599))
|
1369
|
+
cb.error(out);
|
1370
|
+
else
|
1371
|
+
return cb.response(out);
|
1372
|
+
};
|
1373
|
+
|
1374
|
+
jQuery.support.cors = true;
|
1375
|
+
return jQuery.ajax(obj);
|
1376
|
+
}
|
1377
|
+
|
1378
|
+
/*
|
1379
|
+
* ShredHttpClient is a light-weight, node or browser HTTP client
|
1380
|
+
*/
|
1381
|
+
var ShredHttpClient = function(options) {
|
1382
|
+
this.options = (options||{});
|
1383
|
+
this.isInitialized = false;
|
1384
|
+
|
1385
|
+
var identity, toString;
|
1386
|
+
|
1387
|
+
if (typeof window !== 'undefined') {
|
1388
|
+
this.Shred = require("./shred");
|
1389
|
+
this.content = require("./shred/content");
|
1390
|
+
}
|
1391
|
+
else
|
1392
|
+
this.Shred = require("shred");
|
1393
|
+
this.shred = new this.Shred(options);
|
1394
|
+
}
|
1395
|
+
|
1396
|
+
ShredHttpClient.prototype.initShred = function () {
|
1397
|
+
this.isInitialized = true;
|
1398
|
+
this.registerProcessors(this.shred);
|
1399
|
+
}
|
1400
|
+
|
1401
|
+
ShredHttpClient.prototype.registerProcessors = function(shred) {
|
1402
|
+
var identity = function(x) {
|
1403
|
+
return x;
|
1404
|
+
};
|
1405
|
+
var toString = function(x) {
|
1406
|
+
return x.toString();
|
1407
|
+
};
|
1408
|
+
|
1409
|
+
if (typeof window !== 'undefined') {
|
1410
|
+
this.content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
|
1411
|
+
parser: identity,
|
1412
|
+
stringify: toString
|
1413
|
+
});
|
1414
|
+
} else {
|
1415
|
+
this.Shred.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
|
1416
|
+
parser: identity,
|
1417
|
+
stringify: toString
|
1418
|
+
});
|
1419
|
+
}
|
1420
|
+
}
|
1421
|
+
|
1422
|
+
ShredHttpClient.prototype.execute = function(obj) {
|
1423
|
+
if(!this.isInitialized)
|
1424
|
+
this.initShred();
|
1425
|
+
|
1426
|
+
var cb = obj.on, res;
|
1427
|
+
|
1428
|
+
var transform = function(response) {
|
1429
|
+
var out = {
|
1430
|
+
headers: response._headers,
|
1431
|
+
url: response.request.url,
|
1432
|
+
method: response.request.method,
|
1433
|
+
status: response.status,
|
1434
|
+
data: response.content.data
|
1435
|
+
};
|
1436
|
+
|
1437
|
+
var contentType = (response._headers["content-type"]||response._headers["Content-Type"]||null)
|
1438
|
+
|
1439
|
+
if(contentType != null) {
|
1440
|
+
if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) {
|
1441
|
+
if(response.content.data && response.content.data !== "")
|
1442
|
+
try{
|
1443
|
+
out.obj = JSON.parse(response.content.data);
|
1444
|
+
}
|
1445
|
+
catch (e) {
|
1446
|
+
// unable to parse
|
1447
|
+
}
|
1448
|
+
else
|
1449
|
+
out.obj = {}
|
1450
|
+
}
|
1451
|
+
}
|
1452
|
+
return out;
|
1453
|
+
};
|
1454
|
+
|
1455
|
+
res = {
|
1456
|
+
error: function(response) {
|
1457
|
+
if (obj)
|
1458
|
+
return cb.error(transform(response));
|
1459
|
+
},
|
1460
|
+
redirect: function(response) {
|
1461
|
+
if (obj)
|
1462
|
+
return cb.redirect(transform(response));
|
1463
|
+
},
|
1464
|
+
307: function(response) {
|
1465
|
+
if (obj)
|
1466
|
+
return cb.redirect(transform(response));
|
1467
|
+
},
|
1468
|
+
response: function(response) {
|
1469
|
+
if (obj)
|
1470
|
+
return cb.response(transform(response));
|
1471
|
+
}
|
1472
|
+
};
|
1473
|
+
if (obj) {
|
1474
|
+
obj.on = res;
|
1475
|
+
}
|
1476
|
+
return this.shred.request(obj);
|
1477
|
+
};
|