package com.baosight.iplat4j.test.soa;
import java.util.ArrayList;
import java.util.List;
import com.github.vaibhavsinha.kong.impl.KongClient;
import com.github.vaibhavsinha.kong.model.admin.api.Api;
import com.github.vaibhavsinha.kong.model.admin.api.ApiList;
public class TestKongAdminApi {
public static void main(String[] args) {
deleteAllApi();
// testCreateApi();
// TestRetrieveApi();
// TestListApi();
// TestUpdateApi();
// TestUpdateOrCreateApi();
// TestDeleteApi("");
}
public static void deleteAllApi() {
ApiList apiList = TestListApi();
List<Api> lis = apiList.getData();
System.err.println(lis.size());
for (Api api : lis) {
TestDeleteApi(api.getName());
}
System.err.println("OK");
}
// api 创建
public static void testCreateApi() {
List<String> list = new ArrayList<>();
List<String> listHosts = new ArrayList<>();
List<String> listuris = new ArrayList<>();
// 当使用uris 参数时就会出现验证路由为空
listuris.add("/GateWayService");// 单独“/”是可以路由成功的
//listHosts.add("pages");
KongClient kongClient = new KongClient("http://192.168.99.100:8001/");
Api api = new Api();
api.setCreatedAt(new Long("1488830759005"));
api.setName("plon26");
api.setUpstreamUrl("http://localhost:8080/GateWayService/");
// api.setHosts(listHosts);
api.setMethods(list);
api.setUris(listuris);
api.setHttpIfTerminated(false);
api.setHttpsOnly(false);
api.setPreserveHost(false);
api.setRetries(5);
api.setStripUri(true);
api.setUpstreamConnectTimeout(60000);
api.setUpstreamReadTimeout(60000);
api.setUpstreamSendTimeout(60000);
Api response = kongClient.getApiService().createApi(api);
System.err.println(response);
}
// 检索api
public static Api TestRetrieveApi() {
Api response=null;
try {
KongClient kongClient = new KongClient("http://192.168.99.100:8001/");
response = kongClient.getApiService().getApi("plon26");
System.err.println(response);
} catch (Exception e) {
System.err.println(e.getMessage());
}
return response;
}
// 列举
public static ApiList TestListApi() {
KongClient kongClient = new KongClient("http://192.168.99.100:8001/");
// String id="8ef2a0ac-89f0-4406-a406-dfcaf41181ce";
// String upstreamUrl="http://baidu.com";
// String name="example02-api";
long retries = 5l;
long size = 100l;
// String offset="example02-api";
ApiList response = kongClient.getApiService().listApis(null, null, null, null, null, null);
System.err.println(response);
return response;
}
// 更新api
public static void TestUpdateApi() {
List<String> list = new ArrayList<>();
List<String> listHosts = new ArrayList<>();
List<String> listuris = new ArrayList<>();
listuris.add("/pages");
listuris.add("/ssa");
listHosts.add("platform.baosight.com");
listHosts.add("getkong.org");
list.add("GET");
list.add("HEAD");
KongClient kongClient = new KongClient("http://192.168.99.100:8001/");
Api api = new Api();
api.setCreatedAt(new Long("1488830759004"));
api.setName("plon04");
api.setUpstreamUrl("http://platform.baosight.com:8090/pages/viewpage.action?pageId=12553609");
api.setHosts(listHosts);
api.setMethods(list);
api.setUris(listuris);
api.setHttpIfTerminated(false);
api.setHttpsOnly(false);
api.setPreserveHost(false);
api.setRetries(5);
api.setStripUri(false);
api.setUpstreamConnectTimeout(60000);
api.setUpstreamReadTimeout(60000);
api.setUpstreamSendTimeout(60000);
Api response = kongClient.getApiService().updateApi("plon04", api);
System.err.println(response);
}
// 更新或创建api
public static void TestUpdateOrCreateApi() {
KongClient kongClient = new KongClient("http://192.168.99.100:8001/");
List<String> list = new ArrayList<>();
List<String> lisUris = new ArrayList<>();
//listHosts.add("gitddd.org");
lisUris.add("/users");
list.add("GET");
try {
Api api = new Api();
api.setCreatedAt(new Long(System.currentTimeMillis()));
api.setUris(lisUris);
api.setName("users");
api.setUpstreamUrl("http://iplat.baosight.com/users/sign_in");
api.setMethods(list);
api.setHttpIfTerminated(false);
api.setHttpsOnly(false);
api.setPreserveHost(false);
api.setRetries(5);
api.setStripUri(true);
api.setUpstreamConnectTimeout(60000);
api.setUpstreamReadTimeout(60000);
api.setUpstreamSendTimeout(60000);
Api response = kongClient.getApiService().createOrUpdateApi(api);
System.err.println(response);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
// 删除api
public static void TestDeleteApi(String name) {
KongClient kongClient = new KongClient("http://192.168.99.100:8001/");
kongClient.getApiService().deleteApi(name);
}
}