Hey there, fellow developer! Ready to dive into the world of Salesforce Commerce Cloud API integration? You're in for a treat. This guide will walk you through creating a robust Python integration with Salesforce Commerce Cloud API. We'll cover everything from authentication to handling core endpoints, all while keeping things snappy and to the point. Let's get cracking!
Before we jump in, make sure you've got:
requests
, json
Got all that? Great! Let's move on.
First things first, we need to get you authenticated:
import requests def get_access_token(client_id, client_secret): url = "https://account.demandware.com/dw/oauth2/access_token" data = { "grant_type": "client_credentials" } response = requests.post(url, auth=(client_id, client_secret), data=data) return response.json()["access_token"]
Now, let's create a base API client class:
class SFCCClient: def __init__(self, client_id, client_secret, base_url): self.client_id = client_id self.client_secret = client_secret self.base_url = base_url self.access_token = None def get_headers(self): if not self.access_token: self.access_token = get_access_token(self.client_id, self.client_secret) return { "Authorization": f"Bearer {self.access_token}", "Content-Type": "application/json" } def make_request(self, method, endpoint, data=None): url = f"{self.base_url}{endpoint}" response = requests.request(method, url, headers=self.get_headers(), json=data) response.raise_for_status() return response.json()
Let's add methods for core endpoints:
class SFCCClient(SFCCClient): def get_product(self, product_id): return self.make_request("GET", f"/products/{product_id}") def create_order(self, order_data): return self.make_request("POST", "/orders", data=order_data) def get_customer(self, customer_id): return self.make_request("GET", f"/customers/{customer_id}") def update_inventory(self, inventory_data): return self.make_request("PATCH", "/inventory", data=inventory_data)
Don't forget to handle those pesky errors and respect rate limits:
import time from requests.exceptions import RequestException class SFCCClient(SFCCClient): def make_request(self, method, endpoint, data=None, retries=3): for attempt in range(retries): try: response = super().make_request(method, endpoint, data) return response except RequestException as e: if attempt == retries - 1: raise time.sleep(2 ** attempt) # Exponential backoff
Keep it simple with JSON handling:
import json def parse_response(response): return json.loads(response)
Let's put our client to work:
client = SFCCClient("your_client_id", "your_client_secret", "https://your-instance.demandware.net/s/-/dw/data/v21_3") # Fetch product info product = client.get_product("product123") print(f"Product name: {product['name']}") # Create an order order_data = {"customer": {"email": "[email protected]"}, "products": [{"id": "product123", "quantity": 1}]} order = client.create_order(order_data) print(f"Order created with ID: {order['id']}") # Update inventory inventory_data = {"product_id": "product123", "quantity": 100} client.update_inventory(inventory_data) print("Inventory updated successfully")
Don't skip testing! Here's a quick unit test example:
import unittest from unittest.mock import patch class TestSFCCClient(unittest.TestCase): @patch('requests.request') def test_get_product(self, mock_request): mock_request.return_value.json.return_value = {"id": "product123", "name": "Test Product"} client = SFCCClient("test_id", "test_secret", "https://test.com") product = client.get_product("product123") self.assertEqual(product["name"], "Test Product") if __name__ == '__main__': unittest.main()
And there you have it! You've just built a solid Salesforce Commerce Cloud API integration in Python. Remember, this is just the beginning. Keep exploring the API documentation, experiment with different endpoints, and don't be afraid to push the boundaries of what you can do with this integration.
Happy coding, and may your API calls always return 200 OK!