Skip to content

Commit d5ac99a

Browse files
author
Emmanouil Konstantinidis
committed
Init Making the Request & Getting the response
1 parent 5726c62 commit d5ac99a

File tree

2 files changed

+88
-5
lines changed
  • rest_framework_docs

2 files changed

+88
-5
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
$( document ).ready(function() {
22

3+
var makeRequest = function () {
4+
var url = $('#requestForm #urlInput').val();
5+
var method = 'POST';
36

7+
$.ajax({
8+
url: url,
9+
method: method,
10+
context: document.body,
11+
data: {
12+
username: 'test',
13+
password: 'test'
14+
}
15+
}).always(function(response) {
16+
17+
$('#responseStatusCode').text(response.status);
18+
$('#responseStatusText').text(response.statusText);
19+
$('#responseData').text(JSON.stringify(response.responseJSON));
20+
21+
console.log(response);
22+
console.log(response.responseJSON);
23+
});
24+
};
25+
26+
var setupForm = function (data) {
27+
console.log('------');
28+
console.log(data.path);
29+
console.log('------');
30+
31+
$('#urlInput').val(data.path);
32+
33+
$('#requestForm').submit(function (e) {
34+
// Prevent Submit
35+
e.preventDefault();
36+
37+
// Make Request
38+
makeRequest(data);
39+
});
40+
};
41+
42+
$('.plug').bind('click', function(e) {
43+
// Prevent the accordion from collapsing
44+
e.stopPropagation();
45+
46+
// Open Modal
47+
$('#liveAPIModal').modal('toggle');
48+
49+
// Setup the form
50+
var data = $(this).data();
51+
setupForm(data);
52+
});
453

554
});

rest_framework_docs/templates/rest_framework_docs/home.html

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ <h4 class="panel-title title">
4141
{% for method in endpoint.allowed_methods %}
4242
<li class="method {{ method|lower }}">{{ method }}</li>
4343
{% endfor %}
44-
<li class="method plug" data-toggle="modal" data-target="#myModal"><i class="fa fa-plug"></i></li>
44+
<li class="method plug"
45+
data-toggle="modal"
46+
data-path="{{ endpoint.path }}"
47+
data-methods="{{ endpoint.allowed_methods }}"
48+
data-fields="{{ endpoint.fields }}">
49+
<i class="fa fa-plug"></i></li>
4550
</ul>
4651
</div>
4752
</div>
@@ -83,19 +88,48 @@ <h2 class="text-center">No endpoints found for {{ query }}.</h2>
8388

8489
<!-- Modal -->
8590
<!-- Modal -->
86-
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
87-
<div class="modal-dialog" role="document">
91+
<div class="modal fade" id="liveAPIModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
92+
<div class="modal-dialog modal-lg" role="document">
8893
<div class="modal-content">
8994
<div class="modal-header">
9095
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
9196
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
9297
</div>
9398
<div class="modal-body">
94-
...
99+
100+
<div class="row">
101+
<div class="col-md-6">
102+
103+
<h3>Request</h3>
104+
<form id="requestForm">
105+
<div class="form-group">
106+
<label for="urlInput">Endpoint</label>
107+
<input type="text" class="form-control" id="urlInput" placeholder="Url">
108+
</div>
109+
<div class="form-group">
110+
<label for="exampleInputPassword1">Password</label>
111+
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
112+
</div>
113+
<button type="submit" class="btn btn-default">Send</button>
114+
</form>
115+
116+
117+
</div>
118+
<div class="col-md-6">
119+
<h3>Response</h3>
120+
<ul class="list">
121+
<li>Status Code: <span id="responseStatusCode"></span></li>
122+
<li>Status Text: <span id="responseStatusText"></span></li>
123+
</ul>
124+
125+
<pre id="responseData"></pre>
126+
</div>
127+
</div>
128+
129+
95130
</div>
96131
<div class="modal-footer">
97132
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
98-
<button type="button" class="btn btn-primary">Save changes</button>
99133
</div>
100134
</div>
101135
</div>

0 commit comments

Comments
 (0)