top_questions / main.py
Siddhant
recent posts service added
8073ee5 unverified
raw
history blame
1.66 kB
from fastapi import FastAPI, HTTPException, Header, status, Body, Request
from typing import Annotated
import os
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
from utils.GetTopAndRecentQuestions import return_top_question, return_recent_posts
load_dotenv()
app = FastAPI(docs_url="/documentation", redoc_url=None)
origins = [
"http://localhost:4200",
"https://releasepreview.twimbit.com"
]
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
token = os.getenv("token")
@app.get("/get_top_questions", status_code=status.HTTP_200_OK)
def get_top_questions_(limit: int = 5, Authorization: Annotated[list[str] | None, Header()] = None):
if Authorization is None or Authorization[0] != "Bearer {}".format(token):
raise HTTPException(status_code=401, detail="Unauthorised.")
try:
return {'data': return_top_question(limit)}
except Exception as e:
# return a success message
raise HTTPException(status_code=400, detail=str(e))
@app.get("/get_recent_posts", status_code=status.HTTP_200_OK)
def get_recent_posts_(limit: int = 5, strategy: str = 'recent',
Authorization: Annotated[list[str] | None, Header()] = None):
if Authorization is None or Authorization[0] != "Bearer {}".format(token):
raise HTTPException(status_code=401, detail="Unauthorised.")
try:
return {'data': return_recent_posts(limit, strategy)}
except Exception as e:
# return a success message
raise HTTPException(status_code=400, detail=str(e))