Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4cf140d
Several PyCharm's code inspection warnings removed
JaviCerveraIngram Mar 4, 2019
78e8515
More code inspection warnings removed
JaviCerveraIngram Mar 4, 2019
aebe426
Added type hints for models
JaviCerveraIngram Mar 4, 2019
06c7f9d
Corrected syntax for parse_requirements
JaviCerveraIngram Mar 5, 2019
e59d6ad
Skip exception can now receive arguments
JaviCerveraIngram Mar 5, 2019
6984e25
Fixed Asset.tiers type
JaviCerveraIngram Mar 5, 2019
96ba27f
Fixed typo in Tier model
JaviCerveraIngram Mar 5, 2019
671c98c
Merge branch 'contactinfo' into type-hints
JaviCerveraIngram Mar 5, 2019
13b070e
Added type hints for Contact, ContactInfo and Phonenumber
JaviCerveraIngram Mar 5, 2019
82001da
Merge branch 'contactinfo' into type-hints
JaviCerveraIngram Mar 6, 2019
f1650ee
Added Item and ItemSchema to exported classes by models package
JaviCerveraIngram Mar 6, 2019
5f424fc
Merge branch 'model_methods' into type-hints
JaviCerveraIngram Mar 6, 2019
5f24c55
Merge branch 'model_methods' into type-hints
JaviCerveraIngram Mar 7, 2019
647a820
Merge branch 'model_methods' into type-hints
JaviCerveraIngram Mar 7, 2019
45de30a
Added typing to Config.get_instance()
JaviCerveraIngram Mar 7, 2019
784ac29
Merge branch 'master' into type-hints
JaviCerveraIngram Mar 7, 2019
768348c
Merge branch 'master' into type-hints
JaviCerveraIngram Mar 11, 2019
f9aed9e
Merge branch 'suppress-warnings' into type-hints
JaviCerveraIngram Mar 11, 2019
dfdc932
Update README.md
JaviCerveraIngram Mar 11, 2019
f437f1a
Merge branch 'suppress-warnings' into type-hints
JaviCerveraIngram Mar 13, 2019
5737683
Update asset.py
JaviCerveraIngram Mar 13, 2019
7abbf74
Merge branch 'suppress-warnings' into type-hints
JaviCerveraIngram Mar 13, 2019
fdaf953
Merge branch 'suppress-warnings' into type-hints
JaviCerveraIngram Mar 13, 2019
c86bcd8
Renamed TiersMixin to Tiers and HubsMixin to Hubs
JaviCerveraIngram Mar 13, 2019
79e745c
More type hints
JaviCerveraIngram Mar 13, 2019
d7b1aac
Changed type of Config.products to List[str]
JaviCerveraIngram Mar 14, 2019
f57a670
Merge branch 'listing-api-fix' into type-hints
JaviCerveraIngram Mar 18, 2019
fe06e75
Merge branch 'suppress-warnings' into type-hints
JaviCerveraIngram Mar 19, 2019
a8dd253
Merge branch 'listing-api-fix' into type-hints
JaviCerveraIngram Mar 19, 2019
1a88b92
Fixes for flake8 validation
JaviCerveraIngram Mar 19, 2019
e4faeb4
Updated with Vladimir's proposals (and fixed an important bug I misse…
JaviCerveraIngram Mar 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions connect/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@
import json
import os

from typing import List, Union


class Config(object):
_instance = None # Global instance
# Global instance
_instance = None # type: Config

# noinspection PyShadowingBuiltins
def __init__(
self,
api_url=None,
api_key=None,
products=None,
file=None
api_url=None, # type: str
api_key=None, # type: str
products=None, # type: Union[str, List[str]]
file=None # type: str
):
"""
initialization config for public api
Initialization config for public api
:param api_url: Public api url
:param api_key: Service user ApiKey
:param products (optional): Id products
:param file: Config file path
:param file: Config file name
"""

