diff --git a/camlib/photobox.py b/camlib/photobox.py new file mode 100644 index 0000000..94df427 --- /dev/null +++ b/camlib/photobox.py @@ -0,0 +1,46 @@ +import requests + +class PhotoboxClient: + def __init__(self, client_id, client_secret): + self.base_url = "https://api.photobox.com" + self.client_id = client_id + self.client_secret = client_secret + self.token = self._get_access_token() + + def _get_access_token(self): + """Get OAuth2 access token""" + auth_url = f"{self.base_url}/oauth/token" + response = requests.post( + auth_url, + auth=(self.client_id, self.client_secret), + data={"grant_type": "client_credentials"} + ) + response.raise_for_status() + return response.json()["access_token"] + + def upload_image(self, image_data): + """Upload image to Photobox""" + endpoint = f"{self.base_url}/v1/media" + headers = {"Authorization": f"Bearer {self.token}"} + response = requests.post( + endpoint, + files={"file": image_data}, + headers=headers + ) + response.raise_for_status() + return response.json() + + def create_order(self, product_id, image_id, quantity=1): + """Create print order with Photobox""" + endpoint = f"{self.base_url}/v1/orders" + headers = {"Authorization": f"Bearer {self.token}"} + payload = { + "items": [{ + "productId": product_id, + "imageId": image_id, + "quantity": quantity + }] + } + response = requests.post(endpoint, json=payload, headers=headers) + response.raise_for_status() + return response.json() diff --git a/camlib/shutterfly.py b/camlib/shutterfly.py new file mode 100644 index 0000000..e3ad9f9 --- /dev/null +++ b/camlib/shutterfly.py @@ -0,0 +1,30 @@ +import requests +from requests.auth import HTTPBasicAuth + +class ShutterflyClient: + def __init__(self, api_key, api_secret): + self.base_url = "https://api.shutterfly.com" + self.auth = HTTPBasicAuth(api_key, api_secret) + + def upload_photo(self, image_data, product_type="prints"): + """Upload photo to Shutterfly for printing""" + endpoint = f"{self.base_url}/media" + response = requests.post( + endpoint, + files={"file": image_data}, + auth=self.auth, + params={"productType": product_type} + ) + response.raise_for_status() + return response.json() + + def create_order(self, items): + """Create print order with Shutterfly""" + endpoint = f"{self.base_url}/orders" + response = requests.post( + endpoint, + json={"items": items}, + auth=self.auth + ) + response.raise_for_status() + return response.json() diff --git a/config-example.py b/config-example.py index abaa312..d438795 100644 --- a/config-example.py +++ b/config-example.py @@ -8,3 +8,12 @@ # Sentry configuration for error logging. use_sentry = False sentry_dsn = "https://public@sentry.example.com/1" + +# Printing service configurations +use_shutterfly = False +shutterfly_api_key = "your_shutterfly_key" +shutterfly_api_secret = "your_shutterfly_secret" + +use_photobox = False +photobox_client_id = "your_photobox_id" +photobox_client_secret = "your_photobox_secret" diff --git a/routes/order_finish.py b/routes/order_finish.py index f2568a1..2f3e85c 100644 --- a/routes/order_finish.py +++ b/routes/order_finish.py @@ -1,12 +1,40 @@ from camlib import response - +from camlib.shutterfly import ShutterflyClient +from camlib.photobox import PhotoboxClient +import config +import logging @response() -def notice_order_finish(_): +def notice_order_finish(request): + error_items = [] + messages = [] + + try: + if config.use_shutterfly: + shutterfly = ShutterflyClient(config.shutterfly_api_key, config.shutterfly_api_secret) + shutterfly.create_order(request.json["items"]) + messages.append("Order sent to Shutterfly") + except Exception as e: + logging.error(f"Shutterfly error: {str(e)}") + error_items.append(1) + messages.append("Shutterfly order failed") + + try: + if config.use_photobox: + photobox = PhotoboxClient(config.photobox_client_id, config.photobox_client_secret) + photobox.create_order( + product_id=request.json["product_id"], + image_id=request.json["image_id"] + ) + messages.append("Order sent to Photobox") + except Exception as e: + logging.error(f"Photobox error: {str(e)}") + error_items.append(2) + messages.append("Photobox order failed") + return { - "errorItems": [0], + "errorItems": error_items if error_items else [0], "available": 0, - "fixOrderText": "Your order has been finalized!", - "messageBoardText": "Congratulations! Your order has been dispatched via email. " - "Please allow up to 10 minutes for delivery.", + "fixOrderText": "Order processing completed", + "messageBoardText": ". ".join(messages) if messages else "Order processed successfully" }