Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions camlib/photobox.py
Original file line number Diff line number Diff line change
@@ -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()
30 changes: 30 additions & 0 deletions camlib/shutterfly.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions config-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@
# Sentry configuration for error logging.
use_sentry = False
sentry_dsn = "https://[email protected]/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"
40 changes: 34 additions & 6 deletions routes/order_finish.py
Original file line number Diff line number Diff line change
@@ -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"
}