# Check arguments
if not file and not any([api_key, api_url]):
raise ValueError('Filename or api_key and api_url are expected'
Expand Down Expand Up @@ -71,18 +75,22 @@ def __init__(

@classmethod
def get_instance(cls):
# type: () -> Config
if not cls._instance:
cls._instance = Config(file='config.json')
return cls._instance

@property
def api_url(self):
# type: () -> str
return self._api_url

@property
def api_key(self):
# type: () -> str
return self._api_key

@property
def products(self):
# type: () -> List[str]
return self._products
5 changes: 4 additions & 1 deletion connect/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
from .base import BaseSchema
from .fulfillment import FulfillmentSchema
from .parameters import Param, ParamSchema
from .product import Item, ItemSchema
from .server_error import ServerErrorSchema

__all__ = [
'ActivationTemplateResponse',
'ActivationTileResponse',
'BaseSchema',
'FulfillmentSchema',
'ServerErrorSchema',
'Item',
'ItemSchema',
'Param',
'ParamSchema',
'ServerErrorSchema',
]
15 changes: 9 additions & 6 deletions connect/models/activation_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@


class ActivationTileResponse(object):
tile = 'Activation succeeded'
tile = 'Activation succeeded' # type: str

def __init__(self, markdown=None):
if markdown:
try:
self.tile = json.loads(markdown)
except ValueError:
self.tile = markdown
# type: (str) -> None
try:
self.tile = json.loads(markdown)
except ValueError:
self.tile = markdown or self.__class__.tile


class ActivationTemplateResponse(object):
template_id = None # type: str

def __init__(self, template_id):
# type: (str) -> None
self.template_id = template_id
22 changes: 15 additions & 7 deletions connect/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@
"""

from marshmallow import fields, post_load
from typing import List

from .base import BaseModel, BaseSchema
from .connection import ConnectionSchema
from .parameters import ParamSchema
from .product import ItemSchema, ProductSchema
from .tiers import TiersSchemaMixin
from .connection import Connection, ConnectionSchema
from .parameters import Param, ParamSchema
from .product import Item, ItemSchema, Product, ProductSchema
from .tiers import Tiers, TiersSchema


class Asset(BaseModel):
status = None # type: str
external_id = None # type: str
external_uid = None # type: str
product = None # type: Product
connection = None # type: Connection
items = None # type: List[Item]
params = None # type: List[Param]
tiers = None # type: Tiers

def get_param_by_id(self, id_):
try:
# noinspection PyUnresolvedReferences
return list(filter(lambda param: param.id == id_, self.params))[0]
except IndexError:
return None

def get_item_by_mpn(self, mpn):
try:
# noinspection PyUnresolvedReferences
return list(filter(lambda item: item.mpn == mpn, self.items))[0]
except IndexError:
return None
Expand All @@ -40,7 +48,7 @@ class AssetSchema(BaseSchema):
)
items = fields.List(fields.Nested(ItemSchema))
params = fields.List(fields.Nested(ParamSchema))
tiers = fields.Nested(TiersSchemaMixin)
tiers = fields.Nested(TiersSchema)

@post_load
def make_object(self, data):
Expand Down
9 changes: 5 additions & 4 deletions connect/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@


class BaseModel:
id = None # type: str

def __init__(self, **kwargs):
self.id = kwargs.get('id')
if kwargs:
for attr, val in kwargs.items():
setattr(self, attr, val)
# Inject parsed properties in the model
for attr, val in kwargs.items():
setattr(self, attr, val)


class BaseSchema(Schema):
Expand Down
2 changes: 1 addition & 1 deletion connect/models/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class Company(BaseModel):
pass
name = None # type: str


class CompanySchema(BaseSchema):
Expand Down
12 changes: 8 additions & 4 deletions connect/models/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
from marshmallow import fields, post_load

from .base import BaseModel, BaseSchema
from .company import CompanySchema
from .hub import HubSchema
from .product import ProductSchema
from .company import Company, CompanySchema
from .hub import Hub, HubSchema
from .product import Product, ProductSchema


class Connection(BaseModel):
pass
type = None # type: str
provider = None # type: Company
vendor = None # type: Company
product = None # type: Product
hub = None # type: Hub


class ConnectionSchema(BaseSchema):
Expand Down
18 changes: 15 additions & 3 deletions connect/models/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@


class PhoneNumber(BaseModel):
pass
country_code = None # type: str
area_code = None # type: str
phone_number = None # type: str
extension = None # type: str


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


class Contact(BaseModel):
pass
email = None # type: str
first_name = None # type: str
last_name = None # type: str
phone_number = None # type: PhoneNumber


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


class ContactInfo(BaseModel):
pass
address_line1 = None # type: str
address_line2 = None # type: str
city = None # type: str
contact = None # type: Contact
country = None # type: str
postal_code = None # type: str
state = None # type: str


class ContactInfoSchema(BaseSchema):
Expand Down
18 changes: 16 additions & 2 deletions connect/models/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@
This file is part of the Ingram Micro Cloud Blue Connect SDK.
Copyright (c) 2019 Ingram Micro. All Rights Reserved.
"""
from typing import List

from connect.models import Param
from .server_error import ServerError


class Message(Exception):
code = None # type: str
obj = None # type: object

def __init__(self, message='', code='', obj=None):
# type: (str, str, object) -> None
self.message = message
self.code = code
self.obj = obj


class FulfillmentFail(Message):
def __init__(self, *args, **kwargs):
# type: (*any, **any) -> None
super(FulfillmentFail, self).__init__(*args, **kwargs)
self.message = self.message or 'Request failed'
self.code = 'fail'


class FulfillmentInquire(Message):
params = None # type: List[Param]

def __init__(self, *args, **kwargs):
# type: (*any, **any) -> None
super(FulfillmentInquire, self).__init__(*args, **kwargs)
self.message = self.message or 'Correct user input required'
self.code = 'inquire'
Expand All @@ -32,21 +42,25 @@ def __init__(self, *args, **kwargs):

class Skip(Message):
def __init__(self, *args, **kwargs):
# type: (*any, **any) -> None
super(Skip, self).__init__(*args, **kwargs)
self.message = self.message or 'Request skipped'
self.code = 'skip'


class ServerErrorException(Exception):
message = 'Server error'
message = 'Server error' # type: str

def __init__(self, error=None, *args, **kwargs):
# type: (ServerError, *any, **any) -> None

if error and isinstance(error, ServerError):
# noinspection PyUnresolvedReferences
self.message = str({
"error_code": error.error_code,
"params": kwargs.get('params', []),
"errors": error.errors,
})
else:
self.message = self.__class__.message

super(ServerErrorException, self).__init__(self.message, *args)
15 changes: 13 additions & 2 deletions connect/models/fulfillment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@

from marshmallow import fields, post_load

from .asset import AssetSchema
from .asset import Asset, AssetSchema
from .base import BaseModel, BaseSchema
from .marketplace import ContractSchema, MarketplaceSchema
from .marketplace import Contract, ContractSchema, Marketplace, MarketplaceSchema


class Fulfillment(BaseModel):
activation_key = None # type: str
asset = None # type: Asset
status = None # type: str
type = None # type: str
updated = None # type: str
created = None # type: str
reason = None # type: str
params_from_url = None # type: str
contract = None # type: Contract
marketplace = None # type: Marketplace

@property
def new_items(self):
# noinspection PyUnresolvedReferences
Expand Down
13 changes: 11 additions & 2 deletions connect/models/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class Hub(BaseModel):
pass
name = None # type: str


class HubSchema(BaseSchema):
Expand All @@ -22,6 +22,15 @@ def make_object(self, data):
return Hub(**data)


class HubsSchemaMixin(Schema):
class Hubs(BaseModel):
hub = None # type: Hub
external_id = None # type: str


class HubsSchema(Schema):
hub = fields.Nested(HubSchema, only=('id', 'name'))
external_id = fields.Str()

@post_load
def make_object(self, data):
return Hubs(**data)
Loading