-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_getter.py
110 lines (97 loc) · 4.77 KB
/
package_getter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Module for sending Items and Boxes to Paccurate API to return Packages"""
import requests
import json
from warehouse import ItemSet, Box, Package
class Packer:
"""Returning list of packages using the Paccurate API"""
def __init__(self):
self.payload = None
self.headers = None
self.response = None
self.url = 'https://api.paccurate.io/'
def set_api_key(self, key):
api_key_value = f'apikey {key}'
self.headers = {'Authorization': api_key_value}
def get_payload(self, items, box): # Deprecated TODO Delete
concatenate = '''{"itemSets": ['''
for item in items:
item_string = '''{
"refId": %s,
"color": %s,
"dimensions": {"x": %s, "y": %s, "z": %s},
"quantity": %s
},''' % (str(item.ref_id), str(item.colour), str(item.dimensions[0]), str(item.dimensions[1]), str(item.dimensions[2]), str(item.quantity))
print(item_string)
concatenate += item_string
concatenate = concatenate[:-1]
concatenate += '''],'''
box_string = '''
"boxTypes": [{
"weightMax": %s,
"name": %s,
"dimensions": {"x": %s, "y": %s, "z": %s}
}]
}''' % (str(box.max_weight), str(box.name), str(box.dimensions[0]), str(box.dimensions[1]), str(box.dimensions[2]))
concatenate += box_string
self.payload = json.dumps(concatenate)
def set_payload(self, items, boxes):
"""Items is a list of class ItemSet objects and boxes is a list of class Box objects"""
itemsets = list()
for itemset in items:
itemset_dimensions = dict(x=itemset.dimensions[0],
y=itemset.dimensions[1],
z=itemset.dimensions[2])
itemsets.append(dict(refId=itemset.ref_id,
weight=itemset.weight,
dimensions=itemset_dimensions,
quantity=itemset.quantity,
color=itemset.colour))
boxtypes = list()
for box in boxes:
box_dimensions = dict(x=box.dimensions[0],
y=box.dimensions[1],
z=box.dimensions[2])
boxtypes.append(dict(weightMax=box.max_weight,
name=box.name,
dimensions=box_dimensions))
payload = dict(itemSets=itemsets,
boxTypes=boxtypes)
self.payload = json.dumps(payload)
def get_response(self):
if self.url is None:
raise ValueError('The Paccurate API URL is not set')
if self.payload is None:
raise ValueError('There is no payload to send to the Paccurate API')
if self.headers is None:
raise ValueError('The Paccurate API key is not set')
self.response = requests.post(self.url, data=self.payload, headers=self.headers)
def get_packages(self):
"""Returns a list of class Package items"""
if self.response is None:
raise ValueError('There is no response from the Paccurate API')
response_json = json.loads(self.response.text)
# TODO save the values of the lenBoxes, lenItems keys for debugging
packages = list()
for box, image in zip(response_json['boxes'], response_json['svgs']):
items_and_locations = list()
for item in box['box']['items']:
itemset_dimensions = (item['item']['dimensions']['x'],
item['item']['dimensions']['y'],
item['item']['dimensions']['z'])
itemset = ItemSet(itemset_dimensions, item['item']['weight'], item['item']['refId'], 1,item['item']['color'])
print(item['item']['color'])
itemset_location = (item['item']['origin']['x'],
item['item']['origin']['y'],
item['item']['origin']['z'])
items_and_locations.append((itemset, itemset_location))
box_dimensions = (box['box']['dimensions']['x'],
box['box']['dimensions']['y'],
box['box']['dimensions']['z'])
box = Box(box['box']['name'], box_dimensions, box['box']['weightMax'])
packages.append(Package(items_and_locations, box, image))
return packages
def pack(self, items, boxes):
"""Wrapper function for set_payload, get_response and get_packages"""
self.set_payload(items, boxes)
self.get_response()
return self.get_packages()