s
This commit is contained in:
1 parent
53f221884a
commit
734d43b817
1 file changed
+20
-32
+20
-32
@@ -54,7 +54,7 @@ async def get_item_by_id(item_id: str) -> dict:
|
||||
"""
|
||||
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:
|
||||
item_id: Square catalog item ID
|
||||
@@ -67,40 +67,28 @@ async def get_item_by_id(item_id: str) -> dict:
|
||||
Exception: If API error
|
||||
"""
|
||||
try:
|
||||
cursor = None
|
||||
page_count = 0
|
||||
# Use batch_get to retrieve item directly by ID
|
||||
result = await client.catalog.batch_get(
|
||||
object_ids=[item_id],
|
||||
include_related_objects=False,
|
||||
)
|
||||
result_json = json.loads(result.model_dump_json())
|
||||
|
||||
while True:
|
||||
page_count += 1
|
||||
print(f"[@DEBUG] Searching for item {item_id} (page {page_count})...")
|
||||
if not result_json.get("objects"):
|
||||
raise ValueError(f"Item {item_id} not found (may have been deleted)")
|
||||
|
||||
result = await client.catalog.search(
|
||||
object_types=["ITEM"],
|
||||
limit=1000,
|
||||
cursor=cursor,
|
||||
)
|
||||
result_json = json.loads(result.model_dump_json())
|
||||
obj = result_json["objects"][0]
|
||||
item_data = obj.get("item_data", {})
|
||||
|
||||
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)")
|
||||
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"),
|
||||
}
|
||||
|
||||
except ValueError:
|
||||
raise
|
||||
|
||||
Reference in new issue
Block a user