All Products
Search
Document Center

SDK for Python

Last Updated: Jan 21, 2021

Download link

You can download the AIRec SDK for Python package at https://pypi.org/project/aliyun-python-sdk-airec. You can view the source code of AIRec SDK for Python at https://github.com/aliyun/aliyun-openapi-python-sdk/tree/master/aliyun-python-sdk-airec.

Procedure

To use AIRec SDK for Python, perform the following steps:

Step 1: Create an Alibaba Cloud account

For more information, see Sign up with Alibaba Cloud. We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2: Obtain an Alibaba Cloud AccessKey pair

Before you use AIRec SDK for Java, you must apply for an Alibaba Cloud AccessKey pair.

Go to the Security Management page. Select an AccessKey pair for AIRec SDK for Java. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. If no AccessKey pair is available, create one and make sure that it is in the Enable state. For more information about how to create an AccessKey pair, see Preparations.

Step 3: Install Alibaba Cloud SDK for Python

To install Alibaba Cloud SDK for Python, perform the following steps:

  1. Install the core library of Alibaba Cloud SDK for Python.

    • If you are using Python 2.X, run the following command:

      pip install aliyun-python-sdk-core
    • If you are using Python 3.X, run the following command:

      pip install aliyun-python-sdk-core-v3
  2. Install AIRec SDK for Python.

Run the following command:

pip install aliyun-python-sdk-airec

Step 4: Use Alibaba Cloud SDK for Python

You can perform the following operations to use Alibaba Cloud SDK for Python:

  1. Create a client. When you create a client, you must obtain the region ID, AccessKey ID, and AccessKey secret.

  2. Configure the endpoint.

  3. Create a request and configure parameters.

  4. Initiate the request and handle the response or exception.

Sample code for pushing data

#! /usr/bin/python
# coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkairec.request.v20181012.PushDocumentRequest import PushDocumentRequest

# Create a client of the AcsClient class.
client = AcsClient(
    ak="Alibaba Cloud AccessKey ID",
    secret="Alibaba Cloud AccessKey secret",
    #Enter the region. If the region is China (Beijing), enter cn-beijing.
    region_id="cn-hangzhou"
)
# Configure the endpoint.
# Specify the region ID, service name, and endpoint.
client.add_endpoint("cn-hangzhou", "Airec", "airec.cn-hangzhou.aliyuncs.com")
# Create a request and configure parameters.
# Create a request for a specific API operation. The class of the request is named by adding Request to the end of the API operation name.
# For example, the name of the API operation that is used to obtain pushed documents is PushDocument. In this case, the name of the request class is PushDocumentRequest.
request = PushDocumentRequest()
request.set_DBInstanceId("Instance ID")
request.set_TableName("item")
# Configure parameters for the request.
content = "JSON-formatted data"

request.set_content(content)
request.set_content_type("application/json")
# Initiate the request by using the method that is supported by the client, obtain the response, and handle the exception.
try:
    response = client.do_action_with_exception(request)
    print(response)
except ClientException as e:
    print(e)
except ServerException as e:
    print(e)

Sample code for obtaining recommendation results

#! /usr/bin/env python
#coding=utf-8

# Obtain recommendation results.
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')

request = CommonRequest()
request.set_accept_format('json')
request.set_method('GET')
request.set_protocol_type('https') # https | http
request.set_domain('airec.cn-hangzhou.aliyuncs.com')
request.set_version('2018-10-12')

# You must specify the scene ID and the number of returned results. You must specify the user ID or IMEI.
request.add_query_param('ReturnCount', "2")
request.add_query_param('UserId', "Enter the user ID")
request.add_query_param('SceneId', "Enter the scene ID")
request.add_query_param('Imei', "Enter the device number")
request.add_header('Content-Type', 'application/json')
request.set_uri_pattern('/openapi/instances/[InstanceId]/actions/recommend')
body = '''{}'''
request.set_content(body.encode('utf-8'))

response = client.do_action_with_exception(request)

# python2:  print(response) 
print(str(response, encoding = 'utf-8'))