All Products
Search
Document Center

ApsaraVideo Live:Use the server SDK for Java

Last Updated:Jan 26, 2024

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

Prerequisites

  • Obtain an AccessKey pair to complete identity verification so that you can call server API operations. For more information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.

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

Procedure

  1. Add the dependencies on Alibaba Cloud Core SDK and ApsaraVideo Live SDK to the pom.xml file in the JAR package of the server SDK for Java. In the following example, the version of ApsaraVideo Live SDK is for reference only. For more information about how to download the latest version of the SDK, visit the ApsaraVideo Live SDK page.

    1. Import Alibaba Cloud Core SDK.

      <dependencies>
          <dependency>    
              <groupId>com.aliyun</groupId>    
              <artifactId>aliyun-java-sdk-core</artifactId>    
              <version>4.4.6</version>
          </dependency>
      </dependencies>
    2. Import ApsaraVideo Live SDK.

      <dependencies>
          <dependency>
              <groupId>com.aliyun</groupId>
              <artifactId>aliyun-java-sdk-live</artifactId>
              <version>3.9.0</version>
          </dependency>
      </dependencies>
  2. Initialize the IAcsClient instance.

    You must use the Spring framework to import configurations into your project. For more information about how to build a Spring project, visit the Spring official website.

    Add the following content to the configuration file:

    # The AccessKey ID and AccessKey secret for the client request.
    live.accessKeyId=<yourAccessKeyId>
    live.accessKeySecret=<yourAccessKeySecret>

    The API operations encapsulated in ApsaraVideo Live SDK are called by using the IAcsClient instance. Therefore, you must initialize the IAcsClient instance before you call API operations.

    @Value("${live.accessKeyId}")
    private String accessKeyId;
    
    @Value("${live.accessKeySecret}")
    private String accessKeySecret;
    
    DefaultAcsClient client;
    
    @Before
    public void initClient() throws Exception {
        // 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. 
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        client = new DefaultAcsClient(profile);
    }
  3. Initialize a request.

    Before you call an API operation, you must initialize the corresponding request instance. The following sample code uses the DescribeLiveSnapshotConfig operation as an example:

    public void requestInitSample() {
         DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
         describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com");
         //describeLiveSnapshotConfigRequest.setProtocol(ProtocolType.HTTPS); // Specify the request protocol.
         //describeLiveSnapshotConfigRequest.setAcceptFormat(FormatType.JSON); // Specify the response format of the API operation.
         //describeLiveSnapshotConfigRequest.setMethod(MethodType.POST); // Specify the request method.
         //describeLiveSnapshotConfigRequest.setRegionId("cn-shanghai");// Specify the region in which you want to perform the specified operation. This setting is valid only for the current request, and does not affect the default client settings.
         try {
             HttpResponse httpResponse = client.doAction(describeLiveSnapshotConfigRequest);
             System.out.println(httpResponse.getUrl());
             System.out.println(new String(httpResponse.getContent()));
             //todo something
         } catch (ServerException e) {
             e.printStackTrace();
         } catch (ClientException e) {
             e.printStackTrace();
         }
    
    }
  4. Call API operations and parse the result.

    The IAcsClient instance provides two methods to obtain the response of the call:

    • Call the doAction method to obtain a response of the HttpResponse type, which is the original response of the call. Sample code:

      public void invokeSample() {
           DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
           describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com");
           try {
               HttpResponse httpResponse = client.doAction(describeLiveSnapshotConfigRequest);
               System.out.println(httpResponse.getUrl());
               System.out.println(new String(httpResponse.getContent()));
               //todo something else
           } catch (ServerException e) {
               e.printStackTrace();
           } catch (ClientException e) {
               e.printStackTrace();
           }
       }

      Check the returned HTTP status code:

      • If the HTTP status code is greater than or equal to 200 but less than 300, the call is successful.

      • If the HTTP status code is greater than or equal to 300 but less than 500, the server SDK for Java throws a ClientException, which indicates a client error.

      • If the HTTP status code is greater than or equal to 500, the server SDK for Java throws a ServerException, which indicates a server error.

    • Call the getAcsResponse method to obtain a deserialized object. Sample code:

      public void invokeSample() {
           DescribeLiveSnapshotConfigRequest describeLiveSnapshotConfigRequest = new DescribeLiveSnapshotConfigRequest();
           describeLiveSnapshotConfigRequest.setDomainName("example.aliyundoc.com");
           try {
               DescribeLiveSnapshotConfigResponse describeLiveSnapshotConfigResponse = client.getAcsResponse(describeLiveSnapshotConfigRequest);
               //todo something
           } catch (ServerException e) {
               e.printStackTrace();
           } catch (ClientException e) {
               e.printStackTrace();
           }
       }