File size: 1,660 Bytes
bd8cd5c
 
 
 
 
8073ee5
bd8cd5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01c0c82
bd8cd5c
 
 
8073ee5
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
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))