Skip to content
Snippets Groups Projects
Commit fada137f authored by Neda Moeini's avatar Neda Moeini
Browse files

Improved the code base to avoid getting new token for every request.

parent bb31d767
No related branches found
No related tags found
1 merge request!37Feature/dboard3 921
...@@ -31,17 +31,17 @@ def get_token(aai_config: dict) -> str: ...@@ -31,17 +31,17 @@ def get_token(aai_config: dict) -> str:
return response.json()['access_token'] return response.json()['access_token']
def make_request(body: dict) -> dict: def make_request(body: dict, token: str) -> dict:
"""Make a request to the orchestrator using the given body.""" """Make a request to the orchestrator using the given body."""
config = current_app.config['INVENTORY_PROVIDER_CONFIG'] config = current_app.config['INVENTORY_PROVIDER_CONFIG']
api_url = f'{config["orchestrator"]["url"]}/api/graphql' api_url = f'{config["orchestrator"]["url"]}/api/graphql'
headers = {'Authorization': f'Bearer {get_token(config["aai"])}'} headers = {'Authorization': f'Bearer {token}'}
response = requests.post(api_url, headers=headers, json=body) response = requests.post(api_url, headers=headers, json=body)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
def extract_router_info(device: dict) -> dict or None: def extract_router_info(device: dict, token: str) -> dict or None:
tag_to_key_map = { tag_to_key_map = {
"RTR": "router", "RTR": "router",
"OFFICE_ROUTER": "officeRouter", "OFFICE_ROUTER": "officeRouter",
...@@ -71,7 +71,7 @@ def extract_router_info(device: dict) -> dict or None: ...@@ -71,7 +71,7 @@ def extract_router_info(device: dict) -> dict or None:
}} }}
""" """
response = make_request(body={'query': query}) response = make_request(body={'query': query}, token=token)
page_data = response.get('data', {}).get('subscriptions', {}).get('page') page_data = response.get('data', {}).get('subscriptions', {}).get('page')
if not page_data: if not page_data:
...@@ -92,7 +92,7 @@ def extract_router_info(device: dict) -> dict or None: ...@@ -92,7 +92,7 @@ def extract_router_info(device: dict) -> dict or None:
def load_routers_from_orchestrator() -> dict: def load_routers_from_orchestrator() -> dict:
"""Gets devices from the orchestrator and returns a dictionary of FQDNs and vendors.""" """Gets devices from the orchestrator and returns a dictionary of FQDNs and vendors."""
token = get_token(current_app.config['INVENTORY_PROVIDER_CONFIG']['aai'])
query = """ query = """
{ {
subscriptions( subscriptions(
...@@ -110,14 +110,14 @@ def load_routers_from_orchestrator() -> dict: ...@@ -110,14 +110,14 @@ def load_routers_from_orchestrator() -> dict:
} }
""" """
routers = {} routers = {}
response = make_request(body={'query': query}) response = make_request(body={'query': query}, token=token)
try: try:
devices = response['data']['subscriptions']['page'] devices = response['data']['subscriptions']['page']
except (TypeError, KeyError): except (TypeError, KeyError):
devices = [] devices = []
with concurrent.futures.ThreadPoolExecutor() as executor: with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(extract_router_info, device) for device in devices] futures = [executor.submit(extract_router_info, device, token) for device in devices]
for future in concurrent.futures.as_completed(futures): for future in concurrent.futures.as_completed(futures):
router_info = future.result() router_info = future.result()
if router_info is not None: if router_info is not None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment