Refactor code for consistent formatting and style.

This commit is contained in:
2025-04-14 09:50:35 +02:00
parent 97b7f8371a
commit 16a586c080
8 changed files with 70 additions and 106 deletions
@@ -31,22 +31,19 @@ import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
* managing authentication, and handling JSON serialization.
*/
@Slf4j
abstract class CfBasicHttpClient
{
abstract class CfBasicHttpClient {
private final String baseUrl;
private final String authEmail;
private final String authKey;
private final ObjectMapper objectMapper;
CfBasicHttpClient(String baseUrl, String authEmail, String authKey) throws IllegalArgumentException
{
if (authEmail == null || authEmail.isBlank())
{
CfBasicHttpClient(String baseUrl, String authEmail, String authKey)
throws IllegalArgumentException {
if (authEmail == null || authEmail.isBlank()) {
throw new IllegalArgumentException("Authentication email must not be null or blank!");
}
if (authKey == null || authKey.isBlank())
{
if (authKey == null || authKey.isBlank()) {
throw new IllegalArgumentException("Authentication key must not be null or blank!");
}
this.baseUrl = baseUrl;
@@ -55,8 +52,7 @@ abstract class CfBasicHttpClient
this.objectMapper = initObjectMapper();
}
private ObjectMapper initObjectMapper()
{
private ObjectMapper initObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
@@ -65,8 +61,7 @@ abstract class CfBasicHttpClient
return mapper;
}
private CloseableHttpClient createHttpClient()
{
private CloseableHttpClient createHttpClient() {
return HttpClients.custom()
.addRequestInterceptorFirst(
(request, context, execChain) -> {
@@ -82,11 +77,9 @@ abstract class CfBasicHttpClient
}
private <T extends AbstractResponse> T executeRequest(
ClassicHttpRequest request, Class<T> responseType) throws CloudflareApiException
{
ClassicHttpRequest request, Class<T> responseType) throws CloudflareApiException {
String logUri = null;
try (CloseableHttpClient client = createHttpClient())
{
try (CloseableHttpClient client = createHttpClient()) {
ResultWrapper result =
client.execute(
request,
@@ -96,11 +89,9 @@ abstract class CfBasicHttpClient
EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)));
logUri = request.getRequestUri();
if (result.statusCode >= 200 && result.statusCode < 300)
{
if (result.statusCode >= 200 && result.statusCode < 300) {
return objectMapper.readValue(result.responseBody, responseType);
} else
{
} else {
log.error(
"{} request failed for URL {}: Status {}",
request.getMethod(),
@@ -109,93 +100,67 @@ abstract class CfBasicHttpClient
throw new CloudflareApiException(
request.getMethod() + " request failed with status code: " + result.statusCode);
}
} catch (JsonProcessingException e)
{
} catch (JsonProcessingException e) {
log.error("JSON parsing error for request to {}", logUri, e);
throw new CloudflareApiException("Error processing JSON response", e);
} catch (Exception e)
{
} catch (Exception e) {
log.error("Error during request execution", e);
throw new CloudflareApiException("Request failed", e);
}
}
/**
* Sends a GET request to the given endpoint and maps the response.
*/
/** Sends a GET request to the given endpoint and maps the response. */
<T extends AbstractResponse> T getRequest(String endpoint, Class<T> responseType)
throws CloudflareApiException
{
throws CloudflareApiException {
HttpGet request = new HttpGet(buildUrl(endpoint));
return executeRequest(request, responseType);
}
/**
* Sends a DELETE request to the given endpoint and maps the response.
*/
<T extends AbstractResponse> T deleteRequest(String endpoint)
throws CloudflareApiException
{
/** Sends a DELETE request to the given endpoint and maps the response. */
<T extends AbstractResponse> T deleteRequest(String endpoint) throws CloudflareApiException {
HttpDelete request = new HttpDelete(buildUrl(endpoint));
return executeRequest(request, (Class<T>) codes.thischwa.cf.model.RecordSingleResponse.class);
}
/**
* Sends a POST request with a payload to the given endpoint and maps the response.
*/
/** Sends a POST request with a payload to the given endpoint and maps the response. */
<T extends AbstractResponse, R extends AbstractEntity> T postRequest(
String endpoint, R requestPayload) throws CloudflareApiException
{
String endpoint, R requestPayload) throws CloudflareApiException {
HttpPost request = new HttpPost(buildUrl(endpoint));
setRequestPayload(request, requestPayload);
return executeRequest(request, (Class<T>) codes.thischwa.cf.model.RecordSingleResponse.class);
}
/**
* Sends a PUT request with a payload to the given endpoint and maps the response.
*/
/** Sends a PUT request with a payload to the given endpoint and maps the response. */
<T extends AbstractResponse, R extends AbstractEntity> T putRequest(
String endpoint, R requestPayload, Class<T> responseType) throws CloudflareApiException
{
String endpoint, R requestPayload, Class<T> responseType) throws CloudflareApiException {
HttpPut request = new HttpPut(buildUrl(endpoint));
setRequestPayload(request, requestPayload);
return executeRequest(request, responseType);
}
/**
* Sends a PATCH request with a payload to the given endpoint and maps the response.
*/
/** Sends a PATCH request with a payload to the given endpoint and maps the response. */
<T extends AbstractResponse, R extends AbstractEntity> T patchRequest(
String endpoint, R requestPayload) throws CloudflareApiException
{
String endpoint, R requestPayload) throws CloudflareApiException {
HttpPatch request = new HttpPatch(buildUrl(endpoint));
setRequestPayload(request, requestPayload);
return executeRequest(request, (Class<T>) codes.thischwa.cf.model.RecordSingleResponse.class);
}
/**
* Sets the JSON payload for a request.
*/
/** Sets the JSON payload for a request. */
private <R extends AbstractEntity> void setRequestPayload(
BasicClassicHttpRequest request, R requestPayload) throws CloudflareApiException
{
try
{
BasicClassicHttpRequest request, R requestPayload) throws CloudflareApiException {
try {
request.setEntity(
new StringEntity(
objectMapper.writeValueAsString(requestPayload), ContentType.APPLICATION_JSON));
} catch (JsonProcessingException e)
{
} catch (JsonProcessingException e) {
throw new CloudflareApiException("Error serializing JSON payload", e);
}
}
private String buildUrl(String endpoint)
{
private String buildUrl(String endpoint) {
return baseUrl + endpoint;
}
private record ResultWrapper(int statusCode, String responseBody)
{
}
private record ResultWrapper(int statusCode, String responseBody) {}
}
@@ -82,10 +82,7 @@ public class CfDnsClient extends CfBasicHttpClient {
* @param authKey The API key for authenticating the client with Cloudflare services.
*/
public CfDnsClient(
boolean emptyResultThrowsException,
String baseUrl,
String authEmail,
String authKey) {
boolean emptyResultThrowsException, String baseUrl, String authEmail, String authKey) {
super(baseUrl, authEmail, authKey);
this.emptyResultThrowsException = emptyResultThrowsException;
}
+20 -22
View File
@@ -9,46 +9,44 @@ import lombok.Getter;
@Getter
public enum CfRequest {
/**
* Represents the API endpoint path for retrieving the list of DNS zones.
*/
/** Represents the API endpoint path for retrieving the list of DNS zones. */
ZONE_LIST("/zones"),
/**
* Represents the API endpoint path for retrieving information about a specific DNS zone
* by its name. The endpoint path supports a placeholder for the zone name, which needs
* to be provided to construct the complete path.
* Represents the API endpoint path for retrieving information about a specific DNS zone by its
* name. The endpoint path supports a placeholder for the zone name, which needs to be provided to
* construct the complete path.
*/
ZONE_INFO("/zones?name=%s"),
/**
* Represents the API endpoint path for creating a new DNS record within a specific DNS zone.
* The endpoint path includes a placeholder for the zone identifier, which needs to
* be provided to construct the complete path.
* Represents the API endpoint path for creating a new DNS record within a specific DNS zone. The
* endpoint path includes a placeholder for the zone identifier, which needs to be provided to
* construct the complete path.
*/
RECORD_CREATE("/zones/%s/dns_records"),
/**
* Represents the API endpoint path for retrieving information about a DNS record within a specific
* DNS zone by its name. The endpoint path includes placeholders for the zone identifier and
* the record name, which need to be provided to construct the complete path.
* Represents the API endpoint path for retrieving information about a DNS record within a
* specific DNS zone by its name. The endpoint path includes placeholders for the zone identifier
* and the record name, which need to be provided to construct the complete path.
*/
RECORD_INFO_NAME("/zones/%s/dns_records?name=%s"),
/**
* Represents the API endpoint path for retrieving information about a DNS record within a specific
* DNS zone by its name and type.
* The endpoint path includes placeholders for the zone identifier, record name, and record type,
* which need to be provided to construct the complete path.
* Represents the API endpoint path for retrieving information about a DNS record within a
* specific DNS zone by its name and type. The endpoint path includes placeholders for the zone
* identifier, record name, and record type, which need to be provided to construct the complete
* path.
*/
RECORD_INFO_NAME_TYPE("/zones/%s/dns_records?name=%s&type=%s"),
/**
* Represents the API endpoint path for updating an existing DNS record within a specific DNS zone.
* The endpoint path includes placeholders for the zone identifier and the record identifier,
* which need to be provided to construct the complete path.
* Represents the API endpoint path for updating an existing DNS record within a specific DNS
* zone. The endpoint path includes placeholders for the zone identifier and the record
* identifier, which need to be provided to construct the complete path.
*/
RECORD_UPDATE("/zones/%s/dns_records/%s"),
/**
* Represents the API endpoint path for deleting an existing DNS record within a specific DNS zone.
* The endpoint path includes placeholders for the zone identifier and the record identifier,
* which need to be provided to construct the complete path.
* Represents the API endpoint path for deleting an existing DNS record within a specific DNS
* zone. The endpoint path includes placeholders for the zone identifier and the record
* identifier, which need to be provided to construct the complete path.
*/
RECORD_DELETE("/zones/%s/dns_records/%s");
@@ -21,8 +21,10 @@ public class CloudflareApiException extends Exception {
/**
* Constructs a new CloudflareApiException with the specified detail message and cause.
*
* @param message the detail message, which provides additional context or information about the exception.
* @param cause the cause of this exception, which is the underlying throwable that triggered this exception.
* @param message the detail message, which provides additional context or information about the
* exception.
* @param cause the cause of this exception, which is the underlying throwable that triggered this
* exception.
*/
public CloudflareApiException(String message, Throwable cause) {
super(message, cause);
@@ -31,8 +33,8 @@ public class CloudflareApiException extends Exception {
/**
* Constructs a new CloudflareApiException with the specified cause.
*
* @param cause the cause of this exception, which is the underlying throwable
* that triggered this exception.
* @param cause the cause of this exception, which is the underlying throwable that triggered this
* exception.
*/
public CloudflareApiException(Throwable cause) {
super(cause);
@@ -12,8 +12,8 @@ public class CloudflareNotFoundException extends CloudflareApiException {
/**
* Constructs a new CloudflareNotFoundException with the specified detail message.
*
* @param message the detail message, which provides additional context about the "not found" error
* encountered during interaction with the Cloudflare API.
* @param message the detail message, which provides additional context about the "not found"
* error encountered during interaction with the Cloudflare API.
*/
public CloudflareNotFoundException(String message) {
super(message);
@@ -22,9 +22,10 @@ public class CloudflareNotFoundException extends CloudflareApiException {
/**
* Constructs a new CloudflareNotFoundException with the specified detail message and cause.
*
* @param message the detail message, which provides additional context about the "not found" error
* encountered during interaction with the Cloudflare API.
* @param cause the cause of this exception, which is the underlying throwable that triggered this exception.
* @param message the detail message, which provides additional context about the "not found"
* error encountered during interaction with the Cloudflare API.
* @param cause the cause of this exception, which is the underlying throwable that triggered this
* exception.
*/
public CloudflareNotFoundException(String message, Throwable cause) {
super(message, cause);
@@ -33,8 +34,8 @@ public class CloudflareNotFoundException extends CloudflareApiException {
/**
* Constructs a new CloudflareNotFoundException with the specified cause.
*
* @param cause the cause of this exception, which is the underlying throwable
* that triggered this exception.
* @param cause the cause of this exception, which is the underlying throwable that triggered this
* exception.
*/
public CloudflareNotFoundException(Throwable cause) {
super(cause);
@@ -40,10 +40,10 @@ public class RecordEntity extends AbstractEntity {
@Nullable private LocalDateTime createdOn;
/**
* Initializes a new instance of the RecordEntity class and invokes the parent constructor
* from the AbstractEntity class. The RecordEntity class represents a DNS record entity
* within a specific zone, encapsulating attributes such as type, name, content, TTL,
* and other related metadata.
* Initializes a new instance of the RecordEntity class and invokes the parent constructor from
* the AbstractEntity class. The RecordEntity class represents a DNS record entity within a
* specific zone, encapsulating attributes such as type, name, content, TTL, and other related
* metadata.
*/
public RecordEntity() {
super();
@@ -125,6 +125,7 @@ public enum RecordType {
* encrypted or signed email exchanges.
*
* <p>Key features include:
*
* <ul>
* <li>Use in Secure/Multipurpose Internet Mail Extensions (S/MIME)-based messaging.
* <li>Facilitating secure email communications by publishing certificates in DNS.