Skip to content

Commit 1ead321

Browse files
Merge pull request #22 from JaviCerveraIngram/type-hints
Type hints
2 parents 5facb7f + e4faeb4 commit 1ead321

24 files changed

+250
-80
lines changed

connect/config.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,29 @@
88
import json
99
import os
1010

11+
from typing import List, Union
12+
1113

1214
class Config(object):
13-
_instance = None # Global instance
15+
# Global instance
16+
_instance = None # type: Config
1417

1518
# noinspection PyShadowingBuiltins
1619
def __init__(
1720
self,
18-
api_url=None,
19-
api_key=None,
20-
products=None,
21-
file=None
21+
api_url=None, # type: str
22+
api_key=None, # type: str
23+
products=None, # type: Union[str, List[str]]
24+
file=None # type: str
2225
):
2326
"""
24-
initialization config for public api
27+
Initialization config for public api
2528
:param api_url: Public api url
2629
:param api_key: Service user ApiKey
2730
:param products (optional): Id products
28-
:param file: Config file path
31+
:param file: Config file name
2932
"""
33+
3034
# Check arguments
3135
if not file and not any([api_key, api_url]):
3236
raise ValueError('Filename or api_key and api_url are expected'
@@ -71,18 +75,22 @@ def __init__(
7175

7276
@classmethod
7377
def get_instance(cls):
78+
# type: () -> Config
7479
if not cls._instance:
7580
cls._instance = Config(file='config.json')
7681
return cls._instance
7782

7883
@property
7984
def api_url(self):
85+
# type: () -> str
8086
return self._api_url
8187

8288
@property
8389
def api_key(self):
90+
# type: () -> str
8491
return self._api_key
8592

8693
@property
8794
def products(self):
95+
# type: () -> List[str]
8896
return self._products

connect/models/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
from .base import BaseSchema
1010
from .fulfillment import FulfillmentSchema
1111
from .parameters import Param, ParamSchema
12+
from .product import Item, ItemSchema
1213
from .server_error import ServerErrorSchema
1314

1415
__all__ = [
1516
'ActivationTemplateResponse',
1617
'ActivationTileResponse',
1718
'BaseSchema',
1819
'FulfillmentSchema',
19-
'ServerErrorSchema',
20+
'Item',
21+
'ItemSchema',
2022
'Param',
2123
'ParamSchema',
24+
'ServerErrorSchema',
2225
]

connect/models/activation_response.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99

1010

1111
class ActivationTileResponse(object):
12-
tile = 'Activation succeeded'
12+
tile = 'Activation succeeded' # type: str
1313

1414
def __init__(self, markdown=None):
15-
if markdown:
16-
try:
17-
self.tile = json.loads(markdown)
18-
except ValueError:
19-
self.tile = markdown
15+
# type: (str) -> None
16+
try:
17+
self.tile = json.loads(markdown)
18+
except ValueError:
19+
self.tile = markdown or self.__class__.tile
2020

2121

2222
class ActivationTemplateResponse(object):
23+
template_id = None # type: str
24+
2325
def __init__(self, template_id):
26+
# type: (str) -> None
2427
self.template_id = template_id

connect/models/asset.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,33 @@
66
"""
77

88
from marshmallow import fields, post_load
9+
from typing import List
910

1011
from .base import BaseModel, BaseSchema
11-
from .connection import ConnectionSchema
12-
from .parameters import ParamSchema
13-
from .product import ItemSchema, ProductSchema
14-
from .tiers import TiersSchemaMixin
12+
from .connection import Connection, ConnectionSchema
13+
from .parameters import Param, ParamSchema
14+
from .product import Item, ItemSchema, Product, ProductSchema
15+
from .tiers import Tiers, TiersSchema
1516

1617

1718
class Asset(BaseModel):
19+
status = None # type: str
20+
external_id = None # type: str
21+
external_uid = None # type: str
22+
product = None # type: Product
23+
connection = None # type: Connection
24+
items = None # type: List[Item]
25+
params = None # type: List[Param]
26+
tiers = None # type: Tiers
27+
1828
def get_param_by_id(self, id_):
1929
try:
20-
# noinspection PyUnresolvedReferences
2130
return list(filter(lambda param: param.id == id_, self.params))[0]
2231
except IndexError:
2332
return None
2433

2534
def get_item_by_mpn(self, mpn):
2635
try:
27-
# noinspection PyUnresolvedReferences
2836
return list(filter(lambda item: item.mpn == mpn, self.items))[0]
2937
except IndexError:
3038
return None
@@ -40,7 +48,7 @@ class AssetSchema(BaseSchema):
4048
)
4149
items = fields.List(fields.Nested(ItemSchema))
4250
params = fields.List(fields.Nested(ParamSchema))
43-
tiers = fields.Nested(TiersSchemaMixin)
51+
tiers = fields.Nested(TiersSchema)
4452

4553
@post_load
4654
def make_object(self, data):

connect/models/base.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010

1111
class BaseModel:
12+
id = None # type: str
13+
1214
def __init__(self, **kwargs):
13-
self.id = kwargs.get('id')
14-
if kwargs:
15-
for attr, val in kwargs.items():
16-
setattr(self, attr, val)
15+
# Inject parsed properties in the model
16+
for attr, val in kwargs.items():
17+
setattr(self, attr, val)
1718

1819

1920
class BaseSchema(Schema):

connect/models/company.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class Company(BaseModel):
14-
pass
14+
name = None # type: str
1515

1616

1717
class CompanySchema(BaseSchema):

connect/models/connection.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
from marshmallow import fields, post_load
99

1010
from .base import BaseModel, BaseSchema
11-
from .company import CompanySchema
12-
from .hub import HubSchema
13-
from .product import ProductSchema
11+
from .company import Company, CompanySchema
12+
from .hub import Hub, HubSchema
13+
from .product import Product, ProductSchema
1414

1515

1616
class Connection(BaseModel):
17-
pass
17+
type = None # type: str
18+
provider = None # type: Company
19+
vendor = None # type: Company
20+
product = None # type: Product
21+
hub = None # type: Hub
1822

1923

2024
class ConnectionSchema(BaseSchema):

connect/models/contact.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212

1313
class PhoneNumber(BaseModel):
14-
pass
14+
country_code = None # type: str
15+
area_code = None # type: str
16+
phone_number = None # type: str
17+
extension = None # type: str
1518

1619

1720
class PhoneNumberSchema(BaseSchema):
@@ -26,7 +29,10 @@ def make_object(self, data):
2629

2730

2831
class Contact(BaseModel):
29-
pass
32+
email = None # type: str
33+
first_name = None # type: str
34+
last_name = None # type: str
35+
phone_number = None # type: PhoneNumber
3036

3137

3238
class ContactSchema(BaseSchema):
@@ -41,7 +47,13 @@ def make_object(self, data):
4147

4248

4349
class ContactInfo(BaseModel):
44-
pass
50+
address_line1 = None # type: str
51+
address_line2 = None # type: str
52+
city = None # type: str
53+
contact = None # type: Contact
54+
country = None # type: str
55+
postal_code = None # type: str
56+
state = None # type: str
4557

4658

4759
class ContactInfoSchema(BaseSchema):

connect/models/exception.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,36 @@
44
This file is part of the Ingram Micro Cloud Blue Connect SDK.
55
Copyright (c) 2019 Ingram Micro. All Rights Reserved.
66
"""
7+
from typing import List
78

9+
from connect.models import Param
810
from .server_error import ServerError
911

1012

1113
class Message(Exception):
14+
code = None # type: str
15+
obj = None # type: object
16+
1217
def __init__(self, message='', code='', obj=None):
18+
# type: (str, str, object) -> None
1319
self.message = message
1420
self.code = code
1521
self.obj = obj
1622

1723

1824
class FulfillmentFail(Message):
1925
def __init__(self, *args, **kwargs):
26+
# type: (*any, **any) -> None
2027
super(FulfillmentFail, self).__init__(*args, **kwargs)
2128
self.message = self.message or 'Request failed'
2229
self.code = 'fail'
2330

2431

2532
class FulfillmentInquire(Message):
33+
params = None # type: List[Param]
34+
2635
def __init__(self, *args, **kwargs):
36+
# type: (*any, **any) -> None
2737
super(FulfillmentInquire, self).__init__(*args, **kwargs)
2838
self.message = self.message or 'Correct user input required'
2939
self.code = 'inquire'
@@ -32,21 +42,25 @@ def __init__(self, *args, **kwargs):
3242

3343
class Skip(Message):
3444
def __init__(self, *args, **kwargs):
45+
# type: (*any, **any) -> None
3546
super(Skip, self).__init__(*args, **kwargs)
3647
self.message = self.message or 'Request skipped'
3748
self.code = 'skip'
3849

3950

4051
class ServerErrorException(Exception):
41-
message = 'Server error'
52+
message = 'Server error' # type: str
4253

4354
def __init__(self, error=None, *args, **kwargs):
55+
# type: (ServerError, *any, **any) -> None
56+
4457
if error and isinstance(error, ServerError):
45-
# noinspection PyUnresolvedReferences
4658
self.message = str({
4759
"error_code": error.error_code,
4860
"params": kwargs.get('params', []),
4961
"errors": error.errors,
5062
})
63+
else:
64+
self.message = self.__class__.message
5165

5266
super(ServerErrorException, self).__init__(self.message, *args)

connect/models/fulfillment.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,23 @@
77

88
from marshmallow import fields, post_load
99

10-
from .asset import AssetSchema
10+
from .asset import Asset, AssetSchema
1111
from .base import BaseModel, BaseSchema
12-
from .marketplace import ContractSchema, MarketplaceSchema
12+
from .marketplace import Contract, ContractSchema, Marketplace, MarketplaceSchema
1313

1414

1515
class Fulfillment(BaseModel):
16+
activation_key = None # type: str
17+
asset = None # type: Asset
18+
status = None # type: str
19+
type = None # type: str
20+
updated = None # type: str
21+
created = None # type: str
22+
reason = None # type: str
23+
params_from_url = None # type: str
24+
contract = None # type: Contract
25+
marketplace = None # type: Marketplace
26+
1627
@property
1728
def new_items(self):
1829
# noinspection PyUnresolvedReferences

0 commit comments

Comments
 (0)