-
-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial prototype of API explorer at /-/api, refs #1871
- Loading branch information
Showing
4 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}API Explorer{% endblock %} | ||
|
||
{% block content %} | ||
|
||
<h1>API Explorer</h1> | ||
|
||
<p>Use this tool to try out the Datasette write API.</p> | ||
|
||
{% if errors %} | ||
{% for error in errors %} | ||
<p class="message-error">{{ error }}</p> | ||
{% endfor %} | ||
{% endif %} | ||
|
||
<form method="post" id="api-explorer"> | ||
<div> | ||
<label for="auth-token">API token:</label> | ||
<input type="text" id="auth-token" name="token" value="" style="width: 40%"> | ||
</div> | ||
<div> | ||
<label for="path">API path:</label> | ||
<input type="text" id="path" name="path" value="/fixtures/searchable/-/insert" style="width: 40%"> | ||
</div> | ||
<div> | ||
<textarea name="json" style="width: 60%; height: 200px; font-family: monospace; font-size: 0.8em;"></textarea> | ||
</div> | ||
<p><button id="json-format" type="button">Format JSON</button> <input type="submit" value="POST"></p> | ||
</form> | ||
|
||
<script> | ||
document.querySelector('#json-format').addEventListener('click', (ev) => { | ||
ev.preventDefault(); | ||
const json = document.querySelector('textarea[name="json"]').value; | ||
try { | ||
const parsed = JSON.parse(json); | ||
document.querySelector('textarea[name="json"]').value = JSON.stringify(parsed, null, 2); | ||
} catch (e) { | ||
alert("Error parsing JSON: " + e); | ||
} | ||
}); | ||
var form = document.getElementById('api-explorer'); | ||
form.addEventListener("submit", (ev) => { | ||
ev.preventDefault(); | ||
var formData = new FormData(form); | ||
var json = formData.get('json'); | ||
var path = formData.get('path'); | ||
var token = formData.get('token'); | ||
// Validate JSON | ||
try { | ||
var data = JSON.parse(json); | ||
} catch (err) { | ||
alert("Invalid JSON: " + err); | ||
return; | ||
} | ||
// POST JSON to path with content-type application/json | ||
fetch(path, { | ||
method: 'POST', | ||
body: json, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${token}` | ||
} | ||
}).then(r => r.json()).then(r => { | ||
alert(JSON.stringify(r, null, 2)); | ||
}).catch(err => { | ||
alert("Error: " + err); | ||
}); | ||
}); | ||
</script> | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters