Orders API

Uploading Orders into Big Block

Orders provide a structured way to manage and track shipments, ensuring that items are accurately accounted for from creation to fulfillment. By associating shipments with an order number, businesses can streamline logistics, improve traceability, and reduce errors in inventory handling. Our system is designed to support flexible order management, allowing you to create and retrieve orders programmatically via our API. However, using the API is entirely optional—our platform is fully functional without it, and you can light devices directly without associating them with Orders, Shipments, or Items.

Example Overview

Below is some example Python code using the popular requests library. This code demonstrates how to interact with your Django REST framework Order API endpoints. Adjust the base URL (BASE_URL) and authentication (if required) as needed. The key endpoints we’ll use are:

  • GET /api/orders/ (list)
  • POST /api/orders/ (create)
  • GET /api/orders/{pk}/ (retrieve)
  • DELETE /api/orders/{pk}/ (delete)

Setup

import requests
import json

# Change this to the actual address where your Django app is running
BASE_URL = "http://localhost:8000/api"

All API requests requires authentication headers (e.g., token auth, session cookies, oauth), include them in a headers dictionary. For example:

 headers = {
    "API_KEY": "YOUR_API_KEY_HERE",
    "Content-Type": "application/json"
}

1) List All Orders

 def list_orders():
    url = f"{BASE_URL}/orders/"
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        orders = response.json()
        print("List of Orders:", json.dumps(orders, indent=2))
    else:
        print("Failed to list orders. Status code:", response.status_code)
        print("Response:", response.text)

list_orders()

2) Create a New Order

2a) Create an Order with Shipments

If you provide shipments data, each shipment can contain an array of items. For each Item, be sure to supply required fields (name, description, SKU, UPC, ordered_quantity, quantity_fulfilled, warehouse_location, etc.). The server code will create the Shipment and Item objects accordingly.

 def create_order_with_shipments():
    url = f"{BASE_URL}/orders/"

    new_order = {
        "order_number": "SO-12345",
        "shipments": [
            {
                "shipment_number": "SHIP-001",
                "items": [
                    {
                    "name": "Widget A",
                    "description": "A sample widget",
                    "SKU": "SKU-A",
                    "UPC": "UPC-A",
                    "ordered_quantity": 10,
                    "quantity_fulfilled": 0,
                    "warehouse_location": "Aisle 3",
                    # 'device' can be null or omitted if not needed:
                    "device": None
                },
                    {
                    "name": "Widget B",
                    "description": "Another sample widget",
                    "SKU": "SKU-B",
                    "UPC": "UPC-B",
                    "ordered_quantity": 5,
                    "quantity_fulfilled": 0,
                    "warehouse_location": "Aisle 4",
                    "device": None
                }
            ]
        }
        ]
    }

    response = requests.post(url, headers=headers, data=json.dumps(new_order))

    if response.status_code == 201:
        created_order = response.json()
        print("Created Order (with shipments):", json.dumps(created_order, indent=2))
        return created_order
    else:
        print("Failed to create order. Status code:", response.status_code)
        print("Response:", response.text)
        return None

created_order = create_order_with_shipments()

2b) Create an Order without explicit Shipments

If you omit shipments but provide items, the server’s serializer automatically creates a “dummy” shipment with the shipment_number set to the order number. For example:

 def create_order_with_items_only():
    url = f"{BASE_URL}/orders/"

    new_order = {
        "order_number": "SO-6789",
        "items": [
            {
                "name": "Widget C",
                "description": "Single widget example",
                "SKU": "SKU-C",
                "UPC": "UPC-C",
                "ordered_quantity": 2,
                "quantity_fulfilled": 0,
                "warehouse_location": "Aisle 1",
                "device": None
        }
        ]
    }

    response = requests.post(url, headers=headers, data=json.dumps(new_order))

    if response.status_code == 201:
        created_order = response.json()
        print("Created Order (items only):", json.dumps(created_order, indent=2))
        return created_order
    else:
        print("Failed to create order. Status code:", response.status_code)
        print("Response:", response.text)
        return None

created_order_items_only = create_order_with_items_only()

3) Retrieve a Single Order by ID (Primary Key)

Use the id returned in the create/list response to retrieve a specific order:

 def get_order(order_id):
    url = f"{BASE_URL}/orders/{order_id}/"
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        order_data = response.json()
        print(f"Order {order_id} details:", json.dumps(order_data, indent=2))
        return order_data
    else:
        print(f"Failed to retrieve order {order_id}. Status code:", response.status_code)
        print("Response:", response.text)
        return None

if created_order:
    order_id = created_order["id"]
    get_order(order_id)

4) Delete an Order

 def delete_order(order_id):
    url = f"{BASE_URL}/orders/{order_id}/"
    response = requests.delete(url, headers=headers)

    if response.status_code == 204:
        print(f"Order {order_id} deleted successfully.")
    else:
        print(f"Failed to delete order {order_id}. Status code:", response.status_code)
        print("Response:", response.text)

if created_order:
    order_id = created_order["id"]
    delete_order(order_id)

Putting It All Together

Below is a short script that you can modify to test all the steps one after another:

 if __name__ == "__main__":
    # 1) List existing orders
    list_orders()

    # 2a) Create an order with shipments
    created_with_shipments = create_order_with_shipments()

    # 2b) Create an order with items only (no explicit shipments)
    created_with_items_only = create_order_with_items_only()

    # 3) Retrieve the first created order
    if created_with_shipments:
        get_order(created_with_shipments["id"])

    # 4) Delete the first created order
    if created_with_shipments:
        delete_order(created_with_shipments["id"])

    # Finally, list orders again to see the updated list
    list_orders()

Notes & Tips

  1. Authentication: If your API is secured, make sure to include proper authorization headers or session credentials.
  2. Validation Errors: If you get a status code like 400 (Bad Request), check the response body for detailed validation error messages from DRF.
  3. Nested Data: Make sure your nested shipments and items JSON matches exactly what your DRF serializers expect.
  4. Optional Fields: Certain fields (like device) are optional. You can pass null or omit them if not needed.
  5. Unique Constraints: Your model enforces unique_together on (order_number, voodoo_customer). Ensure you don’t reuse the same order_number for the same voodoo_customer (if you’re using multi-tenant logic).

With this approach, you can create, list, retrieve, and delete Order objects (and their related Shipment and Item objects) from a Python client.

© Copyright 2024 Voodoo Robotics. All Rights Reserved. Patents Pending. Voodoo Robotics, Voodoo Devices, Big Block Server, and Turbo names and logos are all trademarks of Voodoo Robotics.