Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Выброс ошибки в API методах, если метод не привязан к навыку #17

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions aliceio/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,14 @@ def __str__(self) -> str:
f"{original_type.__module__}.{original_type.__name__}: {self.original}\n"
f"Content: {self.data}"
)


class MethodNotMountedToSkillError(DetailedAliceioError):
def __init__(self) -> None:
super().__init__(
message="This method is not mounted to a any skill instance, "
"please call it explicilty "
"with skill instance `await skill(method)`\n"
"or mount method to a skill instance `method.as_(skill)` "
"and then call it `await method()`",
)
11 changes: 2 additions & 9 deletions aliceio/methods/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from aliceio.client.context_controller import SkillContextController

from ..client.alice import AliceAPIServer
from ..exceptions import MethodNotMountedToSkillError

if TYPE_CHECKING:
from ..client.skill import Skill
Expand Down Expand Up @@ -47,18 +48,10 @@ def api_url(self, api_server: AliceAPIServer) -> str:
pass

async def emit(self, skill: "Skill") -> AliceType:
if not self._skill:
self._skill = self.skill
return await skill(self)

def __await__(self) -> Generator[Any, None, AliceType]:
skill = self._skill
if not skill:
raise RuntimeError(
"This method is not mounted to a any skill instance, "
"please call it explicilty "
"with skill instance `await skill(method)`\n"
"or mount method to a skill instance `method.as_(skill)` "
"and then call it `await method()`",
)
raise MethodNotMountedToSkillError
return self.emit(skill).__await__()
11 changes: 5 additions & 6 deletions aliceio/methods/images/delete_image.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from typing import TYPE_CHECKING, Any, cast
from typing import TYPE_CHECKING, Any

from aliceio.client.alice import AliceAPIServer
from aliceio.enums import FileType, HttpMethod
from aliceio.exceptions import MethodNotMountedToSkillError
from aliceio.methods.base import AliceMethod
from aliceio.types import Result

if TYPE_CHECKING:
from aliceio.client.skill import Skill


class DeleteImage(AliceMethod[Result]):
__returning__ = Result
Expand All @@ -29,9 +27,10 @@ def __init__(
)

def api_url(self, api_server: AliceAPIServer) -> str:
skill: Skill = cast("Skill", self.skill)
if self.skill is None:
raise MethodNotMountedToSkillError
return api_server.delete_file_url(
skill_id=skill.id,
skill_id=self.skill.id,
file_type=FileType.IMAGES,
file_id=self.file_id,
)
11 changes: 4 additions & 7 deletions aliceio/methods/images/get_images.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
from typing import TYPE_CHECKING, cast

from aliceio.client.alice import AliceAPIServer
from aliceio.enums import FileType, HttpMethod
from aliceio.exceptions import MethodNotMountedToSkillError
from aliceio.methods.base import AliceMethod
from aliceio.types import UploadedImagesList

if TYPE_CHECKING:
from aliceio.client.skill import Skill


class GetImages(AliceMethod[UploadedImagesList]):
__returning__ = UploadedImagesList
__http_method__ = HttpMethod.GET

def api_url(self, api_server: AliceAPIServer) -> str:
skill: Skill = cast("Skill", self.skill)
if self.skill is None:
raise MethodNotMountedToSkillError
return api_server.get_all_files_url(
skill_id=skill.id,
skill_id=self.skill.id,
file_type=FileType.IMAGES,
)
12 changes: 5 additions & 7 deletions aliceio/methods/images/upload_image.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from typing import TYPE_CHECKING, Any, Optional, cast
from typing import TYPE_CHECKING, Any, Optional

from aliceio.client.alice import AliceAPIServer
from aliceio.enums import FileType, HttpMethod
from aliceio.exceptions import AliceWrongFieldError
from aliceio.exceptions import AliceWrongFieldError, MethodNotMountedToSkillError
from aliceio.methods.base import AliceMethod
from aliceio.types import InputFile, PreUploadedImage

if TYPE_CHECKING:
from aliceio.client.skill import Skill


class UploadImage(AliceMethod[PreUploadedImage]):
__returning__ = PreUploadedImage
Expand Down Expand Up @@ -41,8 +38,9 @@ def model_post_init(self, __context: Any) -> None:
raise AliceWrongFieldError('"file" and "url" cannot be specified together')

def api_url(self, api_server: AliceAPIServer) -> str:
skill: Skill = cast("Skill", self.skill)
if self.skill is None:
raise MethodNotMountedToSkillError
return api_server.upload_file_url(
skill_id=skill.id,
skill_id=self.skill.id,
file_type=FileType.IMAGES,
)
11 changes: 5 additions & 6 deletions aliceio/methods/sounds/delete_sound.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from typing import TYPE_CHECKING, Any, cast
from typing import TYPE_CHECKING, Any

from aliceio.client.alice import AliceAPIServer
from aliceio.enums import FileType, HttpMethod
from aliceio.exceptions import MethodNotMountedToSkillError
from aliceio.methods.base import AliceMethod
from aliceio.types import Result

if TYPE_CHECKING:
from aliceio.client.skill import Skill


class DeleteSound(AliceMethod[Result]):
__returning__ = Result
Expand All @@ -29,9 +27,10 @@ def __init__(
)

