Get Contract Agreement and Download Artifact

Example: Perform a contract agreement and download an artifact from an external connector

Last update: 2024-01-27

This request requests a contract agreement and retrieves content for an external artifact using a connection to a custom connector. It loads environment variables from a .env file, establishes a connection to the custom connector, and then requests a contract agreement and retrieves content for the first external artifact.

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. Retrieve and print information about external connector self-description

  4. Extract (from self-descriptions) information regarding available artifacts

  5. Request a contract agreement for the first artifact

  6. Retrieve content for the first artifact

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 download an artifact from an external connector.

 1if __name__ == "__main__":
 2    from pprint import pprint
 3    from loguru import logger
 4    from dotenv import dotenv_values
 5    from tsg_client.controllers import TSGController
 6
 7    # Comment the line below to enable internal logger:
 8    logger.disable("")
 9
10    # Load environment variables:
11    config = dotenv_values('.env')
12
13    # Example of external connector configs (TNO Playground)
14    EXTERNAL_CONNECTOR = {
15        "CONNECTOR_ID": 'urn:playground:tsg:connectors:TestConnector',
16        "ACCESS_URL": 'https://test-connector.playground.dataspac.es',
17        "AGENT_ID": 'urn:playground:tsg:TNO'
18    }
19
20    # Connect to our TSG connector:
21    conn = TSGController(
22        api_key=config['API_KEY'],
23        connector_id=config['CONNECTOR_ID'],
24        access_url=config['ACCESS_URL'],
25        agent_id=config['AGENT_ID']
26    )
27
28    # Get external connector info (self-descriptions):
29    self_description = conn.get_connector_selfdescription(
30        access_url=EXTERNAL_CONNECTOR['ACCESS_URL'],
31        connector_id=EXTERNAL_CONNECTOR['CONNECTOR_ID'],
32        agent_id=EXTERNAL_CONNECTOR['AGENT_ID']
33    )
34
35    # Get external connector artifacts
36    resource_type = "ids:ContractOffer"
37    artifacts = conn.parse_catalog_artifacts(
38        self_description,
39        catalog_id=self_description.catalogs[1].id,
40        resource_type=resource_type,
41        creation_date_gt="2021-08-10T00:00:00.000Z",
42        creation_date_lt="2030-01-01T00:00:00.000Z",
43        return_last_artifact=True,
44        valid_contract_only=False
45    )
46
47    # Preview first artifact contract offer & request agreement
48    example_artifact = artifacts[0]  # first artifact
49    print("-" * 79)
50    print("> Contract Offer (from external connector):")
51    pprint(example_artifact['contract_offer'])
52
53    # Request contract agreement for the first artifact
54    contract_agreement_id = conn.request_agreement(
55        connector_id=EXTERNAL_CONNECTOR['CONNECTOR_ID'],
56        artifact_access_url=example_artifact['access_url'],
57        artifact_contract_offer=example_artifact['contract_offer']
58    )
59
60    print("-" * 79)
61    print("> Contract Agreement Identifier:")
62    print(contract_agreement_id)
63
64    # Retrieve content for the first artifact
65    artifact_content = conn.request_data_artifact(
66        artifact_id=artifacts[0]['id'],
67        artifact_access_url=artifacts[0]['access_url'],
68        agent_id=EXTERNAL_CONNECTOR['AGENT_ID'],
69        connector_id=EXTERNAL_CONNECTOR['CONNECTOR_ID'],
70        contract_agreement_id=contract_agreement_id,
71        keep_original_format=True,
72        file_path=""
73    )