Spaces:
Runtime error
Runtime error
Create repositories/github_api.py
Browse files- repositories/github_api.py +27 -0
repositories/github_api.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
class GitHubAPI:
|
4 |
+
def __init__(self):
|
5 |
+
self.base_url = 'https://api.github.com'
|
6 |
+
|
7 |
+
def get_repository(self, repo_url):
|
8 |
+
try:
|
9 |
+
owner, repo = repo_url.split("github.com/")[1].split("/")
|
10 |
+
api_url = f"{self.base_url}/repos/{owner}/{repo}/contents"
|
11 |
+
response = requests.get(api_url)
|
12 |
+
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
13 |
+
return response.json()
|
14 |
+
except requests.exceptions.RequestException as e:
|
15 |
+
return f"Error fetching repository: {e}"
|
16 |
+
except IndexError:
|
17 |
+
return "Invalid GitHub repository URL. Please use a format like: https://github.com/owner/repo"
|
18 |
+
except Exception as e:
|
19 |
+
return f"An unexpected error occurred: {e}"
|
20 |
+
|
21 |
+
def get_file_content(self, download_url):
|
22 |
+
try:
|
23 |
+
response = requests.get(download_url)
|
24 |
+
response.raise_for_status()
|
25 |
+
return response.text
|
26 |
+
except requests.exceptions.RequestException as e:
|
27 |
+
return f"Error fetching file content: {e}"
|