Skip to content

Commit dee160a

Browse files
Merge branch 'CPS-8-get-tier-config'
2 parents 655ede7 + 4810a9e commit dee160a

24 files changed

+807
-685
lines changed

connect/models/__init__.py

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,111 +4,64 @@
44
# Copyright (c) 2019 Ingram Micro. All Rights Reserved.
55

66
from .activation_response import ActivationTemplateResponse, ActivationTileResponse
7-
from .asset import Asset, AssetSchema
8-
from .base import BaseModel, BaseSchema
9-
from .company import Company, CompanySchema
10-
from .connection import Connection, ConnectionSchema
11-
from .contact import Contact, ContactInfo, ContactInfoSchema, ContactSchema, \
12-
PhoneNumber, PhoneNumberSchema
13-
from .event import EventInfo, EventInfoSchema, Events, EventsSchema
14-
from .fulfillment import Fulfillment, FulfillmentSchema
15-
from .hub import Hub, HubInstance, HubInstanceSchema, ExtIdHub, HubSchema, ExtIdHubSchema, \
16-
HubStats, HubStatsSchema
17-
from .marketplace import Activation, ActivationSchema, Agreement, AgreementSchema, AgreementStats,\
18-
AgreementStatsSchema, Contract, ContractSchema, Marketplace, MarketplaceSchema
19-
from .parameters import Constraints, ConstraintsSchema, Param, ParamSchema, ValueChoice, \
20-
ValueChoiceSchema
21-
from .product import CustomerUiSettings, CustomerUiSettingsSchema, Document, DocumentSchema, \
22-
DownloadLink, DownloadLinkSchema, Item, ItemSchema, Product, ProductConfiguration, \
23-
ProductConfigurationSchema, ProductSchema, Renewal, RenewalSchema
24-
from .server_error_response import ServerErrorResponse, ServerErrorResponseSchema
25-
from .tier_config import Account, AccountSchema, Template, TemplateSchema, TierConfig, \
26-
TierConfigRequest, TierConfigRequestSchema, TierConfigSchema
27-
from .tiers import Tier, Tiers, TierSchema, TiersSchema
28-
from .usage import UsageFile, UsageFileSchema, UsageListing, UsageListingSchema, UsageRecord, \
29-
UsageRecords, UsageRecordSchema, UsageRecordsSchema
7+
from .asset import Asset
8+
from .base import BaseModel
9+
from .company import Company
10+
from .connection import Connection
11+
from .contact import Contact, ContactInfo, PhoneNumber
12+
from .event import EventInfo, Events
13+
from .fulfillment import Fulfillment
14+
from .hub import Hub, HubInstance, ExtIdHub, HubStats
15+
from .marketplace import Activation, Agreement, AgreementStats, Contract, Marketplace
16+
from .parameters import Constraints, Param, ValueChoice
17+
from .product import CustomerUiSettings, Document, DownloadLink, Item, Product, \
18+
ProductConfiguration, Renewal
19+
from .server_error_response import ServerErrorResponse
20+
from .tier_config import Account, Template, TierConfig, TierConfigRequest
21+
from .tiers import Tier, Tiers
22+
from .usage import UsageFile, UsageListing, UsageRecord, UsageRecords
3023

3124
__all__ = [
3225
'Account',
33-
'AccountSchema',
3426
'Activation',
35-
'ActivationSchema',
3627
'ActivationTemplateResponse',
3728
'ActivationTileResponse',
3829
'Agreement',
39-
'AgreementSchema',
4030
'AgreementStats',
41-
'AgreementStatsSchema',
4231
'Asset',
43-
'AssetSchema',
4432
'BaseModel',
45-
'BaseSchema',
4633
'Company',
47-
'CompanySchema',
4834
'Connection',
49-
'ConnectionSchema',
5035
'Constraints',
51-
'ConstraintsSchema',
5236
'Contact',
5337
'ContactInfo',
54-
'ContactInfoSchema',
55-
'ContactSchema',
5638
'Contract',
57-
'ContractSchema',
5839
'CustomerUiSettings',
59-
'CustomerUiSettingsSchema',
6040
'Document',
61-
'DocumentSchema',
6241
'DownloadLink',
63-
'DownloadLinkSchema',
6442
'EventInfo',
65-
'EventInfoSchema',
6643
'Events',
67-
'EventsSchema',
6844
'ExtIdHub',
69-
'ExtIdHubSchema',
7045
'Fulfillment',
71-
'FulfillmentSchema',
7246
'Hub',
7347
'HubInstance',
74-
'HubInstanceSchema',
75-
'HubSchema',
7648
'HubStats',
77-
'HubStatsSchema',
7849
'Item',
79-
'ItemSchema',
8050
'Marketplace',
81-
'MarketplaceSchema',
8251
'Param',
83-
'ParamSchema',
8452
'PhoneNumber',
85-
'PhoneNumberSchema',
8653
'Product',
8754
'ProductConfiguration',
88-
'ProductConfigurationSchema',
89-
'ProductSchema',
9055
'Renewal',
91-
'RenewalSchema',
9256
'ServerErrorResponse',
93-
'ServerErrorResponseSchema',
9457
'Template',
95-
'TemplateSchema',
9658
'Tier',
9759
'TierConfig',
9860
'TierConfigRequest',
99-
'TierConfigRequestSchema',
100-
'TierConfigSchema',
10161
'Tiers',
102-
'TierSchema',
103-
'TiersSchema',
10462
'UsageFile',
105-
'UsageFileSchema',
10663
'UsageListing',
107-
'UsageListingSchema',
10864
'UsageRecord',
10965
'UsageRecords',
110-
'UsageRecordSchema',
111-
'UsageRecordsSchema',
11266
'ValueChoice',
113-
'ValueChoiceSchema',
11467
]

