|
@@ -0,0 +1,43 @@
|
|
|
|
+package com.aizuda.boot.modules.noco.nocodb;
|
|
|
|
+
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class NocoDB {
|
|
|
|
+ private final NdProperties ndProperties;
|
|
|
|
+ private final RestTemplate restTemplate;
|
|
|
|
+
|
|
|
|
+ public NocoDB(RestTemplate restTemplate, NdProperties ndProperties) {
|
|
|
|
+ this.restTemplate = restTemplate;
|
|
|
|
+ this.ndProperties = ndProperties;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private HttpHeaders getHttpHeaders() {
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.set("Xc-Token", ndProperties.getXcToken());
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
+ return headers;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private HttpEntity<String> getHttpEntity() {
|
|
|
|
+ return new HttpEntity<>(getHttpHeaders());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public <T> T getForObject(String uri, Class<T> responseType, Object... uriVariables) {
|
|
|
|
+ return restTemplate.exchange(ndProperties.getApiUrl() + uri, HttpMethod.GET, getHttpEntity(), responseType, uriVariables).getBody();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public <T> T postForObject(String uri, Class<T> responseType, Object... uriVariables) {
|
|
|
|
+ return restTemplate.exchange(ndProperties.getApiUrl() + uri, HttpMethod.POST, getHttpEntity(), responseType, uriVariables).getBody();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public <T> T postForObject(String uri, String jsonBody, Class<T> responseType, Object... uriVariables) {
|
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(jsonBody, getHttpHeaders());
|
|
|
|
+ return restTemplate.exchange(ndProperties.getApiUrl() + uri, HttpMethod.POST, entity, responseType, uriVariables).getBody();
|
|
|
|
+ }
|
|
|
|
+}
|