-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
38 lines (33 loc) · 1.03 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from fastapi import FastAPI
from typing import Optional
from starlette.middleware.cors import CORSMiddleware
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
import httpx
import os
app = FastAPI()
HEADERS = {'Token': os.environ["TOKEN"], 'Route-User': os.environ["ROUTE_USER"]}
URL = os.environ["ROR_API_URL"]
origins = [
os.environ['ALLOWED_ORIGINS']
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/generateid")
async def get_ror_id(mode: Optional[str] = None):
# if being sent in any mode but production
# mock an id
if mode:
data = {'id':'https://ror.org/012DEV089'}
json_compatible_item_data = jsonable_encoder(data)
response = JSONResponse(content=json_compatible_item_data)
else:
async with httpx.AsyncClient() as client:
response = await client.get(URL, headers=HEADERS)
response = response.json()
return response