connect/models/asset.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +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 marshmallow import fields, post_load
76
from typing import List, Optional
87

9-
from .base import BaseModel, BaseSchema
10-
from .connection import Connection, ConnectionSchema
11-
from .parameters import Param, ParamSchema
12-
from .product import Item, ItemSchema, Product, ProductSchema
13-
from .tiers import Tiers, TiersSchema
8+
from .base import BaseModel
9+
from .connection import Connection
10+
from .parameters import Param
11+
from .product import Item, Product
12+
from .tiers import Tiers
13+
from connect.models.schemas import AssetSchema
1414

1515

1616
class Asset(BaseModel):
@@ -39,6 +39,8 @@ class Asset(BaseModel):
3939
one asset from another.
4040
"""
4141

42+
_schema = AssetSchema()
43+
4244
status = None # type: str
4345
""" Assets may have one of the following statuses:
4446
@@ -95,20 +97,3 @@ def get_item_by_mpn(self, mpn):
9597
return list(filter(lambda item: item.mpn == mpn, self.items))[0]
9698
except IndexError:
9799
return None
98-
99-
100-
class AssetSchema(BaseSchema):
101-
status = fields.Str()
102-
external_id = fields.Str()
103-
external_uid = fields.Str(allow_none=True)
104-
product = fields.Nested(ProductSchema, only=('id', 'name'))
105-
connection = fields.Nested(
106-
ConnectionSchema, only=('id', 'type', 'provider', 'vendor'),
107-
)
108-
items = fields.Nested(ItemSchema, many=True)
109-
params = fields.Nested(ParamSchema, many=True)
110-
tiers = fields.Nested(TiersSchema)
111-
112-
@post_load
113-
def make_object(self, data):
114-
return Asset(**data)

connect/models/base.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import json
77

8-
from marshmallow import Schema, fields, post_load
8+
from .schemas import BaseSchema
99

1010

1111
class BaseModel(object):
@@ -14,6 +14,8 @@ class BaseModel(object):
1414
All the arguments provided on creation of the model are injected as attributes on the object.
1515
"""
1616

17+
_schema = BaseSchema() # type: BaseSchema
18+
1719
id = None # type: str
1820
""" (str) Globally unique id. """
1921

@@ -31,10 +33,32 @@ def json(self):
3133
dump = json.dumps(self, default=lambda o: getattr(o, '__dict__', str(o)))
3234
return json.loads(dump)
3335

36+
@classmethod
37+
def deserialize(cls, json_str):
38+
""" Deserialize a string containing JSON data into a model.
39+
40+
:param str json_str: String containing the JSON data to be deserialized.
41+
:return: An instance of the same class as the receiver of the call, or a list of instances.
42+
:rtype: Any|list[Any]
43+
"""
44+
return cls.deserialize_json(json.loads(json_str))
3445

35-
class BaseSchema(Schema):
36-
id = fields.Str()
46+
@classmethod
47+
def deserialize_json(cls, json_data):
48+
""" Deserialize JSON data into a model.
3749
38-
@post_load
39-
def make_object(self, data):
40-
return BaseModel(**data)
50+
:param dict|list json_data: JSON list or dictionary to be deserialized.
51+
:return: An instance of the same class as the receiver of the call, or a list of instances.
52+
:rtype: Any|list[Any]
53+
"""
54+
objects, error = cls._schema.load(json_data, many=isinstance(json_data, list))
55+
if error:
56+
raise TypeError(
57+
'Invalid structure for initialization of `{type}`. \n'
58+
'Error: {error}. \nJSON data: {data}'
59+
.format(
60+
type=cls.__name__,
61+
error=error,
62+
data=json_data),
63+
)
64+
return objects

connect/models/company.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +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 marshmallow import fields, post_load
7-
8-
from .base import BaseModel, BaseSchema
6+
from .base import BaseModel
7+
from connect.models.schemas import CompanySchema
98

109

1110
class Company(BaseModel):
1211
""" Represents a company within the platform. """
1312

13+
_schema = CompanySchema()
14+
1415
name = None # type: str
1516
""" (str) Company name. """
16-
17-
18-
class CompanySchema(BaseSchema):
19-
name = fields.Str()
20-
21-
@post_load
22-
def make_object(self, data):
23-
return Company(**data)

connect/models/connection.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
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 marshmallow import fields, post_load
7-
8-
from .base import BaseModel, BaseSchema
9-
from .company import Company, CompanySchema
10-
from .hub import Hub, HubSchema
11-
from .product import Product, ProductSchema
6+
from .base import BaseModel
7+
from .company import Company
8+
from .hub import Hub
9+
from .product import Product
10+
from connect.models.schemas import ConnectionSchema
1211

1312

1413
class Connection(BaseModel):
@@ -18,6 +17,8 @@ class Connection(BaseModel):
1817
Standalone connection is required for each product and for each provider account.
1918
"""
2019

