Created
July 16, 2017 03:41
-
-
Save alanzchen/5468334e96437993e01eb9adfe06818d to your computer and use it in GitHub Desktop.
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
from flask import Flask, request, jsonify | |
import json | |
from hashlib import sha256 | |
from datetime import datetime | |
from glob import glob | |
import os | |
app = Flask(__name__) | |
SECRET = "some_random_string" | |
@app.route('/') | |
def greet(): | |
return 'Hello World!' | |
@app.route('/<endpoint>', methods=["POST", "GET"]) | |
def collect(endpoint): | |
ts = datetime.timestamp(datetime.utcnow()) | |
data = request.get_json(silent=True) | |
if data: | |
try: | |
data_hash = sha256(str(str(ts) + str(data)).encode()).hexdigest() | |
path = os.path.join(endpoint, data_hash + ".json") | |
save_json(data, path) | |
return jsonify(status="success", message="Request saved to file.") | |
except: | |
return jsonify(status="failed", message="Internal server error."), 500 | |
else: | |
return jsonify(status="failed", message="Unable to parse incoming request."), 400 | |
@app.route('/<secret>/<endpoint>') | |
def export(secret, endpoint): | |
if secret == SECRET: | |
files = glob(endpoint + "/*.json") | |
data = [load_json(f) for f in files] | |
return json.dumps(data) | |
else: | |
return jsonify(message="Authentication failed."), 403 | |
def save_json(obj, filename): | |
with open(filename, 'w') as f: | |
json.dump(obj, f) | |
def load_json(filename): | |
with open(filename, 'r') as f: | |
return json.load(f) | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment