marcmaxmeister commited on
Commit
42b057a
·
verified ·
1 Parent(s): 5bdc12f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -10,8 +10,9 @@ app = FastAPI(
10
  redoc_url=None
11
  )
12
 
13
- # Access the secret environment variable
14
  private_space_token = os.environ.get('api_key')
 
15
  api_url = "https://givingtuesday-annotated-990-data.hf.space"
16
  headers = {
17
  "Authorization": f"Bearer {private_space_token}",
@@ -24,6 +25,7 @@ async def root():
24
 
25
  @app.get('/help/', response_class=HTMLResponse)
26
  async def pass_docs_html():
 
27
  async with httpx.AsyncClient(timeout=60.0) as client:
28
  response = await client.get(f"{api_url}/docs#/default/get_eins_eins__get", headers=headers)
29
  html_content = response.text
@@ -56,19 +58,22 @@ async def proxy_request(request: Request):
56
  # Construct the full URL for the private space endpoint
57
  private_api_url = f"{api_url}/eins/?{encoded_params}"
58
 
59
- # Use httpx to make an asynchronous GET request to the private API
60
- try:
61
- print('params', private_api_url)
62
- async with httpx.AsyncClient(timeout=60.0) as client:
63
- response = await client.get(private_api_url, headers=headers)
64
- response.raise_for_status() # Raise an exception for 4xx/5xx responses
65
- return response.json()
66
-
67
- except httpx.HTTPStatusError as e:
68
- raise HTTPException(status_code=response.status_code, detail=f"Error: {str(e)}")
69
- except httpx.RequestError as e:
70
- raise HTTPException(status_code=500, detail=f"Network error: {str(e)}")
71
-
 
 
 
72
 
73
  if __name__ == "__main__":
74
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
10
  redoc_url=None
11
  )
12
 
13
+ # Access the secret environment variables
14
  private_space_token = os.environ.get('api_key')
15
+ use_token = os.environ.get('use_token')
16
  api_url = "https://givingtuesday-annotated-990-data.hf.space"
17
  headers = {
18
  "Authorization": f"Bearer {private_space_token}",
 
25
 
26
  @app.get('/help/', response_class=HTMLResponse)
27
  async def pass_docs_html():
28
+ """BUG: the javascript renders THIS fastapi, not the html pulled. Use a static content page here"""
29
  async with httpx.AsyncClient(timeout=60.0) as client:
30
  response = await client.get(f"{api_url}/docs#/default/get_eins_eins__get", headers=headers)
31
  html_content = response.text
 
58
  # Construct the full URL for the private space endpoint
59
  private_api_url = f"{api_url}/eins/?{encoded_params}"
60
 
61
+ # check access token match
62
+ if 'key' in query_params and query_params['key'] == use_token:
63
+ # Use httpx to make an asynchronous GET request to the private API
64
+ try:
65
+ print('params', private_api_url)
66
+ async with httpx.AsyncClient(timeout=60.0) as client:
67
+ response = await client.get(private_api_url, headers=headers)
68
+ response.raise_for_status() # Raise an exception for 4xx/5xx responses
69
+ return response.json()
70
+
71
+ except httpx.HTTPStatusError as e:
72
+ raise HTTPException(status_code=response.status_code, detail=f"Error: {str(e)}")
73
+ except httpx.RequestError as e:
74
+ raise HTTPException(status_code=500, detail=f"Network error: {str(e)}")
75
+ else:
76
+ return {"error": "access token required"}
77
 
78
  if __name__ == "__main__":
79
  uvicorn.run(app, host="0.0.0.0", port=7860)