20+
_schema = ConnectionSchema()
21+
2122
type = None # type: str
2223
""" (str) Type of connection. """
2324

@@ -32,15 +33,3 @@ class Connection(BaseModel):
3233

3334
hub = None # type: Hub
3435
""" (:py:class:`.Hub`) Hub Reference. """
35-
36-
37-
class ConnectionSchema(BaseSchema):
38-
type = fields.Str()
39-
provider = fields.Nested(CompanySchema, only=('id', 'name'))
40-
vendor = fields.Nested(CompanySchema, only=('id', 'name'))
41-
product = fields.Nested(ProductSchema)
42-
hub = fields.Nested(HubSchema)
43-
44-
@post_load
45-
def make_object(self, data):
46-
return Connection(**data)

connect/models/contact.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
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 marshmallow import fields, post_load
76
from typing import Optional
87

9-
from .base import BaseModel, BaseSchema
8+
from .base import BaseModel
9+
from connect.models.schemas import PhoneNumberSchema, ContactSchema, ContactInfoSchema
1010

1111

1212
class PhoneNumber(BaseModel):
13+
""" Phone number. """
14+
15+
_schema = PhoneNumberSchema()
16+
1317
country_code = None # type: Optional[str]
1418
""" (str|None) Country code. """
1519

@@ -23,20 +27,11 @@ class PhoneNumber(BaseModel):
2327
""" (str|None) Phone extension. """
2428

2529

26-
class PhoneNumberSchema(BaseSchema):
27-
country_code = fields.Str(allow_none=True)
28-
area_code = fields.Str(allow_none=True)
29-
phone_number = fields.Str(allow_none=True)
30-
extension = fields.Str(allow_none=True)
31-
32-
@post_load
33-
def make_object(self, data):
34-
return PhoneNumber(**data)
35-
36-
3730
class Contact(BaseModel):
3831
""" Person of contact. """
3932

33+
_schema = ContactSchema()
34+
4035
first_name = None # type: Optional[str]
4136
""" (str|None) First name. """
4237

@@ -50,20 +45,11 @@ class Contact(BaseModel):
5045
""" (:py:class:`.PhoneNumber`) Phone number."""
5146

5247

53-
class ContactSchema(BaseSchema):
54-
email = fields.Str()
55-
first_name = fields.Str(allow_none=True)
56-
last_name = fields.Str(allow_none=True)
57-
phone_number = fields.Nested(PhoneNumberSchema)
58-
59-
@post_load
60-
def make_object(self, data):
61-
return Contact(**data)
62-
63-
6448
class ContactInfo(BaseModel):
6549
""" Represents the information of a contact. """
6650

51+
_schema = ContactInfoSchema()
52+
6753
address_line1 = None # type: str
6854
""" (str) Street address, first line. """
6955

@@ -84,17 +70,3 @@ class ContactInfo(BaseModel):
8470

8571
contact = None # type: Contact
8672
""" (:py:class:`.Contact`) Person of contact. """
87-
88-
89-
class ContactInfoSchema(BaseSchema):
90-
address_line1 = fields.Str()
91-
address_line2 = fields.Str(allow_none=True)
92-
city = fields.Str()
93-
contact = fields.Nested(ContactSchema)
94-
country = fields.Str()
95-
postal_code = fields.Str()
96-
state = fields.Str()
97-
98-
@post_load
99-
def make_object(self, data):
100-
return ContactInfo(**data)

0 commit comments

Comments
 (0)