dssfgfds
This commit is contained in:
1 parent
da382942d3
commit
5f4bbb2565
1 file changed
+18
-17
+18
-17
@@ -54,6 +54,8 @@ 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.
|
||||
|
||||
Args:
|
||||
item_id: Square catalog item ID
|
||||
|
||||
@@ -61,30 +63,21 @@ async def get_item_by_id(item_id: str) -> dict:
|
||||
dict: Item details including name, category_id, variations, etc.
|
||||
|
||||
Raises:
|
||||
Exception: If item not found or API error
|
||||
ValueError: If item not found
|
||||
Exception: If API error
|
||||
"""
|
||||
try:
|
||||
# Search for item by searching all items and filtering
|
||||
# This is not ideal but the async client lacks direct retrieval
|
||||
result = await client.catalog.search(
|
||||
object_types=["ITEM"],
|
||||
limit=1000, # Broad search
|
||||
# Use batch retrieve to get item directly by ID
|
||||
result = await client.catalog.batch_retrieve(
|
||||
object_ids=[item_id],
|
||||
include_related_objects=False,
|
||||
)
|
||||
result_json = json.loads(result.model_dump_json())
|
||||
|
||||
if not result_json.get("objects"):
|
||||
raise ValueError(f"No items found")
|
||||
|
||||
# Find the specific item by ID
|
||||
obj = None
|
||||
for item in result_json["objects"]:
|
||||
if item["id"] == item_id:
|
||||
obj = item
|
||||
break
|
||||
|
||||
if not obj:
|
||||
raise ValueError(f"Item {item_id} not found")
|
||||
raise ValueError(f"Item {item_id} not found (may have been deleted)")
|
||||
|
||||
obj = result_json["objects"][0]
|
||||
item_data = obj.get("item_data", {})
|
||||
|
||||
return {
|
||||
@@ -97,6 +90,9 @@ async def get_item_by_id(item_id: str) -> dict:
|
||||
"version": obj.get("version"),
|
||||
}
|
||||
|
||||
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}")
|
||||
raise
|
||||
@@ -240,7 +236,12 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None):
|
||||
print(
|
||||
f"[@DEBUG] Fetching fresh item data before update to get current version..."
|
||||
)
|
||||
try:
|
||||
fresh_item = await get_item_by_id(item_id)
|
||||
except ValueError as e:
|
||||
# Item was deleted between webhook and enrichment
|
||||
print(f"[@WARNING] {e} - skipping update (item may have been deleted)")
|
||||
return
|
||||
|
||||
variations = fresh_item.get("variations", [])
|
||||
|
||||
|
||||
Reference in new issue
Block a user