55
66from datetime import datetime
77from typing import TYPE_CHECKING , Any , Dict , List , Optional
8- from urllib .parse import urlencode
98
10- from pydantic import BaseModel , Field # field_validator
9+ from pydantic import BaseModel , Field # field_validator
1110
1211
1312if TYPE_CHECKING :
14- from opds2 .provider import DataProvider
13+ from pyopds2 .provider import DataProvider
1514
1615
1716class Link (BaseModel ):
1817 """Represents a link in OPDS 2.0.
19-
18+
2019 Links are used to associate resources with a publication or catalog.
2120 """
2221 href : str = Field (..., description = "URI or URI template of the linked resource" )
@@ -42,7 +41,7 @@ class Contributor(BaseModel):
4241
4342class Metadata (BaseModel ):
4443 """Metadata for a publication or catalog.
45-
44+
4645 Contains descriptive information about the resource.
4746 """
4847 title : str = Field (..., description = "Title of the resource" )
@@ -65,7 +64,7 @@ class Metadata(BaseModel):
6564
6665class Publication (BaseModel ):
6766 """Represents a publication in OPDS 2.0.
68-
67+
6968 A publication is a digital work (book, audiobook, etc.) with metadata and links.
7069 """
7170 metadata : Metadata = Field (..., description = "Metadata about the publication" )
@@ -93,13 +92,13 @@ class Navigation(BaseModel):
9392
9493class Catalog (BaseModel ):
9594 """Represents an OPDS 2.0 catalog/feed.
96-
95+
9796 A catalog is a collection of publications with optional navigation.
9897 """
9998 metadata : Metadata = Field (..., description = "Metadata about the catalog" )
10099 links : List [Link ] = Field (default_factory = list , description = "Links (self, search, etc.)" )
101100 publications : Optional [List [Publication ]] = Field (
102- None ,
101+ None ,
103102 description = "List of publications in this catalog"
104103 )
105104 navigation : Optional [List [Navigation ]] = Field (
@@ -144,7 +143,7 @@ def model_dump_json(self, **kwargs) -> str:
144143 # Use model_dump to get the dict with @context, then serialize to JSON
145144 data = self .model_dump (** kwargs )
146145 return json .dumps (data , default = str )
147-
146+
148147 def add_pagination (self , response : 'DataProvider.SearchResponse' ):
149148 """
150149 Add pagination to the current Catalog based on the SearchResponse.
@@ -196,7 +195,7 @@ def add_pagination(self, response: 'DataProvider.SearchResponse'):
196195
197196 @classmethod
198197 def create (
199- cls ,
198+ cls ,
200199 response : Optional ['DataProvider.SearchResponse' ] = None ,
201200 paginate : bool = True ,
202201 # Catalog properties
@@ -209,12 +208,12 @@ def create(
209208 ) -> 'Catalog' :
210209 """
211210 Create an OPDS Catalog, optionally from search results.
212-
211+
213212 Args:
214213 response: Optional SearchResponse for paginated search results
215214 paginate: Whether to add pagination links (requires data)
216215 """
217- metadata = metadata or Metadata ()
216+ metadata = metadata or Metadata (title = "OPDS Catalog" )
218217 links = links or []
219218 publications = publications or []
220219
@@ -229,10 +228,16 @@ def create(
229228
230229 if response :
231230 if publications :
232- raise ValueError ("Cannot specify both publications and response parameters - publications are generated from the response" )
233- catalog .publications = [record .to_publication () for record in response .records ]
234-
235- if paginate :
231+ raise ValueError (
232+ "Cannot specify both publications and response "
233+ "parameters - publications are generated from "
234+ "the response"
235+ )
236+ catalog .publications = [
237+ record .to_publication () for record in response .records
238+ ]
239+
240+ if paginate :
236241 catalog .add_pagination (response )
237242
238243 return catalog
0 commit comments