vrasdfas
This commit is contained in:
1 parent
5f4bbb2565
commit
53f221884a
1 file changed
+22
-11
+22
-11
@@ -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,19 +67,24 @@ 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,
|
||||
cursor = None
|
||||
page_count = 0
|
||||
|
||||
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())
|
||||
|
||||
if not result_json.get("objects"):
|
||||
raise ValueError(f"Item {item_id} not found (may have been deleted)")
|
||||
|
||||
obj = result_json["objects"][0]
|
||||
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", ""),
|
||||
@@ -90,8 +95,14 @@ async def get_item_by_id(item_id: str) -> dict:
|
||||
"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}")
|
||||
|
||||
Reference in new issue
Block a user