Created
July 24, 2015 06:55
-
-
Save aperezdc/d69ab1039076ca09d6a9 to your computer and use it in GitHub Desktop.
Simple HiPack conversion web service
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>HiPack Web Service Test</title> | |
<script type="text/javascript"> | |
function convertHiPack() { | |
var req = new XMLHttpRequest(); | |
req.onload = function () { | |
if (req.readyState == 4) { | |
var output = document.getElementById("output"); | |
if (req.status == 200) { | |
output.innerHTML = req.responseText; | |
} else { | |
output.innerHTML = "(error)"; | |
} | |
} | |
}; | |
var format = document.getElementById("format"); | |
var input = document.getElementById("input"); | |
req.open("POST", "/" + format.value + "?t=" + ((new Date()).getTime()), true); | |
req.send(input.innerHTML); | |
return false; | |
} | |
</script> | |
</head> | |
<body> | |
<form method="post" action="#" onsubmit="return convertHiPack()"> | |
<label for="format">Input format:</label> | |
<select id="format"> | |
<option value="json" selected>JSON</option> | |
<option value="yaml">YAML</option> | |
<option value="hipack">HiPack</option> | |
</select> | |
<br> | |
<textarea id="input">{ "input": "json", "compact": false }</textarea> | |
<br> | |
<input type="submit" value="Convert"></input> | |
</form> | |
<pre id="output"></pre> | |
</body> | |
</html> |
This file contains hidden or 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
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# vim:fenc=utf-8 | |
# | |
# Copyright © 2015 Adrian Perez <[email protected]> | |
# | |
# Distributed under terms of the MIT license. | |
import bottle | |
import hipack, yaml, json | |
loaders = { | |
"hipack": hipack.load, | |
"yaml" : yaml.safe_load, | |
"json" : json.load, | |
} | |
stats = { | |
"num_requests": 0, | |
"num_requests_failed": 0, | |
"num_requests_with_nulls": 0, | |
} | |
@bottle.route("/") | |
def index_html(): | |
from os import path | |
return bottle.static_file("hipack-webservice.html", | |
root=path.dirname(__file__)) | |
@bottle.route("/<format>", method="POST") | |
def hipack_from(format): | |
global stats | |
stats["num_requests"] += 1 | |
bottle.response.set_header("Content-Type", "text/plain") | |
try: | |
data = loaders[format](bottle.request.body) | |
except Exception as e: | |
bottle.response.status = 500 | |
stats["num_requests_failed"] += 1 | |
return e.message | |
try: | |
return hipack.dumps(data) | |
except TypeError as e: | |
if e.message == "Values of type <type 'NoneType'> cannot be dumped": | |
stats["num_requests_with_nulls"] += 1 | |
stats["num_requests_failed"] += 1 | |
raise | |
except Exception: | |
stats["num_requests_failed"] += 1 | |
raise | |
@bottle.route("/stats") | |
def get_stats(): | |
bottle.response.set_header("Content-Type", "text/plain") | |
return hipack.dumps(stats) | |
if __name__ == "__main__": | |
bottle.run(host="localhost", port=10080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment