All Products
Search
Document Center

ApsaraVideo Live:Use the server SDK for Python

Last Updated:Dec 26, 2023

This topic describes how to use the server SDK for Python provided by ApsaraVideo Live and provides relevant sample code. This topic uses the API operation that queries production studios as an example to show you how to call an ApsaraVideo Live API operation.

Prerequisites

  • Python 2.7 or later is installed.

  • The server SDK for Python is downloaded. For more information, see SDK download.

Procedure

  1. Install the SDKs.

    1. Install Alibaba Cloud Core SDK.

      sudo pip install aliyun-python-sdk-core

    2. Install ApsaraVideo Live SDK.

      sudo pip install aliyun-python-sdk-live

  2. Update the SDKs.

    1. Update Alibaba Cloud Core SDK.

      sudo pip install aliyun-python-sdk-core --upgrade

    2. Update ApsaraVideo Live SDK.

      sudo pip install aliyun-python-sdk-live --upgrade

  3. Use the server SDK for Python.

    1. Read the file description. In this example, the DescribeCastersRequest.py file in the SDK is used.

      # Licensed to the Apache Software Foundation (ASF) under one
      # or more contributor license agreements.  See the NOTICE file
      # distributed with this work for additional information
      # regarding copyright ownership.  The ASF licenses this file
      # to you under the Apache License, Version 2.0 (the
      # "License"); you may not use this file except in compliance
      # with the License.  You may obtain a copy of the License at
      #
      #
      #     http://www.apache.org/licenses/LICENSE-2.0
      #
      #
      # Unless required by applicable law or agreed to in writing,
      # software distributed under the License is distributed on an
      # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      # KIND, either express or implied.  See the License for the
      # specific language governing permissions and limitations
      # under the License.
      
      from aliyunsdkcore.request import RpcRequest
      from aliyunsdklive.endpoint import endpoint_data
      
      class DescribeCastersRequest(RpcRequest):
      
          def __init__(self):
              RpcRequest.__init__(self, 'live', '2016-11-01', 'DescribeCasters','live')
              self.set_method('POST')
      
              if hasattr(self, "endpoint_map"):
                  setattr(self, "endpoint_map", endpoint_data.getEndpointMap())
              if hasattr(self, "endpoint_regional"):
                  setattr(self, "endpoint_regional", endpoint_data.getEndpointRegional())
      
          def get_StartTime(self): # String
              return self.get_query_params().get('StartTime')
      
          def set_StartTime(self, StartTime):  # String
              self.add_query_param('StartTime', StartTime)
          def get_PageNum(self): # Integer
              return self.get_query_params().get('PageNum')
      
          def set_PageNum(self, PageNum):  # Integer
              self.add_query_param('PageNum', PageNum)
          def get_CasterName(self): # String
              return self.get_query_params().get('CasterName')
      
          def set_CasterName(self, CasterName):  # String
              self.add_query_param('CasterName', CasterName)
          def get_PageSize(self): # Integer
              return self.get_query_params().get('PageSize')
      
          def set_PageSize(self, PageSize):  # Integer
              self.add_query_param('PageSize', PageSize)
          def get_NormType(self): # String
              return self.get_query_params().get('NormType')
      
          def set_NormType(self, NormType):  # String
              self.add_query_param('NormType', NormType)
          def get_CasterId(self): # String
              return self.get_query_params().get('CasterId')
      
          def set_CasterId(self, CasterId):  # String
              self.add_query_param('CasterId', CasterId)
          def get_EndTime(self): # String
              return self.get_query_params().get('EndTime')
      
          def set_EndTime(self, EndTime):  # String
              self.add_query_param('EndTime', EndTime)
          def get_OwnerId(self): # Long
              return self.get_query_params().get('OwnerId')
      
          def set_OwnerId(self, OwnerId):  # Long
              self.add_query_param('OwnerId', OwnerId)
          def get_OrderByModifyAsc(self): # String
              return self.get_query_params().get('OrderByModifyAsc')
      
          def set_OrderByModifyAsc(self, OrderByModifyAsc):  # String
              self.add_query_param('OrderByModifyAsc', OrderByModifyAsc)
          def get_ChargeType(self): # Integer
              return self.get_query_params().get('ChargeType')
      
              def set_ChargeType(self, ChargeType):  # Integer
              self.add_query_param('ChargeType', ChargeType)
              def get_Status(self): # Integer
              return self.get_query_params().get('Status')
      
              def set_Status(self, Status):  # Integer
              self.add_query_param('Status', Status)
                                  

      The DescribeCastersRequest class in the file has multiple sets of corresponding get_X and set_X methods, which are used to obtain and specify the parameters of an API request, respectively.

    2. Create a configuration file named config.ini and place it in the conf directory. Include your AccessKey ID and AccessKey secret in the configuration file. Example:

      [default]
      access_key_id = YOUR_ACCESS_KEY_ID
      access_key_secret = YOUR_ACCESS_KEY_SECRET

      Replace YOUR_ACCESS_KEY_ID and YOUR_ACCESS_KEY_SECRET with your actual AccessKey ID and AccessKey secret.

      Then, you can use the following Python code to read the configuration file and invoke the SDK.

      The following sample code uses the DescribeCasters operation in v20161101 of the SDK as an example:

      #!/usr/bin/env python
      #coding=utf-8
      
      from configparser import ConfigParser
      from aliyunsdkcore.client import AcsClient
      from aliyunsdkcore.acs_exception.exceptions import ClientException
      from aliyunsdkcore.acs_exception.exceptions import ServerException
      from aliyunsdkcore.auth.credentials import AccessKeyCredential
      from aliyunsdkcore.auth.credentials import StsTokenCredential
      from aliyunsdklive.request.v20161101.DescribeCastersRequest import DescribeCastersRequest
      
      config = ConfigParser()
      config.read('conf/config.ini')
      
      # The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
      # We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
      # In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. 
      access_key_id = config.get('default', 'access_key_id')
      access_key_secret = config.get('default', 'access_key_secret')
      # Initialize the client.
      # Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. You can obtain them in the Alibaba Cloud Management Console.
      # Set the region to cn-hangzhou in this example.
      credentials = AccessKeyCredential(access_key_id, access_key_secret)
      # use STS Token
      # credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
      client = AcsClient(region_id='cn-hangzhou', credential=credentials)
      # Construct an API request.
      request = DescribeCastersRequest()
      request.set_accept_format('json')
      
      request.set_CasterId("testCase")
      request.set_CasterName("testName")
      request.set_StartTime("2020-01-01T12:00:01")
      # Send the request.
      response = client.do_action_with_exception(request)
      # python2:  print(response)
      # Obtain the result.
      print(str(response, encoding='utf-8'))

What to do next

To uninstall the relevant SDKs, run the following commands:

  • sudo pip uninstall aliyun-python-sdk-core

  • sudo pip uninstall aliyun-python-sdk-live