| from fastapi import FastAPI, HTTPException, Header, Query |
| from pydantic import BaseModel, StrictStr |
| from searcher import user_Data |
| import json |
|
|
| app = FastAPI() |
| user_Agent = user_Data() |
| |
| with open("country_city_dict.json", "r", encoding="utf-8") as f: |
| COUNTRY_CITY_DATA = json.load(f) |
|
|
| with open("languages.json", "r", encoding="utf-8") as f: |
| LANGUAGES_DATA = json.load(f) |
|
|
| @app.get("/") |
| async def get_request(): |
| HTTPException(status_code=500, detail="Internal Server Error") |
|
|
|
|
| class InputData(BaseModel): |
| businessName: StrictStr |
| country: StrictStr |
| city: StrictStr |
|
|
| @app.post("/get") |
| async def run_script(input_Data: InputData, token: str = Header(None, alias="api-token")): |
| try: |
| if token: |
| res = user_Agent.get_Search_Results(input_Data, token) |
| if res: |
| return {"status": 200, "data": res} |
| else: |
| raise HTTPException(status_code=500, detail="The request can not be fulfilled") |
| else: |
| raise HTTPException(status_code=400, detail="Missing 'api-token'.") |
| except Exception as e: |
| raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
| @app.get("/cities") |
| async def get_cities(country: str = Query(default="", description="Country name")): |
| """ |
| - If 'country' is empty: returns list of supported countries |
| - If 'country' is valid: returns list of cities |
| - Else: raises 400 error |
| """ |
| |
| if not country: |
| return {"data": sorted(list(COUNTRY_CITY_DATA.keys()))} |
| |
| if country in COUNTRY_CITY_DATA: |
| return {"data": sorted(COUNTRY_CITY_DATA[country])} |
| |
| |
| raise HTTPException(status_code=400, detail=f"Unsupported country: {country}") |
|
|
|
|
| @app.get("/languages") |
| async def get_cities(): |
| """ |
| returns a json of languages as keys and theyr code as value |
| """ |
| |
| return {"data": dict(sorted(LANGUAGES_DATA.items(), key=lambda item: item[1]))} |
| |
|
|
|
|
| |