Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fastapi import FastAPI, HTTPException, Path, Body
- import httpx
- import json
- from pydantic import BaseModel
- from typing import Any, Dict, List
- app = FastAPI()
- # The base URL of the target service
- TARGET_BASE_URL = "http://20.212.243.159:7800"
- # Pydantic model for the request body
- class AnswerRequest(BaseModel):
- answer: str
- # Pydantic model for the sync database request
- class SyncDatabaseRequest(BaseModel):
- mode: str
- test_users: List[str]
- @app.post("/api/forms/{form_id}/{user_id}/answer")
- async def proxy_answer(
- form_id: str = Path(..., description="Form ID"),
- user_id: str = Path(..., description="User ID"),
- request_data: AnswerRequest = Body(...)
- ):
- """
- Proxy endpoint that forwards form answer requests to the target service.
- """
- target_url = f"{TARGET_BASE_URL}/api/forms/{form_id}/{user_id}/answer"
- try:
- async with httpx.AsyncClient() as client:
- response = await client.post(
- target_url,
- json={"answer": request_data.answer},
- headers={
- "Content-Type": "application/json",
- "accept": "application/json"
- }
- )
- # Return the response from the target service
- return response.json()
- except httpx.HTTPError as e:
- raise HTTPException(status_code=500, detail=f"Error forwarding request: {str(e)}")
- @app.post("/api/sync-database")
- async def sync_database(
- request_data: SyncDatabaseRequest = Body(...)
- ):
- """
- Proxy endpoint that forwards database synchronization requests to the target service.
- """
- target_url = f"{TARGET_BASE_URL}/api/sync-database"
- try:
- async with httpx.AsyncClient() as client:
- response = await client.post(
- target_url,
- json={
- "mode": request_data.mode,
- "test_users": request_data.test_users
- },
- headers={
- "Content-Type": "application/json",
- "accept": "application/json"
- }
- )
- # Return the response from the target service
- return response.json()
- except httpx.HTTPError as e:
- raise HTTPException(status_code=500, detail=f"Error forwarding request: {str(e)}")
- @app.get("/api/forms/{form_id}/{user_id}/current-question")
- async def get_current_question(
- form_id: str = Path(..., description="Form ID"),
- user_id: str = Path(..., description="User ID")
- ):
- """
- Proxy endpoint that forwards requests to get the current question from the target service.
- """
- target_url = f"{TARGET_BASE_URL}/api/forms/{form_id}/{user_id}/current-question"
- try:
- async with httpx.AsyncClient() as client:
- response = await client.get(
- target_url,
- headers={
- "accept": "application/json"
- }
- )
- # Return the response from the target service
- return response.json()
- except httpx.HTTPError as e:
- raise HTTPException(status_code=500, detail=f"Error forwarding request: {str(e)}")
- if __name__ == "__main__":
- import uvicorn
- uvicorn.run(app, host="0.0.0.0", port=8000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement