Skip to content
This repository was archived by the owner on Mar 25, 2022. It is now read-only.
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "apywrapper"
version = "0.0.0"
version = "0.0.0.post123.dev0+96fa6fa"
description = "make wrapper for RESTful API"
license = "GPL-3.0-or-later"
authors = ["sh1ma <in9lude@gmail.com>"]
Expand Down
6 changes: 3 additions & 3 deletions src/apywrapper/_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def wrapper(
params = func(*args, **kwargs)
path = Path(path_str, params)
response = request_func(path, params)
if (
if hook_func:
return hook_func(entity, response)
elif (
entity is None or response.status_code == 204
): # entity is None or response body is None
return None
if hook_func:
return hook_func(entity, response)
elif serialize_func:
response.raise_for_status()
return serialize_func(entity, response.json())
Expand Down
11 changes: 1 addition & 10 deletions src/apywrapper/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
from httpx import Response


class DataclassEntityType(Protocol):
# pylint: disable=too-few-public-methods
"""
Entity Object Type on dataclass
"""

__dataclass_fields__: Dict


class PydanticEntityType(Protocol):
"""
Entity Object Type on Pydantic
Expand All @@ -25,7 +16,7 @@ class PydanticEntityType(Protocol):
__fields__: Dict


EntityType = Union[DataclassEntityType, PydanticEntityType]
EntityType = PydanticEntityType


Entity = Optional[Union[List[EntityType], EntityType]]
Expand Down
22 changes: 10 additions & 12 deletions src/apywrapper/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
"""

from inspect import signature
from typing import Any, Optional, Type, cast, get_args, get_origin
from typing import Any

from ._types import ApiFunc, EntityType
from ._types import ApiFunc


def get_returntype_from_annotation(func: ApiFunc) -> Optional[Type[EntityType]]:
def get_returntype_from_annotation(func: ApiFunc) -> Any:
return_annotation = signature(func).return_annotation
if return_annotation is None:
return None
return resolve_returntype(return_annotation)
return return_annotation


def resolve_returntype(tp: Any) -> Type[EntityType]:
if (tp_origin := get_origin(tp)) is not None:
if tp_origin is not list:
return cast(Type[EntityType], get_origin(tp))
return resolve_returntype(get_args(tp)[0])
return cast(Type[EntityType], tp)
# def resolve_returntype(tp: Any) -> Type[EntityType]:
# if (tp_origin := get_origin(tp)) is not None:
# if tp_origin is not list:
# return cast(Type[EntityType], get_origin(tp))
# return resolve_returntype(get_args(tp)[0])
# return cast(Type[EntityType], tp)
6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test4() -> List[Union[str, int]]:
def test5() -> str:
pass

assert get_returntype_from_annotation(test1) is str
assert get_returntype_from_annotation(test2) is dict
assert get_returntype_from_annotation(test1) is List[str]
assert get_returntype_from_annotation(test2) is Dict[str, str]
assert get_returntype_from_annotation(test3) is None
assert get_returntype_from_annotation(test4) is Union
assert get_returntype_from_annotation(test4) is List[Union[str, int]]
assert get_returntype_from_annotation(test5) is str