async def submit_query_async(querystr: str, auth_required=False): """Submit a query to the CE (async). Arguments: querystr: The query to be submitted auth_required: If true, send an authentication key with this request. Don't send a key if the global config.server_auth_required is false """ q = {"query": querystr} headers = {} if auth_required and config.server_auth_required: token = config.jwt_token headers["Authorization"] = f"Bearer {token}" r = requests.post(config.host, json=q, headers=headers) try: r.raise_for_status() except requests.exceptions.HTTPError: print("error") print(r.content) try: resp = r.json() if "errors" in resp.keys(): raise QueryException(resp['errors']) except ValueError: raise QueryException(r.content) return resp
def submit_query(querystr: str, auth_required=False): """Submit a query to the CE. Args: querystr: The query to be submitted auth_required: If ``True``, send an authentication key with this request. Required if the CE has authentication enabled and you are sending a mutation. Not required for queries. The request is not authenticated if the ``auth.required`` config item is ``False`` """ q = {"query": querystr} headers = {} if auth_required and config.server_auth_required: token = config.jwt_token headers["Authorization"] = f"Bearer {token}" r = requests.post(config.host, json=q, headers=headers) try: r.raise_for_status() except requests.exceptions.HTTPError: print("error") print(r.content) try: resp = r.json() if "errors" in resp.keys(): raise QueryException(resp['errors']) except ValueError: raise QueryException([{"message": r.content}]) return resp
def create_listitem_node(name: str, contributor: str = None, creator: str = None, description: str = None, position: Optional[int] = None): """Create a ListItem object and return the corresponding identifier. (https://schema.org/ListItem) Arguments: name: The name of the ListItem object. contributor: A person, an organization, or a service responsible for contributing the ListItem to the web resource. creator: The person, organization or service who created the ListItem. description: The description of the ItemList object position: the position of the ListItem Raises: QueryException if the query fails to execute Returns: The identifier of the ListItem object created """ mutation = mutations_itemlist.mutation_create_listitem( contributor=contributor, name=name, creator=creator, description=description, position=position) resp = submit_query(mutation) result = resp.get("data", {}).get("CreateListItem") if result: element_id = result["identifier"] else: raise QueryException(resp['errors']) return element_id
def merge_itemlist_itemlistelement_nodes(itemlist_id: str, element_id: str): """Add a ThingInterface in an ItemList object based on the identifiers. (https://schema.org/itemListElement) Arguments: itemlist_id: The unique identifier of the ItemList object. element_id: The unique identifier of the ThingInterface object. Raises: QueryException if the query fails to execute """ mutation = mutations_itemlist.mutation_add_itemlist_itemlist_element( itemlist_id=itemlist_id, element_id=element_id) resp = submit_query(mutation) result = resp.get("data", {}).get("MergeItemListItemListElement") if not result: raise QueryException(resp['errors'])
def merge_sequence_listitem_nextitem_nodes(listitem_ids: list): """Add a sequence of NextItem to a ListItem objects based on the identifiers. (https://schema.org/nextItem) Arguments: listitem_ids: The list of unique identifiers of the ListItem objects. Raises: QueryException if the query fails to execute """ mutation = mutations_itemlist.mutation_sequence_add_listitem_nextitem( listitem_ids=listitem_ids) resp = submit_query(mutation) result = resp.get("data", {}) if not result: raise QueryException(resp['errors'])
async def create_entrypointcontrolaction_CE( created_app_id, entrypoint_name, contributor, subject, description_ep, creator, source, language, actionPlatform, contentType, encodingType, formatin, control_name, description_ca, actionStatus): """ Creates an entry point and control action in the contributor environment and linkss the two to the application. Arguments: created_app_id: The id of the application that the entry point and control actions should be linked to. Entry point arguments: entrypoint_name: The name of the entry point. source: The URL of the web resource to be represented by the node. description: A description of the application. formatin: The format of the input files to be used for the application. subject: The subject associated with the application. contributor: A person, an organization, or a service responsible for adding the software application. This can be either a name or a base URL. creator: The person, organization or service responsible for adding the software application. actionPlatform: The action platform. contentType: The content type associated with the entry point, should be a mimetype. encodingType: The encoding type associated with the entry point, should be a mimetype. Controal Action arguments: control_name: the name of the control action. actionStatus: The default actionStatus for a newly instantiated ControlAction 'job'. """ create_entrypoint_query = mutation_create_entry_point(entrypoint_name, contributor, subject, description_ep, creator, \ source, language, actionPlatform, contentType, encodingType, formatin) resp = await submit_query(create_entrypoint_query) created_ep_id = resp['data']['CreateEntryPoint']['identifier'] add_entrypoint_query = mutation_add_entrypoint_application( created_app_id, created_ep_id) resp = await submit_query(add_entrypoint_query) created_control_action = mutation_create_controlaction( control_name, description_ca, actionStatus) resp = await submit_query(created_control_action) created_ca_id = resp['data']['CreateControlAction']['identifier'] add_entrypoint_controlaction_query = mutation_add_entrypoint_controlaction( created_ep_id, created_ca_id) resp = await submit_query(add_entrypoint_controlaction_query) if "errors" in resp.keys(): raise QueryException(resp['errors']) return created_ep_id, created_ca_id
def create_musicplaylist_node(title: str, contributor: str, creator: str, source: str, format_: str, name: str = None, language: str = None, num_tracks: int = None): """Create a MusicPlaylist object and return the corresponding identifier. (https://schema.org/MusicPlaylist) Arguments: name: The name of the MusicPlaylist object. creator: The person, organization or service who created the thing the web resource is about. contributor: A person, an organization, or a service responsible f for contributing the MusicPlaylist to the web resource. This can be either a name or a base URL. format_: A MimeType of the format of the page describing the MusicPlaylist. source: The URL of the web resource about this MusicPlaylist. title: The title of the resource indicated by `source` numTracks: The number of tracks in the MusicPlaylist Raises: QueryException if the query fails to execute Returns: The identifier of the MusicPlaylist object created """ mutation = mutations_playlist.mutation_create_musicplaylist( title=title, contributor=contributor, creator=creator, source=source, format_=format_, name=name, language=language, num_tracks=num_tracks) resp = submit_query(mutation) result = resp.get("data", {}).get("CreateMusicPlaylist") if result: playlist_id = result["identifier"] else: raise QueryException(resp['errors']) return playlist_id
def update_listitem_position(listitem_id: str, position: int): """ Submit a mutation which changes the value of the position field on a given ListItem Arguments: listitem_id: the CE identifier of a ListItem position: The value to set the position field to Raises: QueryException if the query fails to execute """ mutation = mutations_itemlist.mutation_update_listitem( identifier=listitem_id, position=position) resp = submit_query(mutation) result = resp.get("data", {}).get("UpdateListItem") if not result: raise QueryException(resp['errors'])
def merge_listitem_nextitem_nodes(listitem_id: str, nextitem_id: str): """Add a NextItem to a ListItem object based on the identifier. (https://schema.org/nextItem) Arguments: listitem_id: The unique identifier of the ListItem object. nextitem_id: The unique identifier of the NextItem object. Raises: QueryException if the query fails to execute """ mutation = mutations_itemlist.mutation_add_listitem_nextitem( listitem_id=listitem_id, nextitem_id=nextitem_id) resp = submit_query(mutation) result = resp.get("data", {}).get("MergeListItemNextItem") if not result: raise QueryException(resp['errors'])
def itemlist_node_exists(itemlist_id: str): """ Check if ItemList already exists in the CE Arguments: itemlist_id: The unique identifiers of the ItemList object Raises: QueryException if the query fails to execute Return: True if ListItem object exists """ query = query_itemlist(identifier=itemlist_id) resp = submit_query(query) result = resp.get("data", {}).get("ItemList") if not result: raise QueryException(resp['errors']) else: return result
def merge_musicplaylist_itemlist(playlist_id: str, itemlist_id: str): """Add a ItemList to a MusicPlaylist based on the identifiers. (https://schema.org/MusicPlaylist) Arguments: playlist_id: the MusicPlaylist identifier. itemlist_id: the ItemList identifier. Raises: QueryException if the query fails to execute """ mutation = mutations_playlist.mutation_merge_musicplaylist_itemlist( playlist_identifier=playlist_id, itemlist_identifier=itemlist_id) resp = submit_query(mutation) result = resp.get("data", {}) if not result: raise QueryException(resp['errors'])
def merge_sequence_listitem_item_nodes(listitem_ids: list, item_ids: list): """Add a sequence of Items to a ListItems objects based on the identifiers. (https://schema.org/item) Arguments: listitem_ids: The list of unique identifier of the ListItem object. item_ids: The list of unique identifier of the Item object. Raises: QueryException if the query fails to execute ValueError if not all the Items passed as input are found """ mutation = mutations_itemlist.mutation_sequence_add_listitem_item( listitem_ids=listitem_ids, item_ids=item_ids) resp = submit_query(mutation) result = resp.get("data", {}) if not result: raise QueryException(resp['errors']) elif len(result.keys()) != len(listitem_ids): raise ValueError( "Number of Item objects founds does not match with input list")
def merge_sequence_itemlist_itemlistelement_nodes(itemlist_id: str, element_ids: list): """Add a sequence of ListItem objects to a ItemList object. Arguments: itemlist_id: The unique identifier of the ItemList object. element_ids: The list of unique identifier of the ThingInterface object. Raises: QueryException if the query fails to execute ValueError if not all the ListItems passed as input are found """ mutation = mutations_itemlist.mutation_sequence_add_itemlist_itemlist_element( itemlist_id=itemlist_id, element_ids=element_ids) resp = submit_query(mutation) result = resp.get("data", {}) if not result: raise QueryException(resp['errors']) elif len(result.keys()) != len(element_ids): raise ValueError( "Number of ListItem objects founds does not match with input list")
def create_itemlist_node(name: str, contributor: str = None, creator: str = None, description: str = None, ordered: bool = False): """Create a ItemList object and return the corresponding identifier. (https://schema.org/ItemList) Arguments: name: The name of the ItemList object. contributor: A person, an organization, or a service responsible for contributing the ItemList to the web resource. creator: The person, organization or service who created the ItemList. description: The description of the ItemList object ordered: The type of ordering for the list (ascending, descending, unordered, ordered) Raises: QueryException if the query fails to execute Returns: The identifier of the ItemList object created """ itemlistorder = ItemListOrderType.ItemListUnordered if ordered: itemlistorder = ItemListOrderType.ItemListOrderAscending mutation = mutations_itemlist.mutation_create_itemlist( contributor=contributor, creator=creator, name=name, itemlistorder=itemlistorder, description=description) resp = submit_query(mutation) result = resp.get("data", {}).get("CreateItemList") if result: itemlist_id = result["identifier"] else: raise QueryException(resp['errors']) return itemlist_id
def get_nonexistent_listitem_nodes(item_ids: list): """ Check if Items already exist in the CE Arguments: item_ids: The list of unique identifiers of the Item objects Raises: QueryException if the query fails to execute Return: Set of identifiers not found """ query = query_listitems(identifiers=item_ids) resp = submit_query(query) result = resp.get("data", {}).get("ThingInterface") not_found = set() if not result: raise QueryException(resp['errors']) elif len(result) != len(item_ids): not_found = set(item_ids) - set( [item['identifier'] for item in result]) return not_found
def create_sequence_listitem_nodes(name: str, listitems: list, ids_mode: bool, contributor: str): """Create a sequence of ListItem object and return the corresponding identifiers. (https://schema.org/ListItem) Arguments: listitems: the ListItems objects to create ids_mode: the type of Items to create (from ID or from string value) contributor: A person, an organization, or a service responsible for contributing the ListItem to the web resource. name: The name of the ListItem object. Raises: QueryException if the query fails to execute Returns: The identifiers of the ListItem objects created. """ if not ids_mode: description = [item for item in listitems] else: description = [None for item in listitems] mutation = mutations_itemlist.mutation_sequence_create_listitem( name=name, listitems=listitems, description=description, contributor=contributor) resp = submit_query(mutation) result = resp.get("data", {}) if len(result.keys()) != len(listitems): raise QueryException(resp['errors']) else: listitems_ids = [ result[listalias]['identifier'] for listalias in result ] return listitems_ids