vrasdfas
This commit is contained in:
1 parent
5f4bbb2565
commit
53f221884a
1 file changed
+32
-21
+32
-21
@@ -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,31 +67,42 @@ 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,
|
|
||||||
)
|
|
||||||
result_json = json.loads(result.model_dump_json())
|
|
||||||
|
|
||||||
if not result_json.get("objects"):
|
while True:
|
||||||
raise ValueError(f"Item {item_id} not found (may have been deleted)")
|
page_count += 1
|
||||||
|
print(f"[@DEBUG] Searching for item {item_id} (page {page_count})...")
|
||||||
|
|
||||||
obj = result_json["objects"][0]
|
result = await client.catalog.search(
|
||||||
item_data = obj.get("item_data", {})
|
object_types=["ITEM"],
|
||||||
|
limit=1000,
|
||||||
|
cursor=cursor,
|
||||||
|
)
|
||||||
|
result_json = json.loads(result.model_dump_json())
|
||||||
|
|
||||||
return {
|
if result_json.get("objects"):
|
||||||
"id": obj["id"],
|
for obj in result_json["objects"]:
|
||||||
"name": item_data.get("name", ""),
|
if obj["id"] == item_id:
|
||||||
"description": item_data.get("description", ""),
|
item_data = obj.get("item_data", {})
|
||||||
"category_id": item_data.get("category_id"),
|
return {
|
||||||
"variations": item_data.get("variations", []),
|
"id": obj["id"],
|
||||||
"updated_at": obj.get("updated_at"),
|
"name": item_data.get("name", ""),
|
||||||
"version": obj.get("version"),
|
"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:
|
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}")
|
||||||
|
|||||||
Reference in new issue
Block a user