diff --git a/clients_protocol/__init__.py b/clients_protocol/__init__.py index 049aeb9..3f025f6 100644 --- a/clients_protocol/__init__.py +++ b/clients_protocol/__init__.py @@ -1,5 +1,5 @@ -"""# Common HTTP/REST clients interface -""" +"""# Common HTTP/REST clients interface""" + import urllib.parse import abc @@ -11,6 +11,10 @@ import bs4 import lxml.html import requests +from __version import __version__ + +__all__ = ['__version__'] + logger = logging.getLogger(__name__) @@ -40,8 +44,10 @@ class AbstractClient(abc.ABC): def fetch(self, url: str, **kwargs) -> requests.Response: r = self._fetch(url, **kwargs) - if r.status_code in {301,302,303}: - msg = f'Redirection: {r.request.method} {url} -> GET {r.headers["Location"]}' + if r.status_code in {301, 302, 303}: + msg = ( + f'Redirection: {r.request.method} {url} -> GET {r.headers["Location"]}' + ) raise Exception(msg) r.raise_for_status() return r @@ -51,7 +57,9 @@ class AbstractClient(abc.ABC): kwargs.setdefault('allow_redirects', True) url_parsed = urllib.parse.urlparse(url) - origin_url = url_parsed._replace(path='',params='',query='',fragment='').geturl() + origin_url = url_parsed._replace( + path='', params='', query='', fragment='' + ).geturl() kwargs.setdefault('headers', {}).setdefault('Origin', origin_url) kwargs.setdefault('headers', {}).setdefault('Alt-Used', url_parsed.hostname) @@ -68,21 +76,21 @@ class AbstractClient(abc.ABC): url: str, **kwargs, ) -> None | bs4.BeautifulSoup: - kwargs.setdefault('headers', {}).setdefault('Accept', 'text/html') + kwargs.setdefault('headers', {}).setdefault('Accept', 'text/html') text = self.fetch_text(url, **kwargs) if text is None: return None return lxml.html.document_fromstring(text) def fetch_soup(self, url: str, **kwargs) -> None | bs4.BeautifulSoup: - kwargs.setdefault('headers', {}).setdefault('Accept', 'text/html') + kwargs.setdefault('headers', {}).setdefault('Accept', 'text/html') text = self.fetch_text(url, **kwargs) if text is None: return None return bs4.BeautifulSoup(text, 'html.parser') def fetch_json(self, url: str, **kwargs) -> None | dict[str, Any]: - kwargs.setdefault('headers', {}).setdefault('Accept', 'application/json') + kwargs.setdefault('headers', {}).setdefault('Accept', 'application/json') response = self.fetch(url=url, **kwargs) loaded_json = response.json() diff --git a/clients_protocol/stores.py b/clients_protocol/stores.py index 8371462..3f0966c 100644 --- a/clients_protocol/stores.py +++ b/clients_protocol/stores.py @@ -15,6 +15,7 @@ DEFAULT_MAX_RESULTS = 1 URL = str + @dataclasses.dataclass(frozen=True, order=True, slots=True) class SellerInfo: name: str diff --git a/clients_protocol/wishlist.py b/clients_protocol/wishlist.py index 613fd8a..90e4242 100644 --- a/clients_protocol/wishlist.py +++ b/clients_protocol/wishlist.py @@ -7,6 +7,7 @@ import dataclasses logger = logging.getLogger(__name__) + @dataclasses.dataclass(frozen=True) class WishlistItem: """A single wishlished product.""" @@ -14,12 +15,11 @@ class WishlistItem: product_name: str # Name of the game/product reference_url: str # URL to a reference page for the item. image_url: str | None = None # URL to the product image - console_name: str | None = None # Gaming platform/console name - reference_price: fin_defs.AssetAmount | None = None # Reference price, if any + console_name: str | None = None # Gaming platform/console name + reference_price: fin_defs.AssetAmount | None = None # Reference price, if any class WishlistClient(abc.ABC): - @abc.abstractmethod def get_wishlist(self) -> Sequence[WishlistItem]: pass diff --git a/test/test_init.py b/test/test_init.py index ae2528d..ebf324a 100644 --- a/test/test_init.py +++ b/test/test_init.py @@ -1,5 +1,3 @@ - def test_init(): import clients_protocol import clients_protocol.autoload -