33# This file is part of the Ingram Micro Cloud Blue Connect SDK.
44# Copyright (c) 2019 Ingram Micro. All Rights Reserved.
55
6+ from copy import copy
7+
68from connect .config import Config
79from connect .models .asset import Asset
810from connect .models .product import Product
911from connect .models .tier_config import TierConfig
1012from connect .resources .base import ApiClient
13+ from connect .rql import Query
1114
1215
1316class Directory (object ):
@@ -24,15 +27,12 @@ def __init__(self, config=None):
2427 def list_assets (self , filters = None ):
2528 """ List the assets.
2629
27- :param ( dict[str, Any] filters: Filters to pass to the request.
30+ :param dict|Query filters: Filters to pass to the request.
2831 :return: A list with the assets that match the given filters.
2932 :rtype: list[Asset]
3033 """
31- products = ',' .join (self ._config .products ) if self ._config .products else None
32- url = self ._config .api_url + 'assets?in(product.id,(' + products + '))' \
33- if products \
34- else 'assets'
35- text , code = ApiClient (self ._config , url ).get (params = filters )
34+ query = self ._get_filters_query (filters , True )
35+ text , code = ApiClient (self ._config , 'assets' + query .compile ()).get ()
3636 return Asset .deserialize (text )
3737
3838 def get_asset (self , asset_id ):
@@ -45,13 +45,15 @@ def get_asset(self, asset_id):
4545 text , code = ApiClient (self ._config , 'assets/' + asset_id ).get ()
4646 return Asset .deserialize (text )
4747
48- def list_products (self ):
49- """ List the products. Filtering is not possible at the moment.
48+ def list_products (self , filters = None ):
49+ """ List the products.
5050
51+ :param dict|Query filters: Filters to pass to the request.
5152 :return: A list with all products.
5253 :rtype: list[Product]
5354 """
54- text , code = ApiClient (self ._config , 'products' ).get ()
55+ query = self ._get_filters_query (filters , False )
56+ text , code = ApiClient (self ._config , 'products' + query .compile ()).get ()
5557 return Product .deserialize (text )
5658
5759 def get_product (self , product_id ):
@@ -71,11 +73,8 @@ def list_tier_configs(self, filters=None):
7173 :return: A list with the tier configs that match the given filters.
7274 :rtype: list[TierConfig]
7375 """
74- filters = filters or {}
75- products_key = 'product.id'
76- if products_key not in filters and self ._config .products :
77- filters [products_key ] = ',' .join (self ._config .products )
78- text , code = ApiClient (self ._config , 'tier/configs' ).get (params = filters )
76+ query = self ._get_filters_query (filters , True )
77+ text , code = ApiClient (self ._config , 'tier/configs' + query .compile ()).get ()
7978 return TierConfig .deserialize (text )
8079
8180 def get_tier_config (self , tier_config_id ):
@@ -87,3 +86,15 @@ def get_tier_config(self, tier_config_id):
8786 """
8887 text , code = ApiClient (self ._config , 'tier/configs/' + tier_config_id ).get ()
8988 return TierConfig .deserialize (text )
89+
90+ def _get_filters_query (self , filters , add_product ):
91+ """
92+ :param dict|Query filters: Filters to return as query (with product.id field).
93+ :param bool add_product: Whether to add a product.id field to the query.
94+ :return: The query.
95+ :rtype: Query
96+ """
97+ query = copy (filters ) if isinstance (filters , Query ) else Query (filters )
98+ if add_product and self ._config .products :
99+ query .in_ ('product.id' , self ._config .products )
100+ return query
0 commit comments