pagination fix
This commit is contained in:
1 parent
6c0d15dec9
commit
424d7795bc
1 file changed
+55
-31
+55
-31
@@ -125,42 +125,66 @@ async def get_item_by_variation_id(variation_id: str) -> dict:
|
||||
# Search LEGO category items to find which one has this variation
|
||||
print(f"[@DEBUG] Searching for variation {variation_id} in LEGO category")
|
||||
|
||||
result = await client.catalog.search(
|
||||
object_types=["ITEM"],
|
||||
query={
|
||||
"exact_query": {
|
||||
"attribute_name": "category_id",
|
||||
"attribute_value": LEGO_CAT_ID,
|
||||
}
|
||||
},
|
||||
limit=1000,
|
||||
)
|
||||
result_json = json.loads(result.model_dump_json())
|
||||
cursor = None
|
||||
page_count = 0
|
||||
|
||||
if not result_json.get("objects"):
|
||||
raise ValueError(f"No items found in LEGO category")
|
||||
while True:
|
||||
page_count += 1
|
||||
print(f"[@DEBUG] Searching page {page_count}...")
|
||||
|
||||
# Search through items to find which has this variation
|
||||
for obj in result_json["objects"]:
|
||||
item_data = obj.get("item_data", {})
|
||||
variations = item_data.get("variations", [])
|
||||
|
||||
for variation in variations:
|
||||
if variation.get("id") == variation_id:
|
||||
# Found the parent item!
|
||||
print(f"[@DEBUG] Found parent item: {obj['id']}")
|
||||
return {
|
||||
"id": obj["id"],
|
||||
"name": item_data.get("name", ""),
|
||||
"description": item_data.get("description", ""),
|
||||
"category_id": item_data.get("category_id"),
|
||||
"variations": variations,
|
||||
"updated_at": obj.get("updated_at"),
|
||||
"version": obj.get("version"),
|
||||
search_params = {
|
||||
"object_types": ["ITEM"],
|
||||
"query": {
|
||||
"exact_query": {
|
||||
"attribute_name": "category_id",
|
||||
"attribute_value": LEGO_CAT_ID,
|
||||
}
|
||||
},
|
||||
"limit": 1000,
|
||||
}
|
||||
|
||||
if cursor:
|
||||
search_params["cursor"] = cursor
|
||||
|
||||
result = await client.catalog.search(**search_params)
|
||||
result_json = json.loads(result.model_dump_json())
|
||||
|
||||
if not result_json.get("objects"):
|
||||
if page_count == 1:
|
||||
raise ValueError(f"No items found in LEGO category")
|
||||
else:
|
||||
# No more results
|
||||
break
|
||||
|
||||
# Search through items on this page to find which has this variation
|
||||
for obj in result_json["objects"]:
|
||||
item_data = obj.get("item_data", {})
|
||||
variations = item_data.get("variations", [])
|
||||
|
||||
for variation in variations:
|
||||
if variation.get("id") == variation_id:
|
||||
# Found the parent item!
|
||||
print(
|
||||
f"[@DEBUG] Found parent item: {obj['id']} on page {page_count}"
|
||||
)
|
||||
return {
|
||||
"id": obj["id"],
|
||||
"name": item_data.get("name", ""),
|
||||
"description": item_data.get("description", ""),
|
||||
"category_id": item_data.get("category_id"),
|
||||
"variations": 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:
|
||||
# No more pages
|
||||
break
|
||||
|
||||
raise ValueError(
|
||||
f"Variation {variation_id} not found in any LEGO category item"
|
||||
f"Variation {variation_id} not found in any LEGO category item (searched {page_count} pages)"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in new issue
Block a user