Compare commits

..

4 commits

4 changed files with 183 additions and 34 deletions

130
poetry.lock generated
View file

@ -168,6 +168,19 @@ docs = ["cogapp", "furo", "myst-parser", "sphinx", "sphinx-notfound-page", "sphi
tests = ["cloudpickle ; platform_python_implementation == \"CPython\"", "hypothesis", "mypy (>=1.11.1) ; platform_python_implementation == \"CPython\" and python_version >= \"3.10\"", "pympler", "pytest (>=4.3.0)", "pytest-mypy-plugins ; platform_python_implementation == \"CPython\" and python_version >= \"3.10\"", "pytest-xdist[psutil]"] tests = ["cloudpickle ; platform_python_implementation == \"CPython\"", "hypothesis", "mypy (>=1.11.1) ; platform_python_implementation == \"CPython\" and python_version >= \"3.10\"", "pympler", "pytest (>=4.3.0)", "pytest-mypy-plugins ; platform_python_implementation == \"CPython\" and python_version >= \"3.10\"", "pytest-xdist[psutil]"]
tests-mypy = ["mypy (>=1.11.1) ; platform_python_implementation == \"CPython\" and python_version >= \"3.10\"", "pytest-mypy-plugins ; platform_python_implementation == \"CPython\" and python_version >= \"3.10\""] tests-mypy = ["mypy (>=1.11.1) ; platform_python_implementation == \"CPython\" and python_version >= \"3.10\"", "pytest-mypy-plugins ; platform_python_implementation == \"CPython\" and python_version >= \"3.10\""]
[[package]]
name = "colorama"
version = "0.4.6"
description = "Cross-platform colored terminal text."
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
groups = ["test"]
markers = "sys_platform == \"win32\""
files = [
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
]
[[package]] [[package]]
name = "frozenlist" name = "frozenlist"
version = "1.7.0" version = "1.7.0"
@ -297,6 +310,18 @@ files = [
[package.extras] [package.extras]
all = ["flake8 (>=7.1.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"] all = ["flake8 (>=7.1.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"]
[[package]]
name = "iniconfig"
version = "2.1.0"
description = "brain-dead simple config-ini parsing"
optional = false
python-versions = ">=3.8"
groups = ["test"]
files = [
{file = "iniconfig-2.1.0-py3-none-any.whl", hash = "sha256:9deba5723312380e77435581c6bf4935c94cbfab9b1ed33ef8d238ea168eb760"},
{file = "iniconfig-2.1.0.tar.gz", hash = "sha256:3abbd2e30b36733fee78f9c7f7308f2d0050e88f0087fd25c2645f63c773e1c7"},
]
[[package]] [[package]]
name = "multidict" name = "multidict"
version = "6.6.4" version = "6.6.4"
@ -417,6 +442,34 @@ files = [
{file = "multidict-6.6.4.tar.gz", hash = "sha256:d2d4e4787672911b48350df02ed3fa3fffdc2f2e8ca06dd6afdf34189b76a9dd"}, {file = "multidict-6.6.4.tar.gz", hash = "sha256:d2d4e4787672911b48350df02ed3fa3fffdc2f2e8ca06dd6afdf34189b76a9dd"},
] ]
[[package]]
name = "packaging"
version = "25.0"
description = "Core utilities for Python packages"
optional = false
python-versions = ">=3.8"
groups = ["test"]
files = [
{file = "packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484"},
{file = "packaging-25.0.tar.gz", hash = "sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f"},
]
[[package]]
name = "pluggy"
version = "1.6.0"
description = "plugin and hook calling mechanisms for python"
optional = false
python-versions = ">=3.9"
groups = ["test"]
files = [
{file = "pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746"},
{file = "pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3"},
]
[package.extras]
dev = ["pre-commit", "tox"]
testing = ["coverage", "pytest", "pytest-benchmark"]
[[package]] [[package]]
name = "propcache" name = "propcache"
version = "0.3.2" version = "0.3.2"
@ -659,17 +712,90 @@ files = [
[package.dependencies] [package.dependencies]
typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0" typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0"
[[package]]
name = "pygments"
version = "2.19.2"
description = "Pygments is a syntax highlighting package written in Python."
optional = false
python-versions = ">=3.8"
groups = ["test"]
files = [
{file = "pygments-2.19.2-py3-none-any.whl", hash = "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b"},
{file = "pygments-2.19.2.tar.gz", hash = "sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887"},
]
[package.extras]
windows-terminal = ["colorama (>=0.4.6)"]
[[package]]
name = "pytest"
version = "8.4.2"
description = "pytest: simple powerful testing with Python"
optional = false
python-versions = ">=3.9"
groups = ["test"]
files = [
{file = "pytest-8.4.2-py3-none-any.whl", hash = "sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79"},
{file = "pytest-8.4.2.tar.gz", hash = "sha256:86c0d0b93306b961d58d62a4db4879f27fe25513d4b969df351abdddb3c30e01"},
]
[package.dependencies]
colorama = {version = ">=0.4", markers = "sys_platform == \"win32\""}
iniconfig = ">=1"
packaging = ">=20"
pluggy = ">=1.5,<2"
pygments = ">=2.7.2"
[package.extras]
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "requests", "setuptools", "xmlschema"]
[[package]]
name = "pytest-asyncio"
version = "1.2.0"
description = "Pytest support for asyncio"
optional = false
python-versions = ">=3.9"
groups = ["test"]
files = [
{file = "pytest_asyncio-1.2.0-py3-none-any.whl", hash = "sha256:8e17ae5e46d8e7efe51ab6494dd2010f4ca8dae51652aa3c8d55acf50bfb2e99"},
{file = "pytest_asyncio-1.2.0.tar.gz", hash = "sha256:c609a64a2a8768462d0c99811ddb8bd2583c33fd33cf7f21af1c142e824ffb57"},
]
[package.dependencies]
pytest = ">=8.2,<9"
typing-extensions = {version = ">=4.12", markers = "python_version < \"3.13\""}
[package.extras]
docs = ["sphinx (>=5.3)", "sphinx-rtd-theme (>=1)"]
testing = ["coverage (>=6.2)", "hypothesis (>=5.7.1)"]
[[package]]
name = "pytest-randomly"
version = "3.16.0"
description = "Pytest plugin to randomly order tests and control random.seed."
optional = false
python-versions = ">=3.9"
groups = ["test"]
files = [
{file = "pytest_randomly-3.16.0-py3-none-any.whl", hash = "sha256:8633d332635a1a0983d3bba19342196807f6afb17c3eef78e02c2f85dade45d6"},
{file = "pytest_randomly-3.16.0.tar.gz", hash = "sha256:11bf4d23a26484de7860d82f726c0629837cf4064b79157bd18ec9d41d7feb26"},
]
[package.dependencies]
pytest = "*"
[[package]] [[package]]
name = "typing-extensions" name = "typing-extensions"
version = "4.15.0" version = "4.15.0"
description = "Backported and Experimental Type Hints for Python 3.9+" description = "Backported and Experimental Type Hints for Python 3.9+"
optional = false optional = false
python-versions = ">=3.9" python-versions = ">=3.9"
groups = ["main"] groups = ["main", "test"]
files = [ files = [
{file = "typing_extensions-4.15.0-py3-none-any.whl", hash = "sha256:f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548"}, {file = "typing_extensions-4.15.0-py3-none-any.whl", hash = "sha256:f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548"},
{file = "typing_extensions-4.15.0.tar.gz", hash = "sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466"}, {file = "typing_extensions-4.15.0.tar.gz", hash = "sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466"},
] ]
markers = {test = "python_version < \"3.13\""}
[[package]] [[package]]
name = "typing-inspection" name = "typing-inspection"
@ -808,4 +934,4 @@ propcache = ">=0.2.1"
[metadata] [metadata]
lock-version = "2.1" lock-version = "2.1"
python-versions = ">=3.11" python-versions = ">=3.11"
content-hash = "11201d4008a14740f88f3639c07c98395d62f610d89918beae58e58453da20fc" content-hash = "8ad261ff9b638f6a43a53816d78da99ecb5fa8f32397e8fcb142ce7e7260e9bc"

View file

@ -15,6 +15,10 @@ dependencies = [
[tool.poetry] [tool.poetry]
packages = [{include = "TaxisLibrary", from = "src"}] packages = [{include = "TaxisLibrary", from = "src"}]
[tool.poetry.group.test.dependencies]
pytest-randomly = "^3.15.0"
pytest = "^8.2.0"
pytest-asyncio = "^1.2.0"
[build-system] [build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"] requires = ["poetry-core>=2.0.0,<3.0.0"]

View file

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

21
tests/test_login.py Normal file
View file

@ -0,0 +1,21 @@
import asyncio
import pytest
from TaxisLibrary.taxis_client import TaxisClient, ApiKeyOut
@pytest.fixture
def client():
return TaxisClient("http://127.0.0.1:7000")
class TestLogin:
@pytest.mark.asyncio
async def test_login(self, client):
result = await client.login(username="root", password="12345")
assert isinstance(result, ApiKeyOut)
@pytest.mark.asyncio
async def test_login_error(self, client):
with pytest.raises(Exception):
await client.login(username="root", password="123456")