封装平台webhhok和bot权限接口,单测

This commit is contained in:
623756217 2023-02-27 20:25:16 +08:00
parent 6fac9b76e2
commit f2a1a8c95a
9 changed files with 227 additions and 6 deletions

24
pom.xml
View File

@ -127,12 +127,12 @@
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>junit</groupId>-->
<!-- <artifactId>junit</artifactId>-->
<!-- <version>4.13.2</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>io.springfox</groupId>
@ -144,6 +144,18 @@
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,119 @@
package com.gitlink.softbot.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitlink.softbot.global.vo.Response;
import com.gitlink.softbot.vo.Webhook;
import org.mapstruct.BeforeMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Component
public class GitLinkApi {
@Value("${gitlink.url}")
private String URL;
@Value("${gitlink.token}")
private String TOKEN;
@Autowired
private RestTemplate restTemplate;
private Map<String, String> headParams;
public Map<String, String> getHeader(){
Map<String, String> headParams = new HashMap<>();
headParams.put("Authorization", "Bearer "+TOKEN);
return headParams;
}
public Response getUserByUid(Integer uid){
Map<String, String> inputParams = new HashMap<>();
inputParams.put("uid", uid.toString());
Response response = RestTemplateUtil.httpRequest(restTemplate, HttpMethod.GET, URL+"/api", null, "/users/get_user_info.json", inputParams, null ,getHeader());
return response;
}
public Response getWebhookById(Integer uid, Object[] params,Integer webhookId) throws JsonProcessingException {
Map<String, String> inputParams = new HashMap<>();
inputParams.put("uid", uid.toString());
ObjectMapper mapper = new ObjectMapper();
Response response = RestTemplateUtil.httpRequest(restTemplate, HttpMethod.GET, URL+"/api/v1", params, "/webhooks/"+webhookId+".json", inputParams, null,getHeader());
return response;
}
public Response addWebhook(Integer uid, Object[] params, Webhook webhook) throws JsonProcessingException {
Map<String, String> inputParams = new HashMap<>();
inputParams.put("uid", uid.toString());
ObjectMapper mapper = new ObjectMapper();
Response response = RestTemplateUtil.httpRequest(restTemplate, HttpMethod.POST, URL+"/api/v1", params, "/webhooks.json", inputParams, mapper.writeValueAsString(webhook) ,getHeader());
return response;
}
public Response updateWebhook(Integer uid, Object[] params, Integer webhookId, Webhook webhook) throws JsonProcessingException {
Map<String, String> inputParams = new HashMap<>();
inputParams.put("uid", uid.toString());
ObjectMapper mapper = new ObjectMapper();
Response response = RestTemplateUtil.httpRequest(restTemplate, HttpMethod.PATCH, URL+"/api/v1", params, "/webhooks/"+webhookId+".json", inputParams, mapper.writeValueAsString(webhook) ,getHeader());
return response;
}
public Response deleteWebhook(Integer uid, Object[] params, Integer webhookId) {
Map<String, String> inputParams = new HashMap<>();
inputParams.put("uid", uid.toString());
ObjectMapper mapper = new ObjectMapper();
Response response = RestTemplateUtil.httpRequest(restTemplate, HttpMethod.DELETE, URL+"/api/v1", params, "/webhooks/"+webhookId+".json", inputParams,null, getHeader());
return response;
}
/**
* 调用平台中与bot 身份认证相关的接口auth_active, update_secret, update_private_key
*
* @param uid bot拥有者用户的uid
* @param botId bot主键id
* @param suffix 方法类别 auth_active, update_secret, update_private_key
* @return Response 统一响应对象
*/
public Response activeBotAuth(Integer uid, Integer botId, String suffix){
Map<String, String> inputParams = new HashMap<>();
inputParams.put("uid", uid.toString());
Object[] params = new Object[]{botId.toString()};
Response response = RestTemplateUtil.httpRequest(restTemplate, HttpMethod.POST, URL+"/app", params, "/"+suffix, inputParams, null ,getHeader());
return response;
}
// public Response activeBotAuth(Integer uid, Integer botId){
// Map<String, String> inputParams = new HashMap<>();
// inputParams.put("uid", uid.toString());
// Object[] params = new Object[]{botId.toString()};
// Response response = RestTemplateUtil.httpRequest(restTemplate, HttpMethod.POST, URL+"/app", params, "/auth_active", inputParams, null ,getHeader());
// return response;
// }
//
// public Response updateSecet(Integer uid, Integer botId){
// Map<String, String> inputParams = new HashMap<>();
// inputParams.put("uid", uid.toString());
// Object[] params = new Object[]{botId.toString()};
// Response response = RestTemplateUtil.httpRequest(restTemplate, HttpMethod.POST, URL+"/app", params, "/update_secret", inputParams, null ,getHeader());
// return response;
// }
//
// public Response updatePrivateKey(Integer uid, Integer botId){
// Map<String, String> inputParams = new HashMap<>();
// inputParams.put("uid", uid.toString());
// Object[] params = new Object[]{botId.toString()};
// Response response = RestTemplateUtil.httpRequest(restTemplate, HttpMethod.POST, URL+"/app", params, "/update_private_key", inputParams, null ,getHeader());
// return response;
// }
}

