Skip to content

Commit

Permalink
feat: Add protocols support in url builder (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
depostnykh committed Mar 22, 2024
1 parent e98a00d commit e6d0795
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
13 changes: 10 additions & 3 deletions pybotx/client/botx_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Type,
TypeVar,
)
from urllib.parse import urlunparse
from urllib.parse import urlparse, urlunparse
from uuid import UUID

import httpx
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
46 changes: 46 additions & 0 deletions tests/client/test_botx_method.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from http import HTTPStatus
from typing import Literal
from urllib.parse import urlunparse
from uuid import UUID

import httpx
Expand Down Expand Up @@ -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

0 comments on commit e6d0795

Please sign in to comment.