Skip to content

Commit aa44726

Browse files
authored
Merge pull request #84 from JaviCerveraIngram/CPS-49-rql
Cps 49 rql
2 parents 362eb46 + 46bd5b8 commit aa44726

File tree

8 files changed

+429
-21
lines changed

8 files changed

+429
-21
lines changed

connect/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(self, config=None):
4242
'logger',
4343
'models',
4444
'resources',
45+
'rql',
4546
'FulfillmentAutomation',
4647
'TierConfigAutomation',
4748
]

connect/models/product.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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
67
import datetime
78
from typing import Optional
89

@@ -17,6 +18,7 @@
1718
from .template import Template
1819
from connect.config import Config
1920
from connect.resources.base import ApiClient
21+
from connect.rql import Query
2022

2123

2224
class Product(BaseModel):
@@ -80,7 +82,7 @@ def get_templates(self, config=None):
8082

8183
def get_product_configurations(self, filters=None, config=None):
8284
"""
83-
:param Dict[str, Any] filters: Filters for the requests. Supported filters are:
85+
:param dict|Query filters: Filters for the requests. Supported filters are:
8486
- ``parameter.id``
8587
- ``parameter.title``
8688
- ``parameter.scope``
@@ -93,6 +95,7 @@ def get_product_configurations(self, filters=None, config=None):
9395
:return: A list with the product configuration parameter data.
9496
:rtype: List[ProductConfigurationParameter]
9597
"""
98+
query = copy(filters) if isinstance(filters, Query) else Query(filters)
9699
text, _ = ApiClient(config or Config.get_instance(),
97-
'products/' + self.id + '/configurations').get(params=filters)
100+
'products/' + self.id + '/configurations' + query.compile()).get()
98101
return ProductConfigurationParameter.deserialize(text)

connect/resources/directory.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
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+
68
from connect.config import Config
79
from connect.models.asset import Asset
810
from connect.models.product import Product
911
from connect.models.tier_config import TierConfig
1012
from connect.resources.base import ApiClient
13+
from connect.rql import Query
1114

1215

1316
class 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

connect/rql/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
4+
# Copyright (c) 2019 Ingram Micro. All Rights Reserved.
5+
6+
from .query import Query
7+
8+
__all__ = [
9+
'Query'
10+
]

0 commit comments

Comments
 (0)