From 8b7d77f76d84b2c67665cc0558dab3ced2b01684 Mon Sep 17 00:00:00 2001 From: Simon Moser Date: Thu, 20 Jan 2022 01:01:35 +0100 Subject: [PATCH] first commit --- .gitignore | 2 ++ README.md | 0 api.py | 55 ++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 57 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 api.py create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..151121e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +env +__pycache__ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/api.py b/api.py new file mode 100644 index 0000000..40cd966 --- /dev/null +++ b/api.py @@ -0,0 +1,55 @@ +from flask import Flask, jsonify, request, render_template + +app = Flask(__name__) + +incomes = [ + { 'description': 'engineer', 'amount': 5000 }, + { 'description': 'cleaner', 'amount': 1000 } +] + + +@app.route('/', methods=["GET"]) +def frontend(): + return render_template("index.html") + +@app.route('/incomes', methods=['GET']) +def get_incomes(): + return jsonify(incomes) + +@app.route('/incomes/average', methods=["GET"]) +def avg(): + salaries = [] + for inc in incomes: + salaries.append(int(inc["amount"])) + return str(sum(salaries)/len(salaries)) + +@app.route('/income', methods=['POST']) +def add_income(): + if(request.headers.get('x-api-key') != "secretp4ssw0rd"): + return '', 401 + incomes.append(request.get_json()) + return '', 204 + +@app.route('/income/', methods=['GET']) +def get_income(desc): + try: + return jsonify([inc for inc in incomes if inc['description'] == desc]) + except: + return '', 404 + +@app.route('/income/', methods=['DELETE']) +def del_income(desc): + if(request.headers.get('x-api-key') != "secretp4ssw0rd"): + return '', 401 + for inc in incomes: + if inc['description'] == desc: + incomes.remove(inc) + return '', 204 + + +@app.after_request +def apply_caching(response): + response.headers["Access-Control-Allow-Origin"] = "*" + response.headers["Access-Control-Allow-Methods"] = "*" + response.headers["Access-Control-Allow-Headers"] = "*" + return response diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..9e7cbb6 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,57 @@ + + + Income client + + +

Please share your income for comparison!!

+
Placeholder
+ Average: +


+
+ Description:
+ Amount:
+ +
+ + +