更新时间:2022-05-06 17:45:53
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.
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:
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);
}
}
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:
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)
}