11"""Работа с каталогом"""
22
3- from typing import TYPE_CHECKING , Optional
43import urllib .parse
4+ from typing import TYPE_CHECKING , Optional
5+
56from human_requests .abstraction import FetchResponse , HttpMethod
67
78from ..enums import PurchaseMode , Sorting
9+
810if TYPE_CHECKING :
911 from ..manager import PyaterochkaAPI
1012
@@ -18,108 +20,101 @@ class ClassCatalog:
1820
1921 def __init__ (self , parent : "PyaterochkaAPI" ):
2022 self ._parent : "PyaterochkaAPI" = parent
21- self .Product : ProductService = ProductService (
22- parent = self ._parent
23- )
23+ self .Product : ProductService = ProductService (parent = self ._parent )
2424 """Сервис для работы с товарами в каталоге."""
2525
26- async def tree (self ,
27- sap_code_store_id : str ,
28- subcategories : bool = False ,
29- include_restrict : bool = True ,
30- mode : PurchaseMode = PurchaseMode .STORE ) -> FetchResponse :
26+ async def tree (
27+ self ,
28+ sap_code_store_id : str ,
29+ subcategories : bool = False ,
30+ include_restrict : bool = True ,
31+ mode : PurchaseMode = PurchaseMode .STORE ,
32+ ) -> FetchResponse :
3133 """
32- Asynchronously retrieves a list of categories from the Pyaterochka API .
34+ Список категорий (глобальный) .
3335
34- Args:
35- subcategories (bool, optional): Whether to include subcategories in the response. Defaults to False.
36- include_restrict (bool, optional): I DO NOT KNOW WHAT IS IT
37- mode (PurchaseMode, optional): The purchase mode to use. Defaults to PurchaseMode.STORE.
38- sap_code_store_id (str, optional): The store ID (official name in API is "sap_code") to use. Defaults to "{self.DEFAULT_STORE_ID}". This lib not support search ID stores.
39-
40- Returns:
41- list[dict]: A dictionary representing the categories list if the request is successful, error otherwise.
42-
43- Raises:
44- Exception: If the response status is not 200 (OK) or 403 (Forbidden / Anti-bot).
36+ include_restrict - включать ли в выдачу закончившиеся в магазине товары.
4537 """
4638
4739 request_url = f"{ self ._parent .CATALOG_URL } /catalog/v2/stores/{ sap_code_store_id } /categories?mode={ mode .value } &include_restrict={ include_restrict } &include_subcategories={ 1 if subcategories else 0 } "
48- return await self ._parent ._request (method = HttpMethod .GET , url = request_url , add_unstandard_headers = True )
40+ return await self ._parent ._request (
41+ method = HttpMethod .GET , url = request_url , add_unstandard_headers = True
42+ )
4943
50- async def tree_extended (self ,
51- sap_code_store_id : str ,
52- category_id : str ,
53- include_restrict : bool = True ,
54- mode : PurchaseMode = PurchaseMode .STORE ) -> FetchResponse :
44+ async def tree_extended (
45+ self ,
46+ sap_code_store_id : str ,
47+ category_id : str ,
48+ include_restrict : bool = True ,
49+ mode : PurchaseMode = PurchaseMode .STORE ,
50+ ) -> FetchResponse :
5551 """Расширенное представление категории и её подкатегорий."""
5652 request_url = f"{ self ._parent .CATALOG_URL } /catalog/v2/stores/{ sap_code_store_id } /categories/{ category_id } /extended?mode={ mode .value } &include_restrict={ str (include_restrict ).lower ()} "
57- return await self ._parent ._request (method = HttpMethod .GET , url = request_url , add_unstandard_headers = True )
58-
59- async def search (self ,
60- sap_code_store_id : str ,
61- query : str ,
62- include_restrict : bool = True ,
63- mode : PurchaseMode = PurchaseMode .STORE ,
64- limit : int = 12 ) -> FetchResponse :
53+ return await self ._parent ._request (
54+ method = HttpMethod .GET , url = request_url , add_unstandard_headers = True
55+ )
56+
57+ async def search (
58+ self ,
59+ sap_code_store_id : str ,
60+ query : str ,
61+ include_restrict : bool = True ,
62+ mode : PurchaseMode = PurchaseMode .STORE ,
63+ limit : int = 12 ,
64+ ) -> FetchResponse :
6565 """Поиск по товарам И категориям."""
6666 q = urllib .parse .quote (query )
6767 request_url = f"{ self ._parent .CATALOG_URL } /catalog/v3/stores/{ sap_code_store_id } /search?mode={ mode .value } &include_restrict={ str (include_restrict ).lower ()} &q={ q } &limit={ limit } "
68- return await self ._parent ._request (method = HttpMethod .GET , url = request_url , add_unstandard_headers = True )
68+ return await self ._parent ._request (
69+ method = HttpMethod .GET , url = request_url , add_unstandard_headers = True
70+ )
6971
7072 async def products_list (
71- self ,
72- category_id : str ,
73- sap_code_store_id : str ,
74- price_min : Optional [int ] = None ,
75- price_max : Optional [int ] = None ,
76- brands : list [str ] = [],
77- include_restrict : bool = True ,
78- mode : PurchaseMode = PurchaseMode .STORE ,
79- limit : int = 30
73+ self ,
74+ category_id : str ,
75+ sap_code_store_id : str ,
76+ price_min : Optional [int ] = None ,
77+ price_max : Optional [int ] = None ,
78+ brands : list [str ] = [],
79+ include_restrict : bool = True ,
80+ mode : PurchaseMode = PurchaseMode .STORE ,
81+ limit : int = 30 ,
8082 ) -> FetchResponse :
81- f"""
82- Asynchronously retrieves a list of products from the Pyaterochka API for a given category.
83-
84- Args:
85- category_id (str): The ID of the (sub)category.
86- mode (PurchaseMode, optional): The purchase mode to use. Defaults to PurchaseMode.STORE.
87- sap_code_store_id (str, optional): The store ID (official name in API is "sap_code") to use. This lib not support search ID stores.
88- limit (int, optional): The maximum number of products to retrieve. Defaults to 30. Must be between 1 and 499.
89-
90- Returns:
91- dict: A dictionary representing the products list if the request is successful, error otherwise.
83+ """
84+ Список категорий (основная лента каталога).
9285
93- Raises:
94- ValueError: If the limit is not between 1 and 499.
95- Exception: If the response status is not 200 (OK) or 403 (Forbidden / Anti-bot).
86+ brands - должно быть полное совпадение, другие едпоинты предоставляют их.
9687 """
9788
9889 if limit < 1 or limit >= 500 :
9990 raise ValueError ("Limit must be between 1 and 499" )
10091
10192 request_url = f"{ self ._parent .CATALOG_URL } /catalog/v2/stores/{ sap_code_store_id } /categories/{ category_id } /products?mode={ mode .value } &limit={ limit } &include_restrict={ str (include_restrict ).lower ()} "
10293 if price_min :
103- request_url += "&price_min=" + str (price_min )
94+ request_url += "&price_min=" + str (price_min )
10495 if price_max :
105- request_url += "&price_max=" + str (price_max )
96+ request_url += "&price_max=" + str (price_max )
10697 if len (brands ) > 0 :
107- encoded_brands = [f' brands={ urllib .parse .quote (brand )} ' for brand in brands ]
108- request_url += "&" + '&&' .join (encoded_brands )
98+ encoded_brands = [f" brands={ urllib .parse .quote (brand )} " for brand in brands ]
99+ request_url += "&" + "&&" .join (encoded_brands )
109100
110- return await self ._parent ._request (method = HttpMethod .GET , url = request_url , add_unstandard_headers = True )
101+ return await self ._parent ._request (
102+ method = HttpMethod .GET , url = request_url , add_unstandard_headers = True
103+ )
111104
112105 async def products_line (
113- self ,
114- category_id : str ,
115- sap_code_store_id : str ,
116- include_restrict : bool = True ,
117- mode : PurchaseMode = PurchaseMode .STORE ,
118- order_by : Sorting = Sorting .POPULARITY
119- ) -> FetchResponse :
106+ self ,
107+ category_id : str ,
108+ sap_code_store_id : str ,
109+ include_restrict : bool = True ,
110+ mode : PurchaseMode = PurchaseMode .STORE ,
111+ order_by : Sorting = Sorting .POPULARITY ,
112+ ) -> FetchResponse :
120113 """Рекомендованные товары \" что интересного?\" ."""
121114 request_url = f"{ self ._parent .CATALOG_URL } /catalog/v1/stores/{ sap_code_store_id } /categories/{ category_id } /products_line?mode={ mode .value } &include_restrict={ str (include_restrict ).lower ()} &order_by={ order_by .value } "
122- return await self ._parent ._request (method = HttpMethod .GET , url = request_url , add_unstandard_headers = True )
115+ return await self ._parent ._request (
116+ method = HttpMethod .GET , url = request_url , add_unstandard_headers = True
117+ )
123118
124119
125120class ProductService :
@@ -133,18 +128,12 @@ async def info(
133128 sap_code_store_id : str ,
134129 plu_id : int ,
135130 mode : PurchaseMode = PurchaseMode .STORE ,
136- include_restrict : bool = True
131+ include_restrict : bool = True ,
137132 ) -> FetchResponse :
138133 """
139- Asynchronously retrieves product information from the Pyaterochka API for a given PLU ID. Average time processing 2 seconds (first start 6 seconds).
140-
141- Args:
142- plu_id (int): The PLU ID of the product.
143- Returns:
144- dict: A dictionary representing the product information.
145- Raises:
146- ValueError: If the response does not contain the expected JSON data.
134+ Подробная информация о конкретном товаре.
147135 """
148-
149136 request_url = f"{ self ._parent .CATALOG_URL } /catalog/v2/stores/{ sap_code_store_id } /products/{ plu_id } ?mode={ mode .value } &include_restrict={ str (include_restrict ).lower ()} "
150- return await self ._parent ._request (method = HttpMethod .GET , url = request_url , add_unstandard_headers = True )
137+ return await self ._parent ._request (
138+ method = HttpMethod .GET , url = request_url , add_unstandard_headers = True
139+ )
0 commit comments