This commit is contained in:
C committed 2026-01-04 12:36:42 -08:00
1 parent 5f4bbb2565
commit 53f221884a
1 file changed
+22 -11
+22 -11
View File
@@ -54,7 +54,7 @@ async def get_item_by_id(item_id: str) -> dict:
""" """
Retrieve full item details by Square catalog item ID. 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: Args:
item_id: Square catalog item ID item_id: Square catalog item ID
@@ -67,19 +67,24 @@ async def get_item_by_id(item_id: str) -> dict:
Exception: If API error Exception: If API error
""" """
try: try:
# Use batch retrieve to get item directly by ID cursor = None
result = await client.catalog.batch_retrieve( page_count = 0
object_ids=[item_id],
include_related_objects=False, while True:
page_count += 1
print(f"[@DEBUG] Searching for item {item_id} (page {page_count})...")
result = await client.catalog.search(
object_types=["ITEM"],
limit=1000,
cursor=cursor,
) )
result_json = json.loads(result.model_dump_json()) result_json = json.loads(result.model_dump_json())
if not result_json.get("objects"): if result_json.get("objects"):
raise ValueError(f"Item {item_id} not found (may have been deleted)") for obj in result_json["objects"]:
if obj["id"] == item_id:
obj = result_json["objects"][0]
item_data = obj.get("item_data", {}) item_data = obj.get("item_data", {})
return { return {
"id": obj["id"], "id": obj["id"],
"name": item_data.get("name", ""), "name": item_data.get("name", ""),
@@ -90,8 +95,14 @@ async def get_item_by_id(item_id: str) -> dict:
"version": obj.get("version"), "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: except ValueError:
# Re-raise ValueError as-is for clear error messages
raise raise
except Exception as e: except Exception as e:
print(f"[@ERROR] Failed to get item {item_id}: {e}") print(f"[@ERROR] Failed to get item {item_id}: {e}")