diff --git a/src/TaxisLibrary/taxis_client.py b/src/TaxisLibrary/taxis_client.py index 88bb867..c2691a0 100644 --- a/src/TaxisLibrary/taxis_client.py +++ b/src/TaxisLibrary/taxis_client.py @@ -209,16 +209,19 @@ class HTTPValidationError(BaseModel): detail: List[ValidationError] # Resolve forward references -CarOut.update_forward_refs() -ClientOut.update_forward_refs() -DriverOut.update_forward_refs() -OrderOut.update_forward_refs() +CarOut.model_rebuild() +ClientOut.model_rebuild() +DriverOut.model_rebuild() +OrderOut.model_rebuild() # Client class TaxisClient: def __init__(self, base_url: str, api_key: Optional[str] = None): self.base_url = base_url.rstrip('/') self.api_key = api_key + self.headers={ + "Authorization": f"Bearer {self.api_key}" if self.api_key else "" + } async def __aenter__(self): return self @@ -227,15 +230,11 @@ class TaxisClient: pass - async def _request(self, method: str, endpoint: str, params: Dict[str, Any] = None, json_data: Dict[str, Any] = None) -> Any: + async def _request(self, method: str, endpoint: str, **kwargs) -> Any: url = f"{self.base_url}/{endpoint.strip('/')}" - params = params or {} - json_data = json_data or {} - headers={ - "Authorization": f"Bearer {self.api_key}" if self.api_key else None - } - async with aiohttp.ClientSession(headers=headers) as session: - async with session.request(method, url, params=params, json=json_data) as resp: + + async with aiohttp.ClientSession(headers=self.headers) as session: + async with session.request(method, url, **kwargs) as resp: if resp.status == 401: raise Exception("Unauthorized") resp.raise_for_status() @@ -245,19 +244,18 @@ class TaxisClient: return await resp.text() async def login(self, username: str, password: str, grant_type: str = "password", - scope: str = "", client_id: Optional[str] = None, - client_secret: Optional[str] = None) -> 'ApiKeyOut': + scope: str = "") -> 'ApiKeyOut': data = { "grant_type": grant_type, "username": username, "password": password, "scope": scope, - "client_id": client_id, - "client_secret": client_secret + "client_id": "taxi", + "client_secret": "taxi" } result = await self._request("POST", "/auth/login", data=data) self.api_key = result["api_key"] - self.session.headers.update({"Authorization": f"Bearer {self.api_key}"}) + self.headers.update({"Authorization": f"Bearer {self.api_key}"}) return ApiKeyOut(**result) # Prompts @@ -266,7 +264,7 @@ class TaxisClient: return [PromptOut(**item) for item in result] async def create_prompt(self, prompt: 'PromptIn') -> 'PromptOut': - result = await self._request("POST", "/prompts/", json_data=prompt.dict()) + result = await self._request("POST", "/prompts/", json=prompt.dict()) return PromptOut(**result) async def get_prompt(self, prompt_id: int, **params) -> 'PromptOut': @@ -274,7 +272,7 @@ class TaxisClient: return PromptOut(**result) async def update_prompt(self, prompt_id: int, prompt: 'PromptIn') -> 'PromptOut': - result = await self._request("PUT", f"/prompts/{prompt_id}", json_data=prompt.dict()) + result = await self._request("PUT", f"/prompts/{prompt_id}", json=prompt.dict()) return PromptOut(**result) async def delete_prompt(self, prompt_id: int) -> None: @@ -286,7 +284,7 @@ class TaxisClient: return [CarOut(**item) for item in result] async def create_car(self, car: 'CarIn') -> 'CarOut': - result = await self._request("POST", "/cars/", json_data=car.dict()) + result = await self._request("POST", "/cars/", json=car.dict()) return CarOut(**result) async def get_car(self, car_id: int, **params) -> 'CarOut': @@ -294,7 +292,7 @@ class TaxisClient: return CarOut(**result) async def update_car(self, car_id: int, car: 'CarIn') -> 'CarOut': - result = await self._request("PUT", f"/cars/{car_id}", json_data=car.dict()) + result = await self._request("PUT", f"/cars/{car_id}", json=car.dict()) return CarOut(**result) async def delete_car(self, car_id: int) -> None: @@ -306,7 +304,7 @@ class TaxisClient: return [DriverOut(**item) for item in result] async def create_driver(self, driver: 'DriverIn') -> 'DriverOut': - result = await self._request("POST", "/drivers/", json_data=driver.dict()) + result = await self._request("POST", "/drivers/", json=driver.dict()) return DriverOut(**result) async def get_driver(self, driver_id: int, **params) -> 'DriverOut': @@ -318,14 +316,14 @@ class TaxisClient: return DriverOut(**result) async def update_driver(self, driver_id: int, driver: 'DriverIn') -> 'DriverOut': - result = await self._request("PUT", f"/drivers/{driver_id}", json_data=driver.dict()) + result = await self._request("PUT", f"/drivers/{driver_id}", json=driver.dict()) return DriverOut(**result) async def delete_driver(self, driver_id: int) -> None: await self._request("DELETE", f"/drivers/{driver_id}") async def update_driver_address(self, driver_id: int, address: 'DriverAddressUpdate') -> 'DriverOut': - result = await self._request("PUT", f"/drivers/{driver_id}/address", json_data=address.dict()) + result = await self._request("PUT", f"/drivers/{driver_id}/address", json=address.dict()) return DriverOut(**result) async def update_driver_status(self, driver_id: int, status: 'DriverStatus') -> 'DriverOut': @@ -354,7 +352,7 @@ class TaxisClient: return [ClientOut(**item) for item in result] async def create_client(self, client: 'ClientIn') -> 'ClientOut': - result = await self._request("POST", "/clients/", json_data=client.dict()) + result = await self._request("POST", "/clients/", json=client.dict()) return ClientOut(**result) async def get_client(self, client_id: int, **params) -> 'ClientOut': @@ -366,7 +364,7 @@ class TaxisClient: return ClientOut(**result) async def update_client(self, client_id: int, client: 'ClientIn') -> 'ClientOut': - result = await self._request("PUT", f"/clients/{client_id}", json_data=client.dict()) + result = await self._request("PUT", f"/clients/{client_id}", json=client.dict()) return ClientOut(**result) async def delete_client(self, client_id: int) -> None: @@ -378,7 +376,7 @@ class TaxisClient: return [OrderOut(**item) for item in result] async def create_order(self, order: 'OrderIn') -> 'OrderOut': - result = await self._request("POST", "/orders/", json_data=order.dict()) + result = await self._request("POST", "/orders/", json=order.dict()) return OrderOut(**result) async def get_order(self, order_id: int, **params) -> 'OrderOut': @@ -386,7 +384,7 @@ class TaxisClient: return OrderOut(**result) async def update_order(self, order_id: int, order: 'OrderInUpdate') -> 'OrderOut': - result = await self._request("PUT", f"/orders/{order_id}", json_data=order.dict()) + result = await self._request("PUT", f"/orders/{order_id}", json=order.dict()) return OrderOut(**result) async def delete_order(self, order_id: int) -> None: @@ -410,7 +408,7 @@ class TaxisClient: return [OrderNotificationsOut(**item) for item in result] async def create_order_notification(self, notification: 'OrderNotificationsIn') -> 'OrderNotificationsOut': - result = await self._request("POST", "/order_notifications/", json_data=notification.dict()) + result = await self._request("POST", "/order_notifications/", json=notification.dict()) return OrderNotificationsOut(**result) async def get_order_notification(self, notification_id: int, **params) -> 'OrderNotificationsOut': @@ -430,7 +428,7 @@ class TaxisClient: return GlobalSettingsOut(**result) async def update_global_settings(self, settings: 'GlobalSettingsUpdate') -> 'GlobalSettingsOut': - result = await self._request("PUT", "/settings/global", json_data=settings.dict()) + result = await self._request("PUT", "/settings/global", json=settings.dict()) return GlobalSettingsOut(**result) async def get_nominatium_settings(self, nominatium_id: int, **params) -> 'NominatiumSettingsOut': @@ -449,9 +447,9 @@ class TaxisClient: return Geocode(**result) async def get_distance(self, request: 'DistanceRequest') -> float: - result = await self._request("POST", "/union/distance", json_data=request.dict()) + result = await self._request("POST", "/union/distance", json=request.dict()) return float(result) async def get_distance_by_address(self, request: 'AddressRequest') -> float: - result = await self._request("POST", "/union/distance_by_address", json_data=request.dict()) + result = await self._request("POST", "/union/distance_by_address", json=request.dict()) return float(result)