Skip to content

Commit 347f72a

Browse files
committed
cleanup parsing
1 parent 17ad2d0 commit 347f72a

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

openlibrary/fastapi/partials.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,39 @@ async def partials_endpoint(
3939
4040
For other components, uses the data parameter (JSON).
4141
"""
42-
# Handle BPListsSection with separate query parameters
43-
if _component == "BPListsSection":
44-
parsed_data = {"workId": workId, "editionId": editionId}
45-
elif _component == "FulltextSearchSuggestion":
46-
# For FulltextSearchSuggestion, the data query param is just a string, not a dict
47-
parsed_data = data if data else ""
48-
elif data:
49-
# For other components, parse JSON data
42+
# Parse JSON data for components that need it
43+
parsed_data: dict | None = None
44+
if data and _component in ("SearchFacets", "AffiliateLinks"):
5045
try:
5146
parsed_data = json.loads(data)
5247
except json.JSONDecodeError:
5348
raise HTTPException(
5449
status_code=400, detail="Invalid JSON in data parameter"
5550
)
56-
else:
57-
raise HTTPException(status_code=400, detail="Missing required data parameter")
5851

5952
if _component == "SearchFacets":
53+
if not parsed_data:
54+
raise HTTPException(
55+
status_code=400, detail="Missing required data parameter"
56+
)
6057
return SearchFacetsPartial(data=parsed_data).generate()
61-
elif _component == "BPListsSection":
62-
return BookPageListsPartial(data=parsed_data).generate()
58+
6359
elif _component == "AffiliateLinks":
60+
if not parsed_data:
61+
raise HTTPException(
62+
status_code=400, detail="Missing required data parameter"
63+
)
6464
return AffiliateLinksPartial(data=parsed_data).generate()
65+
66+
elif _component == "BPListsSection":
67+
# Use separate query parameters
68+
return BookPageListsPartial(
69+
data={"workId": workId, "editionId": editionId}
70+
).generate()
71+
6572
elif _component == "FulltextSearchSuggestion":
66-
return FullTextSuggestionsPartial(data=parsed_data).generate()
73+
# data is just a string, not JSON
74+
return FullTextSuggestionsPartial(data=data or "").generate()
75+
6776
else:
6877
raise HTTPException(status_code=400, detail=f"Unknown component: {_component}")

0 commit comments

Comments
 (0)