def _call( self, endpoint: List[Union[str, int]], params: Optional[Dict[str, Union[str, int]]] = None, ) -> Dict[str, Any]: """ Make request for api endpoints. :param str endpoint: The endpoint to request information from. :param dict params: Parameters to add to the request. """ if params is None: params = {} cache_params = "" if params: ordered_params = OrderedDict( sorted(params.items(), key=lambda t: t[0])) cache_params = f"?{urlencode(ordered_params)}" url = self.api_url.format("/".join(str(e) for e in endpoint)) cache_key = f"{url}{cache_params}" cached_response = self._get_results_from_cache(cache_key) if cached_response is not None: return cached_response data = self._request_data(url, params) if "detail" in data: raise exceptions.ApiError(data["detail"]) self._save_results_to_cache(cache_key, data) return data
def __init__(self, response) -> None: """Initialize a new CreatorsList.""" self.creators = [] schema = CreatorSchema() for creator_dict in response["results"]: try: result = schema.load(creator_dict) except ValidationError as error: raise exceptions.ApiError(error) from error self.creators.append(result)
def __init__(self, response): """Initialize a new ArcsList.""" self.arcs = [] schema = ArcSchema() for arc_dict in response["results"]: try: result = schema.load(arc_dict) except ValidationError as error: raise exceptions.ApiError(error) from error self.arcs.append(result)
def __init__(self, response): """Initialize a new IssuesList.""" self.issues = [] schema = IssueSchema() for issue_dict in response["results"]: try: result = schema.load(issue_dict) except ValidationError as error: raise exceptions.ApiError(error) from error self.issues.append(result)
def __init__(self, response): """Initialize a new PublishersList.""" self.publishers = [] schema = PublisherSchema() for pub_dict in response["results"]: try: result = schema.load(pub_dict) except ValidationError as error: raise exceptions.ApiError(error) from error self.publishers.append(result)
def issue(self, _id: int) -> issues.Issue: """ Request data for an issue based on it's ``_id``. :param int _id: The issue id. :return: :class:`Issue` object :rtype: Issue :raises: :class:`ApiError` """ try: result = issues.IssueSchema().load(self._call(["issue", _id])) except ValidationError as error: raise exceptions.ApiError(error) from error return result
def series(self, _id: int) -> ser.Series: """ Request data for a series based on its ``_id``. :param int _id: The series id. :return: :class:`Series` object :rtype: Series :raises: :class:`ApiError` """ try: result = ser.SeriesSchema().load(self._call(["series", _id])) except ValidationError as error: raise exceptions.ApiError(error) from error return result
def arc(self, _id: int) -> arcs.Arc: """ Request data for a story arc based on its ``_id``. :param int _id: The story arc id. :return: :class:`Arc` object :rtype: Arc :raises: :class:`ApiError` """ try: result = arcs.ArcSchema().load(self._call(["arc", _id])) except ValidationError as error: raise exceptions.ApiError(error) from error return result
def team(self, _id: int) -> teams.Team: """ Request data for a team based on its ``_id``. :param int _id: The team id. :return: :class:`Team` object :rtype: Team :raises: :class:`ApiError` """ try: result = teams.TeamSchema().load(self._call(["team", _id])) except ValidationError as error: raise exceptions.ApiError(error) from error return result
def creator(self, _id: int) -> creators.Creator: """ Request data for a creator based on its ``_id``. :param int _id: The creator id. :return: :class:`Creator` object :rtype: Creator :raises: :class:`ApiError` """ try: result = creators.CreatorSchema().load(self._call(["creator", _id])) except ValidationError as error: raise exceptions.ApiError(error) from error return result
def publisher(self, _id: int) -> publishers.Publisher: """ Request data for a publisher based on its ``_id``. :param int _id: The publisher id. :return: :class:`Publisher` object :rtype: Publisher :raises: :class:`ApiError` """ try: result = publishers.PublisherSchema().load( self._call(["publisher", _id])) except ValidationError as error: raise exceptions.ApiError(error) from error return result
def _request_data( self, url: str, params: Optional[Dict[str, Union[str, int]]] = None) -> Any: if params is None: params = {} try: session = requests.Session() retry = Retry(connect=3, backoff_factor=0.5) session.mount("https://", HTTPAdapter(max_retries=retry)) response = session.get( url, params=params, timeout=2.5, auth=(self.username, self.passwd), headers=self.header, ).json() except requests.exceptions.ConnectionError as e: raise exceptions.ApiError(f"Connection error: {repr(e)}") from e return response