@@ -50,20 +50,18 @@ async def get_collection(
5050 async def get_item (
5151 self , * , request : Request , item_id : str , collection_id : str , ** kwargs : Any
5252 ) -> Item :
53- client = cast (DuckdbClient , request .state .client )
54- hrefs = cast (dict [str , str ], request .state .hrefs )
55- if href := hrefs .get (collection_id ):
56- item_collection = client .search (
57- href , ids = [item_id ], collections = [collection_id ], ** kwargs
58- )
59- if len (item_collection ["features" ]) == 1 :
60- return Item (** item_collection ["features" ][0 ])
61- else :
62- raise NotFoundError (
63- f"Item does not exist: { item_id } in collection { collection_id } "
64- )
53+ item_collection = await self .get_search (
54+ request = request ,
55+ ids = [item_id ],
56+ collections = [collection_id ],
57+ ** kwargs ,
58+ )
59+ if len (item_collection ["features" ]) == 1 :
60+ return Item (** item_collection ["features" ][0 ])
6561 else :
66- raise NotFoundError (f"Collection does not exist: { collection_id } " )
62+ raise NotFoundError (
63+ f"Item does not exist: { item_id } in collection { collection_id } "
64+ )
6765
6866 async def get_search (
6967 self ,
@@ -189,6 +187,9 @@ async def search(
189187 href ,
190188 ** search_dict ,
191189 )
190+ item_collection ["features" ] = [
191+ self .item_with_links (item , request ) for item in item_collection ["features" ]
192+ ]
192193 num_items = len (item_collection ["features" ])
193194 limit = int (search_dict .get ("limit" , None ) or num_items )
194195 offset = int (search_dict .get ("offset" , None ) or 0 )
@@ -249,6 +250,40 @@ async def search(
249250 item_collection ["links" ] = links
250251 return ItemCollection (** item_collection )
251252
253+ def item_with_links (self , item : dict [str , Any ], request : Request ) -> dict [str , Any ]:
254+ links = [
255+ {
256+ "href" : str (request .url_for ("Landing Page" )),
257+ "rel" : "root" ,
258+ "type" : "application/json" ,
259+ },
260+ ]
261+ if collection_id := item .get ("collection" ):
262+ href = str (request .url_for ("Get Collection" , collection_id = collection_id ))
263+ links .append (
264+ {"href" : href , "rel" : "collection" , "type" : "application/json" }
265+ )
266+ links .append ({"href" : href , "rel" : "parent" , "type" : "application/json" })
267+ if item_id := item .get ("id" ):
268+ links .append (
269+ {
270+ "href" : str (
271+ request .url_for (
272+ "Get Item" ,
273+ collection_id = collection_id ,
274+ item_id = item_id ,
275+ )
276+ ),
277+ "rel" : "self" ,
278+ "type" : "application/geo+json" ,
279+ }
280+ )
281+ for link in item .get ("links" , []):
282+ if link ["rel" ] not in ("root" , "parent" , "collection" , "self" ):
283+ links .append (link )
284+ item ["links" ] = links
285+ return item
286+
252287
253288def collection_with_links (
254289 collection : dict [str , Any ], request : Request
0 commit comments