This repository was archived by the owner on Dec 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP] ENH: RESTful API to serve the data format #4 #9
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
54da786
Ajout API
christianmarechal 63ff5ce
api acceptant minuscule ou majuscule pour les noms de fichier
christianmarechal ebb048a
ENH: API first commit
45d0a1a
Update api/resources/resources.py
alexisthual 3b77c0f
Remove the sql mention
jblemoine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # API | ||
|
|
||
| This is a RESTful API to serve the data format. | ||
|
|
||
|
|
||
| ## Usage | ||
|
|
||
| ### Store | ||
|
|
||
| You must send a post request to: http://localhost:5000/store | ||
| The body of your request must match the following template | ||
|
|
||
| { | ||
| "output_format": "json", | ||
| "fhir_resource_name": "patient" | ||
| } | ||
|
|
||
| ### Mapping | ||
|
|
||
| You must send a post request to: http://localhost:5000/mapping | ||
| The body of your request must match the following template | ||
|
|
||
| { | ||
| "output_format": "json", | ||
| "fhir_resource_name": "patient" | ||
| "database": "CW" | ||
| } |
Empty file.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from flask import Flask, jsonify | ||
| from flask_restful import Api | ||
|
|
||
| from api.resources import Mapping, Store | ||
|
|
||
|
|
||
| app = Flask(__name__) | ||
| api = Api(app) | ||
|
|
||
| api.add_resource(Store, '/store') | ||
| api.add_resource(Mapping, '/mapping') | ||
|
|
||
| @app.route('/') | ||
| def index(): | ||
| return jsonify( | ||
| {'message': 'Welcome to Fhir API'} | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| app.run(debug=True) |
Empty file.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import glob | ||
| import re | ||
|
|
||
|
|
||
| def fhir_resource_path(fhir_resource, parent_folder): | ||
| # this is highly inefficient, since we need to scan the entire folder. | ||
| # It might be interesting to cache the folder structure | ||
|
|
||
| # case insensitive glob | ||
| # source: https://medium.com/@tylercubell/python-case-insensitive-glob-80ba7306b327 | ||
| pattern = ".*/{}.yml".format(fhir_resource) | ||
| matching_files = [file for file in glob.glob(f'../{parent_folder}/**', recursive=True) if re.match(pattern, file, flags=re.IGNORECASE)] | ||
| if not matching_files: | ||
| return None | ||
| else: | ||
| return matching_files[0] | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .resources import Mapping, Store |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| from flask import jsonify, send_from_directory | ||
| # note: prefer send_from_directory to send_file as send_file is not secured. | ||
|
jblemoine marked this conversation as resolved.
|
||
| # From the official documentation : "Please never pass filenames to this function from user sources; | ||
| # you should use send_from_directory() instead." | ||
| from flask_restful import Resource, reqparse | ||
| import os | ||
| import yaml | ||
|
|
||
| from api.common.utils import fhir_resource_path | ||
|
|
||
|
|
||
| class Store(Resource): | ||
| def post(self): | ||
|
|
||
| parser = reqparse.RequestParser() | ||
| parser.add_argument('output_format', required=True, type=str, choices=('json', 'yml')) | ||
| parser.add_argument('fhir_resource_name', required=True, type=str) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| file_path = fhir_resource_path(args['fhir_resource_name'], parent_folder='yml') | ||
|
jblemoine marked this conversation as resolved.
Outdated
|
||
| if not file_path: | ||
| return jsonify( | ||
| {"message": "Fhir resource not found."} | ||
| ) | ||
|
|
||
| folder = os.path.dirname(file_path) | ||
| file = os.path.basename(file_path) | ||
| if args['output_format'] == 'json': | ||
| return jsonify(yaml.load(open(file_path))) | ||
|
|
||
| elif args['output_format'] == 'yml': | ||
| return send_from_directory(folder, file) | ||
|
|
||
|
|
||
| class Mapping(Resource): | ||
| def post(self): | ||
|
|
||
| parser = reqparse.RequestParser() | ||
| parser.add_argument('output_format', required=True, type=str, choices=('sql', 'yml')) | ||
|
jblemoine marked this conversation as resolved.
Outdated
|
||
| parser.add_argument('fhir_resource_name', required=True, type=str) | ||
| parser.add_argument('database', required=True, type=str) | ||
|
|
||
| args = parser.args() | ||
|
|
||
| file_path = fhir_resource_path(args['fhir_resource_name'], parent_folder=args['database']) | ||
| if not file_path: | ||
| return jsonify( | ||
| {"message": "Fhir resource not found."} | ||
| ) | ||
|
|
||
| folder = os.path.dirname(file_path) | ||
| file = os.path.basename(file_path) | ||
|
|
||
| if args['output_format'] == 'json': | ||
| return jsonify(yaml.load(open(file_path))) | ||
|
|
||
| elif args['output_format'] == 'yml': | ||
| return send_from_directory(folder, file) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.