I'm new to Python, I am primarily a JavaScript developer so still trying to get to grips with everything. Really enjoying it so far though.
The overall task is to take all orders received in Shopify from the last day using the Shopify API, and then post them to the Elogii Logistics Platform API so that we can manage the delivery routes more efficiently.
I have connected to both API's fine and have the data for both stored as Python Objects. I now need to know how to iterate across both Objects and find out if the order from Shopify has already been created in Elogii, and if not, post it to Elogii.
I tried using diff(ELOGII_RESPONSE_OBJECT, SHOPIFY_ORDERS)
but that's about as far as I've got.
Any suggestions?
import shopify
import requests
import json
from jsondiff import diff
from datetime import datetime, timedelta, date
from collections import namedtuple
# Shopify Params
SHOPIFY_API_KEY = '######'
SHOPIFY_PASSWORD = '######'
SHOP_NAME = '#####'
SHOP_URL = "https://%s:%s@%s.myshopify.com/admin" % (SHOPIFY_API_KEY, SHOPIFY_PASSWORD, SHOP_NAME)
shopify.ShopifyResource.set_site(SHOP_URL)
SHOP = shopify.Shop.current()
TIME_FORMAT = "%Y-%m-%dT%H:%M:%S+04:30"
TODAY = date.today()
FORMATTED_TODAY = TODAY.strftime(TIME_FORMAT)
# Elogii Params
ELOGII_AUTH='####'
ELOGII_HEADER = {'Content-Type': 'application/json', 'Authorization': ELOGII_AUTH}
ELOGII_URL = 'https://api.elogii.com/deliveries'
ELOGII_RESPONSE = requests.get(ELOGII_URL, headers=ELOGII_HEADER)
ELOGII_RESPONSE_JSON = ELOGII_RESPONSE.json()
ELOGII_RESPONSE_JSON_STR = json.dumps(ELOGII_RESPONSE_JSON)
# print(ELOGII_RESPONSE_JSON)
ELOGII_RESPONSE_OBJECT = json.loads(ELOGII_RESPONSE_JSON_STR, object_hook=lambda d: namedtuple('X', d.keys(), rename=True)(*d.values()))
print(ELOGII_RESPONSE_OBJECT)
def get_all_resources_date(resource, **kwargs):
SHOPIFY_RESOURCE_COUNT = resource.count(**kwargs)
SHOPIFY_RESOURCES = []
if SHOPIFY_RESOURCE_COUNT > 0:
for page in range(1, ((SHOPIFY_RESOURCE_COUNT-1) // 250) + 2):
kwargs.update({"created_at_min" : FORMATTED_TODAY, "limit" : 250, "page" : page})
SHOPIFY_RESOURCES.extend(resource.find(**kwargs))
return SHOPIFY_RESOURCES
SHOPIFY_ORDERS = get_all_resources_date(shopify.Order())
DIFFERENCE = diff(ELOGII_RESPONSE_OBJECT, SHOPIFY_ORDERS)
print(DIFFERENCE)