class ShipStationClient(): 'Handles order creation and check on shipstation.' def __init__(self) -> None: self.client = ShipStation(key=settings.SHIPSTATION_API_KEY, secret=settings.SHIPSTATION_API_SECRET) def get_client(self) -> ShipStation: 'Get native shipstation client' return self.client def add_lead_order( self, lead: Lead, status: typing.Optional[str] = None) -> ShipStationOrder: 'Create order for given lead.' if not lead.shipstation_order_number: random_str = str(uuid.uuid4()).replace('-', '')[:10] lead.shipstation_order_number = '{}__{}'.format( lead.raspberry_pi.rpid, random_str) lead.save() order = ShipStationOrder(order_key=lead.shipstation_order_number, order_number=lead.shipstation_order_number) order.set_customer_details( username=lead.safe_name(), email=lead.email, ) if status: order.set_status(status) shipping_address = ShipStationAddress( name=lead.safe_name(), # company=sf_lead.company, street1=lead.street, street2=lead.apartment, city=lead.city, postal_code=lead.postal_code, # country=sf_lead.country, country='US', state=lead.state or '', phone=lead.phone, ) order.set_shipping_address(shipping_address) order.set_billing_address(shipping_address) item = ShipStationItem( name=lead.raspberry_pi.rpid, quantity=1, unit_price=0, ) item.set_weight(ShipStationWeight(units='ounces', value=0)) order.add_item(item) order.set_status('awaiting_shipment') self.client.add_order(order) self.submit_orders() return order def submit_orders(self) -> None: 'Submit all created or changed orders.' for order in self.client.orders: self.post(endpoint='/orders/createorder', data=order.as_dict()) def post(self, endpoint: str, data: typing.Dict) -> None: 'Rewrite original shipstaion.post method to catch exceptions.' url = '{}{}'.format(self.client.url, endpoint) headers = {'content-type': 'application/json'} response = requests.post( url, auth=(self.client.key, self.client.secret), data=json.dumps(data), headers=headers, ) if response.status_code not in [200, 201]: raise ValueError('Shipstation Error', response.status_code, data, response.text) @staticmethod def get_lead_order_data(lead: Lead) -> typing.Optional[typing.Dict]: 'Get order data for lead.' if not lead.shipstation_order_number: return None data = requests.get( 'https://ssapi.shipstation.com/orders', params={ 'orderNumber': lead.shipstation_order_number }, auth=requests.auth.HTTPBasicAuth(settings.SHIPSTATION_API_KEY, settings.SHIPSTATION_API_SECRET), ).json().get('orders') data = data[0] if data else None return data
from shipstation.api import ShipStation, ShipStationOrder, ShipStationAddress, ShipStationWeight, ShipStationContainer ss = ShipStation(key=os.getenv('API_KEY'), secret=os.getenv('API_SECRET')) for record in records: if record['Status'] == 'awaiting shipment': ss_order = ShipStationOrder(order_number='w2021award') ss_order.set_status('awaiting_shipment') ss_weight = ShipStationWeight(units='ounces', value=4) ss_container = ShipStationContainer( units='inches', length=12.25, width=9.75, height=0.25 ) ss_container.set_weight(ss_weight) shipping_address = ShipStationAddress( name=record['Name'], street1=record['addrline1'], street2=record['addrline2'], city=record['City'], state=record['State'], postal_code=record['ZIP'], country=record['Country'] ) ss_order.set_billing_address(shipping_address) ss_order.set_shipping_address(shipping_address) ss_order.set_dimensions(ss_container) ss.add_order(ss_order) row = worksheet.find(str(record['discord ID'])).row worksheet.update_cell(row=row, col=12, value='in shipstation') ss.submit_orders()