It is possible to make API calls to external sources from Python functions, but doing so requires additional configuration. This configuration and external source usage are detailed below.
This functionality requires that your functions be deployed and cannot be used with serverless Python functions.
By default, Python functions are not allowed to call external APIs. To enable calling external systems from your Python function, you must configure a source in Data Connection to allow Foundry to connect with an external system.
For Python functions to connect to your source's external system securely, your source must be configured to enable exports and allow the import of your source into Code Repositories. Both of these can be configured by navigating to the source in Data Connection and opening the the Connection settings section. Here you will find the Code import configuration tab, where you can also configure an API name if your source does not yet have one. This API name will be used to reference your source in code.
To make API calls from a Python function, you must first import your source using the resource imports sidebar in a Python functions repository. Once imported, grant your functions access to the source by adding the source's API name to the sources
parameter of the @function
decorator.
With the source imported and declared in the function decorator, you can make external API calls to the system represented by that source.
An example of this is shown below:
Copied!1 2 3 4 5 6 7 8 9 10 11
from functions.api import function from functions.sources import get_source @function(sources=["<SOURCE API NAME>"]) def my_external_function(name: str) -> str: source = get_source("<SOURCE API NAME>") url = source.get_https_connection().url client = source.get_https_connection().get_client() response = client.get(url) return response.txt
After publishing your function, you can use it to make external calls in Pipeline Builder and Workshop.
It is not currently possible to make API calls in function live preview, you will have to publish your function to test it.
You can access source attributes by using the get_https_connection()
method on the built-in connection object. The example below shows how to obtain the base URL of the source in the example above.
Copied!1
url = get_source("<SOURCE API NAME>").get_https_connection().url
You can also access additional secrets or credentials stored on the source by using the following syntax to access secrets:
Copied!1
secret = get_source("<SOURCE API NAME>").get_secret("<SECRET API NAME>")
For sources that provide a REST API, the source object allows you to interact with a built-in HTTPS client. This client will be pre-configured with the details specified on the source, including any server or client certificates, so you can start making requests to the external system.
Copied!1 2 3
connection = get_source("<SOURCE API NAME>").get_https_connection() client = connection.get_client() response = client.get(connection.url)
Currently, it is not possible to access source attributes that are not credentials unless the source provides an HTTPS client. For example, you will not be able to access the hostname
or other non-secret attributes on a PostgreSQL source.