0

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)
Leo
  • 3,822
  • 3
  • 21
  • 26
  • Well... there is many steps and with this information it's hard to help. That diff isn't shortcut to working solution anyway. I'd start to making list from Elogil data including all Shopii order id's. Then you could loop throug Shipify data and check if a order exist in that list. If not, posting it to Elogil would be next problem to solve. – ex4 Apr 28 '20 at 13:00
  • @ex4 Sorry for the confusion. I've updated the post now with my code so you can see exactly what I'm currently working with. I basically want to import each new order from the Shopify API into the Elogii API. I feel like the first step of this is to do a check to make sure that the order doesn't already exist in Elogii before posting. Just need some help getting to there :) – Richie McIlroy Apr 28 '20 at 13:20
  • What does this print now? Especially what does that Elogii query return? Could you give simple example of data structure. – ex4 Apr 28 '20 at 17:10

1 Answers1

0

One of the eLogii devs here - good to see you making progress on this! What I assume would be the most straightforward approach is to actually not compare lists of objects (JSON objects from eLogii from one side and Shopify Order objects on the other), but just the IDs as strings. You can use the meta.reference field when sending a delivery to eLogii to store the Shopify order ID - then the rough workflow could be:

  • get orders from Shopify at the end of the day and map through it to get a list of IDs
  • query eLogii API to get deliveries already scheduled for that date or range of dates (you're currently not providing additional query parameters in your call, which is fine but the result might become bulky over time)
  • extract from the result list just the values of meta.reference fields (which you would again most likely do using the map function)
  • compare the lists of Shopify IDs from Shopify to the ones extracted from eLogii - Get difference between two lists provides a good way of getting the difference between 2 such lists in Python.
  • map through the result array and send new Shopify orders as deliveries to eLogii

Hope this gives you a hint on the general direction - feel free to reach out if we can provide any assistance!

Leo
  • 3,822
  • 3
  • 21
  • 26