-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Method for getting a list of SmartApps
- Loading branch information
1 parent
6d7bdbf
commit eb5b3ee
Showing
5 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from typing import List, Literal, Optional, Tuple | ||
from uuid import UUID | ||
|
||
from pybotx.client.authorized_botx_method import AuthorizedBotXMethod | ||
from pybotx.missing import Missing, Undefined | ||
from pybotx.models.api_base import UnverifiedPayloadBaseModel, VerifiedPayloadBaseModel | ||
from pybotx.models.smartapps import SmartApp | ||
|
||
|
||
class BotXAPISmartAppsListRequestPayload(UnverifiedPayloadBaseModel): | ||
version: Missing[int] = Undefined | ||
|
||
@classmethod | ||
def from_domain( | ||
cls, | ||
version: Missing[int] = Undefined, | ||
) -> "BotXAPISmartAppsListRequestPayload": | ||
return cls(version=version) | ||
|
||
|
||
class BotXAPISmartAppEntity(VerifiedPayloadBaseModel): | ||
app_id: str | ||
enabled: bool | ||
id: UUID | ||
name: str | ||
avatar: Optional[str] = None | ||
avatar_preview: Optional[str] = None | ||
|
||
|
||
class BotXAPISmartAppsListResult(VerifiedPayloadBaseModel): | ||
phonebook_version: int | ||
smartapps: List[BotXAPISmartAppEntity] | ||
|
||
|
||
class BotXAPISmartAppsListResponsePayload(VerifiedPayloadBaseModel): | ||
result: BotXAPISmartAppsListResult | ||
status: Literal["ok"] | ||
|
||
def to_domain(self) -> Tuple[List[SmartApp], int]: | ||
smartapps_list = [ | ||
SmartApp( | ||
app_id=smartapp.app_id, | ||
enabled=smartapp.enabled, | ||
id=smartapp.id, | ||
name=smartapp.name, | ||
avatar=smartapp.avatar, | ||
avatar_preview=smartapp.avatar_preview, | ||
) | ||
for smartapp in self.result.smartapps | ||
] | ||
return ( | ||
smartapps_list, | ||
self.result.phonebook_version, | ||
) | ||
|
||
|
||
class SmartAppsListMethod(AuthorizedBotXMethod): | ||
status_handlers = { | ||
**AuthorizedBotXMethod.status_handlers, | ||
} | ||
|
||
async def execute( | ||
self, | ||
payload: BotXAPISmartAppsListRequestPayload, | ||
) -> BotXAPISmartAppsListResponsePayload: | ||
path = "/api/v3/botx/smartapps/list" | ||
|
||
response = await self._botx_method_call( | ||
"GET", | ||
self._build_url(path), | ||
params=payload.jsonable_dict(), | ||
) | ||
|
||
return self._verify_and_extract_api_model( | ||
BotXAPISmartAppsListResponsePayload, | ||
response, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
from uuid import UUID | ||
|
||
|
||
@dataclass | ||
class SmartApp: | ||
"""SmartApp from list of SmartApps. | ||
Attributes: | ||
app_id: Readable SmartApp id. | ||
enabled: Is the SmartApp enabled or not. | ||
id: SmartApp uuid. | ||
name: SmartApp name. | ||
avatar: SmartApp avatar url. | ||
avatar_preview: SmartApp avatar preview url. | ||
""" | ||
|
||
app_id: str | ||
enabled: bool | ||
id: UUID | ||
name: str | ||
avatar: Optional[str] = None | ||
avatar_preview: Optional[str] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pybotx" | ||
version = "0.46.0" | ||
version = "0.47.0" | ||
description = "A python library for interacting with eXpress BotX API" | ||
authors = [ | ||
"Sidnev Nikolay <[email protected]>", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from http import HTTPStatus | ||
from uuid import UUID | ||
|
||
import httpx | ||
import pytest | ||
from respx.router import MockRouter | ||
|
||
from pybotx import Bot, HandlerCollector, lifespan_wrapper | ||
from pybotx.models.bot_account import BotAccountWithSecret | ||
from pybotx.models.smartapps import SmartApp | ||
|
||
pytestmark = [ | ||
pytest.mark.asyncio, | ||
pytest.mark.mock_authorization, | ||
pytest.mark.usefixtures("respx_mock"), | ||
] | ||
|
||
|
||
async def test__smartapps_list__succeed( | ||
respx_mock: MockRouter, | ||
host: str, | ||
bot_id: UUID, | ||
bot_account: BotAccountWithSecret, | ||
) -> None: | ||
# - Arrange - | ||
endpoint = respx_mock.get( | ||
f"https://{host}/api/v3/botx/smartapps/list", | ||
headers={"Authorization": "Bearer token"}, | ||
).mock( | ||
return_value=httpx.Response( | ||
HTTPStatus.OK, | ||
json={ | ||
"result": { | ||
"phonebook_version": 1, | ||
"smartapps": [ | ||
{ | ||
"app_id": "amazing_smartapp", | ||
"avatar": "https://cts.example.com/uploads/profile_avatar/foo", | ||
"avatar_preview": "https://cts.example.com/uploads/profile_avatar/bar", | ||
"enabled": True, | ||
"id": "dc4acaf2-310f-4b0f-aec7-253b9def42ac", | ||
"name": "Amazing SmartApp", | ||
}, | ||
], | ||
}, | ||
"status": "ok", | ||
}, | ||
), | ||
) | ||
|
||
built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) | ||
|
||
# - Act - | ||
async with lifespan_wrapper(built_bot) as bot: | ||
smartapps_list, version = await bot.get_smartapps_list( | ||
bot_id=bot_id, | ||
) | ||
|
||
# - Assert - | ||
assert endpoint.called | ||
assert smartapps_list == [ | ||
SmartApp( | ||
app_id="amazing_smartapp", | ||
avatar="https://cts.example.com/uploads/profile_avatar/foo", | ||
avatar_preview="https://cts.example.com/uploads/profile_avatar/bar", | ||
enabled=True, | ||
id=UUID("dc4acaf2-310f-4b0f-aec7-253b9def42ac"), | ||
name="Amazing SmartApp", | ||
), | ||
] | ||
assert version == 1 |