From e6d07958c99cb9f00f267f9d90e719c1aaed20e6 Mon Sep 17 00:00:00 2001 From: depostnykh <78929155+depostnykh@users.noreply.github.com> Date: Fri, 22 Mar 2024 09:55:38 +0300 Subject: [PATCH] feat: Add protocols support in url builder (#458) --- pybotx/client/botx_method.py | 13 ++++++--- pyproject.toml | 2 +- tests/client/test_botx_method.py | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/pybotx/client/botx_method.py b/pybotx/client/botx_method.py index 34eff0c2..95074957 100644 --- a/pybotx/client/botx_method.py +++ b/pybotx/client/botx_method.py @@ -12,7 +12,7 @@ Type, TypeVar, ) -from urllib.parse import urlunparse +from urllib.parse import urlparse, urlunparse from uuid import UUID import httpx @@ -88,8 +88,15 @@ async def execute(self, *args: Any, **kwargs: Any) -> Any: # type: ignore raise NotImplementedError("You should define `execute` method") def _build_url(self, path: str) -> str: - host = self._bot_accounts_storage.get_host(self._bot_id).rstrip("/") - return urlunparse(("https", host, path, "", "", "")) + host = self._bot_accounts_storage.get_host(self._bot_id) + + if "://" not in host: + host = f"https://{host}" + + host_parts = urlparse(host) + path = host_parts.path.rstrip("/") + path + + return urlunparse((host_parts.scheme, host_parts.netloc, path, "", "", "")) def _verify_and_extract_api_model( self, diff --git a/pyproject.toml b/pyproject.toml index 20e800d0..ece39115 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pybotx" -version = "0.63.0" +version = "0.64.0" description = "A python library for interacting with eXpress BotX API" authors = [ "Sidnev Nikolay ", diff --git a/tests/client/test_botx_method.py b/tests/client/test_botx_method.py index 7e79cc3b..0c9b2b2a 100644 --- a/tests/client/test_botx_method.py +++ b/tests/client/test_botx_method.py @@ -1,5 +1,6 @@ from http import HTTPStatus from typing import Literal +from urllib.parse import urlunparse from uuid import UUID import httpx @@ -240,3 +241,48 @@ async def test__botx_method__succeed( # - Assert - assert botx_api_foo_bar.to_domain() == UUID("21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3") assert endpoint.called + + +@pytest.mark.parametrize( + argnames="protocol", + argvalues=["http", "https"], + ids=["http", "https"], +) +async def test__botx_method__host_with_protocol__succeed( + httpx_client: httpx.AsyncClient, + respx_mock: MockRouter, + host: str, + bot_id: UUID, + bot_account: BotAccountWithSecret, + protocol: str, +) -> None: + # - Arrange - + bot_account.host = urlunparse(components=(protocol, host, "", "", "", "")) + + endpoint = respx_mock.post( + f"{protocol}://{host}/foo/bar", + json={"baz": 1}, + headers={"Content-Type": "application/json"}, + ).mock( + return_value=httpx.Response( + HTTPStatus.OK, + json={ + "status": "ok", + "result": {"sync_id": "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3"}, + }, + ), + ) + + method = FooBarMethod( + bot_id, + httpx_client, + BotAccountsStorage([bot_account]), + ) + payload = BotXAPIFooBarRequestPayload.from_domain(baz=1) + + # - Act - + botx_api_foo_bar = await method.execute(payload) + + # - Assert - + assert botx_api_foo_bar.to_domain() == UUID("21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3") + assert endpoint.called