From f30d96aaafc2f400930cf33534275d9bf21b8701 Mon Sep 17 00:00:00 2001 From: Carolina Catorze Date: Thu, 23 May 2024 13:26:17 +0000 Subject: [PATCH] Merge branch 'develop' into 'main' Develop See merge request cpes/european-projects/enershare/tsg-client!75 --- examples/get_administrative_users.py | 47 +++++++++++++++++++ examples/update_administrative_user.py | 60 +++++++++++++++++++++++++ tsg_client/controllers/TSGController.py | 21 ++++++--- tsg_client/utils/contracts.py | 46 +++++++++++++++++++ 4 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 examples/get_administrative_users.py create mode 100644 examples/update_administrative_user.py create mode 100644 tsg_client/utils/contracts.py diff --git a/examples/get_administrative_users.py b/examples/get_administrative_users.py new file mode 100644 index 0000000..95945bd --- /dev/null +++ b/examples/get_administrative_users.py @@ -0,0 +1,47 @@ +""" + +Example - Get the administrative users from your connector + +Last update: 2024-04-16 + +This request gets the administrative users of your connector using a connection to the connector. +It uses a pre-established connection from the examples request to our connector. + +The following operations are demonstrated: + + 1. Load environment variables (your connector configs) from a `.env` file. + 2. Establish a connection to your TSG connector. + 3. Gets the administrative users of your connector. + + +Important: + + - Ensure that the required environment variables (Your Connector `API_KEY`, `CONNECTOR_ID`, `ACCESS_URL` and `AGENT_ID`) are set in the .env file before using this request. + + - The connector `API_KEY` can be retrieved by loging into the TSG connector UI and navigating to the 'API Keys' tab. + +Execute the code below to get the administrative users of your connector. +""" + + +if __name__ == "__main__": + from loguru import logger + from dotenv import dotenv_values + from tsg_client.controllers import TSGController + + # Comment the line below to enable internal logger: + logger.disable("") + + # Load environment variables: + config = dotenv_values('.env') + + # Connect to our TSG connector: + conn = TSGController( + api_key=config['API_KEY'], + connector_id=config['CONNECTOR_ID'], + access_url=config['ACCESS_URL'], + agent_id=config['AGENT_ID'] + ) + + res = conn.get_administrative_users() + print(res) diff --git a/examples/update_administrative_user.py b/examples/update_administrative_user.py new file mode 100644 index 0000000..a1a0cf0 --- /dev/null +++ b/examples/update_administrative_user.py @@ -0,0 +1,60 @@ +""" + +Example - Update an administrative user from your connector + +Last update: 2024-03-28 + +This request updates an administrative user from your connector on a custom connector using a connection to the connector. +It uses a pre-established connection from the examples request to our connector. + +The following operations are demonstrated: + + 1. Load environment variables (your connector configs) from a `.env` file. + 2. Establish a connection to your TSG connector. + 3. Updates the administrative user from your connector. + + +Important: + + - Ensure that the required environment variables (Your Connector `API_KEY`, `CONNECTOR_ID`, `ACCESS_URL` and `AGENT_ID`) are set in the .env file before using this request. + + - The connector `API_KEY` can be retrieved by loging into the TSG connector UI and navigating to the 'API Keys' tab. + +Execute the code below to publish a new administrative user on your connector. + +Ensure that the required parameters are specified before executing the request: + + - id: The id of the user (username). + - new_password: The new secret and secure password. + - new_roles: The new list of roles this user should have. (optional) + +Note: The update method creates if the username does not exist +""" + + +if __name__ == "__main__": + from loguru import logger + from dotenv import dotenv_values + from tsg_client.controllers import TSGController + + # Comment the line below to enable internal logger: + logger.disable("") + + # Load environment variables: + config = dotenv_values('.env') + + # Connect to our TSG connector: + conn = TSGController( + api_key=config['API_KEY'], + connector_id=config['CONNECTOR_ID'], + access_url=config['ACCESS_URL'], + agent_id=config['AGENT_ID'] + ) + + # Specify the required parameters: + id = "test_id_" + new_password = "test_password" + new_roles = ["ROLE_ADMIN"] + + res = conn.update_administrative_user(id, new_password, new_roles) + print(res) diff --git a/tsg_client/controllers/TSGController.py b/tsg_client/controllers/TSGController.py index 2a1cf51..e0a836f 100644 --- a/tsg_client/controllers/TSGController.py +++ b/tsg_client/controllers/TSGController.py @@ -71,16 +71,19 @@ def __validate_connection(self): except Exception as e: raise Exception(f"Error connecting to the TSG connector: {repr(e)}") - def get_connector_selfdescription(self, connector_id, access_url, agent_id=""): + def get_connector_selfdescription(self, + access_url, + agent_id="", + connector_id=""): """ Get self-descriptions from a connector from another dataspace participant, given its connector CONNECTOR_ID and ACCESS_URL. - :param connector_id: Connector ID - :type connector_id: str :param access_url: Access URL :type access_url: str - :param agent_id: Agent ID + :param connector_id: (optional) Connector ID + :type connector_id: str + :param agent_id: (optional) Agent ID :type agent_id: str :return: SelfDescription object """ @@ -454,7 +457,7 @@ def openapi_request( else: headers.update(_headers) - full_endpoint = f"{self.endpoints.OPEN_API}/{api_version}/{endpoint}" + full_endpoint = f"{self.endpoints.OPEN_API}/{api_version}{endpoint}" rsp = "" if method == "get": @@ -497,6 +500,14 @@ def query_metadata_broker(self): return rsp.json() + def get_administrative_users(self): + + # Get the Administrative Users: + rsp = self.controller.get( + endpoint=self.endpoints.AUTH_USERS_MANAGER) + + return rsp.json() + def new_administrative_user(self, id, password, roles): bytes = password.encode('utf-8') diff --git a/tsg_client/utils/contracts.py b/tsg_client/utils/contracts.py new file mode 100644 index 0000000..2adacd0 --- /dev/null +++ b/tsg_client/utils/contracts.py @@ -0,0 +1,46 @@ +import json +import os + +def update_and_save_contract(contract_id, contract_start, contract_end): + + contract_template = { + "@context" : { + "ids" : "https://w3id.org/idsa/core/", + "idsc" : "https://w3id.org/idsa/code/" + }, + "@type" : "ids:ContractOffer", + "@id" : f"https://w3id.org/idsa/autogen/contractOffer/{contract_id}", + "ids:permission" : [ { + "@type" : "ids:Permission", + "@id" : "https://w3id.org/idsa/autogen/permission/15f85a6b-f921-47fd-b541-3f8367998048", + "ids:action" : [ { + "@id" : "https://w3id.org/idsa/code/USE" + }, { + "@id" : "https://w3id.org/idsa/code/READ" + } ] + } ], + "ids:contractStart" : { + "@value" : contract_start, + "@type" : "http://www.w3.org/2001/XMLSchema#dateTimeStamp" + }, + "ids:contractEnd" : { + "@value" : contract_end, + "@type" : "http://www.w3.org/2001/XMLSchema#dateTimeStamp" + } + } + + # Convert dictionary to JSON string + contract_json = json.dumps(contract_template, indent=2) + + # Define the path to save the contract + save_path = os.path.join("docs", "contracts") + + # Ensure the directory exists + os.makedirs(save_path, exist_ok=True) + + # Define the filename + filename = f"contract_{contract_id}.json" + + # Write the JSON to a file + with open(os.path.join(save_path, filename), "w") as file: + file.write(contract_json) \ No newline at end of file