Skip to content

Instantly share code, notes, and snippets.

@rod-dot-codes
Created April 26, 2017 16:15
Show Gist options
  • Save rod-dot-codes/d103abdd31d7b2c3511e05c649c0802a to your computer and use it in GitHub Desktop.
Save rod-dot-codes/d103abdd31d7b2c3511e05c649c0802a to your computer and use it in GitHub Desktop.
API Request
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from multiprocessing import Process
from Queue import Queue, Empty
import json
import threading
import time
import requests
import uuid
from requests.auth import HTTPBasicAuth
USERNAME = ""
PASSWORD = ""
COMPANY = "generic-company-14"
URL = "https://demo.scurri.co.uk"
def shipment_with_id(id):
return [{
"identifier": id,
"order_number": id,
"service_id":"UPS|UPS Express Saver® 65",
"warehouse_id":"generic-company-14|Generic Company 14",
"recipient":{
"name":"John Doe",
"first_name":"",
"last_name":"John Doe",
"company_name":"Scurri",
"email_address":"john@scurri.com",
"contact_number":"072 8848292234",
"address":{
"address1":"Innovation House",
"address2":"The Bullring",
"address3":"",
"city":"Wexford",
"state":"Wexford",
"postcode":"Y35 DW6E",
"country":"IE",
"store_code":""
},
},
"packages":[
{
"description":"Novelty Item",
"width":12.2,
"height":0.5,
"length":10.0,
"items":[
{
"name":"Jeans",
"quantity":1,
"sku":"822 JEANS",
"country_of_origin":"GB",
"harmonisation_code":"5201",
"weight":1.0,
"value":25.55
}
]
}
],
"delivery_instructions":"",
"currency":"GBP",
"options":{
"package_type": "Customer Supplied Package"
},
"shipping_method":"",
"custom_field_1":"",
"custom_field_2":""
}]
def get_shipment_label(id):
response = requests.get('{url}/_api/v1/company/{company}/consignment/{id}/documents?documenttype=application%2Fpdf&label_quantity=1&invoice_quantity=3'.format(url=URL, company=COMPANY, id=id),
auth=HTTPBasicAuth(USERNAME, PASSWORD))
assert len(response.json()['labels']) > 0
assert response.status_code == 200
print response
def create_shipment(id):
shipment = shipment_with_id(id)
response = requests.post('{url}/_api/v1/company/{company}/consignments'.format(url=URL, company=COMPANY),
json=shipment,
auth=HTTPBasicAuth(USERNAME, PASSWORD))
print response
errors = len(json.loads(response.text)['errors'])
print response.text
assert errors == 0
assert response.status_code == 200
get_shipment_label(id)
if __name__ == "__main__":
import random
id = random.randint(0, 10000)
create_shipment("TEST%s" % id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment