Skip to content
Snippets Groups Projects
Commit d934c2f4 authored by Hugh Dickinson's avatar Hugh Dickinson
Browse files

Merge branch 'alta-connector' into 'master'

adding 'filter_on_archive' function to shopping_client

See merge request astron-sdc/esap-userprofile-python-client!1
parents 709040fa 2b5dde24
Branches
Tags
No related merge requests found
......@@ -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:
......
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:
......
......@@ -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 = {
......
......@@ -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),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment