Demo code for building a request

更新时间:
复制 MD 格式

This page shows how to use the management SDK for C# to send a signed request to the OpenSearch Industry Algorithm Edition API. The sample code demonstrates bulk-adding entries to an intervention dictionary.

Prerequisites

Before you begin, ensure that you have:

Important

Use a RAM user AccessKey pair instead of your Alibaba Cloud account AccessKey pair. Keep your AccessKey pair out of source code and other materials that others can access.

Set environment variables

Store your credentials as environment variables so the sample code can read them at runtime.

  • Linux and macOS: Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of your RAM user.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows: Create a system environment variable file, add ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET with their values, and then restart Windows for the changes to take effect.

Add the SDK dependency

Download the package from NuGet and add it to your project:

dotnet add package aliyun-net-sdk-core

Sample code

The following example calls the intervention dictionary API to bulk-add entries. To call a different API, update request.UriPattern and requestBody.

Important

This sample code uses the CommonRequest approach as a temporary workaround. After the official OpenSearch SDK is released, this code may no longer work. Use it for testing only.

using System;
using System.Collections.Generic;
using System.Text;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Auth;

namespace CommonRequestDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read credentials from environment variables set in the previous step.
            // Do not hardcode your AccessKey pair in code.
            AlibabaCloudCredentialsProvider provider = new AccessKeyCredentialProvider(
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));

            /* To use a temporary STS token instead, replace the block above with:
            AlibabaCloudCredentialsProvider provider = new StsCredentialProvider(
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
                Environment.GetEnvironmentVariable("ALIBABA_CLOUD_SECURITY_TOKEN"));
            */

            IClientProfile profile = DefaultProfile.GetProfile("cn-qingdao");
            DefaultAcsClient client = new DefaultAcsClient(profile, provider);

            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;
            request.Domain = "opensearch.cn-qingdao.aliyuncs.com";
            request.Version = "2017-12-25";
            // To target a different API operation, update this URI pattern.
            request.UriPattern = "/v4/openapi/intervention-dictionaries/huan/entries/actions/bulk";
            // request.Protocol = ProtocolType.HTTP;

            request.AddHeadParameters("Content-Type", "application/json");
            // To call a different API operation, update this request body to match its schema.
            String requestBody = "[{\"word\":\"asdf\",\"cmd\":\"add\",\"tokens\":[{\"weight\":7,\"token\":\"asdf\"}]}]";
            request.SetContent(Encoding.UTF8.GetBytes(requestBody), "utf-8", FormatType.JSON);

            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                Console.WriteLine(response.Data);
            }
            catch (ServerException e)
            {
                // Server-side error. Check the API response message for details.
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                // Client-side error. Verify your credentials, endpoint, and request parameters.
                Console.WriteLine(e);
            }
        }
    }
}