View File

@ -2,6 +2,8 @@ package com.gitlink.softbot.utils;
import com.gitlink.softbot.global.vo.Response;
import org.apache.logging.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
@ -17,6 +19,7 @@ import java.util.Objects;
public class RestTemplateUtil {
//private static Logger logger = (Logger) LoggerFactory.getLogger(RestTemplateUtil.class);
/**
* @Description 私有构造函数
*/

View File

@ -58,3 +58,8 @@ logging:
mapper : debug
elasticsearch:
url: localhost:9200
gitlink:
url: https://testforgeplus.trustie.net
token: eyJraWQiOiJUaEVSLVl3Ukg4TWYwOHM0UnJLUDYzXzZLWmVET2NZckZXcmdzN2VUVWdrIiwiYWxnIjoiSFM1MTIifQ.eyJpc3MiOiJHaXRMaW5rIiwiaWF0IjoxNjc1NzYyMDkwLCJqdGkiOiI0MzA1ZDUwZC01ZGRkLTQ0MzUtODMyNS1iZDczYmVhMWMxYjciLCJ1c2VyIjp7ImlkIjpudWxsLCJsb2dpbiI6bnVsbCwibWFpbCI6bnVsbH19.hpHCJeU4Jyz-DM2NBUdB-tQW_E0-tu9H3LoGhsJ7kPHkSsdXJCII0jxhyPb9gwDsgd8SnlRZF8tZDDjnZSoztQ

View File

@ -0,0 +1,77 @@
package com.gitlink.softbot.service.market;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gitlink.softbot.global.vo.Response;
import com.gitlink.softbot.utils.GitLinkApi;
import com.gitlink.softbot.vo.Webhook;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
public class GitLinkApiTest {
@Autowired
private GitLinkApi api;
private Integer uid = 85175;
private Webhook webhooks;
@Test
public void getUserTest(){
Response response = api.getUserByUid(uid);
System.out.println(response.getData());
}
@Test
public void activeBotAuthTest(){
Response response = api.activeBotAuth(84993,800,"auth_active");
System.out.println(response.getData());
}
@Test
public void updateSecretTest(){
Response response = api.activeBotAuth(84993,800,"update_secret");
System.out.println(response.getData());
}
@Test
public void updatePrivateKeyTest(){
Response response = api.activeBotAuth(84993,800,"update_private_key");
System.out.println(response.getData());
}
@Test
public void addWebhook() throws JsonProcessingException {
webhooks = new Webhook(true, "json", "GET", "123456", "http://localhost:10000", "*", new Object[]{"push"});
Object[] params = new Object[]{"xxq250", "ruoyi-vue-pro"};
Response response = api.addWebhook(85175,params,webhooks);
System.out.println(response.getData());
}
@Test
public void updateWebhook() throws JsonProcessingException {
webhooks = new Webhook(false, "json", "GET", "123456", "http://localhost:10000", "*", new Object[]{"push"});
Object[] params = new Object[]{"xxq250", "ruoyi-vue-pro"};
Response response = api.updateWebhook(85175,params,1082,webhooks);
System.out.println(response.getData());
}
@Test
public void deleteWebhook() throws JsonProcessingException {
Object[] params = new Object[]{"xxq250", "ruoyi-vue-pro"};
Response response = api.deleteWebhook(85175,params,1082);
System.out.println(response.getData());
}
@Test
public void getWebhook() throws JsonProcessingException {
Object[] params = new Object[]{"xxq250", "ruoyi-vue-pro"};
Response response = api.getWebhookById(85175,params,983);
System.out.println(response.getData());
}
}

View File

@ -58,3 +58,8 @@ logging:
mapper : debug
elasticsearch:
url: localhost:9200
gitlink:
url: https://testforgeplus.trustie.net
token: eyJraWQiOiJUaEVSLVl3Ukg4TWYwOHM0UnJLUDYzXzZLWmVET2NZckZXcmdzN2VUVWdrIiwiYWxnIjoiSFM1MTIifQ.eyJpc3MiOiJHaXRMaW5rIiwiaWF0IjoxNjc1NzYyMDkwLCJqdGkiOiI0MzA1ZDUwZC01ZGRkLTQ0MzUtODMyNS1iZDczYmVhMWMxYjciLCJ1c2VyIjp7ImlkIjpudWxsLCJsb2dpbiI6bnVsbCwibWFpbCI6bnVsbH19.hpHCJeU4Jyz-DM2NBUdB-tQW_E0-tu9H3LoGhsJ7kPHkSsdXJCII0jxhyPb9gwDsgd8SnlRZF8tZDDjnZSoztQ