Skip to content

Commit

Permalink
fix query param generation and enforce stricter types on the param pa…
Browse files Browse the repository at this point in the history
…rameter
  • Loading branch information
AbstractUmbra committed Nov 15, 2022
1 parent 96c4ba2 commit df35e97
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 53 deletions.
97 changes: 52 additions & 45 deletions hondana/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
from .types.settings import Settings, SettingsPayload
from .types.tags import GetTagListResponse
from .types.token import TokenPayload
from .utils import MANGADEX_QUERY_PARAM_TYPE

T = TypeVar("T")
Response = Coroutine[Any, Any, T]
Expand Down Expand Up @@ -375,7 +376,14 @@ async def _logout(self) -> None:

self._authenticated = False

async def request(self, route: Union[Route, CustomRoute], **kwargs: Any) -> Any:
async def request(
self,
route: Union[Route, CustomRoute],
*,
params: Optional[MANGADEX_QUERY_PARAM_TYPE] = None,
json: Optional[Any] = None,
**kwargs: Any,
) -> Any:
"""|coro|
This performs the HTTP request, handling authentication tokens when doing it.
Expand Down Expand Up @@ -421,13 +429,12 @@ async def request(self, route: Union[Route, CustomRoute], **kwargs: Any) -> Any:
headers["Authorization"] = f"Bearer {token}"
headers["User-Agent"] = self.user_agent

if "json" in kwargs:
if json:
headers["Content-Type"] = "application/json"
kwargs["data"] = to_json(kwargs.pop("json"))
kwargs["data"] = to_json(json)
LOGGER.debug("Current json body is: %s", str(kwargs["data"]))

if "params" in kwargs:
params = kwargs["params"]
if params:
resolved_params = php_query_builder(params)
kwargs["params"] = resolved_params
LOGGER.debug("Current request parameters: %s", resolved_params)
Expand Down Expand Up @@ -555,7 +562,7 @@ def _manga_list(

limit, offset = calculate_limits(limit, offset, max_limit=100)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if title:
query["title"] = title
Expand Down Expand Up @@ -583,7 +590,7 @@ def _manga_list(
query["excludedTagsMode"] = excluded_tags.mode

if status:
query["status"] = status
query["status"] = [s.value for s in status]

if original_language:
query["originalLanguage"] = original_language
Expand All @@ -595,13 +602,13 @@ def _manga_list(
query["availableTranslatedLanguage"] = available_translated_language

if publication_demographic:
query["publicationDemographic"] = publication_demographic
query["publicationDemographic"] = [demo.value for demo in publication_demographic]

if ids:
query["ids"] = ids

if content_rating:
query["contentRating"] = content_rating
query["contentRating"] = [cr.value for cr in content_rating]

if created_at_since:
query["createdAtSince"] = clean_isoformat(created_at_since)
Expand Down Expand Up @@ -699,7 +706,7 @@ def _get_manga_volumes_and_chapters(
) -> Response[manga.GetMangaVolumesAndChaptersResponse]:
route = Route("GET", "/manga/{manga_id}/aggregate", manga_id=manga_id)

query: dict[str, Any] = {}
query: MANGADEX_QUERY_PARAM_TYPE = {}

if translated_language:
query["translatedLanguage"] = translated_language
Expand All @@ -715,7 +722,7 @@ def _get_manga(self, manga_id: str, /, *, includes: Optional[MangaIncludes]) ->
route = Route("GET", "/manga/{manga_id}", manga_id=manga_id)

if includes:
query: dict[str, list[str]] = {"includes": includes.to_query()}
query: MANGADEX_QUERY_PARAM_TYPE = {"includes": includes.to_query()}
return self.request(route, params=query)
return self.request(route)

Expand Down Expand Up @@ -825,7 +832,7 @@ def _manga_feed(

limit, offset = calculate_limits(limit, offset, max_limit=500)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if translated_language:
query["translatedLanguage"] = translated_language
Expand All @@ -837,7 +844,7 @@ def _manga_feed(
query["excludedOriginalLanguage"] = excluded_original_language

if content_rating:
query["contentRating"] = content_rating
query["contentRating"] = [cr.value for cr in content_rating]

if excluded_groups:
query["excludedGroups"] = excluded_groups
Expand Down Expand Up @@ -897,13 +904,13 @@ def _get_random_manga(
) -> Response[manga.GetMangaResponse]:
route = Route("GET", "/manga/random")

query: dict[str, Any] = {}
query: MANGADEX_QUERY_PARAM_TYPE = {}

if includes:
query["includes"] = includes.to_query()

if content_rating:
query["contentRating"] = content_rating
query["contentRating"] = [cr.value for cr in content_rating]

if included_tags:
query["includedTags"] = included_tags.tags
Expand Down Expand Up @@ -943,7 +950,7 @@ def _manga_read_markers(
return self.request(route)

route = Route("GET", "/manga/read")
query: dict[str, Any] = {"ids": manga_ids, "grouped": True}
query: MANGADEX_QUERY_PARAM_TYPE = {"ids": manga_ids, "grouped": True}
return self.request(route, params=query)

def _manga_read_markers_batch(
Expand All @@ -958,24 +965,24 @@ def _manga_read_markers_batch(
route = Route("POST", "/manga/{manga_id}/read", manga_id=manga_id)

body = {}
params = {"updateHistory": update_history} if update_history else None
query: Optional[MANGADEX_QUERY_PARAM_TYPE] = {"updateHistory": update_history} if update_history else None

if read_chapters:
body["chapterIdsRead"] = read_chapters

if unread_chapters:
body["chapterIdsUnread"] = unread_chapters

if params:
return self.request(route, json=body, params=params)
if query:
return self.request(route, json=body, params=query)
return self.request(route, json=body)

def _get_all_manga_reading_status(
self, *, status: Optional[ReadingStatus] = None
) -> Response[manga.MangaMultipleReadingStatusResponse]:
route = Route("GET", "/manga/status")
if status:
query: dict[str, Any] = {"status": status.value}
query: MANGADEX_QUERY_PARAM_TYPE = {"status": status.value}
return self.request(route, params=query)
return self.request(route)

Expand Down Expand Up @@ -1012,7 +1019,7 @@ def _get_manga_draft_list(

limit, offset = calculate_limits(limit, offset, max_limit=100)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if state:
query["state"] = state.value
Expand All @@ -1031,7 +1038,7 @@ def _get_manga_relation_list(
route = Route("GET", "/manga/{manga_id}/relation", manga_id=manga_id)

if includes:
query: dict[str, Any] = {"includes": includes.to_query()}
query: MANGADEX_QUERY_PARAM_TYPE = {"includes": includes.to_query()}
return self.request(route, params=query)

return self.request(route)
Expand Down Expand Up @@ -1079,7 +1086,7 @@ def _chapter_list(

limit, offset = calculate_limits(limit, offset, max_limit=100)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if ids:
query["ids"] = ids
Expand Down Expand Up @@ -1112,7 +1119,7 @@ def _chapter_list(
query["excludedOriginalLanguage"] = excluded_original_language

if content_rating:
query["contentRating"] = content_rating
query["contentRating"] = [cr.value for cr in content_rating]

if excluded_groups:
query["excludedGroups"] = excluded_groups
Expand Down Expand Up @@ -1219,7 +1226,7 @@ def _cover_art_list(

limit, offset = calculate_limits(limit, offset, max_limit=100)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if manga:
query["manga"] = manga
Expand Down Expand Up @@ -1267,7 +1274,7 @@ def _get_cover(self, cover_id: str, /, *, includes: Optional[CoverIncludes]) ->
route = Route("GET", "/cover/{cover_id}", cover_id=cover_id)

if includes:
query: dict[str, list[str]] = {"includes": includes.to_query()}
query: MANGADEX_QUERY_PARAM_TYPE = {"includes": includes.to_query()}
return self.request(route, params=query)
return self.request(route)

Expand Down Expand Up @@ -1317,7 +1324,7 @@ def _scanlation_group_list(

limit, offset = calculate_limits(limit, offset, max_limit=100)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if ids:
query["ids"] = ids
Expand Down Expand Up @@ -1349,7 +1356,7 @@ def _user_list(

limit, offset = calculate_limits(limit, offset, max_limit=100)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if ids:
query["ids"] = ids
Expand Down Expand Up @@ -1397,7 +1404,7 @@ def _get_my_followed_groups(

limit, offset = calculate_limits(limit, offset, max_limit=100)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}
return self.request(route, params=query)

def _check_if_following_group(self, group_id: str, /) -> Response[dict[Literal["result"], Literal["ok"]]]:
Expand All @@ -1409,7 +1416,7 @@ def _get_my_followed_users(self, *, limit: int, offset: int) -> Response[user.Ge

limit, offset = calculate_limits(limit, offset, max_limit=100)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

return self.request(route, params=query)

Expand All @@ -1425,7 +1432,7 @@ def _get_user_custom_list_follows(self, limit: int, offset: int) -> Response[cus
route = Route("GET", "/user/follows/list")

limit, offset = calculate_limits(limit, offset, max_limit=100)
query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

return self.request(route, params=query)

Expand All @@ -1438,7 +1445,7 @@ def _get_user_followed_manga(
) -> Response[manga.MangaSearchResponse]:
route = Route("GET", "/user/follows/manga")

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if includes:
query["includes"] = includes.to_query()
Expand Down Expand Up @@ -1484,7 +1491,7 @@ def _legacy_id_mapping(

def _get_at_home_url(self, chapter_id: str, /, *, ssl: bool) -> Response[chapter.GetAtHomeResponse]:
route = Route("GET", "/at-home/server/{chapter_id}", chapter_id=chapter_id)
query: dict[str, Any] = {"forcePort443": str(ssl).lower()}
query: MANGADEX_QUERY_PARAM_TYPE = {"forcePort443": ssl}
return self.request(route, params=query)

def _create_custom_list(
Expand Down Expand Up @@ -1512,7 +1519,7 @@ def _get_custom_list(
route = Route("GET", "/list/{custom_list_id}", custom_list_id=custom_list_id)

if includes:
query: dict[str, list[str]] = {"includes": includes.to_query()}
query: MANGADEX_QUERY_PARAM_TYPE = {"includes": includes.to_query()}
return self.request(route, params=query)
return self.request(route)

Expand Down Expand Up @@ -1568,7 +1575,7 @@ def _remove_manga_from_custom_list(
def _get_my_custom_lists(self, limit: int, offset: int) -> Response[custom_list.GetMultiCustomListResponse]:
route = Route("GET", "/user/list")

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

return self.request(route, params=query)

Expand All @@ -1577,7 +1584,7 @@ def _get_users_custom_lists(
) -> Response[custom_list.GetMultiCustomListResponse]:
route = Route("GET", "/user/{user_id}/list", user_id=user_id)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

return self.request(route, params=query)

Expand Down Expand Up @@ -1608,7 +1615,7 @@ def _custom_list_manga_feed(

limit, offset = calculate_limits(limit, offset, max_limit=500)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if translated_language:
query["translatedLanguage"] = translated_language
Expand All @@ -1620,7 +1627,7 @@ def _custom_list_manga_feed(
query["excludedOriginalLanguage"] = excluded_original_language

if content_rating:
query["contentRating"] = content_rating
query["contentRating"] = [cr.value for cr in content_rating]

if excluded_groups:
query["excludedGroups"] = excluded_groups
Expand Down Expand Up @@ -1721,7 +1728,7 @@ def _view_scanlation_group(
route = Route("GET", "/group/{scanlation_group_id}", scanlation_group_id=scanlation_group_id)

if includes:
query: dict[str, list[str]] = {"includes": includes.to_query()}
query: MANGADEX_QUERY_PARAM_TYPE = {"includes": includes.to_query()}
return self.request(route, params=query)
return self.request(route)

Expand Down Expand Up @@ -1830,7 +1837,7 @@ def _author_list(

limit, offset = calculate_limits(limit, offset, max_limit=100)

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if ids:
query["ids"] = ids
Expand Down Expand Up @@ -1911,7 +1918,7 @@ def _get_author(
route = Route("GET", "/author/{author_id}", author_id=author_id)

if includes:
query: dict[str, list[str]] = {"includes": includes.to_query()}
query: MANGADEX_QUERY_PARAM_TYPE = {"includes": includes.to_query()}
return self.request(route, params=query)

return self.request(route)
Expand Down Expand Up @@ -1993,7 +2000,7 @@ def _get_artist(
route = Route("GET", "/author/{artist_id}", artist_id=artist_id)

if includes:
query: dict[str, list[str]] = {"includes": includes.to_query()}
query: MANGADEX_QUERY_PARAM_TYPE = {"includes": includes.to_query()}
return self.request(route, params=query)
return self.request(route)

Expand Down Expand Up @@ -2088,7 +2095,7 @@ def _get_reports_current_user(

route = Route("GET", "/report")

query: dict[str, Any] = {"limit": limit, "offset": offset}
query: MANGADEX_QUERY_PARAM_TYPE = {"limit": limit, "offset": offset}

if object_id:
query["objectId"] = object_id
Expand Down Expand Up @@ -2138,7 +2145,7 @@ def _create_report(self, *, details: ReportDetails) -> Response[dict[Literal["re
def _get_my_ratings(self, manga_ids: list[str], /) -> Response[statistics.GetPersonalMangaRatingsResponse]:
route = Route("GET", "/rating")

query: dict[str, Any] = {"manga": manga_ids}
query: MANGADEX_QUERY_PARAM_TYPE = {"manga": manga_ids}

return self.request(route, params=query)

Expand All @@ -2162,7 +2169,7 @@ def _get_manga_statistics(self, manga_id: str, /) -> Response[statistics.GetStat
def _find_manga_statistics(self, manga_ids: list[str], /) -> Response[statistics.BatchGetStatisticsResponse]:
route = Route("GET", "/statistics/manga")

query: dict[str, Any] = {"manga": manga_ids}
query: MANGADEX_QUERY_PARAM_TYPE = {"manga": manga_ids}

return self.request(route, params=query)

Expand Down
Loading

0 comments on commit df35e97

Please sign in to comment.