forked from ychen306/FHIR-Genomics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
112 lines (94 loc) · 4.03 KB
/
api.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
from flask.blueprints import Blueprint
from flask import request, Response
import fhir_api
from fhir_api import NOT_FOUND
from fhir_spec import RESOURCES
from models import Access
from urlparse import urljoin
from models import Session, Client
from functools import partial, wraps
from datetime import datetime
import re
API_URL_PREFIX = 'api'
api = Blueprint(API_URL_PREFIX, __name__)
AUTH_HEADER_RE = re.compile(r'Bearer (?P<access_token>.+)')
FORBIDDEN = Response(status='403')
def verify_access(request, resource_type, access_type):
if request.session is not None:
request.authorizer = request.session.user
return True
elif request.client is not None:
request.authorizer = request.client.authorizer
if datetime.now() > request.client.expire_at:
return False
accesses = Access.query.filter_by(client_code=request.client.code,
access_type=access_type,
resource_type=resource_type)
return accesses.count() > 0
else:
return False
def protected(view):
@wraps(view)
def protected_view(*args, **kwargs):
access_type = 'read' if request.method == 'GET' else 'write'
resource_type = (args[0]
if len(args) > 0
else kwargs['resource_type'])
if not verify_access(request, resource_type, access_type):
return FORBIDDEN
else:
return view(*args, **kwargs)
return protected_view
@api.before_request
def get_client():
session_id = request.cookies.get('session_id')
request.session = Session.query.filter_by(id=session_id).first()
request.client = None
auth_header = AUTH_HEADER_RE.match(request.headers.get('authorization', ''))
if auth_header is not None:
request.client = Client.query.\
filter_by(
access_token=auth_header.group('access_token'),
authorized=True).\
first()
@api.route('/<resource_type>', methods=['GET', 'POST'])
@protected
def handle_resource(resource_type):
if resource_type not in RESOURCES:
return NOT_FOUND
request.api_base = urljoin(request.url_root, API_URL_PREFIX) + '/'
fhir_request = fhir_api.FHIRRequest(request)
if request.method == 'GET':
return fhir_api.handle_search(fhir_request, resource_type)
else:
return fhir_api.handle_create(fhir_request, resource_type)
@api.route('/<resource_type>/<resource_id>', methods=['GET', 'PUT', 'DELETE'])
@protected
def handle_resources(resource_type, resource_id):
if resource_type not in RESOURCES:
return NOT_FOUND
request.api_base = urljoin(request.url_root, API_URL_PREFIX) + '/'
fhir_request = fhir_api.FHIRRequest(request, is_resource=False)
if request.method == 'GET':
return fhir_api.handle_read(fhir_request,
resource_type,
resource_id)
elif request.method == 'PUT':
return fhir_api.handle_update(fhir_request,
resource_type,
resource_id)
else:
return fhir_api.handle_delete(fhir_request,
resource_type,
resource_id)
@api.route('/_history', defaults={'resource_type': None, 'resource_id': None, 'version': None})
@api.route('/<resource_type>/_history', defaults={'resource_id': None, 'version': None})
@api.route('/<resource_type>/<resource_id>/_history', defaults={'version': None})
@api.route('/<resource_type>/<resource_id>/_history/<version>')
@protected
def read_history(resource_type, resource_id, version):
if resource_type is not None and resource_type not in RESOURCES:
return NOT_FOUND
request.api_base = urljoin(request.url_root, API_URL_PREFIX) + '/'
fhir_request = fhir_api.FHIRRequest(request)
return fhir_api.handle_history(fhir_request, resource_type, resource_id, version)