Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example usage of httpx #6325

Merged
merged 9 commits into from
Jan 22, 2025
42 changes: 19 additions & 23 deletions openhands/server/routes/github.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import httpx
import requests
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import JSONResponse
Expand Down Expand Up @@ -47,46 +48,41 @@ async def get_github_repositories(

# Fetch repositories from GitHub
try:
response = await call_sync_from_async(
requests.get, github_api_url, headers=headers, params=params
)
response.raise_for_status() # Raise an error for HTTP codes >= 400
async with httpx.AsyncClient() as client:
response = await client.get(github_api_url, headers=headers, params=params)
response.raise_for_status() # Raise an error for HTTP codes >= 400
json_response = JSONResponse(content=response.json())

# Forward the Link header if it exists
if 'Link' in response.headers:
json_response.headers['Link'] = response.headers['Link']

return json_response

except requests.exceptions.RequestException as e:
raise HTTPException(
status_code=response.status_code if response else 500,
detail=f'Error fetching repositories: {str(e)}',
)

# Create response with the JSON content
json_response = JSONResponse(content=response.json())
response.close()

# Forward the Link header if it exists
if 'Link' in response.headers:
json_response.headers['Link'] = response.headers['Link']

return json_response


@app.get('/user')
async def get_github_user(github_token: str = Depends(require_github_token)):
headers = generate_github_headers(github_token)
try:
response = await call_sync_from_async(
requests.get, 'https://api.github.com/user', headers=headers
)
response.raise_for_status()
async with httpx.AsyncClient() as client:
response = await client.get('https://api.github.com/user', headers=headers)
response.raise_for_status() # Raise an error for HTTP codes >= 400
json_response = JSONResponse(content=response.json())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructing a JSONResponse here is odd, but it looks like we were already doing it...


return json_response

except requests.exceptions.RequestException as e:
raise HTTPException(
status_code=response.status_code if response else 500,
detail=f'Error fetching user: {str(e)}',
)

json_response = JSONResponse(content=response.json())
response.close()

return json_response


@app.get('/installations')
async def get_github_installation_ids(
Expand Down
Loading