Skip to content

Commit

Permalink
Add timeouts to API requests and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DonnieBLT committed Dec 22, 2024
1 parent 7b7764c commit 30418e9
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions website/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ def check_status(request):
"params": [],
},
auth=HTTPBasicAuth(bitcoin_rpc_user, bitcoin_rpc_password),
timeout=2, # Set a timeout to avoid hanging
)
if response.status_code == 200:
data = response.json().get("result", {})
status["bitcoin"] = True
status["bitcoin_block"] = data.get("blocks", None)
except Exception as e:
except requests.exceptions.RequestException as e:
print(f"Bitcoin Core Node Error: {e}")

try:
Expand All @@ -110,14 +111,12 @@ def check_status(request):
else:
try:
headers = {"Authorization": f"token {github_token}"}
response = requests.get("https://api.github.com/user/repos", headers=headers)

print(f"Response Status Code: {response.status_code}")
print(f"Response Content: {response.json()}")
response = requests.get(
"https://api.github.com/user/repos", headers=headers, timeout=5
)

if response.status_code == 200:
status["github"] = True
print("GitHub API token has repository access.")
else:
status["github"] = False
print(
Expand Down Expand Up @@ -228,7 +227,8 @@ def chatbot_conversation(request):

if request_count >= DAILY_REQUEST_LIMIT:
return Response(
{"error": "Daily request limit exceeded."}, status=status.HTTP_429_TOO_MANY_REQUESTS
{"error": "Daily request limit exceeded."},
status=status.HTTP_429_TOO_MANY_REQUESTS,
)

question = request.data.get("question", "")
Expand Down Expand Up @@ -321,7 +321,10 @@ def vote_suggestions(request):

if up_vote or down_vote:
voted = SuggestionVotes.objects.create(
user=user, suggestion=suggestion, up_vote=up_vote, down_vote=down_vote
user=user,
suggestion=suggestion,
up_vote=up_vote,
down_vote=down_vote,
)

if up_vote:
Expand Down Expand Up @@ -643,7 +646,8 @@ def get_last_commit_date():
try:
return (
subprocess.check_output(
["git", "log", "-1", "--format=%cd"], cwd=os.path.dirname(os.path.dirname(__file__))
["git", "log", "-1", "--format=%cd"],
cwd=os.path.dirname(os.path.dirname(__file__)),
)
.decode("utf-8")
.strip()
Expand Down

0 comments on commit 30418e9

Please sign in to comment.