Unverified Commit 620ed793 authored by Daniel's avatar Daniel Committed by GitHub

Refactor call pattern for switching between clients (#81)

* Added a 'request_wrapper' that abstracts away the logic to switch between Envoy clients and basic HTTP requests.
parent bd580b07
This diff is collapsed.
import logging
import requests
from http import HTTPStatus
......@@ -10,7 +9,7 @@ from flask import current_app as app
from flask.blueprints import Blueprint
from amundsen_application.log.action_log import action_logging
from amundsen_application.api.utils.request_utils import get_query_param
from amundsen_application.api.utils.request_utils import get_query_param, request_wrapper
LOGGER = logging.getLogger(__name__)
......@@ -198,14 +197,11 @@ def _search_table(*, search_term: str, page_index: int) -> Dict[str, Any]:
search_term,
page_index)
# TODO: Create an abstraction for this logic that is reused many times
if app.config['SEARCHSERVICE_REQUEST_CLIENT'] is not None:
envoy_client = app.config['SEARCHSERVICE_REQUEST_CLIENT']
envoy_headers = app.config['SEARCHSERVICE_REQUEST_HEADERS']
response = envoy_client.get(url, headers=envoy_headers, raw_response=True)
else:
with requests.Session() as s:
response = s.get(url, timeout=REQUEST_SESSION_TIMEOUT)
response = request_wrapper(method='GET',
url=url,
client=app.config['SEARCHSERVICE_REQUEST_CLIENT'],
headers=app.config['SEARCHSERVICE_REQUEST_HEADERS'],
timeout_sec=REQUEST_SESSION_TIMEOUT)
status_code = response.status_code
......
from typing import Dict
import requests
def get_query_param(args: Dict, param: str, error_msg: str = None) -> str:
......@@ -7,3 +8,39 @@ def get_query_param(args: Dict, param: str, error_msg: str = None) -> str:
msg = 'A {0} parameter must be provided'.format(param) if error_msg is not None else error_msg
raise Exception(msg)
return value
# TODO: Define an interface for envoy_client
def request_wrapper(method: str, url: str, client, headers, timeout_sec: int): # type: ignore
"""
Wraps a request to use Envoy client and headers, if available
:param method: DELETE | GET | POST | PUT
:param url: The request URL
:param client: Optional Envoy client
:param headers: Optional Envoy request headers
:param timeout_sec: Number of seconds before timeout is triggered. Not used with Envoy
:return:
"""
if client is not None:
if method == 'DELETE':
return client.delete(url, headers=headers, raw_response=True)
elif method == 'GET':
return client.get(url, headers=headers, raw_response=True)
elif method == 'POST':
return client.post(url, headers=headers, raw_response=True)
elif method == 'PUT':
return client.put(url, headers=headers, raw_response=True)
else:
raise Exception('Method not allowed: {}'.format(method))
else:
with requests.Session() as s:
if method == 'DELETE':
return s.delete(url, timeout=timeout_sec)
elif method == 'GET':
return s.get(url, timeout=timeout_sec)
elif method == 'POST':
return s.post(url, timeout=timeout_sec)
elif method == 'PUT':
return s.put(url, timeout=timeout_sec)
else:
raise Exception('Method not allowed: {}'.format(method))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment