Skip to content

Commit 6a5eefc

Browse files
authored
Merge pull request #102 from cloudblue/vendor-apiary-scenario
vendor-asset_request-apiary-scenario
2 parents 1099015 + e1eab83 commit 6a5eefc

File tree

6 files changed

+178
-5
lines changed

6 files changed

+178
-5
lines changed

connect/resources/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def filters(self, **kwargs):
160160
filters[key] = val
161161
return filters
162162

163+
@function_log()
163164
def search(self, filters=None):
164165
# type: (Dict[str, Any]) -> List[Any]
165166
filters = filters or self.filters()

connect/resources/template.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# This file is part of the Ingram Micro Cloud Blue Connect SDK.
44
# Copyright (c) 2019-2020 Ingram Micro. All Rights Reserved.
55

6-
from typing import List, Any, Dict
7-
6+
import json
87
from connect.models.activation_template_response import ActivationTemplateResponse
98
from connect.models.activation_tile_response import ActivationTileResponse
109
from .base import BaseResource
@@ -15,9 +14,22 @@ class TemplateResource(BaseResource):
1514

1615
resource = 'templates'
1716

18-
def list(self, filters=None):
17+
def list(self, product_id):
18+
""" List an activation template.
19+
:param str product_id: Primary key of the product to search for.
20+
:return: response object with templates contents.
21+
"""
22+
"""
1923
# type: (Dict[str, Any]) -> List[Any]
20-
raise AttributeError('This resource do not have method `list`')
24+
"""
25+
if not product_id:
26+
raise ValueError('Invalid product Id for list template')
27+
response, _ = self._api.get(
28+
'/public/v1/products/' + product_id + '/templates/',
29+
params={'scope': 'asset', 'type': 'fulfillment'},
30+
)
31+
response = json.loads(response)
32+
return response
2133

2234
def render(self, pk, request_id):
2335
""" Get an activation tile.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
""" This is part of the example of the implementation between connect and
6+
a Vendor System API.
7+
The detail of this scenario is documented in the documentation portal
8+
https://connect.cloudblue.com/community/sdk/
9+
This microservice search all of the Purchase Request in status pending in Connect
10+
and check the status in the Vendor System. If this status is ready
11+
accept the Purchase Request in connect.
12+
"""
13+
14+
import warnings
15+
import logging
16+
import requests
17+
18+
from connect.config import Config
19+
from connect.logger import logger
20+
from connect.models import Fulfillment
21+
from connect.resources.automation_engine import AutomationEngine
22+
from connect.resources.template import TemplateResource
23+
24+
# URL of the Vendor API, in this case the apiary.io scenario
25+
VENDOR_API_URL = 'https://SET_YOUR_OWN_SAMPLE.apiary-mock.com/'
26+
27+
# Enable processing of deprecation warnings
28+
warnings.simplefilter('default')
29+
30+
# Set logger level / default level ERROR
31+
logger.setLevel('DEBUG')
32+
33+
# If we remove this line, it is done implicitly
34+
Config(file='config.json')
35+
36+
37+
class AssetAccept(AutomationEngine):
38+
39+
model_class = Fulfillment
40+
resource = 'requests'
41+
42+
logger = logging.getLogger('Fullfilment.logger')
43+
44+
def dispatch(self, request):
45+
return self.process_request(request)
46+
47+
def process_request(self, request):
48+
template_resource = TemplateResource()
49+
for item in request.asset.items:
50+
purchase_request_id = request.id
51+
product_id = request.asset.product.id
52+
mpn = item.mpn
53+
url = VENDOR_API_URL + 'tenant?externalId=' + mpn
54+
55+
response = requests.get(url, data='').json()
56+
if response['status'] == 'ready':
57+
templates = template_resource.list(product_id)
58+
for template in templates:
59+
template_id = template['id']
60+
break
61+
body = {"activation_tile": template_id}
62+
self.approve(purchase_request_id, body)
63+
return response
64+
65+
66+
if __name__ == '__main__':
67+
asset_accept_example = AssetAccept()
68+
asset_accept_example.process()
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
""" This is part of the example of the implementation between connect and
6+
a Vendor System API.
7+
The detail of this scenario is documented in the documentation portal
8+
https://connect.cloudblue.com/community/sdk/
9+
This microservice search all of the Purchase Request in status pending in Connect
10+
and creates the request in the Vendor System using Vendor System API.
11+
"""
12+
13+
14+
import warnings
15+
import logging
16+
import requests
17+
18+
from connect.config import Config
19+
from connect.logger import logger
20+
from connect.models import Fulfillment
21+
from connect.resources.automation_engine import AutomationEngine
22+
23+
# URL of the Vendor API, in this case the apiary.io scenario
24+
VENDOR_API_URL = 'https://SET_YOUR_OWN_SAMPLE.apiary-mock.com/'
25+
26+
# Enable processing of deprecation warnings
27+
warnings.simplefilter('default')
28+
29+
# Set logger level / default level ERROR
30+
logger.setLevel('DEBUG')
31+
32+
# If we remove this line, it is done implicitly
33+
Config(file='config.json')
34+
35+
36+
class AssetRequest(AutomationEngine):
37+
38+
model_class = Fulfillment
39+
resource = 'requests'
40+
41+
logger = logging.getLogger('Fullfilment.logger')
42+
43+
def dispatch(self, request):
44+
return self.process_request(request)
45+
46+
def process_request(self, request):
47+
for item in request.asset.items:
48+
mpn = item.mpn
49+
quantity = item.quantity
50+
break
51+
52+
url = VENDOR_API_URL + "tenant?externalId=" + mpn
53+
response = requests.get(url, data='').json()
54+
first_name = request.asset.tiers.customer.contact_info.contact.first_name
55+
last_name = request.asset.tiers.customer.contact_info.contact.last_name
56+
address = request.asset.tiers.customer.contact_info.address_line1
57+
postal_code = request.asset.tiers.customer.contact_info.postal_code
58+
account_phone = request.asset.tiers.customer.contact_info.contact.phone_number.phone_number
59+
if response['externalId'] != request.asset.id:
60+
url = VENDOR_API_URL + 'tenant'
61+
payload = {
62+
'Attributes': {
63+
'product': {
64+
'item': mpn,
65+
'quantity': quantity
66+
},
67+
'account': {
68+
'accountFirstName': first_name,
69+
'accountLastName': last_nName,
70+
'accountCompany': request.asset.tiers.customer.name,
71+
'accountAddress': address,
72+
'accountCity': request.asset.tiers.customer.contact_info.city,
73+
'accountState': request.asset.tiers.customer.contact_info.state,
74+
'accountPostalCode': postal_code,
75+
'accountCountry': request.asset.tiers.customer.contact_info.country,
76+
'accountEmail': request.asset.tiers.customer.contact_info.contact.email,
77+
'accountPhone': account_phone
78+
}
79+
}
80+
}
81+
response = requests.post(url, data=payload).json()
82+
return response
83+
84+
85+
if __name__ == '__main__':
86+
asset_request_example = AssetRequest()
87+
asset_request_example.process()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"apiEndpoint": "https://api.connect.cloudblue.com/public/v1",
3+
"apiKey": "ApiKey SU-000-000-000:0000000000000000000000000000000000000000",
4+
"products": "PRD-000-000-000"
5+
}

setup.py

Lines changed: 1 addition & 1 deletion
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-2020 Ingram Micro. All Rights Reserved.
66

7-
from os.path import abspath, dirname, exists, join
7+
from os.path import exists
88
from setuptools import find_packages, setup
99

1010
import pathlib

0 commit comments

Comments
 (0)