Skip to content

Instantly share code, notes, and snippets.

@agehlot
Created August 19, 2021 14:29
Show Gist options
  • Save agehlot/587df77d65a41d3ef5434b11d621088a to your computer and use it in GitHub Desktop.
Save agehlot/587df77d65a41d3ef5434b11d621088a to your computer and use it in GitHub Desktop.
This python script can be used to upload the orders into boxever CDP via batch API.
# https://developer.boxever.com/reference/overview_batch
# https://developer.boxever.com/reference/importing-the-batch-file
# order data model: https://doc.sitecore.com/cdp/en/developers/sitecore-customer-data-platform--data-model-2-1/sitecore-cdp-order-data-model-for-batch-api.html
# Script takes a file, client key and api secret.Name your JSON file outputFile.json, and compress into outputFile.json.gz
# one JSON record per line, see docs for full details on fomatting
import csv
import json
import uuid
import codecs
import sys
import gzip
import base64
import hashlib
import os
import requests
import logging
fileName = "batchUploadOrder.json.gz" ##sys.argv[1]
clientKey = "psfu6uh05hsr9c34rptlr06dn864cqrx" ###sys.argv[2]
apiToken = "uz9h803ertuvxcb9jh2cu05t87at85nl" ###sys.argv[3]
endpoint="https://api.boxever.com/v2/batches"
uuid = str(uuid.uuid4())
### Get the MD5 Checksum of the GZipped JSON file
print("Getting the MD5 Checksum of the GZipped JSON file")
def file_as_bytes(file):
with file:
return file.read()
md5=hashlib.md5(file_as_bytes(open(fileName, 'rb'))).hexdigest()
### Get the size of the file
print("Getting the size of the file")
size = os.path.getsize(fileName)
### Create and send a Request for an upload URL
print("Sending Request for an upload URL")
req={'checksum': md5, 'size': str(size)}
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
response = requests.put(endpoint + "/" + uuid, auth=(clientKey, apiToken), headers=headers, json=req)
### Retrieve the upload URL from the Response
print("Getting the upload URL from the response")
data = json.loads(response.text)
location = data['location']
uploadUrl = location['href']
### Create and send a Request to upload the GZipped JSON file to the upload URL
print("Uploading the GZipped JSON file to the upload URL")
b64md5 = base64.b64encode(bytearray.fromhex(md5))
headers = {'x-amz-server-side-encryption': 'AES256', 'Content-Md5': b64md5}
file_data = open(fileName, 'rb').read()
response = requests.put(uploadUrl, headers=headers, data=file_data)
### Delete the GZipped JSON file
print("Deleting the GZipped JSON file")
os.remove(fileName)
print("\nFILE UPLOADED SUCCESSFULLY\n")
### Generate a URL that can be used to get the status of this upload
print("Use this URL in PostMan (or similar) to retrieve the status of this upload: https://api.boxever.com/v2/batches/" + uuid )
{
"ref":"056621C2-C955-492D-B7EE-C77C6BDDFDEE",
"schema":"order",
"mode":"insert",
"value":{
"referenceId":"B94TXY",
"status":"PURCHASED",
"orderedAt":"2015-08-23T16:17:16.000Z",
"currencyCode":"EUR",
"price":100,
"paymentType":"Card",
"cardType":"Visa",
"contact":{
"title":"Mr",
"firstName":"Arvind",
"lastName":"Gehlot",
"gender":"male",
"dateOfBirth":"1985-12-24T00:00Z",
"email":"[email protected]",
"phoneNumbers":[
"+353161123345",
"+353861123311"
],
"identifiers":[
{
"provider":"BOXEVER_IDENTITY_SYSTEM",
"id":"9689238b-0cd2-424f-91a7-5d895db84d4c",
"expiryDate":"2016-08-23T16:17:16.000Z"
},
{
"provider":"Boxever Loyalty Program",
"id":"123456789t"
}
]
},
"consumers":[
{
"title":"Mr",
"firstName":"Arvind",
"lastName":"Gehlot",
"identifiers":[
{
"provider":"BOXEVER_IDENTITY_SYSTEM",
"id":"9689238b-0cd2-424f-91a7-5d895db84d4c",
"expiryDate":"2016-08-23T16:17:16.000Z"
}
],
"orderItems":[
{
"referenceId":"B94TXY-1"
}
]
}
],
"orderItems":[
{
"type":"FLIGHT",
"productId":"DUB-LAS",
"orderedAt":"2015-08-23T16:17:16.000Z",
"quantity":1,
"price":100.00,
"currencyCode":"EUR",
"originalPrice":100.00,
"originalCurrencyCode":"EUR",
"referenceId":"B94TXY-1",
"status":"PURCHASED",
"consumerTypeCode":"ADT",
"flightSegments":[
{
"id":"1234",
"origin":"DUB",
"destination":"LHR",
"departureDateTime":"2016-08-15T16:00Z",
"arrivalDateTime":"2016-08-15T17:20Z",
"flightNumber":"459",
"fareClass":"Economy",
"fareFamily":"Plus",
"carrier":"FR",
"originDestination":"DUB-LHR"
},
{
"id":"1235",
"origin":"LHR",
"destination":"LAS",
"departureDateTime":"2016-08-15T20:00Z",
"arrivalDateTime":"2016-08-15T22:30Z",
"flightNumber":"460",
"fareClass":"Economy",
"fareFamily":"Plus",
"carrier":"FR",
"originDestination":"LHR-LAS"
}
]
}
]
}
}
{"ref":"056621C2-C955-492D-B7EE-C77C6BDDFDEE","schema":"order","mode":"insert","value":{"referenceId":"B94TXY","status":"PURCHASED","orderedAt":"2015-08-23T16:17:16.000Z","currencyCode":"EUR","price":100,"paymentType":"Card","cardType":"Visa","contact":{"title":"Mr","firstName":"Arvind","lastName":"Gehlot","gender":"male","dateOfBirth":"1985-12-24T00:00Z","email":"[email protected]","phoneNumbers":["+353161123345","+353861123311"],"identifiers":[{"provider":"BOXEVER_IDENTITY_SYSTEM","id":"9689238b-0cd2-424f-91a7-5d895db84d4c","expiryDate":"2016-08-23T16:17:16.000Z"},{"provider":"Boxever Loyalty Program","id":"123456789t"}]},"consumers":[{"title":"Mr","firstName":"Arvind","lastName":"Gehlot","identifiers":[{"provider":"BOXEVER_IDENTITY_SYSTEM","id":"9689238b-0cd2-424f-91a7-5d895db84d4c","expiryDate":"2016-08-23T16:17:16.000Z"}],"orderItems":[{"referenceId":"B94TXY-1"}]}],"orderItems":[{"type":"FLIGHT","productId":"DUB-LAS","orderedAt":"2015-08-23T16:17:16.000Z","quantity":1,"price":100,"currencyCode":"EUR","originalPrice":100,"originalCurrencyCode":"EUR","referenceId":"B94TXY-1","status":"PURCHASED","consumerTypeCode":"ADT","flightSegments":[{"id":"1234","origin":"DUB","destination":"LHR","departureDateTime":"2016-08-15T16:00Z","arrivalDateTime":"2016-08-15T17:20Z","flightNumber":"459","fareClass":"Economy","fareFamily":"Plus","carrier":"FR","originDestination":"DUB-LHR"},{"id":"1235","origin":"LHR","destination":"LAS","departureDateTime":"2016-08-15T20:00Z","arrivalDateTime":"2016-08-15T22:30Z","flightNumber":"460","fareClass":"Economy","fareFamily":"Plus","carrier":"FR","originDestination":"LHR-LAS"}]}]}}
@agehlot
Copy link
Author

agehlot commented Aug 20, 2021

You need to follow the below steps to successfully upload the orders to boxever.

  • Install the latest python on your local machine.
  • Replace clientKey and apiToken values in the python script file.
  • Apply gzip compression on the single line json batchUploadOrder.json
  • If you are using a formatted JSON file, make sure that you convert it into single line JSON file.
  • On the command prompt, reach to the current directory of these files
  • Execute the below command to start the processing.
    python batchUpload.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment