Skip to content

Commit

Permalink
feat: Add stickers (#186)
Browse files Browse the repository at this point in the history
* feat: Add stickers

* Add 'image not valid' error

* fix docstrings

* Update changelog

Co-authored-by: MGorbachev <[email protected]>
  • Loading branch information
Kondrahin and Opti213 committed Nov 1, 2021
1 parent 97c8b1c commit 5c28118
Show file tree
Hide file tree
Showing 43 changed files with 1,854 additions and 11 deletions.
17 changes: 16 additions & 1 deletion botx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
from botx.models.messages.sending.options import MessageOptions, NotificationOptions
from botx.models.messages.sending.payload import MessagePayload, UpdatePayload
from botx.models.status import StatusRecipient
from botx.models.stickers import (
Pagination,
Sticker,
StickerFromPack,
StickerPack,
StickerPackList,
StickerPackPreview,
)
from botx.testing.building.builder import MessageBuilder

try:
Expand All @@ -75,7 +83,7 @@
# DI
"Depends",
# models
# murkup
# markup
"BubbleElement",
"KeyboardElement",
# credentials
Expand Down Expand Up @@ -131,6 +139,13 @@
"MessagePayload",
"UpdatePayload",
"InternalBotNotificationPayload",
# Stickers
"Pagination",
"Sticker",
"StickerPack",
"StickerPackList",
"StickerFromPack",
"StickerPackPreview",
# testing
"TestClient",
"MessageBuilder",
Expand Down
2 changes: 1 addition & 1 deletion botx/bots/mixins/requests/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def get_chat_info(
credentials: SendingCredentials,
chat_id: UUID,
) -> ChatFromSearch:
"""Create new chat.
"""Return chat's info.
Arguments:
credentials: credentials for making request.
Expand Down
4 changes: 3 additions & 1 deletion botx/bots/mixins/requests/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

from loguru import logger

from botx.bots.mixins.requests import (
from botx.bots.mixins.requests import ( # noqa: WPS235
bots,
chats,
command,
events,
files,
internal_bot_notification,
notification,
stickers,
users,
)
from botx.clients.clients.async_client import AsyncClient
Expand Down Expand Up @@ -47,6 +48,7 @@ class BotXRequestsMixin( # noqa: WPS215
users.UsersRequestsMixin,
internal_bot_notification.InternalBotNotificationRequestsMixin,
files.FilesRequestsMixin,
stickers.StickersMixin,
):
"""Mixin that defines methods for communicating with BotX API."""

Expand Down
199 changes: 199 additions & 0 deletions botx/bots/mixins/requests/stickers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
"""Mixin for shortcut for users resource requests."""

from typing import List, Optional, Tuple
from uuid import UUID

from botx.bots.mixins.requests.call_protocol import BotXMethodCallProtocol
from botx.clients.methods.v3.stickers.add_sticker import AddSticker
from botx.clients.methods.v3.stickers.create_sticker_pack import CreateStickerPack
from botx.clients.methods.v3.stickers.delete_sticker import DeleteSticker
from botx.clients.methods.v3.stickers.delete_sticker_pack import DeleteStickerPack
from botx.clients.methods.v3.stickers.edit_sticker_pack import EditStickerPack
from botx.clients.methods.v3.stickers.sticker import GetSticker
from botx.clients.methods.v3.stickers.sticker_pack import GetStickerPack
from botx.clients.methods.v3.stickers.sticker_pack_list import GetStickerPackList
from botx.models.messages.sending.credentials import SendingCredentials
from botx.models.stickers import (
Sticker,
StickerFromPack,
StickerPack,
StickerPackList,
StickerPackPreview,
)


class StickersMixin: # noqa: WPS214
"""Mixin for shortcut for users resource requests."""

async def get_sticker_pack_list(
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
user_huid: Optional[UUID] = None,
limit: int = 1,
after: Optional[str] = None,
) -> Tuple[List[StickerPackPreview], Optional[str]]:
"""Get sticker pack list.
Arguments:
credentials: credentials for making request.
user_huid: author HUID.
limit: returning value count.
after: cursor hash for pagination.
Returns:
Sticker packs list and cursor.
"""
response: StickerPackList = await self.call_method(
GetStickerPackList(user_huid=user_huid, limit=limit, after=after),
credentials=credentials,
)

return response.packs, response.pagination.after

async def get_sticker_pack(
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
pack_id: UUID,
) -> StickerPack:
"""Get sticker pack.
Arguments:
credentials: credentials for making request.
pack_id: sticker pack ID.
Returns:
StickerPack entity.
"""
return await self.call_method(
GetStickerPack(pack_id=pack_id),
credentials=credentials,
)

async def get_sticker_from_pack(
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
pack_id: UUID,
sticker_id: UUID,
) -> StickerFromPack:
"""Get sticker from pack.
Arguments:
credentials: credentials for making request.
pack_id: sticker pack ID.
sticker_id: sticker ID.
Returns:
StickerFromPack entity.
"""
return await self.call_method(
GetSticker(pack_id=pack_id, sticker_id=sticker_id),
credentials=credentials,
)

async def create_sticker_pack(
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
name: str,
user_huid: UUID,
) -> StickerPack:
"""Create sticker pack.
Arguments:
credentials: credentials for making request.
name: sticker pack name.
user_huid: author HUID.
Returns:
StickerPackPreview entity.
"""
return await self.call_method(
CreateStickerPack(name=name, user_huid=user_huid),
credentials=credentials,
)

async def add_sticker(
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
pack_id: UUID,
emoji: str,
image: str,
) -> Sticker:
"""Add sticker.
Arguments:
credentials: credentials for making request.
pack_id: sticker pack ID.
emoji: emoji that the sticker will be associated with.
image: sticker image.
Returns:
Sticker entity.
"""
return await self.call_method(
AddSticker(pack_id=pack_id, emoji=emoji, image=image),
credentials=credentials,
)

async def edit_sticker_pack( # noqa: WPS211
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
pack_id: UUID,
name: str,
preview: Optional[UUID] = None,
stickers_order: Optional[List[UUID]] = None,
) -> StickerPack:
"""Edit sticker pack.
Arguments:
credentials: credentials for making request.
pack_id: sticker pack ID.
name: sticker pack name.
preview: sticker pack preview.
stickers_order: stickers order in sticker pack.
Returns:
StickerPack entity.
"""
return await self.call_method(
EditStickerPack(
pack_id=pack_id,
name=name,
preview=preview,
stickers_order=stickers_order,
),
credentials=credentials,
)

async def delete_sticker_pack(
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
pack_id: UUID,
) -> None:
"""Delete sticker pack.
Arguments:
credentials: credentials for making request.
pack_id: sticker pack ID.
"""
await self.call_method(
DeleteStickerPack(pack_id=pack_id),
credentials=credentials,
)

async def delete_sticker(
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
pack_id: UUID,
sticker_id: UUID,
) -> None:
"""Delete sticker.
Arguments:
credentials: credentials for making request.
pack_id: sticker pack ID.
sticker_id: sticker ID.
"""
await self.call_method(
DeleteSticker(pack_id=pack_id, sticker_id=sticker_id),
credentials=credentials,
)
1 change: 1 addition & 0 deletions botx/clients/methods/errors/stickers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Definition for built-in error handlers for responses from BotX API."""
30 changes: 30 additions & 0 deletions botx/clients/methods/errors/stickers/image_not_valid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Definition for "image is not valid" error."""
from typing import NoReturn

from botx.clients.methods.base import BotXMethod
from botx.clients.types.http import HTTPResponse
from botx.exceptions import BotXAPIError


class ImageNotValidError(BotXAPIError):
"""Error for raising when image is not valid."""

message_template = "image is not valid"


def handle_error(method: BotXMethod, response: HTTPResponse) -> NoReturn:
"""Handle "image is not valid" error response.
Arguments:
method: method which was made before error.
response: HTTP response from BotX API.
Raises:
ImageNotValidError: raised always.
"""
raise ImageNotValidError(
url=method.url,
method=method.http_method,
response_content=response.json_body,
status_content=response.status_code,
)
49 changes: 49 additions & 0 deletions botx/clients/methods/errors/stickers/sticker_pack_not_found.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Definition for "sticker pack was not found" 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 StickerPackNotFoundError(BotXAPIError):
"""Error for raising when sticker pack was not found."""

message_template = "sticker pack {pack_id} not found"

#: sticker pack ID.
pack_id: UUID


class StickerPackNotFoundData(BaseModel):
"""Data for error when sticker pack was not found."""

#: sticker pack ID.
pack_id: UUID


def handle_error(method: BotXMethod, response: HTTPResponse) -> NoReturn:
"""Handle "sticker pack getting error" error response.
Arguments:
method: method which was made before error.
response: HTTP response from BotX API.
Raises:
StickerPackNotFoundError: raised always.
"""
parsed_response = APIErrorResponse[StickerPackNotFoundData].parse_obj(
response.json_body,
)

error_data = parsed_response.error_data
raise StickerPackNotFoundError(
url=method.url,
method=method.http_method,
response_content=response.json_body,
status_content=response.status_code,
pack_id=error_data.pack_id,
)
Loading

1 comment on commit 5c28118

@github-actions
Copy link

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://617fa1017d46184f24db2478--pybotx.netlify.app

Please sign in to comment.