From 1638f40eccc4a1321ee1bf19e3756157b6c965e1 Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Mon, 2 Sep 2024 00:21:13 +0200 Subject: refactored code, use get_ship dependency for ship related calls --- apirouters/agents.py | 21 ++++----------------- apirouters/customize_ship.py | 13 +++++-------- apirouters/tasks.py | 32 +++++++++++++------------------- 3 files changed, 22 insertions(+), 44 deletions(-) (limited to 'apirouters') diff --git a/apirouters/agents.py b/apirouters/agents.py index 1b927e7..49d3740 100644 --- a/apirouters/agents.py +++ b/apirouters/agents.py @@ -1,30 +1,17 @@ from enum import Enum from typing import Annotated, Dict -import openapi_client from fastapi import APIRouter, Depends, Response, HTTPException from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm -from ..modules import task_types from ..modules.database import cursor, conn -from ..modules.ships import Ship +from ..entities import task_types +from ..entities.agent import Agent +from ..entities.ship import Ship router = APIRouter() oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") -agents: Dict[str, 'Agent'] = {} - - -class Agent: - def __init__(self, agent_symbol, token): - config = openapi_client.Configuration(access_token=token) - self.api_client = openapi_client.ApiClient(config) - - self.agents_api = openapi_client.AgentsApi(self.api_client) - self.fleet_api = openapi_client.FleetApi(self.api_client) - self.systems_api = openapi_client.SystemsApi(self.api_client) - - self.agent_symbol = agent_symbol - self.ships: Dict[str, Ship] = {} +agents: Dict[str, Agent] = {} @router.post("/token") diff --git a/apirouters/customize_ship.py b/apirouters/customize_ship.py index c69d421..ca3ef83 100644 --- a/apirouters/customize_ship.py +++ b/apirouters/customize_ship.py @@ -2,7 +2,8 @@ from typing import Annotated from fastapi import APIRouter, Depends from pydantic import BaseModel -from .agents import auth_agent, Agent +from ..modules import ships +from ..entities.ship import Ship router = APIRouter() @@ -11,12 +12,8 @@ class RenameBody(BaseModel): name: str -# TODO depend on ship instead of auth_agent @router.post("/customize_ship/{ship_symbol}/rename") -async def rename(ship_symbol: str, rename_body: RenameBody, agent: Annotated[Agent, Depends(auth_agent)]): - for current_ship in agent.ships.values(): - if current_ship.symbol == ship_symbol: - current_ship.rename(rename_body.name) - return current_ship.get_data() +async def rename(ship_symbol: str, rename_body: RenameBody, ship: Annotated[Ship, Depends(ships.get_ship)]): + ship.rename(rename_body.name) + return ship.get_data() - return {"error": "Unknown ship symbol."} diff --git a/apirouters/tasks.py b/apirouters/tasks.py index 02e091b..684cf60 100644 --- a/apirouters/tasks.py +++ b/apirouters/tasks.py @@ -2,28 +2,26 @@ from typing import Annotated from fastapi import APIRouter, Depends from pydantic import BaseModel -from ..modules import task_types -from .agents import auth_agent, Agent +from . import agents +from ..modules import ships +from ..entities import task_types +from ..entities.ship import Ship router = APIRouter() @router.get("/tasks") -async def get_tasks(agent: Annotated[Agent, Depends(auth_agent)]): - ships = [] +async def get_tasks(agent: Annotated[agents.Agent, Depends(agents.auth_agent)]): + ret_list = [] for current_ship in agent.ships.values(): - ships.append(current_ship.get_data()) + ret_list.append(current_ship.get_data()) - return {"ships": ships} + return {"ships": ret_list} @router.get("/task/{ship_symbol}") -async def get_tasks(ship_symbol: str, agent: Annotated[Agent, Depends(auth_agent)]): - for current_ship in agent.ships.values(): - if current_ship.symbol == ship_symbol: - return current_ship.get_data() - - return {"error": "Unknown ship symbol."} +async def get_tasks(ship: Annotated[Ship, Depends(ships.get_ship)]): + return ship.get_data() class SetTaskBody(BaseModel): @@ -31,10 +29,6 @@ class SetTaskBody(BaseModel): @router.post("/task/{ship_symbol}/set") -async def get_tasks(ship_symbol: str, set_task: SetTaskBody, agent: Annotated[Agent, Depends(auth_agent)]): - for current_ship in agent.ships.values(): - if current_ship.symbol == ship_symbol: - current_ship.set_task(set_task.task) - return current_ship.get_data() - - return {"error": "Unknown ship symbol."} +async def get_tasks(set_task: SetTaskBody, ship: Annotated[Ship, Depends(ships.get_ship)]): + ship.set_task(set_task.task) + return ship.get_data() -- cgit v1.2.3-70-g09d2