feat: добавлены утилиты для deep linking и соответствующие тесты#155
Open
love-apples wants to merge 7 commits into
Open
feat: добавлены утилиты для deep linking и соответствующие тесты#155love-apples wants to merge 7 commits into
love-apples wants to merge 7 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
There was a problem hiding this comment.
Pull request overview
Добавляет в maxapi.utils высокоуровневые утилиты для генерации deep links MAX (start/startapp) и кодирования/декодирования payload в URL-safe base64 без padding, а также документацию и тесты для этого функционала.
Changes:
- Добавлен модуль
maxapi.utils.deep_linkingсencode_payload()/decode_payload()и генераторамиcreate_*_link(). - Добавлены тесты на roundtrip base64, валидацию username/payload, лимиты длины и start/startapp-ссылки.
- Обновлена документация (mkdocs nav + страница utils/deep_linking).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 17 comments.
Show a summary per file
| File | Description |
|---|---|
maxapi/utils/deep_linking.py |
Реализация encode/decode payload и генерации start/startapp deep links с валидацией. |
maxapi/utils/__init__.py |
Реэкспорт новых утилит из maxapi.utils. |
tests/test_utils/test_deep_linking.py |
Покрытие тестами новых утилит и граничных случаев. |
docs/utils/deep_linking.md |
Док-страница с mkdocstrings и примером использования. |
mkdocs.yml |
Добавление страницы deep linking в навигацию Utils. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+52
to
+67
| def decode_payload( | ||
| payload: str, | ||
| decoder: Callable[[bytes], bytes] | None = None, | ||
| ) -> str: | ||
| """ | ||
| Декодирует URL-safe base64 payload. | ||
|
|
||
| Args: | ||
| payload: Закодированный payload. | ||
| decoder: Дополнительный декодировщик байтов. | ||
|
|
||
| Returns: | ||
| Исходная строка payload. | ||
| """ | ||
| if BAD_PAYLOAD_PATTERN.search(payload): | ||
| raise ValueError("payload должен быть в URL-safe base64 формате") |
Comment on lines
+8
to
+37
| from typing import TYPE_CHECKING, Literal, overload | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Callable | ||
|
|
||
| DEEPLINK_PAYLOAD_MAX_LENGTH = 128 | ||
| STARTAPP_PAYLOAD_MAX_LENGTH = 512 | ||
| MAX_DEEPLINK_HOST = "https://max.ru" | ||
| BAD_PAYLOAD_PATTERN = re.compile(r"[^A-Za-z0-9_-]") | ||
| BAD_USERNAME_PATTERN = re.compile(r"[^A-Za-z0-9_.-]") | ||
|
|
||
| __all__ = [ | ||
| "create_deep_link", | ||
| "create_start_link", | ||
| "create_startapp_link", | ||
| "decode_payload", | ||
| "encode_payload", | ||
| ] | ||
|
|
||
|
|
||
| def encode_payload( | ||
| payload: str, | ||
| encoder: Callable[[bytes], bytes] | None = None, | ||
| ) -> str: | ||
| """ | ||
| Кодирует payload в URL-safe base64 без padding. | ||
|
|
||
| Args: | ||
| payload: Строка payload. | ||
| encoder: Дополнительный кодировщик байтов. |
Comment on lines
+90
to
+105
| def create_start_link( | ||
| username: str, | ||
| payload: str, | ||
| *, | ||
| encode: bool = False, | ||
| encoder: Callable[[bytes], bytes] | None = None, | ||
| ) -> str: | ||
| """ | ||
| Создаёт deep link для старта бота. | ||
|
|
||
| Args: | ||
| username: Username бота. | ||
| payload: Данные, которые MAX передаст в BotStarted.payload. | ||
| encode: Кодировать payload через URL-safe base64. | ||
| encoder: Дополнительный кодировщик байтов. | ||
|
|
Comment on lines
+118
to
+136
| def create_startapp_link( | ||
| username: str, | ||
| payload: str | None = None, | ||
| *, | ||
| encode: bool = False, | ||
| encoder: Callable[[bytes], bytes] | None = None, | ||
| ) -> str: | ||
| """ | ||
| Создаёт deep link для мини-приложения MAX. | ||
|
|
||
| Args: | ||
| username: Username бота. | ||
| payload: Данные для параметра startapp. | ||
| encode: Кодировать payload через URL-safe base64. | ||
| encoder: Дополнительный кодировщик байтов. | ||
|
|
||
| Returns: | ||
| Ссылка вида https://max.ru/<botName>?startapp=<payload>. | ||
| """ |
Comment on lines
+146
to
+165
| @overload | ||
| def create_deep_link( | ||
| username: str, | ||
| link_type: Literal["start"], | ||
| payload: str, | ||
| *, | ||
| encode: bool = False, | ||
| encoder: Callable[[bytes], bytes] | None = None, | ||
| ) -> str: ... | ||
|
|
||
|
|
||
| @overload | ||
| def create_deep_link( | ||
| username: str, | ||
| link_type: Literal["startapp"], | ||
| payload: str | None = None, | ||
| *, | ||
| encode: bool = False, | ||
| encoder: Callable[[bytes], bytes] | None = None, | ||
| ) -> str: ... |
Comment on lines
+20
to
+23
| @router.bot_started(F.payload) | ||
| async def on_deep_link(event: BotStarted) -> None: | ||
| user_id = decode_payload(event.payload) | ||
| ``` |
| import base64 | ||
| import binascii | ||
| import re | ||
| from typing import TYPE_CHECKING, Literal, overload |
Comment on lines
+28
to
+37
| def encode_payload( | ||
| payload: str, | ||
| encoder: Callable[[bytes], bytes] | None = None, | ||
| ) -> str: | ||
| """ | ||
| Кодирует payload в URL-safe base64 без padding. | ||
|
|
||
| Args: | ||
| payload: Строка payload. | ||
| encoder: Дополнительный кодировщик байтов. |
| import base64 | ||
| import binascii | ||
| import re | ||
| from typing import TYPE_CHECKING, Literal, overload |
Comment on lines
+28
to
+37
| def encode_payload( | ||
| payload: str, | ||
| encoder: Callable[[bytes], bytes] | None = None, | ||
| ) -> str: | ||
| """ | ||
| Кодирует payload в URL-safe base64 без padding. | ||
|
|
||
| Args: | ||
| payload: Строка payload. | ||
| encoder: Дополнительный кодировщик байтов. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #124
Что сделано
Добавлены высокоуровневые утилиты для deep links MAX:
encode_payload()/decode_payload()для URL-safe base64 payload без paddingcreate_start_link()для ссылок видаhttps://max.ru/<botName>?start=<payload>create_startapp_link()для ссылок видаhttps://max.ru/<botName>?startapp=<payload>create_deep_link()как общий helper дляstartиstartapp