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