feat: implement backend API clients for multi-marketplace integration and frontend service layer
This commit is contained in:
15
backend/flowwow_api/__init__.py
Normal file
15
backend/flowwow_api/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from .client import FlowwowClient
|
||||
from .services.products import ProductsService
|
||||
from .services.orders import OrdersService
|
||||
|
||||
class FlowwowAPI:
|
||||
"""
|
||||
Main entry point for Flowwow SDK.
|
||||
Instantiates all services with a shared client.
|
||||
"""
|
||||
def __init__(self, api_token: str = None):
|
||||
self.client = FlowwowClient(api_token)
|
||||
|
||||
# Initialize services
|
||||
self.products = ProductsService(self.client)
|
||||
self.orders = OrdersService(self.client)
|
||||
72
backend/flowwow_api/client.py
Normal file
72
backend/flowwow_api/client.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import os
|
||||
import requests
|
||||
import time
|
||||
from typing import Dict, Any, Optional
|
||||
from .exceptions import FlowwowAPIError, FlowwowAuthError, FlowwowRateLimitError, FlowwowBadRequestError, FlowwowNotFoundError
|
||||
|
||||
class FlowwowClient:
|
||||
"""Base client for interacting with Flowwow API."""
|
||||
|
||||
BASE_URL = "https://flowwow.com/api/v1"
|
||||
|
||||
def __init__(self, api_token: Optional[str] = None):
|
||||
# Fetch from env if not provided
|
||||
self.api_token = api_token or os.getenv('FLOWWOW_API_TOKEN')
|
||||
|
||||
if not self.api_token:
|
||||
raise ValueError("FLOWWOW_API_TOKEN must be provided")
|
||||
|
||||
self.session = requests.Session()
|
||||
# Flowwow usually uses X-Api-Key or Authorization Bearer.
|
||||
# We will use Bearer as a generic default but it can be changed to X-Api-Key if their spec strictly requires it.
|
||||
self.session.headers.update({
|
||||
"Authorization": f"Bearer {self.api_token}",
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
|
||||
def request(self, method: str, path: str, data: Optional[Dict[str, Any]] = None, params: Optional[Dict[str, Any]] = None, retries: int = 3) -> Dict[str, Any]:
|
||||
"""Perform HTTP request to Flowwow API with automatic retries."""
|
||||
url = f"{self.BASE_URL}{path}"
|
||||
|
||||
for attempt in range(retries):
|
||||
try:
|
||||
response = self.session.request(method, url, json=data, params=params)
|
||||
|
||||
# Check rate limiting
|
||||
if response.status_code == 429:
|
||||
if attempt < retries - 1:
|
||||
time.sleep(2 ** attempt) # Exponential backoff
|
||||
continue
|
||||
raise FlowwowRateLimitError("Rate limit exceeded")
|
||||
|
||||
if response.status_code in (401, 403):
|
||||
raise FlowwowAuthError(f"Authentication failed: {response.text}")
|
||||
|
||||
if response.status_code in (400, 422):
|
||||
raise FlowwowBadRequestError(f"Bad Request: {response.text}")
|
||||
|
||||
if response.status_code == 404:
|
||||
raise FlowwowNotFoundError(f"Not Found: {response.text}")
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
if response.content:
|
||||
return response.json()
|
||||
return {}
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
if attempt < retries - 1:
|
||||
time.sleep(2 ** attempt)
|
||||
continue
|
||||
raise FlowwowAPIError(f"Request failed: {str(e)}")
|
||||
|
||||
raise FlowwowAPIError("Request failed after max retries")
|
||||
|
||||
def post(self, path: str, data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
return self.request("POST", path, data=data)
|
||||
|
||||
def get(self, path: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
return self.request("GET", path, params=params)
|
||||
|
||||
def put(self, path: str, data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
return self.request("PUT", path, data=data)
|
||||
19
backend/flowwow_api/exceptions.py
Normal file
19
backend/flowwow_api/exceptions.py
Normal file
@@ -0,0 +1,19 @@
|
||||
class FlowwowAPIError(Exception):
|
||||
"""Base exception for all Flowwow API errors."""
|
||||
pass
|
||||
|
||||
class FlowwowAuthError(FlowwowAPIError):
|
||||
"""Raised when authentication fails (HTTP 401 or 403)."""
|
||||
pass
|
||||
|
||||
class FlowwowRateLimitError(FlowwowAPIError):
|
||||
"""Raised when API rate limits are exceeded (HTTP 429)."""
|
||||
pass
|
||||
|
||||
class FlowwowBadRequestError(FlowwowAPIError):
|
||||
"""Raised when API returns HTTP 400 or 422."""
|
||||
pass
|
||||
|
||||
class FlowwowNotFoundError(FlowwowAPIError):
|
||||
"""Raised when API returns HTTP 404."""
|
||||
pass
|
||||
29
backend/flowwow_api/services/orders.py
Normal file
29
backend/flowwow_api/services/orders.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from typing import Dict, Any
|
||||
from ..client import FlowwowClient
|
||||
|
||||
class OrdersService:
|
||||
"""Service to handle Flowwow Orders."""
|
||||
|
||||
def __init__(self, client: FlowwowClient):
|
||||
self.client = client
|
||||
|
||||
def get_new_orders(self, limit: int = 50) -> Dict[str, Any]:
|
||||
"""
|
||||
Get list of new, unprocessed orders.
|
||||
Endpoint: /orders/new
|
||||
"""
|
||||
return self.client.get("/orders/new", params={"limit": limit})
|
||||
|
||||
def accept_order(self, order_id: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Accept an order for processing.
|
||||
Endpoint: /orders/{order_id}/accept
|
||||
"""
|
||||
return self.client.post(f"/orders/{order_id}/accept")
|
||||
|
||||
def upload_photo(self, order_id: str, image_url: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Upload a photo of the completed order before shipping.
|
||||
Endpoint: /orders/{order_id}/photo
|
||||
"""
|
||||
return self.client.post(f"/orders/{order_id}/photo", data={"image_url": image_url})
|
||||
30
backend/flowwow_api/services/products.py
Normal file
30
backend/flowwow_api/services/products.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from typing import Dict, Any, List
|
||||
from ..client import FlowwowClient
|
||||
|
||||
class ProductsService:
|
||||
"""Service to handle Flowwow Products."""
|
||||
|
||||
def __init__(self, client: FlowwowClient):
|
||||
self.client = client
|
||||
|
||||
def get_list(self, limit: int = 100, offset: int = 0) -> Dict[str, Any]:
|
||||
"""
|
||||
Get list of products.
|
||||
Endpoint: /products
|
||||
"""
|
||||
params = {"limit": limit, "offset": offset}
|
||||
return self.client.get("/products", params=params)
|
||||
|
||||
def update_price(self, product_id: str, price: float) -> Dict[str, Any]:
|
||||
"""
|
||||
Update product price.
|
||||
Endpoint: /products/{product_id}/price
|
||||
"""
|
||||
return self.client.put(f"/products/{product_id}/price", data={"price": price})
|
||||
|
||||
def update_stock(self, product_id: str, quantity: int) -> Dict[str, Any]:
|
||||
"""
|
||||
Update product stock/availability.
|
||||
Endpoint: /products/{product_id}/stock
|
||||
"""
|
||||
return self.client.put(f"/products/{product_id}/stock", data={"quantity": quantity})
|
||||
Reference in New Issue
Block a user