|
| 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() |
0 commit comments