Skip to content

Commit 7b42424

Browse files
committed
Introduce Search class for query handling (#23)
Added a Search dataclass to facilitate search queries.
1 parent da53b5c commit 7b42424

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

pyopds2/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Navigation,
1515
Publication,
1616
)
17-
from pyopds2.provider import DataProvider, DataProviderRecord
17+
from pyopds2.provider import DataProvider, DataProviderRecord, Search
1818

1919
__all__ = [
2020
"Catalog",
@@ -25,4 +25,5 @@
2525
"Metadata",
2626
"Navigation",
2727
"Publication",
28+
"Search",
2829
]

pyopds2/provider.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Data providers implement the logic for searching and retrieving publications.
44
"""
55

6+
67
from abc import ABC, abstractmethod
8+
from collections.abc import Mapping
79
from dataclasses import dataclass
810
import functools
911
from typing import List, Optional
@@ -44,6 +46,28 @@ def to_publication(self) -> Publication:
4446
)
4547

4648

49+
class Search(BaseModel, Mapping):
50+
query: str
51+
limit: int = 50
52+
offset: int = 0
53+
sort: Optional[str] = None
54+
55+
def __iter__(self):
56+
"""Allows **Search(...) to unpack into a dict for DataProvider.search(**s)"""
57+
return iter(self.model_dump())
58+
59+
def __getitem__(self, item):
60+
return getattr(self, item)
61+
62+
def __len__(self):
63+
return len(self.model_fields)
64+
65+
@property
66+
def params(self) -> dict[str, str]:
67+
d = self.model_dump(exclude_none=True)
68+
return {k: str(v) for k, v in d.items()}
69+
70+
4771
class DataProvider(ABC):
4872
"""Abstract base class for OPDS 2.0 data providers.
4973
@@ -112,9 +136,9 @@ def has_more(self) -> bool:
112136
"""Determine if there are more results beyond the current page."""
113137
return (self.offset + self.limit) < self.total
114138

115-
@staticmethod
116-
@abstractmethod
139+
@classmethod
117140
def search(
141+
cls,
118142
query: str,
119143
limit: int = 50,
120144
offset: int = 0,
@@ -133,8 +157,8 @@ def search(
133157
Returns:
134158
SearchResponse object containing search results
135159
"""
136-
return DataProvider.SearchResponse(
137-
DataProvider,
160+
return cls.SearchResponse(
161+
cls,
138162
records=[],
139163
total=0,
140164
query=query,

0 commit comments

Comments
 (0)