Skip to content

Commit e4faeb4

Browse files
Updated with Vladimir's proposals (and fixed an important bug I missed out on FulfillmentAutomation.process!)
1 parent 1a88b92 commit e4faeb4

File tree

8 files changed

+49
-46
lines changed

8 files changed

+49
-46
lines changed

connect/config.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@ class Config(object):
1818
# noinspection PyShadowingBuiltins
1919
def __init__(
2020
self,
21-
api_url=None,
22-
api_key=None,
23-
products=None,
24-
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
2525
):
26-
# type: (str, str, Union[str, List[str]], str) -> None
27-
2826
"""
29-
initialization config for public api
27+
Initialization config for public api
3028
:param api_url: Public api url
3129
:param api_key: Service user ApiKey
3230
:param products (optional): Id products
33-
:param file: Config file path
31+
:param file: Config file name
3432
"""
33+
3534
# Check arguments
3635
if not file and not any([api_key, api_url]):
3736
raise ValueError('Filename or api_key and api_url are expected'

connect/models/exception.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
from typing import List
88

9+
from connect.models import Param
910
from .server_error import ServerError
1011

1112

@@ -29,7 +30,7 @@ def __init__(self, *args, **kwargs):
2930

3031

3132
class FulfillmentInquire(Message):
32-
params = None # type: List[str]
33+
params = None # type: List[Param]
3334

3435
def __init__(self, *args, **kwargs):
3536
# type: (*any, **any) -> None

connect/resource/base.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"""
77

88
import requests
9-
from typing import Any
9+
from typing import Any, List, Dict
1010

1111
from connect.config import Config
1212
from connect.logger import function_log, logger
1313
from connect.models import BaseSchema, ServerErrorSchema
14+
from connect.models.base import BaseModel
1415
from connect.models.exception import ServerErrorException
1516
from .utils import join_url
1617

@@ -33,7 +34,7 @@ def config(self):
3334

3435
@property
3536
def headers(self):
36-
# type: () -> dict
37+
# type: () -> Dict[str, str]
3738
return {
3839
"Authorization": self.config.api_key,
3940
"Content-Type": "application/json",
@@ -99,8 +100,23 @@ def config(self):
99100
# type: () -> Config
100101
return self._config
101102

103+
@property
104+
def list(self):
105+
# type: () -> List[Any]
106+
filters = self.build_filter()
107+
logger.info('Get list request by filter - {}'.format(filters))
108+
response = self.api.get(url=self._list_url, params=filters)
109+
return self.__loads_schema(response)
110+
111+
def get(self, pk):
112+
# type: (str) -> Any
113+
response = self.api.get(url=self._obj_url(pk))
114+
objects = self.__loads_schema(response)
115+
if isinstance(objects, list) and len(objects) > 0:
116+
return objects[0]
117+
102118
def build_filter(self):
103-
# type: () -> dict
119+
# type: () -> Dict[str, Any]
104120
res_filter = {}
105121
if self.limit:
106122
res_filter['limit'] = self.limit
@@ -117,7 +133,7 @@ def _obj_url(self, pk):
117133
return join_url(self._list_url, pk)
118134

119135
def __loads_schema(self, response):
120-
# type: (str) -> Any
136+
# type: (str) -> List[BaseModel]
121137
objects, error = self.schema.loads(response, many=True)
122138
if error:
123139
raise TypeError(
@@ -126,17 +142,3 @@ def __loads_schema(self, response):
126142
)
127143

128144
return objects
129-
130-
def get(self, pk):
131-
# type: (str) -> Any
132-
response = self.api.get(url=self._obj_url(pk))
133-
objects = self.__loads_schema(response)
134-
if isinstance(objects, list) and len(objects) > 0:
135-
return objects[0]
136-
137-
def list(self):
138-
# type: () -> Any
139-
filters = self.build_filter()
140-
logger.info('Get list request by filter - {}'.format(filters))
141-
response = self.api.get(url=self._list_url, params=filters)
142-
return self.__loads_schema(response)

connect/resource/fulfillment.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import json
99

10+
from typing import List
11+
1012
from connect.logger import function_log
1113
from connect.models import FulfillmentSchema, Param, ActivationTileResponse
1214
from .base import BaseResource
@@ -52,7 +54,7 @@ def render_template(self, pk, template_id):
5254

5355
@function_log
5456
def update_parameters(self, pk, params):
55-
# type: (str, list) -> str
57+
# type: (str, List[Param]) -> str
5658
list_dict = []
5759
for _ in params:
5860
list_dict.append(_.__dict__ if isinstance(_, Param) else _)

connect/resource/fulfillment_automation.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
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 Any
87

98
from connect.logger import logger
109
from connect.models import ActivationTemplateResponse, ActivationTileResponse
@@ -16,19 +15,19 @@
1615
class FulfillmentAutomation(FulfillmentResource):
1716

1817
def process(self):
19-
# type: () -> Any
20-
for _ in self.list():
21-
return self.dispatch(_)
18+
# type: () -> None
19+
for _ in self.list:
20+
self.dispatch(_)
2221

2322
def dispatch(self, request):
24-
# type: (Fulfillment) -> Any
23+
# type: (Fulfillment) -> str
2524
try:
2625
logger.info('Start request process / ID request - {}'.format(request.id))
2726
result = self.process_request(request)
2827

2928
if not result:
3029
logger.info('Method `process_request` did not return result')
31-
return
30+
return ''
3231

3332
params = {}
3433
if isinstance(result, ActivationTileResponse):
@@ -48,8 +47,6 @@ def dispatch(self, request):
4847
except Skip as skip:
4948
return skip.code
5049

51-
return
52-
5350
def process_request(self, request):
54-
# type: (Fulfillment) -> Any
51+
# type: (Fulfillment) -> str
5552
raise NotImplementedError('Please implement `process_request` logic')

connect/resource/template.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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 Any
7+
from typing import List, Any
88

99
from connect.models import ActivationTemplateResponse, ActivationTileResponse
1010
from .base import BaseResource
@@ -18,6 +18,11 @@ class TemplateResource(BaseResource):
1818
"""
1919
resource = 'templates'
2020

21+
@property
22+
def list(self):
23+
# type: () -> List[Any]
24+
raise AttributeError('This resource do not have method `list`')
25+
2126
def render(self, pk, request_id):
2227
# type: (str, str) -> ActivationTileResponse
2328
if not all([pk, request_id]):
@@ -31,7 +36,3 @@ def render(self, pk, request_id):
3136
def get(self, pk):
3237
# type: (str) -> ActivationTemplateResponse
3338
return ActivationTemplateResponse(template_id=pk)
34-
35-
def list(self):
36-
# type: () -> Any
37-
raise AttributeError('This resource do not have method `list`')

example/example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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 Union
78

89
from connect import FulfillmentAutomation
910
from connect.config import Config
@@ -21,7 +22,7 @@
2122

2223
class ExampleRequestProcessor(FulfillmentAutomation):
2324
def process_request(self, request):
24-
# type: (Fulfillment) -> object
25+
# type: (Fulfillment) -> Union[ActivationTemplateResponse, ActivationTileResponse]
2526

2627
logger.info('Processing request {} for contract {}, product {}, marketplace {}'
2728
.format(request.id,

tests/test_models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_create_model_from_response():
4040

4141
# Get requests from response
4242
resource = FulfillmentResource()
43-
requests = resource.list()
43+
requests = resource.list
4444
request_obj = resource.get(pk='PR-000-000-000')
4545

4646
# Assert that all properties exist
@@ -86,7 +86,7 @@ def test_create_model_from_response():
8686
@patch('requests.get', MagicMock(return_value=_get_response2_ok()))
8787
def test_fulfillment_items():
8888
# Get request
89-
requests = FulfillmentResource().list()
89+
requests = FulfillmentResource().list
9090
assert len(requests) == 1
9191
request = requests[0]
9292
assert isinstance(request, Fulfillment)
@@ -116,7 +116,7 @@ def test_fulfillment_items():
116116
@patch('requests.get', MagicMock(return_value=_get_response2_ok()))
117117
def test_asset_methods():
118118
# Get asset
119-
requests = FulfillmentResource().list()
119+
requests = FulfillmentResource().list
120120
assert len(requests) == 1
121121
assert isinstance(requests[0], Fulfillment)
122122
asset = requests[0].asset

0 commit comments

Comments
 (0)