From 2f4bddb11f4b85ee13019f1edb058f2ed1303f28 Mon Sep 17 00:00:00 2001 From: Nathan Falvey Date: Fri, 27 Feb 2026 23:36:14 +0000 Subject: [PATCH] Refactor debug_request function in update_profile.py to utilize do_request for improved error handling and code clarity --- update_profile.py | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/update_profile.py b/update_profile.py index 23c8c3e..21a74bb 100644 --- a/update_profile.py +++ b/update_profile.py @@ -7,33 +7,23 @@ GITEA_URL = "https://gitea.nathan-falvey.synology.me" USERNAME = "nathan" GITEA_TOKEN = os.getenv("GITEA_TOKEN") +def do_request(url, headers=None): + try: + response = requests.get(url, headers=headers) + response.raise_for_status() # Will raise an HTTPError for bad responses + return response.json() + except requests.exceptions.RequestException as e: + print(f"Request failed: {e}") + return None + def debug_request(): headers = {"Authorization": f"token {GITEA_TOKEN}"} print(f"--- Testing Connection to: {GITEA_URL} ---") try: - # 1. Test Version API (Public-ish) - print("\n1. Testing Version API...") - v_res = requests.get(f"{GITEA_URL}/api/v1/version", headers=headers) - print(f"Status: {v_res.status_code}") - print(f"Response: {v_res.text}") - - # 2. Test User Repo API (Requires Token) - print("\n2. Testing User Repos API...") - r_res = requests.get(f"{GITEA_URL}/api/v1/user/repos?type=owner", headers=headers) - print(f"Status: {r_res.status_code}") - - if r_res.status_code != 200: - print("❌ ERROR: API rejected the token or URL is wrong.") - print(f"Server Message: {r_res.text}") - else: - repos = r_res.json() - print(f"✅ Success! Found {len(repos)} repositories.") - # Print just the first repo to see the structure - if repos: - print(f"Example Repo Data: {repos[0]['name']} - {repos[0]['html_url']}") - + print(do_request(f"{GITEA_URL}/api/v1/version", headers=headers)) + print(do_request(f"{GITEA_URL}/api/v1/user/repos?type=owner", headers=headers)) except Exception: print("\n" + "!"*30) print("STACK TRACE (Line Numbers):")