-
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: add pin and unpin methods (#223)
- Loading branch information
Showing
15 changed files
with
325 additions
and
9 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
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,51 @@ | ||
"""Definition for "no permission" error.""" | ||
from typing import NoReturn | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel | ||
|
||
from botx.clients.methods.base import APIErrorResponse, BotXMethod | ||
from botx.clients.types.http import HTTPResponse | ||
from botx.exceptions import BotXAPIError | ||
|
||
|
||
class NoPermissionError(BotXAPIError): | ||
"""Error for raising when there is no permission for operation.""" | ||
|
||
message_template = ( | ||
"Bot doesn't have permission for this operation in chat {group_chat_id}" | ||
) | ||
|
||
#: ID of chat that was requested. | ||
group_chat_id: UUID | ||
|
||
|
||
class NoPermissionErrorData(BaseModel): | ||
"""Data for error when there is no permission for operation.""" | ||
|
||
#: ID of chat that was requested. | ||
group_chat_id: UUID | ||
|
||
|
||
def handle_error(method: BotXMethod, response: HTTPResponse) -> NoReturn: | ||
"""Handle "no permission" error response. | ||
Arguments: | ||
method: method which was made before error. | ||
response: HTTP response from BotX API. | ||
Raises: | ||
NoPermissionError: raised always. | ||
""" | ||
parsed_response = APIErrorResponse[NoPermissionErrorData].parse_obj( | ||
response.json_body, | ||
) | ||
|
||
error_data = parsed_response.error_data | ||
raise NoPermissionError( | ||
url=method.url, | ||
method=method.http_method, | ||
response_content=response.json_body, | ||
status_content=response.status_code, | ||
group_chat_id=error_data.group_chat_id, | ||
) |
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 @@ | ||
"""Method for pinning message in chat.""" | ||
from http import HTTPStatus | ||
from uuid import UUID | ||
|
||
from botx.clients.methods.base import AuthorizedBotXMethod | ||
from botx.clients.methods.errors import chat_not_found, permissions | ||
|
||
|
||
class PinMessage(AuthorizedBotXMethod[str]): | ||
"""Method for pinning message in chat.""" | ||
|
||
__url__ = "/api/v3/botx/chats/pin_message" | ||
__method__ = "POST" | ||
__returning__ = str | ||
__errors_handlers__ = { | ||
HTTPStatus.NOT_FOUND: chat_not_found.handle_error, | ||
HTTPStatus.FORBIDDEN: permissions.handle_error, | ||
} | ||
|
||
#: ID of chat where message should be pinned. | ||
chat_id: UUID | ||
|
||
#: ID of message that should be pinned. | ||
sync_id: UUID |
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,21 @@ | ||
"""Method for unpinning message in chat.""" | ||
from http import HTTPStatus | ||
from uuid import UUID | ||
|
||
from botx.clients.methods.base import AuthorizedBotXMethod | ||
from botx.clients.methods.errors import chat_not_found, permissions | ||
|
||
|
||
class UnpinMessage(AuthorizedBotXMethod[str]): | ||
"""Method for unpinning message in chat.""" | ||
|
||
__url__ = "/api/v3/botx/chats/unpin_message" | ||
__method__ = "POST" | ||
__returning__ = str | ||
__errors_handlers__ = { | ||
HTTPStatus.NOT_FOUND: chat_not_found.handle_error, | ||
HTTPStatus.FORBIDDEN: permissions.handle_error, | ||
} | ||
|
||
#: ID of chat where message should be unpinned. | ||
chat_id: UUID |
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
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
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
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
40 changes: 40 additions & 0 deletions
40
tests/test_clients/test_methods/test_errors/test_permissions.py
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,40 @@ | ||
import uuid | ||
from http import HTTPStatus | ||
|
||
import pytest | ||
|
||
from botx.clients.methods.errors.permissions import ( | ||
NoPermissionError, | ||
NoPermissionErrorData, | ||
) | ||
from botx.clients.methods.v3.chats.pin_message import PinMessage | ||
from botx.concurrency import callable_to_coroutine | ||
|
||
pytestmark = pytest.mark.asyncio | ||
pytest_plugins = ("tests.test_clients.fixtures",) | ||
|
||
|
||
async def test_raising_no_permission(client, requests_client): | ||
method = PinMessage( | ||
host="example.com", | ||
chat_id=uuid.uuid4(), | ||
sync_id=uuid.uuid4(), | ||
) | ||
|
||
errors_to_raise = { | ||
PinMessage: ( | ||
HTTPStatus.FORBIDDEN, | ||
NoPermissionErrorData(group_chat_id=method.chat_id), | ||
), | ||
} | ||
|
||
with client.error_client(errors=errors_to_raise): | ||
request = requests_client.build_request(method) | ||
response = await callable_to_coroutine(requests_client.execute, request) | ||
|
||
with pytest.raises(NoPermissionError): | ||
await callable_to_coroutine( | ||
requests_client.process_response, | ||
method, | ||
response, | ||
) |
Oops, something went wrong.
e234775
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://pybotx.netlify.app as production
🚀 Deployed on https://618c25d53689531dfbc47ece--pybotx.netlify.app