Generate Demo Codes

最終更新日:2022-05-06 17:45:53

Demo Codes

When developers doing API calling test on API Explorer, the Explorer will generate demo codes to assist developers to simplify their developing. At first period, API Explorer provides demo codes with Java and Go language.

Please note demo codes on screen are for reference only. Direct copy and running will not work. You need to press Download Demo to download the codes with necessary headers or index and then integrate to your own system.
[Feature Upgrade] Advanced Access Control

JAVA

With JAVA language, demo codes include:

Content Description
api.models Request, response parameter object. Change based on need
-{xxx}Request.java Request parameters
-{xxx}Response.java Response parameters
-Parameters.java URL request parameter
-Paths.java Path parameter
-RequestHeader.java Request parameter
-ResponseHeader.java Response parameter
auth authentication logic
common common package
exception Exception
model General request object
util General tool
Client.java Request entry. Change based on need

When using the downloaded demo codes, developers only need to change parameters under api.models and Client.java. If you want to integrate several demo codes altogether, please rename Client.java. One API, one Client. After rename Client.java, you can do:

  1. overwrite operation.
  2. copy {xxx}Request.java, {xxx}Response.java and changed Client.java under root directory in api.model to same category under another demo code package

Client.java example:

package com.cnc.wplus;
 
import com.alibaba.fastjson.JSON;
import com.cnc.wplus.api.models.*;
import com.cnc.wplus.auth.*;
import com.cnc.wplus.model.*;

public class Client {
 
    public static void main(String[] args) {
        // build Request parameter
        // JSON.toJSONString(exampleRequest.toMap()) turn object to JSON string
        ExampleRequest exampleRequest = new ExampleRequest();
 
        // Authentication Info
        AkSkConfig akskConfig = new AkSkConfig();
        akskConfig.setAccessKey("{accessKey}");
        akskConfig.setSecretKey("{secretKey}");
        // EndPoint the domain you want to visit, input akskConfig.setEndPoint("api.cdnetworks.com")
        akskConfig.setEndPoint("{endPoint}");
        // the URI you are going to visit. It is auto filled in when you download the demo codes
        akskConfig.setUri("/example");
        akskConfig.setMethod("POST");
 
        // Request 
        // invoke(AkSkConfig, Body)
        String response = AkSkAuth.invoke(akskConfig, JSON.toJSONString(exampleRequest.toMap()));
        System.out.println(response);
    }
}

Requested environment

  • JDK version 7 or above
  • Maven is relied management tool for JAVA. It supports the requested configuration you need and will help to install it to your system. Please visit Maven website to download installation package and administrator guide.

GO

With JAVA language, demo codes include:

Content Description
api Request, response parameter object. Change based on need
- client.go Request, Reponse parameters
Client.go Request entry

When using the downloaded demo codes, developers only need to change Client.go and client.go under api.client. If you want to integrate several demo codes altogether, please rename Client.go and client.go under api.client. One API, one Client.go and client.go. After remane, you can do:

  1. overwrite operation.
  2. move the two renamed files to previous place.

Client.go example:

package main
 
import (
"fmt"
"openApi-authentication/common/auth"
"openApi-authentication/api/client"
)
 
func main() {
 
    // build request parameter
    exampleRequest := client.exampleRequest{}
     
    // Authentication info
    var config auth.AkskConfig
    config.AccessKey = "{accessKey}"
    config.SecretKey = "{secretKey}"
    // EndPoint the domain you are going to visit,input config.EndPoint = "{api.cdnetworks.com}";
    config.EndPoint = "{endPoint}"
    // the URI you are going to visit. It is auto filled in when you download the demo codes
    config.Uri = "/example"
    config.Method = "POST"
 
    //Request entry
    // invoke(config, Body)
    response := auth.Invoke(config, exampleRequest.String())
    fmt.Printf("response body is %#v\n", response)
}