Updated on 2024-01-26 GMT+08:00

Go

Scenarios

To use Go to call an API through App authentication, obtain the Go SDK, create a project, and then call the API by referring to the API calling example.

Prerequisites

  • You have obtained API calling information. For details, see Preparations.
  • You have installed the development tool and Go development environment. For details, see Preparations.
  • You have installed the Go plug-in on IntelliJ IDEA. Otherwise, install it according to Figure 1.
    Figure 1 Installing the Go plug-in

Obtaining the SDK

Old version: Log in to the ROMA Connect console, choose API Connect > API Calling > SDKs, and download the SDK.

New version: Log in to the ROMA Connect console, choose API Connect > Credentials > SDKs, and download the SDK.

The following shows the directory structure after the decompression.

Name

Description

core\escape.go

SDK code

core\signer.go

demo.go

Sample code

Creating a Project

  1. Start IntelliJ IDEA and choose File > New > Project.

    On the displayed New Project page, choose Go and click Next.

    Figure 2 New Project
  2. Click ..., select the directory where the SDK is decompressed, and click Finish.
    Figure 3 Selecting the SDK directory after decompression
  3. View the directory structure of the project.
    Figure 4 Directory structure of the new project

    Modify the parameters in sample code demo.go as required. For details about the sample code, see API Calling Example.

API Calling Example

  1. Import the Go SDK (signer.go) to the project.
    import "apig-sdk/go/core"
  2. Generate a signer and enter the key and secret of the authorized credential. For details about how to obtain the information, see Obtaining API Calling Information.
    s := core.Signer{
    // Directly writing AK/SK in code is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables. 
     // In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK. 
            Key: os.Getenv("HUAWEICLOUD_SDK_AK"),
            Secret:os.Getenv("HUAWEICLOUD_SDK_SK"),
    }
  3. Generate a request, and specify the domain name, method, request URL, query, and body. For details about how to obtain the information, see Obtaining API Calling Information.
    r, _ := http.NewRequest("POST", "http://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/api?a=1&b=2",
                             ioutil.NopCloser(bytes.NewBuffer([]byte("foo=bar"))))
  4. Add the x-stage header to the request to specify the environment. Add other headers to sign as required.
    r.Header.Add("x-stage", "RELEASE")
  5. Execute the following function to add the X-Sdk-Date and Authorization headers for signing:
    s.Sign(r)
  6. If the subdomain name allocated by the system is used to access the API of HTTPS requests, ignore the certificate verification. Otherwise, skip this step.
    client:=&http.Client{
      Transport:&http.Transport{
        TLSClientConfig:&tls.Config{InsecureSkipVerify:true},
      },
    }
  7. Access the API and view the access result.
    resp, err := http.DefaultClient.Do(r)
    body, err := ioutil.ReadAll(resp.Body)