Skip to content

Commit

Permalink
Adding retry.
Browse files Browse the repository at this point in the history
  • Loading branch information
julialawrence committed Nov 15, 2023
1 parent ac895ed commit a9fdb23
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions app/azure_active_directory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
from flask import flash
import functools
import time

print = functools.partial(print, flush=True) # redefine to flush the buffer always

Expand Down Expand Up @@ -87,21 +88,30 @@ def create_team_from_group(group_id, access_token):
"funSettings": {"allowGiphy": True, "giphyContentRating": "Moderate"},
}

try:
response = requests.put(
url, headers=headers, json=team_data
) # Using PUT as per Graph API documentation for creating team from group
response.raise_for_status()
# If the request was successful, get the JSON response
return response.json()
except requests.exceptions.HTTPError as err:
print(f"An HTTP error occurred: {err}")
flash("An error occurred while creating the team.", "error")
return None
except Exception as e:
print(f"An unexpected error occurred: {e}")
flash("An unexpected error occurred while creating the team.", "error")
return None
retry_count = 0
max_retries = 5
backoff_time = 10 # seconds

while retry_count < max_retries:

try:
response = requests.put(
url, headers=headers, json=team_data
) # Using PUT as per Graph API documentation for creating team from group
response.raise_for_status()
# If the request was successful, get the JSON response
return response.json()
except requests.exceptions.HTTPError as err:
print(f"HTTP Error {err} encountered...")
if response.status_code == 404 and retry_count < max_retries - 1:
print(f"404 error encountered, retrying in {backoff_time} seconds...")
time.sleep(backoff_time)
retry_count += 1
continue
except Exception as e:
print(f"An unexpected error occurred: {e}")
flash("An unexpected error occurred while creating the team.", "error")
return None


def add_user_as_group_admin(group_id, user_id, access_token):
Expand Down

0 comments on commit a9fdb23

Please sign in to comment.