s
This commit is contained in:
1 parent
53f221884a
commit
734d43b817
1 file changed
+10
-22
+10
-22
@@ -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,24 +67,19 @@ 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],
|
||||||
while True:
|
include_related_objects=False,
|
||||||
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 result_json.get("objects"):
|
if not result_json.get("objects"):
|
||||||
for obj in result_json["objects"]:
|
raise ValueError(f"Item {item_id} not found (may have been deleted)")
|
||||||
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", ""),
|
||||||
@@ -95,13 +90,6 @@ 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:
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in new issue
Block a user