diff --git a/src/asquare.py b/src/asquare.py index 86b459f..a097319 100644 --- a/src/asquare.py +++ b/src/asquare.py @@ -54,7 +54,7 @@ async def get_item_by_id(item_id: str) -> dict: """ Retrieve full item details by Square catalog item ID. - Uses Square's batch retrieve API for direct lookup by ID. + Searches through catalog with pagination to find item by ID. Args: item_id: Square catalog item ID @@ -67,31 +67,42 @@ async def get_item_by_id(item_id: str) -> dict: Exception: If API error """ try: - # Use batch retrieve to get item directly by ID - result = await client.catalog.batch_retrieve( - object_ids=[item_id], - include_related_objects=False, - ) - result_json = json.loads(result.model_dump_json()) + cursor = None + page_count = 0 - if not result_json.get("objects"): - raise ValueError(f"Item {item_id} not found (may have been deleted)") + while True: + page_count += 1 + print(f"[@DEBUG] Searching for item {item_id} (page {page_count})...") - obj = result_json["objects"][0] - item_data = obj.get("item_data", {}) + result = await client.catalog.search( + object_types=["ITEM"], + limit=1000, + cursor=cursor, + ) + result_json = json.loads(result.model_dump_json()) - return { - "id": obj["id"], - "name": item_data.get("name", ""), - "description": item_data.get("description", ""), - "category_id": item_data.get("category_id"), - "variations": item_data.get("variations", []), - "updated_at": obj.get("updated_at"), - "version": obj.get("version"), - } + if result_json.get("objects"): + for obj in result_json["objects"]: + if obj["id"] == item_id: + item_data = obj.get("item_data", {}) + return { + "id": obj["id"], + "name": item_data.get("name", ""), + "description": item_data.get("description", ""), + "category_id": item_data.get("category_id"), + "variations": item_data.get("variations", []), + "updated_at": obj.get("updated_at"), + "version": obj.get("version"), + } + + # Check if there are more pages + cursor = result_json.get("cursor") + if not cursor: + break + + raise ValueError(f"Item {item_id} not found (may have been deleted)") except ValueError: - # Re-raise ValueError as-is for clear error messages raise except Exception as e: print(f"[@ERROR] Failed to get item {item_id}: {e}")