Spaces:
Runtime error
Runtime error
| import requests | |
| def read_file(repo_url: str, file_path="README.md") -> str: | |
| try: | |
| # Extract owner and repo name | |
| parts = repo_url.rstrip("/").split("/") | |
| owner, repo = parts[-2], parts[-1] | |
| # Step 1: Get default branch | |
| repo_api_url = f"https://api.github.com/repos/{owner}/{repo}" | |
| repo_response = requests.get(repo_api_url) | |
| if repo_response.status_code != 200: | |
| return f"❌ Failed to fetch repo info. Status: {repo_response.status_code}" | |
| default_branch = repo_response.json().get("default_branch", "main") | |
| # Step 2: Fetch the file using GitHub Contents API | |
| api_url = f"https://api.github.com/repos/{owner}/{repo}/contents/{file_path}?ref={default_branch}" | |
| headers = {"Accept": "application/vnd.github.v3.raw"} | |
| response = requests.get(api_url, headers=headers) | |
| if response.status_code == 200: | |
| return response.text | |
| else: | |
| return f"❌ Could not retrieve `{file_path}`. Status code: {response.status_code}" | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |