Skip to content

Commit 192a06a

Browse files
author
Francesco Faraone
committed
Add NestedResource class to handle nested collections
Extend the product Item object with missing attributes Switch automation engines to rql Store server error response in ServerError exception
1 parent debddc3 commit 192a06a

21 files changed

+204
-32
lines changed

connect/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class ServerError(Exception):
8484
"""
8585

8686
def __init__(self, error):
87+
self.error = error
8788
super(ServerError, self).__init__(str(error), error.error_code)
8889

8990

connect/models/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .asset import Asset
1414
from .base import BaseModel
1515
from .billing import Billing
16+
from .commitment import Commitment
1617
from .company import Company
1718
from .configuration import Configuration
1819
from .connection import Connection
@@ -55,6 +56,8 @@
5556
from .tier_account_request import TierAccountRequest
5657
from .tier_config import TierConfig
5758
from .tier_config_request import TierConfigRequest
59+
from .ui import UI
60+
from .unit import Unit
5861
from .usage_file import UsageFile
5962
from .usage_listing import UsageListing
6063
from .usage_record import UsageRecord
@@ -83,6 +86,7 @@
8386
'BillingRequest',
8487
'Company',
8588
'Configuration',
89+
'Commitment',
8690
'Connection',
8791
'Constraints',
8892
'Contact',
@@ -125,6 +129,8 @@
125129
'TierAccounts',
126130
'TierConfig',
127131
'TierConfigRequest',
132+
'UI',
133+
'Unit',
128134
'UsageFile',
129135
'UsageListing',
130136
'UsageRecord',

connect/models/commitment.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
4+
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
5+
6+
from .base import BaseModel
7+
from .schemas import CommitmentSchema
8+
9+
10+
class Commitment(BaseModel):
11+
""" Billing commitment object. """
12+
13+
_schema = CommitmentSchema()
14+
15+
multiplier = None # type: str
16+
""" (str) Commitment multiplier. """
17+
18+
count = None # type: int
19+
""" (int) Number of commitments. """

connect/models/item.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
from typing import List, Optional, Union
77

88
from .base import BaseModel
9+
from .commitment import Commitment
910
from .param import Param
1011
from .renewal import Renewal
1112
from .schemas import ItemSchema
13+
from .ui import UI
14+
from .unit import Unit
1215

1316

1417
class Item(BaseModel):
@@ -55,6 +58,15 @@ class Item(BaseModel):
5558
name = None # type: str
5659
""" (str) Name. """
5760

61+
unit = None # type: Unit
62+
""" (Unit) Measure unit. """
63+
64+
commitment = None # type: Optional[Commitment]
65+
""" (Commitment) item billing commitment. """
66+
67+
ui = None # type: UI
68+
""" (UI) UI visibility. """
69+
5870
def get_param_by_id(self, param_id):
5971
""" Get a parameter of the item.
6072

connect/models/schemas.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010

1111
class BaseSchema(Schema):
12+
13+
def __init__(self, *args, **kwargs):
14+
# kwargs['strict'] = True
15+
super(BaseSchema, self).__init__(*args, **kwargs)
16+
1217
id = fields.Str()
1318

1419
# Set allow_none to True in all fields
@@ -334,18 +339,50 @@ def make_object(self, data):
334339
return Param(**data)
335340

336341

342+
class UISchema(BaseSchema):
343+
visibility = fields.Bool()
344+
@post_load
345+
def make_object(self, data):
346+
from connect.models import UI
347+
return UI(**data)
348+
349+
350+
class UnitSchema(BaseSchema):
351+
title = fields.Str()
352+
unit = fields.Str()
353+
354+
@post_load
355+
def make_object(self, data):
356+
from connect.models import Unit
357+
return Unit(**data)
358+
359+
360+
class CommitmentSchema(BaseSchema):
361+
multiplier = fields.Str()
362+
count = fields.Int()
363+
364+
@post_load
365+
def make_object(self, data):
366+
from connect.models import Commitment
367+
return Commitment(**data)
368+
369+
337370
class ItemSchema(BaseSchema):
338371
mpn = fields.Str()
339372
quantity = QuantityField()
340373
old_quantity = QuantityField()
341374
renewal = fields.Nested(RenewalSchema)
375+
unit = fields.Nested(UnitSchema)
376+
commitment = fields.Nested(CommitmentSchema)
342377
params = fields.Nested(ParamSchema, many=True)
343378
display_name = fields.Str()
344379
global_id = fields.Str()
345380
item_type = fields.Str()
381+
description = fields.Str()
346382
period = fields.Str()
347383
type = fields.Str()
348384
name = fields.Str()
385+
ui = fields.Nested(UISchema)
349386

