-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from miracum/add-url-passthrough
feat: enable passthrough urls and resources
- Loading branch information
Showing
6 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,10 +1,24 @@ | ||
import os, yaml | ||
from flask import Flask | ||
from flask_restful import Api | ||
from resources.fhir_facade_passthrough import FHIR_Facade_Passthrough | ||
from resources.fhir_facade_server import FHIR_Facade_Server | ||
|
||
temp = os.getenv("PASSTHROUGH_CONFIG", "") | ||
if temp != "": | ||
pass_conf = yaml.safe_load(temp) | ||
else: | ||
with open("../config/passthrough_config.yml") as cfgfile: | ||
pass_conf = yaml.safe_load(cfgfile.read()) | ||
|
||
app = Flask(__name__) | ||
api = Api(app) | ||
|
||
# set up web server at FACADE_URL+FACADE_PORT | ||
api.add_resource(FHIR_Facade_Server, '/fhir/<string:resource>', '/fhir/<string:resource>/<string:search>') | ||
|
||
api.add_resource( | ||
FHIR_Facade_Server, | ||
"/fhir/<string:resource>", | ||
"/fhir/<string:resource>/<string:search>", | ||
) | ||
if len(pass_conf["URLs"]) > 0: | ||
api.add_resource(FHIR_Facade_Passthrough, *pass_conf["URLs"]) |
This file contains 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,8 @@ | ||
# Add all endpoints and resources that are supposed to be passed through without consent checks | ||
# Endpoints that use the same /fhir/ base url as resources have to be added in the resources section | ||
# E.g. /fhir/metadata | ||
URLs: | ||
- "/metadata" | ||
- "/test/echo" | ||
Resources: | ||
- "metadata" |
This file contains 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,23 @@ | ||
from resources.util.util_functions import get_passthrough_result | ||
from flask import request, Response | ||
from flask_restful import Resource | ||
|
||
|
||
def passthrough_handle_request(self, is_post): | ||
params = request.args.copy() | ||
headers = dict(request.headers) | ||
data = bytes(request.data) | ||
|
||
if is_post: | ||
return get_passthrough_result(request.full_path, params, headers, data, True) | ||
else: | ||
return get_passthrough_result(request.full_path, params, headers, data, False) | ||
|
||
|
||
class FHIR_Facade_Passthrough(Resource): | ||
|
||
def get(self): | ||
return passthrough_handle_request(self, False) | ||
|
||
def post(self): | ||
return passthrough_handle_request(self, True) |
This file contains 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
This file contains 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,45 @@ | ||
import os, requests, json | ||
from requests.auth import HTTPBasicAuth | ||
|
||
|
||
def get_passthrough_result(url, params, headers, data, is_post): | ||
SERVER_URL = os.getenv("FHIR_SERVER_URL", "") | ||
|
||
s = requests.sessions.Session() | ||
auth = HTTPBasicAuth(os.getenv("BA_USER_NAME", ""), os.getenv("BA_PASSWORD", "")) | ||
|
||
# Merge params and jsondata | ||
params["_format"] = "application/fhir+json" | ||
try: | ||
data = json.loads(data) | ||
data.update(params) | ||
except: | ||
data = {} | ||
data.update(params) | ||
|
||
headers.update( | ||
{ | ||
"Accept": "application/fhir+json", | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
} | ||
) | ||
|
||
if is_post: | ||
response = s.post( | ||
SERVER_URL.replace("/fhir/", "") + url, | ||
auth=auth, | ||
headers=headers, | ||
params=data, | ||
verify=False, | ||
).json() | ||
|
||
else: | ||
response = s.get( | ||
SERVER_URL.replace("/fhir/", "") + url, | ||
auth=auth, | ||
headers=headers, | ||
params=data, | ||
verify=False, | ||
).json() | ||
|
||
return response |