api.js
10.3 KB
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
var responseDisplay = 'data'
var coreapi = window.coreapi
var schema = window.schema
function normalizeKeys (arr) {
var _normarr = [];
for (var i = 0; i < arr.length; i++) {
_normarr = _normarr.concat(arr[i].split(' > '));
}
return _normarr;
}
function normalizeHTTPHeader (str) {
// Capitalize HTTP headers for display.
return (str.charAt(0).toUpperCase() + str.substring(1))
.replace(/-(.)/g, function ($1) {
return $1.toUpperCase()
})
.replace(/(Www)/g, function ($1) {
return 'WWW'
})
.replace(/(Xss)/g, function ($1) {
return 'XSS'
})
.replace(/(Md5)/g, function ($1) {
return 'MD5'
})
}
function formEntries (form) {
// Polyfill for new FormData(form).entries()
var formData = new FormData(form)
if (formData.entries !== undefined) {
return Array.from(formData.entries())
}
var entries = []
for (var i = 0; i < form.elements.length; i++) {
var element = form.elements[i]
if (!element.name) {
continue
}
if (element.type === 'file') {
for (var j = 0; j < element.files.length; j++) {
entries.push([element.name, element.files[j]])
}
} else if (element.type === 'select-multiple' || element.type === 'select-one') {
for (var j = 0; j < element.selectedOptions.length; j++) {
entries.push([element.name, element.selectedOptions[j].value])
}
} else if (element.type === 'checkbox') {
if (element.checked) {
entries.push([element.name, element.value])
}
} else {
entries.push([element.name, element.value])
}
}
return entries
}
$(function () {
var $selectedAuthentication = $('#selected-authentication')
var $authControl = $('#auth-control')
var $authTokenModal = $('#auth_token_modal')
var $authBasicModal = $('#auth_basic_modal')
var $authSessionModal = $('#auth_session_modal')
// Language Control
$('#language-control li').click(function (event) {
event.preventDefault()
var $languageMenuItem = $(this).find('a')
var $languageControls = $(this).closest('ul').find('li')
var $languageControlLinks = $languageControls.find('a')
var language = $languageMenuItem.data('language')
$languageControlLinks.not('[data-language="' + language + '"]').parent().removeClass('active')
$languageControlLinks.filter('[data-language="' + language + '"]').parent().addClass('active')
$('#selected-language').text(language)
var $codeBlocks = $('pre.highlight')
$codeBlocks.not('[data-language="' + language + '"]').addClass('hide')
$codeBlocks.filter('[data-language="' + language + '"]').removeClass('hide')
})
// API Explorer
$('form.api-interaction').submit(function (event) {
event.preventDefault()
var $form = $(this).closest('form')
var $requestMethod = $form.find('.request-method')
var $requestUrl = $form.find('.request-url')
var $toggleView = $form.closest('.modal-content').find('.toggle-view')
var $responseStatusCode = $form.find('.response-status-code')
var $meta = $form.find('.meta')
var $responseRawResponse = $form.find('.response-raw-response')
var $requestAwaiting = $form.find('.request-awaiting')
var $responseRaw = $form.find('.response-raw')
var $responseData = $form.find('.response-data')
var key = normalizeKeys($form.data('key'))
var params = {}
var entries = formEntries($form.get()[0])
for (var i = 0; i < entries.length; i++) {
var entry = entries[i]
var paramKey = entry[0]
var paramValue = entry[1]
var $elem = $form.find('[name="' + paramKey + '"]')
var dataType = $elem.data('type') || 'string'
if (dataType === 'integer' && paramValue) {
var value = parseInt(paramValue)
if (!isNaN(value)) {
params[paramKey] = value
}
} else if (dataType === 'number' && paramValue) {
var value = parseFloat(paramValue)
if (!isNaN(value)) {
params[paramKey] = value
}
} else if (dataType === 'boolean' && paramValue) {
var value = {
'true': true,
'false': false
}[paramValue.toLowerCase()]
if (value !== undefined) {
params[paramKey] = value
}
} else if (dataType === 'array' && paramValue) {
try {
params[paramKey] = JSON.parse(paramValue)
} catch (err) {
// Ignore malformed JSON
}
} else if (dataType === 'object' && paramValue) {
try {
params[paramKey] = JSON.parse(paramValue)
} catch (err) {
// Ignore malformed JSON
}
} else if (dataType === 'string' && paramValue) {
params[paramKey] = paramValue
}
}
$form.find(':checkbox').each(function (index) {
// Handle unselected checkboxes
var name = $(this).attr('name')
if (!params.hasOwnProperty(name)) {
params[name] = false
}
})
function requestCallback (request) {
// Fill in the "GET /foo/" display.
var parser = document.createElement('a')
parser.href = request.url
var method = request.options.method
var path = parser.pathname + parser.hash + parser.search
$requestMethod.text(method)
$requestUrl.text(path)
}
function responseCallback (response, responseText) {
// Display the 'Data'/'Raw' control.
$toggleView.removeClass('hide')
// Fill in the "200 OK" display.
$responseStatusCode.removeClass('label-success').removeClass('label-danger')
if (response.ok) {
$responseStatusCode.addClass('label-success')
} else {
$responseStatusCode.addClass('label-danger')
}
$responseStatusCode.text(response.status)
$meta.removeClass('hide')
// Fill in the Raw HTTP response display.
var panelText = 'HTTP/1.1 ' + response.status + ' ' + response.statusText + '\n'
response.headers.forEach(function (header, key) {
panelText += normalizeHTTPHeader(key) + ': ' + header + '\n'
})
if (responseText) {
panelText += '\n' + responseText
}
$responseRawResponse.text(panelText)
}
// Instantiate a client to make the outgoing request.
var options = {
requestCallback: requestCallback,
responseCallback: responseCallback
}
// Setup authentication options.
if (window.auth && window.auth.type === 'token') {
// Header authentication
options.auth = new coreapi.auth.TokenAuthentication({
scheme: window.auth.scheme,
token: window.auth.token
})
} else if (window.auth && window.auth.type === 'basic') {
// Basic authentication
options.auth = new coreapi.auth.BasicAuthentication({
username: window.auth.username,
password: window.auth.password
})
} else if (window.auth && window.auth.type === 'session') {
// Session authentication
options.auth = new coreapi.auth.SessionAuthentication({
csrfCookieName: 'csrftoken',
csrfHeaderName: 'X-CSRFToken'
})
}
var client = new coreapi.Client(options)
client.action(schema, key, params).then(function (data) {
var response = JSON.stringify(data, null, 2)
$requestAwaiting.addClass('hide')
$responseRaw.addClass('hide')
$responseData.addClass('hide').text('').jsonView(response)
if (responseDisplay === 'data') {
$responseData.removeClass('hide')
} else {
$responseRaw.removeClass('hide')
}
}).catch(function (error) {
var response = JSON.stringify(error.content, null, 2)
$requestAwaiting.addClass('hide')
$responseRaw.addClass('hide')
$responseData.addClass('hide').text('').jsonView(response)
if (responseDisplay === 'data') {
$responseData.removeClass('hide')
} else {
$responseRaw.removeClass('hide')
}
})
})
// 'Data'/'Raw' control
$('.toggle-view button').click(function () {
var $modalContent = $(this).closest('.modal-content')
var $modalResponseRaw = $modalContent.find('.response-raw')
var $modalResponseData = $modalContent.find('.response-data')
responseDisplay = $(this).data('display-toggle')
$(this).removeClass('btn-default').addClass('btn-info').siblings().removeClass('btn-info')
if (responseDisplay === 'raw') {
$modalResponseRaw.removeClass('hide')
$modalResponseData.addClass('hide')
} else {
$modalResponseData.removeClass('hide')
$modalResponseRaw.addClass('hide')
}
})
// Authentication: none
$authControl.find("[data-auth='none']").click(function (event) {
event.preventDefault()
window.auth = null
$selectedAuthentication.text('none')
$authControl.find("[data-auth]").closest('li').removeClass('active')
$authControl.find("[data-auth='none']").closest('li').addClass('active')
})
// Authentication: token
$('form.authentication-token-form').submit(function (event) {
event.preventDefault()
var $form = $(this).closest('form')
var scheme = $form.find('input#scheme').val()
var token = $form.find('input#token').val()
window.auth = {
'type': 'token',
'scheme': scheme,
'token': token
}
$selectedAuthentication.text('token')
$authControl.find("[data-auth]").closest('li').removeClass('active')
$authControl.find("[data-auth='token']").closest('li').addClass('active')
$authTokenModal.modal('hide')
})
// Authentication: basic
$('form.authentication-basic-form').submit(function (event) {
event.preventDefault()
var $form = $(this).closest('form')
var username = $form.find('input#username').val()
var password = $form.find('input#password').val()
window.auth = {
'type': 'basic',
'username': username,
'password': password
}
$selectedAuthentication.text('basic')
$authControl.find("[data-auth]").closest('li').removeClass('active')
$authControl.find("[data-auth='basic']").closest('li').addClass('active')
$authBasicModal.modal('hide')
})
// Authentication: session
$('form.authentication-session-form').submit(function (event) {
event.preventDefault()
window.auth = {
'type': 'session'
}
$selectedAuthentication.text('session')
$authControl.find("[data-auth]").closest('li').removeClass('active')
$authControl.find("[data-auth='session']").closest('li').addClass('active')
$authSessionModal.modal('hide')
})
})