350387
@post_load
351388
def make_object(self, data):
@@ -828,8 +865,8 @@ def make_object(self, data):
828865

829866

830867
class AttributesSchema(BaseSchema):
831-
provider = fields.Nested(CompanySchema, only=('external_id'))
832-
vendor = fields.Nested(CompanySchema, only=('external_id'))
868+
provider = fields.Nested(CompanySchema, only=('external_id',))
869+
vendor = fields.Nested(CompanySchema, only=('external_id',))
833870

834871
@post_load
835872
def make_object(self, data):

connect/models/ui.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
4+
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
5+
6+
from .base import BaseModel
7+
from .schemas import UISchema
8+
9+
10+
class UI(BaseModel):
11+
""" UI object. """
12+
13+
_schema = UISchema()
14+
15+
visibility = None # type: bool
16+
""" (str) Item UI visibility. """

connect/models/unit.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
4+
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
5+
6+
from .base import BaseModel
7+
from .schemas import UnitSchema
8+
9+
10+
class Unit(BaseModel):
11+
""" Unit object. """
12+
13+
_schema = UnitSchema()
14+
15+
title = None # type: str
16+
""" (str) Name of measure unit. """
17+
18+
unit = None # type: str
19+
""" (str) unit code of measure unit. """

connect/resources/base.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ class BaseResource(object):
140140

141141
def __init__(self, config=None):
142142
# Set client
143-
if not self.__class__.resource:
143+
if not self.resource:
144144
raise AttributeError('Resource name not specified in class {}. '
145145
'Add an attribute `resource` with the name of the resource'
146146
.format(self.__class__.__name__))
147-
self._api = ApiClient(config, self.__class__.resource)
147+
self._api = ApiClient(config, self.resource)
148148

149149
@property
150150
def config(self):
@@ -161,12 +161,15 @@ def get(self, pk):
161161

162162
def filters(self, **kwargs):
163163
# type: (Dict[str, Any]) -> Dict[str, Any]
164-
filters = {}
164+
query = Query()
165165
if self.limit:
166-
filters['limit'] = self.limit
166+
query = query.limit(self.limit)
167167
for key, val in kwargs.items():
168-
filters[key] = val
169-
return filters
168+
if isinstance(val, (list, tuple)):
169+
query = query.in_(key, val)
170+
else:
171+
query = query.equal(key, val)
172+
return query
170173

171174
@function_log
172175
def search(self, filters=None):
@@ -208,3 +211,14 @@ def update(self, id_obj, body):
208211

209212
def list(self, filters=None):
210213
return self.search(filters)
214+
215+
216+
class NestedResource(BaseResource):
217+
"""Base class for all nested resources"""
218+
219+
def __init__(self, config=None, parent_path=''):
220+
self.resource = '{}/{}'.format(
221+
parent_path,
222+
self.__class__.resource,
223+
)
224+
super(NestedResource, self).__init__(config)

connect/resources/fulfillment_automation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ def filters(self, status='pending', **kwargs):
6868
:return: The set of filters for this resource.
6969
:rtype: dict[str,Any]
7070
"""
71-
filters = super(FulfillmentAutomation, self).filters(status=status, **kwargs)
71+
query = super(FulfillmentAutomation, self).filters(status=status, **kwargs)
7272
if self.config.products:
73-
filters['asset.product.id__in'] = ','.join(self.config.products)
74-
return filters
73+
query.in_('asset.product.id', self.config.products)
74+
return query
7575

7676
@function_log
7777
def dispatch(self, request):

connect/resources/product.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import json
2-
from .base import BaseResource
2+
3+
from .base import BaseResource, NestedResource
4+
from ..models import Product, Item
5+
6+
7+
class ProductItemResource(NestedResource):
8+
resource = 'items'
9+
model_class = Item
310

411

512
class ProductsResource(BaseResource):
613
""" Allows listing and obtaining several types of objects.
714
:param Config config: Config object or ``None`` to use environment config (default).
815
"""
916
resource = 'products'
17+
model_class = Product
1018

1119
def list_parameters(self, product_id):
1220
""" List parameters for a product.
@@ -75,3 +83,7 @@ def delete_parameter(self, product_id, parameter_id):
7583
path=path
7684
)
7785
return response
86+
87+
def items(self, product_id):
88+
"""Returns the ProductItemResource resource"""
89+
return ProductItemResource(self.config, 'products/{}'.format(product_id))

0 commit comments

Comments
 (0)