55API in an easier way.
66"""
77
8+ from aliexpress_api .errors .exceptions import CategoriesNotFoudException
9+ from aliexpress_api .helpers .categories import filter_child_categories , filter_parent_categories
10+ from aliexpress_api .models .category import ChildCategory
811from .skd import setDefaultAppInfo
912from .skd import api as aliapi
1013from .errors import ProductsNotFoudException , InvalidTrackingIdException
@@ -39,6 +42,7 @@ def __init__(self,
3942 self ._language = language
4043 self ._currency = currency
4144 self ._app_signature = app_signature
45+ self .categories = None
4246 setDefaultAppInfo (self ._key , self ._secret )
4347
4448
@@ -56,7 +60,7 @@ def get_products_details(self,
5660 according to the country's tax rate policy.
5761
5862 Returns:
59- ``List [models.Product]``: A list of products.
63+ ``list [models.Product]``: A list of products.
6064
6165 Raises:
6266 ``ProductsNotFoudException``
@@ -188,3 +192,65 @@ def get_hotproducts(self,
188192 return response
189193 else :
190194 raise ProductsNotFoudException ('No products found with current parameters' )
195+
196+
197+ def get_categories (self , ** kwargs ) -> List [Union [models .Category , ChildCategory ]]:
198+ """Get all available categories, both parent and child.
199+
200+ Returns:
201+ ``list[models.Category | models.ChildCategory]``: A list of categories.
202+
203+ Raises:
204+ ``CategoriesNotFoudException``
205+ ``ApiRequestException``
206+ ``ApiRequestResponseException``
207+ """
208+ request = aliapi .rest .AliexpressAffiliateCategoryGetRequest ()
209+ request .app_signature = self ._app_signature
210+
211+ response = api_request (request , 'aliexpress_affiliate_category_get_response' )
212+
213+ if response .total_result_count > 0 :
214+ self .categories = response .categories .category
215+ return self .categories
216+ else :
217+ raise CategoriesNotFoudException ('No categories found' )
218+
219+
220+ def get_parent_categories (self , use_cache = True , ** kwargs ) -> List [models .Category ]:
221+ """Get all available parent categories.
222+
223+ Args:
224+ use_cache (``bool``): Uses cached categories to reduce API requests.
225+
226+ Returns:
227+ ``list[models.Category]``: A list of parent categories.
228+
229+ Raises:
230+ ``CategoriesNotFoudException``
231+ ``ApiRequestException``
232+ ``ApiRequestResponseException``
233+ """
234+ if not use_cache or not self .categories :
235+ self .get_categories ()
236+ return filter_parent_categories (self .categories )
237+
238+
239+ def get_child_categories (self , parent_category_id : int , use_cache = True , ** kwargs ) -> List [models .ChildCategory ]:
240+ """Get all available child categories for a specific parent category.
241+
242+ Args:
243+ parent_category_id (``int``): The parent category id.
244+ use_cache (``bool``): Uses cached categories to reduce API requests.
245+
246+ Returns:
247+ ``list[models.ChildCategory]``: A list of child categories.
248+
249+ Raises:
250+ ``CategoriesNotFoudException``
251+ ``ApiRequestException``
252+ ``ApiRequestResponseException``
253+ """
254+ if not use_cache or not self .categories :
255+ self .get_categories ()
256+ return filter_child_categories (self .categories , parent_category_id )
0 commit comments