The process of integrating Alibaba Cloud Darabonba SDK consists of three steps: configure a credential, import the SDK to your project, and write the code for calling API operations. This topic describes how to integrate Alibaba Cloud Darabonba SDK. In this example, Alibaba Cloud Darabonba SDK for Python
is used to call the SendSms operation of Short Message Service (SMS) to send a text message.
Prerequisites
Python 3.7 or later is installed.
Step 1: Configure a credential
An AccessKey pair is required if you use Alibaba Cloud Darabonba SDK to call API operations. For more information, see Credential. To prevent AccessKey pair leaks, you can record the AccessKey pair in environment variables. For information about other security solutions, see Credential security solutions. In this example, the ALIBABA_CLOUD_ACCESS_KEY_ID
and ALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables are configured.
Linux and macOS
Create the
.bash_profile
file.NoteIf you configure environment variables for the first time, you can run the
touch ~/.bash_profile
command to create the configuration file. If the configuration file already exists, run thevim ~/.bash_profile
command to edit the configuration file.touch ~/.bash_profile vim ~/.bash_profile
Configure the
.bash_profile
file.# Replace <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> in the following commands with your AccessKey ID and AccessKey secret. # Replace the variable names based on your business requirements, such as OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>
Save the file after the configuration.
Use the
.bash_profile
file.# Run the following command in the configuration file. source ~/.bash_profile # Check whether the configuration takes effect. echo $ALIBABA_CLOUD_ACCESS_KEY_ID
If the valid AccessKey ID is returned, the configuration is successful.
Windows
In this example, the
ALIBABA_CLOUD_ACCESS_KEY_ID
andALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables are configured.Use GUI
Procedure
If you want to use GUI to configure environment variables in Windows 10, perform the following steps:
On the Windows desktop, right-click This PC and select Properties. On the page that appears, click Advanced system settings. In the System Properties dialog box, click Environment Variables on the Advanced tab. In the Environment Variables dialog box, click New in the User variables or System variables section. Then, configure the variables described in the following table.
Variable
Example
AccessKey ID
Variable name: ALIBABA_CLOUD_ACCESS_KEY_ID
Variable value: LTAI4GDty8ab9W4Y1D****
AccessKey Secret
Variable name: ALIBABA_CLOUD_ACCESS_KEY_SECRET
Variable value: IrVTNZNy5yQelTETg0cZML3TQn****
Check whether the configuration is successful.
On the Windows desktop, click Start or press Win + R. In the Run dialog box, enter cmd. Then, click OK or press the Enter key. On the page that appears, run the
echo %ALIBABA_CLOUD_ACCESS_KEY_ID%
andecho %ALIBABA_CLOUD_ACCESS_KEY_SECRET%
commands. If the valid AccessKey pair is returned, the configuration is successful.
Use CMD
Procedure
Open a Command Prompt window as an administrator and run the following commands to add environment variables in the operating system:
setx ALIBABA_CLOUD_ACCESS_KEY_ID LTAI4GDty8ab9W4Y1D**** /M setx ALIBABA_CLOUD_ACCESS_KEY_SECRET IrVTNZNy5yQelTETg0cZML3TQn**** /M
/M
indicates that the environment variable is of system level. You cannot use this parameter when you configure a user-level environment variable.Check whether the configuration is successful.
On the Windows desktop, click Start or press Win + R. In the Run dialog box, enter cmd. Then, click OK or press the Enter key. On the page that appears, run the
echo %ALIBABA_CLOUD_ACCESS_KEY_ID%
andecho %ALIBABA_CLOUD_ACCESS_KEY_SECRET%
commands. If the valid AccessKey pair is returned, the configuration is successful.
Use Windows PowerShell
In PowerShell, configure new environment variables. The environment variables apply to all new sessions.
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTAI4GDty8ab9W4Y1D****', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'IrVTNZNy5yQelTETg0cZML3TQn****', [System.EnvironmentVariableTarget]::User)
Configure environment variables for all users. You must run the following commands as a administrator.
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'LTAI4GDty8ab9W4Y1D****', [System.EnvironmentVariableTarget]::Machine) [System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'IrVTNZNy5yQelTETg0cZML3TQn****', [System.EnvironmentVariableTarget]::Machine)
Configure temporary environment variables. The environment variables apply only to the current session.
$env:ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI4GDty8ab9W4Y1D****" $env:ALIBABA_CLOUD_ACCESS_KEY_SECRET = "IrVTNZNy5yQelTETg0cZML3TQn****"
In PowerShell, run the
Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_ID
andGet-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_SECRET
commands. If the valid AccessKey pair is returned, the configuration is successful.
Step 2: Import the SDK to your project
Alibaba Cloud Darabonba SDK allows you to make generic and specialized calls. For more information, see Generic calls and specialized calls. The SDK that you need to import varies based on the call type.
Specialized calls
You can visit OpenAPI Explorer, find the service that you want to use, and view the supported languages and installation methods. Then, import the SDK of the service to your project. In this example, SMS SDK is imported. Perform the following steps:
Visit the SMS SDK page.
In the All languages section, select the language that you want to use.
In the Installation Method section, select an installation method and copy the installation code.
Add the dependency to your project.
Run the following command to install SMS SDK for Python:
pip install alibabacloud_dysmsapi20170525==2.0.24
Generic calls
You do not need to install the SDK of a service to make generic calls. You need to only import the alibabacloud_tea_openapi
core package. For information about the latest version of the core package, see alibabacloud-tea-openapi. Run the following command to install the core package:
pip install alibabacloud-tea-openapi
Step 3: Call an API operation
Download the sample code. For more information, see Get started with Alibaba Cloud Darabonba SDK for Python. In this example, the sample code of a specialized call is used. If you want to make a generic call, turn on Common Request when you download the sample code. For more information, see Generic calls and specialized calls.
Initialize a request client
Method 1: Use an AccessKey pair
from alibabacloud_dysmsapi20170525.client import Client as Dysmsapi20170525Client
from alibabacloud_tea_openapi import models as open_api_models
import os
@staticmethod
def create_client():
config = open_api_models.Config(
# Required. Specify your AccessKey ID.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
# Required. Specify your AccessKey secret.
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET')
)
# Specify the endpoint. For more information, visit https://api.aliyun.com/product/Dysmsapi.
config.endpoint = f'dysmsapi.aliyuncs.com'
# Initialize a request client for a specialized call.
dysmsapi_client = Dysmsapi20170525Client(config)
Method 2: Use an STS token
from alibabacloud_dysmsapi20170525.client import Client as Dysmsapi20170525Client
from alibabacloud_tea_openapi import models as open_api_models
import os
@staticmethod
def create_client():
config = open_api_models.Config(
# Required. Specify your AccessKey ID.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
# Required. Specify your AccessKey secret.
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# Required. Specify your Security Token Service (STS) token.
security_token=os.environ.get('ALIBABA_CLOUD_SECURITY_TOKEN')
)
# Specify the endpoint. For more information, visit https://api.aliyun.com/product/Dysmsapi.
config.endpoint = f'dysmsapi.aliyuncs.com'
# Initialize a request client for a specialized call.
dysmsapi_client = Dysmsapi20170525Client(config)
Method 3: Use the Credentials tool
from alibabacloud_credentials.client import Client as CredClient
from alibabacloud_dysmsapi20170525.client import Client as Dysmsapi20170525Client
from alibabacloud_tea_openapi.models import Config
# Use the default credential to initialize a Credentials client.
cred = CredClient()
config = Config(credential=cred)
# To call the SMS API, initialize the Client class of the SDK package for SMS.
config.endpoint = 'dysmsapi.aliyuncs.com'
# Initialize a request client for a specialized call.
dysmsapi_client = Dysmsapi20170525Client(config)
Create a request object or a runtime configuration object
In most cases, you can create a request object and specify the parameters based on your business requirements. You can also customize runtime configurations.
from alibabacloud_dysmsapi20170525 import models as dysmsapi_20170525_models
from alibabacloud_tea_util import models as util_models
# Create a request object and specify the parameters.
send_sms_request = dysmsapi_20170525_models.SendSmsRequest(
phone_numbers='<YOUR_VALUE>',
sign_name='<YOUR_VALUE>'
)
# Create a runtime configuration object.
runtime = util_models.RuntimeOptions()
Initiate a request
In the Darabonba SDK, the client of each Alibaba Cloud service provides three request methods for all API operations of the service. You can specify a request method for an API operation. The name of the request method is the name of the API operation with the first letter in lowercase. The following list describes the request methods:
<API operation name>
: uses the default runtime configurations to initiate a request.from alibabacloud_credentials.client import Client as CredClient # Import a Credentials client and create an alias for it. from alibabacloud_dysmsapi20170525.client import Client as DysmsApiClient # Import an SMS SDK client and create an alias for it. from alibabacloud_tea_openapi.models import Config from alibabacloud_dysmsapi20170525 import models as dysmsapi_20170525_models # Import the SMS SDK core package. # Use the default credential to initialize the Credentials client. credentialClient = CredClient() smsConfig: Config = Config(credential=credentialClient) # Specify the endpoint of the cloud service. smsConfig.endpoint = 'ecs.aliyuncs.com' # Use the Credentials client to initialize the SMS SDK client. smsClient = DysmsApiClient(smsConfig) # Construct the request object. sendSmsRequest = dysmsapi_20170525_models.SendSmsRequest() # Obtain the response object. sendSmsResponse = smsClient.send_sms(sendSmsRequest) print(sendSmsResponse.to_map())
<API operation name>_with_options
: uses the custom runtime configurations to initiate a request. For more information, see Advanced configuration.from alibabacloud_credentials.client import Client as CredClient # Import a Credentials client and create an alias for it. from alibabacloud_dysmsapi20170525.client import Client as DysmsApiClient # Import an SMS SDK client and create an alias for it. from alibabacloud_tea_openapi.models import Config from alibabacloud_dysmsapi20170525 import models as dysmsapi_20170525_models # Import the SMS SDK core package. from alibabacloud_tea_util import models as util_models # Use the default credential to initialize the Credentials client. credentialClient = CredClient() smsConfig: Config = Config(credential=credentialClient) # Specify the endpoint of the cloud service. smsConfig.endpoint = 'ecs.aliyuncs.com' # Use the Credentials client to initialize the SMS SDK client. smsClient = DysmsApiClient(smsConfig) # Construct the request object. sendSmsRequest = dysmsapi_20170525_models.SendSmsRequest() # Construct the runtime configuration object. runtime = util_models.RuntimeOptions() # Set the timeout period for read requests. runtime.readTimeout=10000; # Set the timeout period for connection requests. runtime.connectTimeout=5000; # Specify whether to automatically retry a request. runtime.autoretry=True; # Obtain the response object. sendSmsResponse = smsClient.send_sms_with_options(sendSmsRequest,runtime) print(sendSmsResponse.to_map())
<API operation name>Advance
: used for API operations that require file uploading. By default, you must specify runtime parameters. The following sample code provides an example on how to initiate a request to upload a file and call an API operation to usethe human face and body recognition feature of Visual Intelligence API (VIAPI)
.# The dependency for the human face and body recognition feature of VIAPI. pip install alibabacloud_facebody20191230==4.0.9
from alibabacloud_credentials.client import Client as CredClient # Import a Credentials client and create an alias for it. from alibabacloud_facebody20191230.client import Client as FacebodyClient # Import a client of the SDK for the human face and body recognition feature and create an alias for it. from alibabacloud_tea_openapi.models import Config # Import the SDK core package for the human face and body recognition feature. from alibabacloud_tea_util import models as util_models from alibabacloud_facebody20191230 import models as facebody_20191230_models # Use the default credential to initialize the Credentials client. credentialClient = CredClient() facebodyConfig = Config(credential=credentialClient) # Specify the endpoint of the cloud service. facebodyConfig.endpoint = 'facebody.cn-shanghai.aliyuncs.com' facebody_client = FacebodyClient(facebodyConfig) detect_body_count_advance_request = facebody_20191230_models.DetectBodyCountAdvanceRequest() detect_body_count_advance_request.image_urlobject = open('<filepath>', 'rb') # Construct the runtime configuration object. runtimeOptions = util_models.RuntimeOptions() # Set the timeout period for read requests. runtimeOptions.readTimeout=10000; # Set the timeout period for connection requests. runtimeOptions.connectTimeout=5000; # Specify whether to automatically retry a request. runtimeOptions.autoretry=True; response = facebody_client.detect_body_count_advance(detect_body_count_advance_request, runtimeOptions) print(response.to_map())