diff --git a/alta/alta_connector.py b/alta/alta_connector.py index 5e69d064102afca4a7de8dbbfbf0c4fdd7fa311e..c514c2697f2e0c252167d62a8c3c15fa8653a1da 100644 --- a/alta/alta_connector.py +++ b/alta/alta_connector.py @@ -9,6 +9,7 @@ from typing import Union, Optional class alta_connector: name = "alta" + archive = "apertif" def basket_item_to_pandas( self, basket_item: Union[dict, pd.Series], validate: bool = True @@ -67,7 +68,7 @@ class alta_connector: """ item_data = json.loads(basket_item["item_data"]) - if "archive" in item_data and item_data["archive"] == "apertif": + if "archive" in item_data and item_data["archive"] == self.archive: if return_loaded: return item_data else: diff --git a/alta/alta_example.py b/alta/alta_example.py index e3d18c5ae4806abb17e933cbffcd6f88f4ef6449..14ec71d6c81bfc0a78ea7b3200035c10e7f7b4dc 100644 --- a/alta/alta_example.py +++ b/alta/alta_example.py @@ -1,27 +1,17 @@ from shopping_client import shopping_client from alta import alta_connector -esap_api_host = "http://localhost:5555/" -access_token = "eyJraWQiOiJyc2ExIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiIyYzQ5YjQ2OS1kN2FkLTRlNTktYTUwMy1jYWRiYWU5YmQzMGEiLCJuYmYiOjE2MjYxNjc3NDEsInNjb3BlIjoib3BlbmlkIGVtYWlsIHByb2ZpbGUiLCJpc3MiOiJodHRwczpcL1wvaWFtLWVzY2FwZS5jbG91ZC5jbmFmLmluZm4uaXRcLyIsImV4cCI6MTYyNjE3MTM0MSwiaWF0IjoxNjI2MTY3NzQxLCJqdGkiOiJiMjFlNWE2MC00ZGQ3LTQxNjQtODk3ZS1jMDI0OTEwYjBkZmEiLCJjbGllbnRfaWQiOiI2NjlkN2JlZi0zMmMwLTQ5ODAtYWUzNS1kOGVkZTU2YmQ1ZWYifQ.UNbZkINze8ZgU5MtfAdUQxn7CmTzHrjEGNxYeFsEhtMQSxBCAid6anlOaCppvuegRGTqNAB0XUTOAedtTyWh3X9c-M3jWUTcjAzaeIehYRnv1d0NzjCcQay5UcQ0G5QQ3bDIWqk-iiY-SGDsb-ODiykkrTo-pNoLLtCAiO9ClhQ" -client = shopping_client(host=esap_api_host,token=access_token) - -# read the basket as is, providing a token -try: - basket = client.get_basket() - print(basket) -except: - print('no basket found') - -# use the alta connector +esap_api_host = "https://sdc-dev.astron.nl:5555/" # Instantiate alta connector ac = alta_connector() # Instantiate ESAP User Profile shopping client, passing alta connector -sc = shopping_client(host=esap_api_host, token=access_token, connectors=[ac]) +#sc = shopping_client(host=esap_api_host, token=access_token, connectors=[ac]) +sc = shopping_client(host=esap_api_host, connectors=[ac]) # Retrieve basket (prompts to enter access token obtained from ESAP GUI) -basket=sc.get_basket(convert_to_pandas=False) +basket=sc.get_basket(filter_archives=True) print(basket) for item in basket: diff --git a/shopping_client/shopping_client.py b/shopping_client/shopping_client.py index 45e4dc396511f8663e730e1ab40745d37919d115..075700aed33ef1d942584efec99acd3d10f424d3 100644 --- a/shopping_client/shopping_client.py +++ b/shopping_client/shopping_client.py @@ -37,7 +37,7 @@ class shopping_client: self.basket = None def get_basket( - self, convert_to_pandas: bool = False, reload: bool = False + self, convert_to_pandas: bool = False, reload: bool = False, filter_archives: bool = False ) -> Union[list, pd.DataFrame, None]: """Retrieve the shopping basket for a user. Prompts for access token if one was not supplied to constructor. @@ -57,6 +57,11 @@ class shopping_client: If `True` a fresh query is issued to the ESAP API to refresh the basket contents. + filter_archives : bool + If `True` then the items are checked for an 'archive' value. + If this archive matches the 'archive' property of the provided connector + then the item is handled further, otherwise it is ignored. + Returns ------- Union[list, pd.DataFrame, None] @@ -72,8 +77,13 @@ class shopping_client: ] else: return None + + if filter_archives: + self.basket = self._filter_on_archive() + if convert_to_pandas: return self._basket_to_pandas() + return self.basket def _request_header(self): @@ -82,6 +92,21 @@ class shopping_client: return dict(Accept="application/json", Authorization=f"Bearer {self.token}") + # filter on items belonging to the provided connectors + def _filter_on_archive(self): + filtered_items = [] + if len(self.connectors): + + for item in self.basket: + item_data = json.loads(item["item_data"]) + + for connector in self.connectors: + if "archive" in item_data and item_data["archive"] == connector.archive: + filtered_items.append(item) + + return filtered_items + + def _basket_to_pandas(self): if len(self.connectors): converted_basket = { diff --git a/zooniverse/zooniverse.py b/zooniverse/zooniverse.py index a2731f37fe112ff55f35d2ad62acfe2b1e1fb3ae..7d94dae18a9549a67b89cf061f96d9d52b72d32b 100644 --- a/zooniverse/zooniverse.py +++ b/zooniverse/zooniverse.py @@ -13,6 +13,7 @@ from panoptes_client.panoptes import PanoptesAPIException class zooniverse: name = "zooniverse" + archive = "zooniverse" entity_types = {"workflow": Workflow, "project": Project} category_converters = { "subjects": dict(metadata=json.loads, locations=json.loads),