def api_url(self, api_server: AliceAPIServer) -> str:
skill: Skill = cast("Skill", self.skill)
if self.skill is None:
raise MethodNotMountedToSkillError
return api_server.delete_file_url(
skill_id=skill.id,
skill_id=self.skill.id,
file_type=FileType.SOUNDS,
file_id=self.file_id,
)
11 changes: 4 additions & 7 deletions aliceio/methods/sounds/get_sounds.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
from typing import TYPE_CHECKING, cast

from aliceio.client.alice import AliceAPIServer
from aliceio.enums import FileType, HttpMethod
from aliceio.exceptions import MethodNotMountedToSkillError
from aliceio.methods.base import AliceMethod
from aliceio.types import UploadedSoundsList

if TYPE_CHECKING:
from aliceio.client.skill import Skill


class GetSounds(AliceMethod[UploadedSoundsList]):
__returning__ = UploadedSoundsList
__http_method__ = HttpMethod.GET

def api_url(self, api_server: AliceAPIServer) -> str:
skill: Skill = cast("Skill", self.skill)
if self.skill is None:
raise MethodNotMountedToSkillError
return api_server.get_all_files_url(
skill_id=skill.id,
skill_id=self.skill.id,
file_type=FileType.SOUNDS,
)
11 changes: 5 additions & 6 deletions aliceio/methods/sounds/upload_sound.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from typing import TYPE_CHECKING, Any, cast
from typing import TYPE_CHECKING, Any

from aliceio.client.alice import AliceAPIServer
from aliceio.enums import FileType, HttpMethod
from aliceio.exceptions import MethodNotMountedToSkillError
from aliceio.methods.base import AliceMethod
from aliceio.types import InputFile, PreUploadedSound

if TYPE_CHECKING:
from aliceio.client.skill import Skill


class UploadSound(AliceMethod[PreUploadedSound]):
__returning__ = PreUploadedSound
Expand All @@ -29,8 +27,9 @@ def __init__(
)

def api_url(self, api_server: AliceAPIServer) -> str:
skill: Skill = cast("Skill", self.skill)
if self.skill is None:
raise MethodNotMountedToSkillError
return api_server.upload_file_url(
skill_id=skill.id,
skill_id=self.skill.id,
file_type=FileType.SOUNDS,
)
3 changes: 2 additions & 1 deletion tests/test_methods/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from aliceio.client.alice import AliceAPIServer
from aliceio.enums import HttpMethod
from aliceio.exceptions import MethodNotMountedToSkillError
from aliceio.methods.base import AliceMethod
from tests.mocked import MockedSkill

Expand Down Expand Up @@ -37,7 +38,7 @@ async def test_await(self, skill: MockedSkill):

async def test_await_without_skill(self, skill: MockedSkill):
method = MyMethod()
with pytest.raises(RuntimeError):
with pytest.raises(MethodNotMountedToSkillError):
await method

async def test_skill_call(self, skill: MockedSkill):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_methods/test_images.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from aliceio.client.alice import PRODUCTION
from aliceio.exceptions import AliceWrongFieldError
from aliceio.exceptions import AliceWrongFieldError, MethodNotMountedToSkillError
from aliceio.methods import DeleteImage, GetImages, UploadImage
from aliceio.types import BufferedInputFile
from tests.mocked import MockedSkill
Expand All @@ -11,7 +11,7 @@ class TestUploadImage:
def test_api_url(self, skill: MockedSkill):
method = UploadImage(file=BufferedInputFile(file=b""))

with pytest.raises(AttributeError):
with pytest.raises(MethodNotMountedToSkillError):
method.api_url(PRODUCTION)

method.as_(skill)
Expand All @@ -32,7 +32,7 @@ class TestGetImages:
def test_api_url(self, skill: MockedSkill):
method = GetImages(file=BufferedInputFile(file=b""))

with pytest.raises(AttributeError):
with pytest.raises(MethodNotMountedToSkillError):
method.api_url(PRODUCTION)

method.as_(skill)
Expand All @@ -44,7 +44,7 @@ class TestDeleteImage:
def test_api_url(self, skill: MockedSkill):
method = DeleteImage(file_id="FILE_ID")

with pytest.raises(AttributeError):
with pytest.raises(MethodNotMountedToSkillError):
method.api_url(PRODUCTION)

method.as_(skill)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_methods/test_sounds.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from aliceio.client.alice import PRODUCTION
from aliceio.exceptions import MethodNotMountedToSkillError
from aliceio.methods import DeleteSound, GetSounds, UploadSound
from aliceio.types import BufferedInputFile
from tests.mocked import MockedSkill
Expand All @@ -10,7 +11,7 @@ class TestUploadImage:
def test_api_url(self, skill: MockedSkill):
method = UploadSound(file=BufferedInputFile(file=b""))

with pytest.raises(AttributeError):
with pytest.raises(MethodNotMountedToSkillError):
method.api_url(PRODUCTION)

method.as_(skill)
Expand All @@ -22,7 +23,7 @@ class TestGetSounds:
def test_api_url(self, skill: MockedSkill):
method = GetSounds(file=BufferedInputFile(file=b""))

with pytest.raises(AttributeError):
with pytest.raises(MethodNotMountedToSkillError):
method.api_url(PRODUCTION)

method.as_(skill)
Expand All @@ -34,7 +35,7 @@ class TestDeleteSound:
def test_api_url(self, skill: MockedSkill):
method = DeleteSound(file_id="FILE_ID")

with pytest.raises(AttributeError):
with pytest.raises(MethodNotMountedToSkillError):
method.api_url(PRODUCTION)

method.as_(skill)
Expand Down
Loading