Sample Code¶
Scenario¶
This section describes how to call the API of a public cloud service by using Eclipse.
The following code shows how to sign a request and how to use an HTTP client to send an HTTPS request. The code is categorized into three classes:
AccessService: an abstract class that merges the GET, POST, PUT, and DELETE methods into the access method.
Demo: execution entry that simulates GET, POST, PUT, and DELETE requests.
AccessServiceImpl: implementation of the access method. The code required for API Gateway communication is included in the access method.
You can download the sample code package from https://apig-demo.obsEndpoint/java/SdkDemo.zip.
You can download the sample code package from https://apig-demo.obs.eu-de.otc.t-systems.com/java/SdkDemo.zip.
Important
The JDK version cannot be earlier than 1.8.
Procedure¶
Download the sample code package and decompress it.
Import the sample project to Eclipse.
Edit the main method in the Demo.java file.
Replace the bold texts with actual values. If you use other methods, such as POST, PUT, and DELETE, see the corresponding annotations.
Replace the parameters in the URL, for example, project_id.
For details on how to obtain your region name, service name, AK/SK, project ID, and domain ID, see Obtaining Required Information.
//TODO: Replace eu-ch2 with the name of the region in which the service to be accessed is located. private static final String region = "eu-ch2"; //TODO: Replace vpc with the name of the service you want to access. For example, ecs, vpc, iam, and elb. private static final String serviceName = "vpc"; public static void main(String[] args) throws UnsupportedEncodingException { //TODO: Replace the AK and SK with those obtained on the My Credential page. String ak = "ZIRRKMTWPTQFQI1WKNKB"; String sk = "Us0mdMNHk******YrRCnW0ecfzl"; //TODO: To specify a project ID (multi-project scenarios), add the X-Project-Id header. //TODO: To access a global service, such as IAM, DNS, CDN, and TMS, add the X-Domain-Id header to specify an account ID. //TODO: To add a header, find "Add special headers" in the AccessServiceImple.java file. //TODO: Test the API String url = "https://vpc.eu-ch2.sc.otc.t-systems.com/v1/{project_id}/vpcs/{vpc_id}"; get(ak, sk, url); //TODO: When creating a VPC, replace {project_id} in postUrl with the actual value. //String postUrl = "https://vpc.eu-ch2.sc.otc.t-systems.com/v1/{project_id}/cloudservers"; //String postbody ="{\"vpc\": {\"name\": \"vpc\",\"cidr\": \"192.168.0.0/16\"}}"; //post(ak, sk, postUrl, postbody); //TODO: When querying a VPC, replace {project_id} in url with the actual value. //String url = "https://vpc.eu-ch2.sc.otc.t-systems.com/v1/{project_id}/vpcs/{vpc_id}"; //get(ak, sk, url); //TODO: When updating a VPC, replace {project_id} and {vpc_id} in putUrl with the actual values. //String putUrl = "https://vpc.eu-ch2.sc.otc.t-systems.com/v1/{project_id}/vpcs/{vpc_id}"; //String putbody ="{\"vpc\":{\"name\": \"vpc1\",\"cidr\": \"192.168.0.0/16\"}}"; //put(ak, sk, putUrl, putbody); //TODO: When deleting a VPC, replace {project_id} and {vpc_id} in deleteUrl with the actual values. //String deleteUrl = "https://vpc.eu-ch2.sc.otc.t-systems.com/v1/{project_id}/vpcs/{vpc_id}"; //delete(ak, sk, deleteUrl); }
(Optional) To call a service API of a sub-project or to add a self-defined header, perform the following steps:
In the main method in the Demo.java file, replace project_id with the sub-project ID of the API.
//TODO: Test the API String url = "https://vpc.eu-ch2.sc.otc.t-systems.com/v1/{project_id}/vpcs/{vpc_id}"; get(ak, sk, url);
Locate the following lines in the AccessServiceImpl.java file, delete "//" to activate the code line, and replace the sub-project ID with the actual one.
//TODO: Add special headers. //request.addHeader("X-Project-Id", "xxxxx");
c. Repeat 4.b to add other self-defined headers.
Compile and run the API calling code.
Find Demo.java in the left pane of the Package Explorer, right-click, and choose Run AS > Java Application.
View the API call logs on the console.