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
Branches
Tags
1 merge request!37Feature/dboard3 921
......@@ -31,17 +31,17 @@ def get_token(aai_config: dict) -> str:
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."""
config = current_app.config['INVENTORY_PROVIDER_CONFIG']
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.raise_for_status()
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 = {
"RTR": "router",
"OFFICE_ROUTER": "officeRouter",
......@@ -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')
if not page_data:
......@@ -92,7 +92,7 @@ def extract_router_info(device: dict) -> dict or None:
def load_routers_from_orchestrator() -> dict:
"""Gets devices from the orchestrator and returns a dictionary of FQDNs and vendors."""
token = get_token(current_app.config['INVENTORY_PROVIDER_CONFIG']['aai'])
query = """
{
subscriptions(
......@@ -110,14 +110,14 @@ def load_routers_from_orchestrator() -> dict:
}
"""
routers = {}
response = make_request(body={'query': query})
response = make_request(body={'query': query}, token=token)
try:
devices = response['data']['subscriptions']['page']
except (TypeError, KeyError):
devices = []
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):
router_info = future.result()
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