Skip to content

Commit

Permalink
Merge pull request #8 from SFDigitalServices/chore/new_proxy
Browse files Browse the repository at this point in the history
chore: update headers for new proxy
  • Loading branch information
hshaosf authored Nov 26, 2024
2 parents dd8e4fb + 0b0fdc7 commit 72817c9
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions service/microservice.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
"""Main application module"""

import os
from urllib import parse
import sentry_sdk
import falcon
import requests


def start_service():
"""Start this service
set SENTRY_DSN environmental variable to enable logging with Sentry
"""
# Initialize Sentry
sentry_sdk.init(os.environ.get('SENTRY_DSN'))
sentry_sdk.init(os.environ.get("SENTRY_DSN"))
# Initialize Falcon
api = falcon.API()
api.add_sink(cloud_storage_service, '')
api.add_sink(cloud_storage_service, "")
return api


def cloud_storage_service(_req, resp):
"""Send the request through to the cloudstorage microservice"""
parsed = parse.urlparse(_req.uri)
Expand All @@ -26,27 +29,28 @@ def cloud_storage_service(_req, resp):
# determine whether to use amazon s3 or azure blob storage
# Use 'az' at beginning of the path to signal that file is from azure
# eg. https://domain.com/az/some_file.pdf
microservice_url = os.environ.get('CLOUDSTORAGE_URL') # default to s3
microservice_url = os.environ.get("CLOUDSTORAGE_URL") # default to s3

request_params = {"name": path}

request_params = {
'name':path,
'apikey':os.environ.get('CLOUDSTORAGE_API_KEY')
headers = {
"ACCESS_KEY": os.environ.get("CLOUDSTORAGE_API_KEY"),
"x-api-key": os.environ.get("CLOUDSTORAGE_API_KEY"),
"Authorization": "Token " + os.environ.get("CLOUDSTORAGE_API_KEY"),
}

if path[0:3] == 'az/':
microservice_url = microservice_url.replace('1.0', '2.0', 1)
request_params['name'] = path[3:] # remove az from name
if path[0:3] == "az/":
microservice_url = microservice_url.replace("1.0", "2.0", 1)
request_params["name"] = path[3:] # remove az from name

# pass thru querystring
parsed_query_dict = parse.parse_qs(parsed.query)
for param, val in parsed_query_dict.items():
request_params[param] = val[0]

response = requests.get(
microservice_url,
params=request_params,
timeout=300
microservice_url, params=request_params, headers=headers, timeout=300
)
resp.status = falcon.get_http_status(response.status_code)
resp.content_type = response.headers['Content-Type']
resp.content_type = response.headers["Content-Type"]
resp.body = response.content

0 comments on commit 72817c9

Please sign in to comment.