All Products
Search
Document Center

:Initialize an SDK client and use the SDK client to initiate API requests

Last Updated:Feb 21, 2023

Initialize an SDK client

The Darabonba SDK uses the main API-based logic to process parameters, send requests, and process return values. The Darabonba SDK can be used to create a client for each Alibaba Cloud service. Each client provides request methods for all API operations of an Alibaba Cloud service. This way, the identity and authentication information about different Alibaba Cloud services is isolated to improve security. The following code provides an example on how to initialize an SDK client in the Darabonba SDK:

func CreateClient(AccessKeyId *string, AccessKeySecret *string) (_result *ecs20140526.Client, err error) {
 config := &openapi.Config{
  AccessKeyId:     AccessKeyId,
  AccessKeySecret: AccessKeySecret,
 }
 // Configure a region or an endpoint to specify the request URL.
 config.RegionId = tea.String("<RegionId>")
 // config.Endpoint = tea.String("ecs-cn-hangzhou.aliyuncs.com")
 client, err := ecs20140526.NewClient(config)
 if err != nil {
    panic(err)
  }
}

Initiate API requests by using the SDK client

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 request method is the name of the API operation with the first letter in lowercase. The request methods are:

  • The request method that does not require runtime parameters.

func main() {
   config := &openapi.Config{
    // Your AccessKey ID.
    AccessKeyId: tea.String("<AccessId>"),
    // Your AccessKey secret.
    AccessKeySecret: tea.String("<AccessSecret>"),
     // The region that you want to access.
    RegionId: tea.String("<RegionId>")
  }
  client, err := ecs20140526.NewClient(config)
  if err != nil {
    panic(err)
  }
  describeRegionsRequest := &ecs20140526.DescribeRegionsRequest{}
  resp, err := client.DescribeRegions(describeRegionsRequest)
  if err != nil {
    panic(err)
  }
  // The response, which contains the body and headers that are returned by the server side.
  body, err := json.Marshal(resp.Body)
  if err != nil {
    panic(err)
  }
  headers, err := json.Marshal(resp.Headers)
  if err != nil {
    panic(err)
  }
  fmt.Printf("body: %s\n", string(body))
  fmt.Printf("header: %s\n", string(headers))
}
  • The request method that requires runtime parameters.

func main() {
   config := &openapi.Config{
    // Your AccessKey ID.
    AccessKeyId: tea.String("<AccessId>"),
    // Your AccessKey secret.
    AccessKeySecret: tea.String("<AccessSecret>"),
     // The region that you want to access.
    RegionId: tea.String("<RegionId>")
  }
  client, err := ecs20140526.NewClient(config)
  if err != nil {
    panic(err)
  }
  describeRegionsRequest := &ecs20140526.DescribeRegionsRequest{}
  // Create a RuntimeOptions instance and configure runtime parameters. 
  runtime := &util.RuntimeOptions{}
  runtime.ReadTimeout = tea.Int(10000)
  resp, err := client.DescribeRegionsWithOptions(describeRegionsRequest, runtime)
  if err != nil {
    panic(err)
  }
  // The response, which contains the body and headers that are returned by the server side.
  body, err := json.Marshal(resp.Body)
  if err != nil {
    panic(err)
  }
  headers, err := json.Marshal(resp.Headers)
  if err != nil {
    panic(err)
  }
  fmt.Printf("body: %s\n", string(body))
  fmt.Printf("header: %s\n", string(headers))
}
  • The request method that is used when you call an API operation to upload images. By default, this request method requires runtime parameters. This method is available only for the API operations that you can call to upload files.

func main() {
    config := &openapi.Config{
    // Your AccessKey ID.
    AccessKeyId: tea.String("<AccessId>"),
    // Your AccessKey secret.
    AccessKeySecret: tea.String("<AccessSecret>"),
     // The region that you want to access.
    RegionId: tea.String("<RegionId>")
  }
  client, err := facebody20191230.NewClient(config)
  if err != nil {
    panic(err)
  }
  f, err := os.Open("The directory of the file that you want to upload in your computer")
  if err != nil {
    return err
  }
 request := &facebody20191230.DetectBodyCountAdvanceRequest{}
 request.ImageURLObject(f);
  // Create a RuntimeOptions instance and configure runtime parameters. 
  runtime := &util.RuntimeOptions{}
  runtime.ReadTimeout = tea.Int(10000)
  resp, err := client.DetectBodyCountAdvance(describeRegionsRequest, runtime)
  if err != nil {
    panic(err)
  }
  // The response, which contains the body and headers that are returned by the server side.
  body, err := json.Marshal(resp.Body)
  if err != nil {
    panic(err)
  }
  headers, err := json.Marshal(resp.Headers)
  if err != nil {
    panic(err)
  }
  fmt.Printf("body: %s\n", string(body))
  fmt.Printf("header: %s\n", string(headers))
}
Note

Each API operation has a unique request object and response object. The request object is named in the ${API}${Request} format. For example, a request object can be DescribeRegionRequest.