1、nVisual API接口作用
nVisual API为用户提供访问nVisual的程序接口。可用作web浏览器接口调用或第三方操作平台调用来实现操作nVIsual。
2、nVisual API接口定义规范
nVisual API接口整体采用resful风格
URL路径规则:
ip:端口/wapi/v1/...
示例:demo.nvisual.com:8081/wapi/v1/authenticate
请求方式:
GET:从服务端获取资源
POST:新建资源到服务端
PUT:更新服务端资源(客户端提供改变后的完整资源)
DELETE:删除服务器资源
示例:
GET /wapi/v1/nodes 查询所有的节点
GET /wapi/v1/nodes/{nodeId} 通过nodeId查询节点
POST /wapi/v1/nodes 创建节点
PUT /wapi/v1/nodes/{nodeId} 通过nodeId修改节点信息
DELETE /wapi/v1/nodes/{nodeId} 通过nodeId删除节点和节点下的子节点
请求参数:
request header:可存放token
地址栏参数:如/wapi/v1/nodes/{nodeId}中{nodeId}
请求body数据:入参数据
响应格式:
{
"code": 200,
"message": "success",
"data": {}
}
具体返回详见具体API接口
3、nVisual API接口调用步骤
postman调用测试:
1、访问获取token /wapi/v1/authenticate POST
body参数{"username":"","passward":""}
2、获取token后,访问其他API接口,例如根据nodeId获取节点(node)信息
httpClient调用测试:
httpClient调用时将token放入请求头中。key值为Authorization,value值为"Bearer "+获取到的token。
4、nVisual API 接口调用代码示例(JAVA示例、PYTHONC#后续补充)
JAVA通过httpClient调用nVIsual API接口:
1、获取token
public String getToken(){
//获取token
String token = "";
Map
map.put("username","dongjunjie");
map.put("password","");
String jsonString = JSON.toJSONString(map);
try {
String responseToken = HttpUtil.postJSON(nvisualApiConfig.getNvisualUrl()+"/wapi/v1/authenticate",jsonString);
Map responseTokenMap = JSONObject.parseObject(responseToken,Map.class);
if("200".equals(responseTokenMap.get("code").toString())){
Map
token = (String) data.get("access_token");
}
} catch (Exception e) {
token = "";
}
return token;
}
httpClient工具类:
/*
* json格式的参数 post请求
* 获取json数据
* */
public static String postJSON(String url,String paramjson){
HttpPost post = new HttpPost(url);
HttpClient httpClient = HttpClientBuilder.create().build();
try {
StringEntity stringEntity = new StringEntity(paramjson, ContentType.APPLICATION_JSON);
post.setEntity(stringEntity);
HttpResponse response = httpClient.execute(post);
if(response.getStatusLine().getStatusCode()==200){
//获取响应的内容对象
HttpEntity httpEntity = response.getEntity();
//转换结果位字符串
return EntityUtils.toString(httpEntity);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
2、根据nodeId获取node示例
public Nodes getNodesById(Long nodeId,String token){
Nodes nodes = null;
String nodesJsonString= HttpClient.getJSON(nvisualApiConfig.getNvisualUrl()+"/wapi/v1/nodes/"+nodeId,"Bearer "+token);
Map responseMap = JSONObject.parseObject(nodesJsonString,Map.class);
if("200".equals(responseMap.get("code").toString())){
nodes = JSON.toJavaObject((JSON) responseMap.get("data"),Nodes.class);
}
return nodes;
}
httpClient中getJSON方法
public static String getJSON(String url,String token){
//1、创建请求方式对象
HttpGet get = new HttpGet(url);
//2、创建客户端对象
HttpClient client = HttpClientBuilder.create().build();
HttpHeaders headers=new HttpHeaders();
headers.setBearerAuth(token);
//设置token
get.addHeader("Authorization", token);
// 传输的类型
get.addHeader("Content-Type", "application/x-www-form-urlencoded");
try {
//3、获取请求结果
HttpResponse response = client.execute(get);
//4、校验http状态码
if(response.getStatusLine().getStatusCode()==200){
//获取响应的内容对象
HttpEntity httpEntity = response.getEntity();
//转换结果位字符串
return EntityUtils.toString(httpEntity);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}