doc-exports/docs/apiu/guidelines/apig-en-api-180328008.html
Chen, Junjie 97b6825626 APIU Guidelines 20240812 version
Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
Co-authored-by: Chen, Junjie <chenjunjie@huawei.com>
Co-committed-by: Chen, Junjie <chenjunjie@huawei.com>
2024-08-13 10:31:24 +00:00

79 lines
11 KiB
HTML

<a name="apig-en-api-180328008"></a><a name="apig-en-api-180328008"></a>
<h1 class="topictitle1">Sample Code</h1>
<div id="body1537444154811"><div class="section" id="apig-en-api-180328008__section1596319488437"><h4 class="sectiontitle">Scenario</h4><p id="apig-en-api-180328008__p1220414484384">This section describes how to call the API of a public cloud service by using Eclipse.</p>
<div class="p" id="apig-en-api-180328008__p363635184312">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:<ul id="apig-en-api-180328008__ul109498014112"><li id="apig-en-api-180328008__li119491103110"><strong id="apig-en-api-180328008__b38114539368">AccessService</strong>: an abstract class that merges the GET, POST, PUT, and DELETE methods into the access method.</li><li id="apig-en-api-180328008__li16949701211"><strong id="apig-en-api-180328008__b16692111103719">Demo</strong>: execution entry that simulates GET, POST, PUT, and DELETE requests.</li><li id="apig-en-api-180328008__li99492015115"><strong id="apig-en-api-180328008__b79386273717">AccessServiceImpl</strong>: implementation of the access method. The code required for API Gateway communication is included in the access method.</li></ul>
</div>
<p id="apig-en-api-180328008__p265415818244">You can download the sample code package from <a href="https://apig-demo.obs.eu-de.otc.t-systems.com/java/SdkDemo.zip" target="_blank" rel="noopener noreferrer">https://apig-demo.obs.eu-de.otc.t-systems.com/java/SdkDemo.zip</a>.</p>
<div class="notice" id="apig-en-api-180328008__note47372794014"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="apig-en-api-180328008__p1773727144016">The JDK version cannot be earlier than 1.8.</p>
</div></div>
</div>
<div class="section" id="apig-en-api-180328008__section98551696457"><h4 class="sectiontitle">Procedure</h4><ol id="apig-en-api-180328008__ol1515318170459"><li id="apig-en-api-180328008__li1115311720450">Download the sample code package and decompress it.</li><li id="apig-en-api-180328008__li1210954191115">Import the sample project to Eclipse.<div class="fignone" id="apig-en-api-180328008__fig16546145205014"><span class="figcap"><b>Figure 1 </b>Selecting an existing project</span><br><span><img id="apig-en-api-180328008__image1474131114720" src="en-us_image_0132432937.png" title="Click to enlarge" class="imgResize"></span></div>
<div class="fignone" id="apig-en-api-180328008__fig767232218519"><span class="figcap"><b>Figure 2 </b>Selecting the sample code file after decompression</span><br><span><img id="apig-en-api-180328008__image13419842205018" src="en-us_image_0132432939.png" title="Click to enlarge" class="imgResize"></span></div>
<div class="fignone" id="apig-en-api-180328008__fig159778103242"><span class="figcap"><b>Figure 3 </b>Structure of the project after importing</span><br><span><img id="apig-en-api-180328008__image128044312389" src="en-us_image_0132518357.png"></span></div>
</li><li id="apig-en-api-180328008__li1985133317503">Edit the main method in the <strong id="apig-en-api-180328008__b12111822184012">Demo.java</strong> file.<p id="apig-en-api-180328008__p2061535616161">Replace the bold texts with actual values. If you use other methods, such as POST, PUT, and DELETE, see the corresponding annotations.</p>
<p id="apig-en-api-180328008__p129401344175213">Replace the parameters in the URL, for example, <strong id="apig-en-api-180328008__b61192044135710">project_id</strong>.</p>
<p id="apig-en-api-180328008__p76929333513">For details on how to obtain your region name, service name, AK/SK, project ID, and domain ID, see <a href="apig-en-api-180328009.html">Obtaining Required Information</a>.</p>
<pre class="screen" id="apig-en-api-180328008__screen1282532262819">//<strong id="apig-en-api-180328008__b682822216285">TODO</strong>: Replace <strong id="apig-en-api-180328008__b1494717204464">eu-de</strong> with the name of the region in which the service to be accessed is located.
<strong id="apig-en-api-180328008__b3836162292818">private</strong> <strong id="apig-en-api-180328008__b14838142214284">static</strong> <strong id="apig-en-api-180328008__b9840162252820">final</strong> String <strong id="apig-en-api-180328008__b168426222280"><em id="apig-en-api-180328008__i78434223289">region</em></strong> = "<strong id="apig-en-api-180328008__b984611224288">eu-de</strong>";
//<strong id="apig-en-api-180328008__b485092242814">TODO</strong>: Replace <strong id="apig-en-api-180328008__b1289103244620">vpc</strong> with the name of the service you want to access. For example, ecs, vpc, iam, and elb.
<strong id="apig-en-api-180328008__b1085511229289">private</strong> <strong id="apig-en-api-180328008__b148569223286">static</strong> <strong id="apig-en-api-180328008__b2858922162819">final</strong> String <strong id="apig-en-api-180328008__b1886002219283"><em id="apig-en-api-180328008__i08607227287">serviceName</em></strong> = "<strong id="apig-en-api-180328008__b10864422132819">vpc</strong>";
<strong id="apig-en-api-180328008__b586872292810">public</strong> <strong id="apig-en-api-180328008__b387022202811">static</strong> <strong id="apig-en-api-180328008__b9872422122810">void</strong> main(String[] args) <strong id="apig-en-api-180328008__b1687452216287">throws</strong> UnsupportedEncodingException
{
//<strong id="apig-en-api-180328008__b14878522192811">TODO</strong>: Replace the AK and SK with those obtained on the <strong id="apig-en-api-180328008__b96051419463">My Credential</strong> page.
String ak = "<strong id="apig-en-api-180328008__b1788482282817">ZIRRKMTWPTQFQI1WKNKB</strong>";
String sk = "<strong id="apig-en-api-180328008__b15888122142815">Us0mdMNHk******YrRCnW0ecfzl</strong>";
//<strong id="apig-en-api-180328008__b138911622102819">TODO</strong>: To specify a project ID (multi-project scenarios), add the X-Project-Id header.
//<strong id="apig-en-api-180328008__b188965224282">TODO</strong>: To access a global service, such as IAM, DNS, CDN, and TMS, add the X-Domain-Id header to specify an account ID.
//<strong id="apig-en-api-180328008__b690112219287">TODO</strong>: To add a header, find "Add special headers" in the <strong id="apig-en-api-180328008__b1545410554465">AccessServiceImple.java</strong> file.
//<strong id="apig-en-api-180328008__b18907182213284">TODO</strong>: Test the API
String url = "<strong id="apig-en-api-180328008__b20913522132813">https://vpc.eu-de.otc.t-systems.com/v1/{project_id}/vpcs/{vpc_id}</strong>";
<em id="apig-en-api-180328008__i1291714227289">get</em>(ak, sk, url);
//<strong id="apig-en-api-180328008__b7926142272813">TODO</strong>: When creating a VPC, replace <em id="apig-en-api-180328008__i6496148124718">{project_id}</em> in postUrl with the actual value.
//String postUrl = "https://vpc.eu-de.otc.t-systems.com/v1/{project_id}/cloudservers";
//String postbody ="{\"<u id="apig-en-api-180328008__u393210221281">vpc</u>\": {\"name\": \"vpc\",\"cidr\": \"192.168.0.0/16\"}}";
//post(ak, sk, postUrl, postbody);
//<strong id="apig-en-api-180328008__b39371722132816">TODO</strong>: When querying a VPC, replace <em id="apig-en-api-180328008__i29291714154718">{project_id}</em> in url with the actual value.
//String url = "https://vpc.eu-de.otc.t-systems.com/v1/{project_id}/vpcs/{vpc_id}";
//get(ak, sk, url);
//<strong id="apig-en-api-180328008__b795092292816">TODO</strong>: When updating a VPC, replace <em id="apig-en-api-180328008__i96031294474">{project_id}</em> and <em id="apig-en-api-180328008__i3423933194716">{vpc_id}</em> in putUrl with the actual values.
//String putUrl = "https://vpc.eu-de.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);
//<strong id="apig-en-api-180328008__b1996262292817">TODO</strong>: When deleting a VPC, replace <em id="apig-en-api-180328008__i1564824510475">{project_id}</em> and <em id="apig-en-api-180328008__i3443250124716">{vpc_id}</em> in deleteUrl with the actual values.
//String deleteUrl = "https://vpc.eu-de.otc.t-systems.com/v1/{project_id}/vpcs/{vpc_id}";
//delete(ak, sk, deleteUrl);
}</pre>
</li><li id="apig-en-api-180328008__li692519271935">(Optional) To call a service API of a sub-project or to add a self-defined header, perform the following steps:<ol type="a" id="apig-en-api-180328008__ol12427135611263"><li id="apig-en-api-180328008__li54271856142617">In the main method in the <strong id="apig-en-api-180328008__b112361621145020">Demo.java</strong> file, replace <strong id="apig-en-api-180328008__b5766162611504">project_id</strong> with the sub-project ID of the API.<pre class="screen" id="apig-en-api-180328008__screen11631945182617">//<strong id="apig-en-api-180328008__b763444562619">TODO</strong>: Test the API
String url = "https://vpc.eu-de.otc.t-systems.com/v1/{<strong id="apig-en-api-180328008__b363934516261">project_id</strong>}/vpcs/{vpc_id}";
<em id="apig-en-api-180328008__i1164219452266">get</em>(ak, sk, url);</pre>
</li><li id="apig-en-api-180328008__li11427145613263"><a name="apig-en-api-180328008__li11427145613263"></a><a name="li11427145613263"></a>Locate the following lines in the <strong id="apig-en-api-180328008__b117131855110">AccessServiceImpl.java</strong> file, delete "//" to activate the code line, and replace the sub-project ID with the actual one.<pre class="screen" id="apig-en-api-180328008__screen17729190153516">//<strong id="apig-en-api-180328008__b167326033518">TODO</strong>: Add special headers.
//request.addHeader("X-Project-Id", "<strong id="apig-en-api-180328008__b77328083512">xxxxx</strong>");</pre>
</li><li id="apig-en-api-180328008__li47751621276">Repeat <a href="#apig-en-api-180328008__li11427145613263">4.b</a> to add other self-defined headers.</li></ol>
</li><li id="apig-en-api-180328008__li18412306320">Compile and run the API calling code.<p id="apig-en-api-180328008__p188281527115415"><a name="apig-en-api-180328008__li18412306320"></a><a name="li18412306320"></a>Find <strong id="apig-en-api-180328008__b350175610616">Demo.java</strong> in the left pane of the Package Explorer, right-click, and choose <strong id="apig-en-api-180328008__b1685091315113">Run AS</strong> &gt; <strong id="apig-en-api-180328008__b340219171313">Java Application</strong>.</p>
<p id="apig-en-api-180328008__p20946560369">View the API call logs on the console.</p>
</li></ol>
</div>
<p id="apig-en-api-180328008__p56708187102740"></p>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="apig-en-api-180328004.html">AK/SK Authentication</a></div>
</div>
</div>
<script language="JavaScript">
<!--
image_size('.imgResize');
var msg_imageMax = "view original image";
var msg_imageClose = "close";
//--></script>