This commit is contained in:
C committed 2026-01-04 12:38:16 -08:00
1 parent 53f221884a
commit 734d43b817
1 file changed
+20 -32
+20 -32
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.
Searches through catalog with pagination to find item by ID. Uses batch_get API for direct ID lookup.
Args: Args:
item_id: Square catalog item ID item_id: Square catalog item ID
@@ -67,40 +67,28 @@ async def get_item_by_id(item_id: str) -> dict:
Exception: If API error Exception: If API error
""" """
try: try:
cursor = None # Use batch_get to retrieve item directly by ID
page_count = 0 result = await client.catalog.batch_get(
object_ids=[item_id],
include_related_objects=False,
)
result_json = json.loads(result.model_dump_json())
while True: if not result_json.get("objects"):
page_count += 1 raise ValueError(f"Item {item_id} not found (may have been deleted)")
print(f"[@DEBUG] Searching for item {item_id} (page {page_count})...")
result = await client.catalog.search( obj = result_json["objects"][0]
object_types=["ITEM"], item_data = obj.get("item_data", {})
limit=1000,
cursor=cursor,
)
result_json = json.loads(result.model_dump_json())
if result_json.get("objects"): return {
for obj in result_json["objects"]: "id": obj["id"],
if obj["id"] == item_id: "name": item_data.get("name", ""),
item_data = obj.get("item_data", {}) "description": item_data.get("description", ""),
return { "category_id": item_data.get("category_id"),
"id": obj["id"], "variations": item_data.get("variations", []),
"name": item_data.get("name", ""), "updated_at": obj.get("updated_at"),
"description": item_data.get("description", ""), "version": obj.get("version"),
"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: except ValueError:
raise raise