Skip to main content

Python

In this example, we will go over the steps required to implement Superface into your Python application development workflow.

Have you created Comlinks?

This guide assumes that you have already installed and run the Superface CLI to generate the Comlinks for the API integration you want to work with.

Install OneSDK

python -m pip install one-sdk

Finally, create a new file called __main__.py, or open whichever file you want to include your integration code in.

If you created your Comlinks with the Superface CLI in a different folder, please move them to a superface folder at the root of your application. The following files are expected by OneSDK.

.
└── superface/ - directory with all the Comlinks in project root
├── <profileScope>.<profileName>.<providerName>.map.js
├── <profileScope>.<profileName>.profile
├── <providerName>.provider.json
└── ...repeat for all the Comlinks

Add the OneSDK function

__main__.py
import sys

from one_sdk import OneClient, PerformError, UnexpectedError

client = OneClient()

profile = client.get_profile("<profileName>")
use_case = profile.get_usecase("<usecaseName>")
try:
r = use_case.perform(
{
# Input parameters as defined in profile:
'<key>': '<value>'
},
provider = '<providerName>',
# Provider specific integration parameters:
parameters = {
'<integrationParameterName>': '<integrationParameterValue>'
},
security = {
# Provider specific security values:
'<securityValueId>': {
# Security values as described in provider or on profile page
}
}
)
print(f"RESULT: {r}")
except PerformError as e:
print(f"ERROR RESULT: {e.error_result}")
except UnexpectedError as e:
print(f"ERROR: {e}", file=sys.stderr)
except Exception as e:
raise e
finally:
client.send_metrics_to_superface()

Run the code

python __main.py__