4949 manage_landing_page ,
5050)
5151from rs_server_catalog .user_handler import (
52+ adapt_links ,
53+ adapt_object_links ,
5254 add_user_prefix ,
5355 filter_collections ,
5456 get_user ,
55- remove_user_from_collection ,
56- remove_user_from_feature ,
5757 reroute_url ,
5858)
5959from rs_server_catalog .utils import (
@@ -137,28 +137,6 @@ def __init__(self, client: CoreCrudClient):
137137 self .client = client
138138 self .s3_files_to_be_deleted : list [str ] = []
139139
140- def remove_user_from_objects (self , content : dict , user : str , object_name : str ) -> dict :
141- """Remove the user id from the object.
142-
143- Args:
144- content (dict): The response content from the middleware
145- 'call_next' loaded in json format.
146- user (str): The user id to remove.
147- object_name (str): Precise the object type in the content.
148- It can be collections or features.
149-
150- Returns:
151- dict: The content with the user id removed.
152- """
153- objects = content [object_name ]
154- nb_objects = len (objects )
155- for i in range (nb_objects ):
156- if object_name == "collections" :
157- objects [i ] = remove_user_from_collection (objects [i ], user )
158- else :
159- objects [i ] = remove_user_from_feature (objects [i ], user )
160- return content
161-
162140 def clear_catalog_bucket (self , content : dict ):
163141 """Used to clear specific files from catalog bucket."""
164142 if not self .s3_handler :
@@ -174,47 +152,6 @@ def clear_catalog_bucket(self, content: dict):
174152 if not int (os .environ .get ("RSPY_LOCAL_CATALOG_MODE" , 0 )): # don't delete files if we are in local mode
175153 self .s3_handler .delete_file_from_s3 (bucket_name , file_key )
176154
177- def adapt_object_links (self , my_object : dict , user : str | None ) -> dict :
178- """adapt all the links from a collection so the user can use them correctly
179-
180- Args:
181- object (dict): The collection
182- user (str): The user id
183-
184- Returns:
185- dict: The collection passed in parameter with adapted links
186- """
187- links = my_object .get ("links" , [])
188- for j , link in enumerate (links ):
189- link_parser = urlparse (link ["href" ])
190- if "properties" in my_object : # If my_object is an item
191- new_path = add_user_prefix (link_parser .path , user , my_object ["collection" ], my_object ["id" ])
192- else : # If my_object is a collection
193- new_path = add_user_prefix (link_parser .path , user , my_object ["id" ])
194- links [j ]["href" ] = link_parser ._replace (path = new_path ).geturl ()
195- return my_object
196-
197- def adapt_links (self , content : dict , user : str | None , collection_id : str | None , object_name : str ) -> dict :
198- """adapt all the links that are outside from the collection section
199-
200- Args:
201- content (dict): The response content from the middleware
202- 'call_next' loaded in json format.
203- user (str): The user id.
204-
205- Returns:
206- dict: The content passed in parameter with adapted links
207- """
208- links = content ["links" ]
209- for link in links :
210- link_parser = urlparse (link ["href" ])
211- new_path = add_user_prefix (link_parser .path , user , collection_id )
212- link ["href" ] = link_parser ._replace (path = new_path ).geturl ()
213- # Go through each item and apply corrections to the links
214- for i in range (len (content [object_name ])):
215- content [object_name ][i ] = self .adapt_object_links (content [object_name ][i ], user )
216- return content
217-
218155 async def get_item_from_collection (self , request : Request ):
219156 """Get an item from the collection.
220157
@@ -692,10 +629,9 @@ async def manage_search_response(self, request: Request, response: StreamingResp
692629 body = [chunk async for chunk in response .body_iterator ]
693630 dec_content = b"" .join (map (lambda x : x if isinstance (x , bytes ) else x .encode (), body )).decode () # type: ignore
694631 content = json .loads (dec_content )
695- content = self .remove_user_from_objects (content , self .request_ids ["owner_id" ], "features" )
696- content = self .adapt_links (content , None , None , "features" )
632+ content = adapt_links (content , "features" )
697633 for collection_id in self .request_ids ["collection_ids" ]:
698- content = self . adapt_links (content , self .request_ids ["owner_id" ], collection_id , "features" )
634+ content = adapt_links (content , "features" , self .request_ids ["owner_id" ], collection_id )
699635
700636 # Add the stac authentication extension
701637 await self .add_authentication_extension (content )
@@ -1012,21 +948,18 @@ async def manage_get_response_content( # pylint: disable=too-many-locals, too-m
1012948 elif (
1013949 "/collections" in request .scope ["path" ] and "/items" not in request .scope ["path" ]
1014950 ): # /catalog/collections/owner_id:collection_id
1015- content = remove_user_from_collection (content , self .request_ids ["owner_id" ])
1016- content = self .adapt_object_links (content , self .request_ids ["owner_id" ])
951+ content = adapt_object_links (content , self .request_ids ["owner_id" ])
1017952 elif (
1018953 "/items" in request .scope ["path" ] and not self .request_ids ["item_id" ]
1019954 ): # /catalog/owner_id/collections/collection_id/items
1020- content = self .remove_user_from_objects (content , self .request_ids ["owner_id" ], "features" )
1021- content = self .adapt_links (
955+ content = adapt_links (
1022956 content ,
957+ "features" ,
1023958 self .request_ids ["owner_id" ],
1024959 self .request_ids ["collection_ids" ][0 ],
1025- "features" ,
1026960 )
1027961 elif self .request_ids ["item_id" ]: # /catalog/owner_id/collections/collection_id/items/item_id
1028- content = remove_user_from_feature (content , self .request_ids ["owner_id" ])
1029- content = self .adapt_object_links (content , self .request_ids ["owner_id" ])
962+ content = adapt_object_links (content , self .request_ids ["owner_id" ])
1030963 else :
1031964 logger .debug (f"No link adaptation performed for { request .scope } " )
1032965
@@ -1102,15 +1035,13 @@ async def manage_put_post_response(self, request: Request, response: StreamingRe
11021035 response_content = json .loads (b"" .join (body ).decode ()) # type: ignore
11031036 # Don't display geometry and bbox for default case since it was added just for compliance.
11041037 if request .scope ["path" ] == CATALOG_COLLECTIONS :
1105- response_content = remove_user_from_collection (response_content , user )
1106- response_content = self .adapt_object_links (response_content , user )
1038+ response_content = adapt_object_links (response_content , self .request_ids ["owner_id" ])
11071039 elif (
11081040 request .scope ["path" ]
11091041 == CATALOG_COLLECTIONS
11101042 + f"/{ user } _{ self .request_ids ['collection_ids' ][0 ]} /items/{ self .request_ids ['item_id' ]} "
11111043 ):
1112- response_content = remove_user_from_feature (response_content , user )
1113- response_content = self .adapt_object_links (response_content , user )
1044+ response_content = adapt_object_links (response_content , self .request_ids ["owner_id" ])
11141045 if response_content .get ("geometry" ) == DEFAULT_GEOM :
11151046 response_content ["geometry" ] = None
11161047 if response_content .get ("bbox" ) == DEFAULT_BBOX :
0 commit comments