-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedical_plan_bp.py
126 lines (95 loc) · 4.24 KB
/
medical_plan_bp.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import json
from flask import Blueprint, request, jsonify
from pydantic import ValidationError
from data_models.medical_plan import PlanSchema, PatchPlanSchema
from services import redis_service
from extensions import google_auth, channel
api_bp = Blueprint('api', __name__)
@api_bp.route('/health_check', methods=['GET'])
def health_check():
return "Hello"
@api_bp.before_request
def before_request_func():
auth_header = request.headers.get('Authorization')
if not auth_header:
return jsonify({"message": "Token is missing!"}), 401
token = auth_header.split(" ")[1]
decoded_token = google_auth.verify_token(token)
if not decoded_token:
return jsonify({"message": "Token is invalid!"}), 401
@api_bp.route('/v1/plan', methods=['POST'])
def create_plan():
try:
plan_schema = PlanSchema(**request.json)
# Publish to RabbitMQ
req = {"action": "create", "document": plan_schema.dict()}
channel.basic_publish(exchange='',
routing_key='medical_plan',
body=json.dumps(req))
return jsonify({"message": "Document queued for processing"}), 202
except ValidationError as e:
return jsonify(e.errors()), 400
except ValueError as e:
return jsonify({'error': str(e)}), 409
except (ConnectionError, TimeoutError) as e:
return jsonify({'error': 'Service Unavailable', 'details': str(e)}), 503
@api_bp.route('/v1/plan/<object_id>', methods=['GET'])
def get_plan(object_id):
try:
plan, etag = redis_service.get_plan(object_id)
# Get the provided If-None-Match header from the request
if_none_match = request.headers.get('If-None-Match')
if plan:
# Compare the If-None-Match header with the plan's ETag
if if_none_match == etag:
# If the ETags match, return 304 Not Modified
return '', 304
else:
# If the ETags don't match, return the plan with updated ETag
return jsonify(plan.dict()), 200, {'ETag': etag}
else:
return jsonify({'error': 'Plan not found'}), 404
except (ConnectionError, TimeoutError) as e:
return jsonify({'error': 'Service Unavailable', 'details': str(e)}), 503
@api_bp.route('/v1/plan/<object_id>', methods=['DELETE'])
def delete_plan(object_id):
try:
req = {"action": "delete", "doc_id": object_id}
channel.basic_publish(exchange='',
routing_key='medical_plan',
body=json.dumps(req))
return jsonify({"message": "request queued for processing"}), 200
except ValidationError as e:
return jsonify(e.errors()), 400
except ValueError as e:
return jsonify({'error': str(e)}), 409
except (ConnectionError, TimeoutError) as e:
return jsonify({'error': 'Service Unavailable', 'details': str(e)}), 503
@api_bp.route('/v1/plans', methods=['GET'])
def get_all_plans():
try:
plans = redis_service.get_all_plans()
return jsonify([plan.dict() for plan in plans]), 200
except (ConnectionError, TimeoutError) as e:
return jsonify({'error': 'Service Unavailable', 'details': str(e)}), 503
@api_bp.route('/v1/patch/<string:key>', methods=['PATCH'])
def patch_item(key):
try:
current_plan_data, current_etag = redis_service.get_plan(key)
if not current_plan_data:
return jsonify({"message": "Item not found"}), 404
if current_etag != request.headers.get('If-Match'):
return jsonify({"message": "ETag does not match"}), 412
patch_data = request.json
new_plan = PatchPlanSchema(**patch_data)
req = {"action": "patch", "document": new_plan.dict()}
channel.basic_publish(exchange='',
routing_key='medical_plan',
body=json.dumps(req))
return jsonify({"message": "request queued for processing"}), 200
except ValidationError as e:
return jsonify(e.errors()), 400
except ValueError as e:
return jsonify({'error': str(e)}), 409
except (ConnectionError, TimeoutError) as e:
return jsonify({'error': 'Service Unavailable', 'details': str(e)}), 503