Skip to content

Commit

Permalink
Edit fetching of teams
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanRosenboim committed Aug 19, 2024
1 parent e32ff26 commit 2303c6e
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions src/shared/clients/jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,35 @@ def list_assets(token: str) -> List[Asset]:


def get_existing_teams(token: str) -> List[TeamAttributes]:
def _handle_resoponse(response, existing_teams):
response = response.json()
data = response['data']
def _handle_response(response, existing_teams):
response_json = response.json()
data = response_json['data']
existing_teams.extend(data)
after = response['metadata']['after']
return after
return response_json['metadata'].get('after')

try:
# Make a GET request to the asset API
url = f"{get_jit_endpoint_base_url()}/teams?limit=100"

headers = get_request_headers(token)
response = requests.get(url, headers=headers)
existing_teams = []
# Check if the request was successful
if response.status_code == 200:
after = _handle_resoponse(response, existing_teams)
while True:
response = requests.get(
f"{url}&after={after}", headers=headers)
if response.status_code == 200:
after = _handle_resoponse(response, existing_teams)
if not after:
break
else:
logger.error(
f"Failed to retrieve teams. Status code: {response.status_code}, {response.text}")
return []
after = None

logger.info(
f"Retrieved {len(existing_teams)} existing teams successfully.")
return [TeamAttributes(**team) for team in existing_teams]
else:
logger.error(
f"Failed to retrieve teams. Status code: {response.status_code}, {response.text}")
return []
while True:
current_url = f"{url}&after={after}" if after else url
response = requests.get(current_url, headers=headers)

if response.status_code != 200:
logger.error(
f"Failed to retrieve teams. Status code: {response.status_code}, {response.text}")
return []

after = _handle_response(response, existing_teams)

if not after:
break

logger.info(
f"Retrieved {len(existing_teams)} existing teams successfully.")
return [TeamAttributes(**team) for team in existing_teams]
except Exception as e:
logger.error(f"Failed to retrieve teams: {str(e)}")
return []
Expand Down

0 comments on commit 2303c6e

Please sign in to comment.