forked from Hithomelabs/CFTunnels
ISSUE-44: Hithomelabs/HomeLabDocker#44 Decoupling the controller and the service layer
This commit is contained in:
parent
a1275ec06c
commit
316dd6b01e
@ -0,0 +1,61 @@
|
||||
package com.hithomelabs.CFTunnels.Services;
|
||||
|
||||
import com.hithomelabs.CFTunnels.Config.CloudflareConfig;
|
||||
import com.hithomelabs.CFTunnels.Headers.AuthKeyEmailHeader;
|
||||
import com.hithomelabs.CFTunnels.Models.Config;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class CloudflareAPIService {
|
||||
|
||||
@Autowired
|
||||
CloudflareConfig cloudflareConfig;
|
||||
|
||||
@Autowired
|
||||
AuthKeyEmailHeader authKeyEmailHeader;
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
public ResponseEntity<Map> getCloudflareTunnels() {
|
||||
|
||||
// * * Resource URL to hit get request at
|
||||
String url = "https://api.cloudflare.com/client/v4/accounts/" + cloudflareConfig.getAccountId() + "/cfd_tunnel";
|
||||
|
||||
HttpEntity<String> httpEntity = new HttpEntity<>("", authKeyEmailHeader.getHttpHeaders());
|
||||
ResponseEntity<Map> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Map.class);
|
||||
return responseEntity;
|
||||
}
|
||||
|
||||
public <T> ResponseEntity<T> getCloudflareTunnelConfigurations(String tunnelId, RestTemplate restTemplate, Class<T> responseType) {
|
||||
|
||||
// * * Resource URL to hit get request at
|
||||
String url = "https://api.cloudflare.com/client/v4/accounts/" + cloudflareConfig.getAccountId() + "/cfd_tunnel/" + tunnelId + "/configurations";
|
||||
|
||||
HttpEntity<String> httpEntity = new HttpEntity<>("",authKeyEmailHeader.getHttpHeaders());
|
||||
ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, responseType);
|
||||
return responseEntity;
|
||||
}
|
||||
|
||||
public <T> ResponseEntity<T> putCloudflareTunnelConfigurations(String tunnelId, RestTemplate restTemplate, Class<T> responseType, Config config) {
|
||||
|
||||
// * * Resource URL to hit get request at
|
||||
String url = "https://api.cloudflare.com/client/v4/accounts/" + cloudflareConfig.getAccountId() + "/cfd_tunnel/" + tunnelId + "/configurations";
|
||||
|
||||
HttpHeaders httpHeaders = authKeyEmailHeader.getHttpHeaders();
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<Config> entity = new HttpEntity<>(config, httpHeaders);
|
||||
ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, entity, responseType);
|
||||
return responseEntity;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user