External open API request

Example: Perform a GET request to an external OpenAPI endpoint (via Data APP)

Last update: 2024-01-27

This request performs an external OpenAPI request using a connection to a custom connector.

It loads environment variables from a .env file, establishes a connection to the custom connector, and then executes an OpenAPI request using the specified API version and endpoint.

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. Execute an HTTP (GET) request to an external OpenAPI endpoint (via Data APP)

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 perform a GET request to an external OpenAPI endpoint (via Data APP)

 1if __name__ == "__main__":
 2    from loguru import logger
 3    from dotenv import dotenv_values
 4    from tsg_client.controllers import TSGController
 5
 6    # Comment the line below to enable internal logger:
 7    logger.disable("")
 8
 9    # Load environment variables:
10    config = dotenv_values('.env')
11
12    # Example of external connector configs (TNO Playground)
13    EXTERNAL_CONNECTOR = {
14        "CONNECTOR_ID": 'urn:playground:tsg:connectors:TestConnector',
15        "ACCESS_URL": 'https://test-connector.playground.dataspac.es',
16        "AGENT_ID": 'urn:playground:tsg:TNO'
17    }
18
19    # Connect to our TSG connector:
20    conn = TSGController(
21        api_key=config['API_KEY'],
22        connector_id=config['CONNECTOR_ID'],
23        access_url=config['ACCESS_URL'],
24        agent_id=config['AGENT_ID']
25    )
26
27    # Get external connector info (self-descriptions):
28    description = conn.get_connector_selfdescription(
29        access_url=EXTERNAL_CONNECTOR['ACCESS_URL'],
30        connector_id=EXTERNAL_CONNECTOR['CONNECTOR_ID'],
31        agent_id=EXTERNAL_CONNECTOR['AGENT_ID']
32    )
33
34    # Get external connector OpenAPI specs:
35    api_version = "0.9.2"
36    open_api_specs = conn.get_openapi_specs(description, api_version)
37
38    # Get first API specification:
39    open_api_specs = open_api_specs[0]
40    example_endpoint = open_api_specs['endpoints'][-1]
41    example_agent_id = open_api_specs['agent']
42
43    print(f"""
44    Performing a request to:
45    - Agent ID: {example_agent_id}
46    - API Version: {api_version}
47    - Endpoint: {example_endpoint}
48    """)
49
50    # Execute external OpenAPI request:
51    response = conn.openapi_request(
52        external_access_url=EXTERNAL_CONNECTOR['ACCESS_URL'],
53        data_app_agent_id=example_agent_id,
54        api_version=api_version,
55        endpoint=example_endpoint,
56        params="",
57        method="get"
58    )
59
60    print("-" * 79)
61    print(f"> Connector {EXTERNAL_CONNECTOR['CONNECTOR_ID']} RESPONSE:")
62    print("Status Code:", response.status_code)
63    print("Response Text:", response.text)