forked from diranetafen/student-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
student_age.py
executable file
·58 lines (49 loc) · 1.63 KB
/
student_age.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!flask/bin/python
from flask import Flask, jsonify
from flask import abort
from flask import make_response
from flask import request
from flask import url_for
from flask_httpauth import HTTPBasicAuth
from flask import g, session, redirect, url_for
from flask_simpleldap import LDAP
import json
import os
auth = HTTPBasicAuth()
app = Flask(__name__)
app.debug = True
@auth.get_password
def get_password(username):
if username == 'toto':
return 'python'
return None
@auth.error_handler
def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), 401)
try:
student_age_file_path
student_age_file_path = os.environ['student_age_file_path']
except NameError:
student_age_file_path = '/data/student_age.json'
student_age_file = open(student_age_file_path, "r")
student_age = json.load(student_age_file)
@app.route('/pozos/api/v1.0/get_student_ages', methods=['GET'])
@auth.login_required
def get_student_ages():
return jsonify({'student_ages': student_age })
@app.route('/pozos/api/v1.0/get_student_ages/<student_name>', methods=['GET'])
@auth.login_required
def get_student_age(student_name):
if student_name not in student_age :
abort(404)
if student_name in student_age :
age = student_age[student_name]
del student_age[student_name]
with open(student_age_file_path, 'w') as student_age_file:
json.dump(student_age, student_age_file, indent=4, ensure_ascii=False)
return age
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')