Authenticate with OpenID Connect
With OpenID Connect, you can instantiate yourCogniteClient with either an OpenID Connect user token or client credentials.
Authenticate with an OpenID Connect user token
client = CogniteClient(project="my-project", token=my_token)
my_token is a string or a callable.Authenticate with client credentials
For more information see authenticate with client credentials on Python. To instantiate yourCogniteClient with client credentials:
python
Create a function
You can create a function from a Jupyter Notebook, a folder, an uploaded zip file, or from CDF. The examples below assume that you have the Python SDK installed in your environment.Create a function from a Jupyter Notebook
To create a function from a Jupyter Notebook, refer directly to the Python function namedhandle. This is the easiest way to get started but it’s only suitable for short and simple functions.
The
function_handle argument points to the handle function defined in the example below, and the name in the definition must be handle.When you deploy with the function_handle argument, you must import within handle(). In other cases (with the folder or file_id arguments), the imports can be outside handle().To create a Function with dependencies, see the Python SDK documentation.
python
python
Create a function from a folder
For more advanced functions, you can define your function in a module that imports other modules. To create a function, replace thefunction_handle argument with the folder argument and set it equal to the path of the folder containing your code.
Example:
python
path/to/folder can have nested imports of arbitrary depth. The only requirement is the file called handler.py (assumed to be in the root folder unless otherwise specified) and a function named handle within this module. This serves as the entry point to the function.
If your handler.py file isn’t in the root folder, you must specify its location via the argument function_path (defaults to handler.py).
If your function depends on other packages, you can list these packages in a requirements.txt file. You must place the file in the root folder. These packages will be pip installed when the function is deployed.
Create a function from an uploaded zip file
You can also upload a zip file containing the function code directly to the files API and refer to itsid when you create the function.
Example:
python
Additional arguments
-
function_path(string): The relative path from the root folder to the file containing thehandlefunction. Defaults tohandler.py. -
secrets(dict): Secrets that will be stored securely on your function and available call-time. -
env_vars(dict): Environment variables that can be accessed inside your function call-time. -
runtime(string): The function runtime. Valid values are given in the SDK documentation. -
index_url(string): A URL pointing to a different package repository. It supports basic HTTP authentication. -
extra_index_urls(list): A list of URLs pointing to additional python package repositories. Supports basic HTTP authentication.
Additional information: Dependency confusion
Additional information: Dependency confusion
Dependency confusion happens when a software installer is tricked into installing malicious software from a public index instead of the intended private index.In the Python ecosystem, this is made possible when multiple indices are specified using the
--extra-index-url option for pip. When installing a package, the dependency resolver will look for a package with the requested name in the default index (PyPI by default, can be overridden by --index-url). If the requested package isn’t found there, it will look at the indices specified in --extra-index-url.This means that if a malicious entity manages to guess the name of a package hosted in the additional indices, the entity can upload a package with the same name to the public package index containing malicious code. When resolving, if the package version isn’t pinned, pip will choose the package with the highest version number. Therefore, if the malicious package has the highest version number, it will be chosen by pip. The user can, to some degree, mitigate this risk by pinning the package version. In that case, pip will pull the package from the private index if that particular version only exists there. However, if the version exists in both indices, the behavior is undefined, and you can’t determine which index pip will pull the package from.The article 3 Ways to Mitigate Risk When Using Private Package Feeds by Microsoft is a good introduction to the dependency confusion problem.The function definition
The entry point of your function, which must be a synchronous Python function, must be namedhandle with any of the following arguments:
-
client- a pre-instantiatedCogniteClientautomatically available inside your function call-time. It’s instantiated with the same permissions as the entity calling the function (on-behalf-of flow). Note that theclientargument is only specified when you define the function but not when you call it; the client is automatically provided when you call it. -
data- (dict) any data you send to your function.
-
secrets- (dict) serves as a way to send sensitive information to your function that must be available when calling it. You can provide these secrets as a dictionary via the secrets argument inclient.functions.create(). -
function_call_info- (dict) contains information about the function being called. It has the keysfunction_id,call_id, and if the call is scheduled, the keysschedule_idandscheduled_time.
client.functions.create() returns a function object with various metadata. One of the properties of this object is status which will start with Queued, change to Deploying when the deploy process has started, and end in Ready if the build was successful, or Failed if it failed. You must wait until the function is Ready before calling it. This typically takes about 3–10 minutes. To retrieve the latest status of your function, do:
Call a function
When your function is Ready, you can call it. Example:python
client here, even though it’s part of the function definition. You get it automatically inside the function.
When the function is called, a session is created on behalf of the user, and the client inside the function inherits the user’s permissions and scopes.
The client is instantiated with a fresh access token based on the newly created session.
The call object has metadata about the call, but not the response itself:
python
Call a function from a function
You can call a function B from within a function A with theclient passed through the handle() function. Just as a session is created for function A on behalf of the user,
a new session will be created for function B from the first sessions (so-called chained sessions). This means that the client in function B also will inherit the
user’s permissions and scopes.
Note that you can’t further call a function C from within function B in the same way. This is due to a limitation in the sessions API, which only allows for chained sessions one level deep.
Schedule a function
Create a schedule to run a function at regular intervals. Note that when you have created the schedule, you can’t change it. You must delete it and create a new schedule. Example:python
function_id argument. Note that you must provide client_credentials. These will be used to create a session for the schedule, which will be kept alive for the lifetime of the schedule.
Deleting a function also deletes the associated schedule.
Upload and run a function in Cognite Data Fusion
In addition to uploading and running functions via the Python SDK and the API, you can upload, run, and share functions directly from CDF at fusion.cognite.com.1
Navigate to Functions
Navigate to Data management > Build solutions > Functions.
2
Upload your function file
Select Upload function, and drag and drop the zip file with at least a Python file called
handler.py with a function named handle with any of following arguments: data, client, secrets, and function_call_info.See Create a function from an uploaded zip file for more information.
3
Configure function settings
Give your function a name and, if necessary, fill in the optional fields.
4
Complete upload
Select Upload.
Call a function from CDF
1
Navigate to Functions
Navigate to Data management > Build solutions > Functions.
2
Call the function
Choose the function you want to call, and then select Call.
3
Optional. Schedule the function
To schedule the function, select Create schedule and enter a name, client credentials, and a cron expression for when you want to run your function.