【APIG】注册API
引导式阅读
Java
【APIG】注册API
作者
C***
上架时间
2021-07-02 08:43:50

版本说明

本示例基于华为云SDK V3.0版本开发。

前提条件

拥有已实名认证的华为云账号,拥有jdk运行环境以及maven环境

代码示例

maven依赖

<dependencies> <dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-core</artifactId> <version>3.0.7-beta</version> </dependency> <dependency> <groupId>com.huawei.apigateway</groupId> <artifactId>java-sdk-core</artifactId> <version>3.0.12</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build>

代码说明

组装认证信息以及请求url

Request request = new Request(); request.setKey("{your ak string}"); request.setSecret("{your sk string}"); request.setMethod("POST"); request.setUrl("https://apig.cn-east-2.myhuaweicloud.com/v1.0/apigw/apis");

>说明:
ak、sk获取请参考具体请参考如何获取访问密钥

组装body体

Map<Object, Object> body = new HashMap<>(); body.put("name", "create_api_test"); body.put("auth_type", "NONE"); body.put("group_id", "{group_id}"); body.put("backend_type", "HTTP"); body.put("req_method", "GET"); body.put("req_protocol", "HTTPS"); body.put("req_uri", "/api_create_test"); body.put("type", 1); Map<Object, Object> backendApi = new HashMap<>(); backendApi.put("req_method","GET"); backendApi.put("req_protocol","HTTPS"); backendApi.put("req_uri","/my_backend_create_api_test"); backendApi.put("timeout", 30000); backendApi.put("url_domain","apig.example.com:7443"); body.put("backend_api", backendApi); request.setBody(JsonUtils.toJSON(body));

>说明:
具体参数含义请参考APIExplorer body体接收json字符串,可以灵活替换JSONObject为其他构造json的方法。

创建客户端

OkHttpClient client = new OkHttpClient() .newBuilder() .build();

>可选:
如果要跳过ssl认证,将上面代码替换为

OkHttpClient client = new OkHttpClient() .newBuilder() .hostnameVerifier((s, sslSession) -> true) .build();

返回示例

{ "name": "create_api_test", "type": 1, "version": "V0.0.1", "req_protocol": "HTTPS", "req_method": "GET", "req_uri": "/api_create_test", "auth_type": "NONE", "auth_opt": { "app_code_auth_type": "DISABLE" }, "tag": "", "cors": false, "match_mode": "NORMAL", "backend_type": "HTTP", "remark": "", "group_id": "a38685a25d48426d9615e69058202393", "body_remark": "", "result_normal_sample": "", "result_failure_sample": "", "authorizer_id": "", "roma_app_id": "", "domain_name": "", "tags": [], "id": "4bac4cf19b9e4bbbad8703f03d786bec", "status": 1, "arrange_necessary": 2, "register_time": "2020-08-21T08:30:12.954573511Z", "update_time": "2020-08-21T08:30:12.954574989Z", "group_name": "test", "run_env_name": "", "run_env_id": "", "publish_id": "", "roma_app_name": "", "backend_api": { "url_domain": "apig.example.com:7443", "version": "", "req_protocol": "HTTPS", "req_method": "GET", "req_uri": "/my_backend_create_api_test", "timeout": 30000, "remark": "", "vpc_status": 2, "livedata_status": 2, "authorizer_id": "", "id": "5710f3bb291b403e97433a6a8ba534f7", "status": 1, "register_time": "2020-08-21T08:30:12.95546622Z", "update_time": "2020-08-21T08:30:12.955467121Z", "vpc_info": "" }, "mock_info": null, "func_info": null, "req_params": [], "backend_params": [], "policy_https": [], "policy_functions": null, "policy_mocks": null }