From 16a586c08013730a9c799d4a26a4b2c633bdb789 Mon Sep 17 00:00:00 2001 From: Thilo Schwarz Date: Mon, 14 Apr 2025 09:50:35 +0200 Subject: [PATCH] Refactor code for consistent formatting and style. --- .../codes/thischwa/cf/CfBasicHttpClient.java | 93 ++++++------------- .../java/codes/thischwa/cf/CfDnsClient.java | 5 +- .../java/codes/thischwa/cf/CfRequest.java | 42 ++++----- .../thischwa/cf/CloudflareApiException.java | 10 +- .../cf/CloudflareNotFoundException.java | 15 +-- .../thischwa/cf/model/PagingRequest.java | 2 +- .../codes/thischwa/cf/model/RecordEntity.java | 8 +- .../codes/thischwa/cf/model/RecordType.java | 1 + 8 files changed, 70 insertions(+), 106 deletions(-) diff --git a/src/main/java/codes/thischwa/cf/CfBasicHttpClient.java b/src/main/java/codes/thischwa/cf/CfBasicHttpClient.java index eeebc9c..d3c428f 100644 --- a/src/main/java/codes/thischwa/cf/CfBasicHttpClient.java +++ b/src/main/java/codes/thischwa/cf/CfBasicHttpClient.java @@ -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 executeRequest( - ClassicHttpRequest request, Class responseType) throws CloudflareApiException - { + ClassicHttpRequest request, Class 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 getRequest(String endpoint, Class 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 deleteRequest(String endpoint) - throws CloudflareApiException - { + /** Sends a DELETE request to the given endpoint and maps the response. */ + T deleteRequest(String endpoint) throws CloudflareApiException { HttpDelete request = new HttpDelete(buildUrl(endpoint)); return executeRequest(request, (Class) 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 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) 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 putRequest( - String endpoint, R requestPayload, Class responseType) throws CloudflareApiException - { + String endpoint, R requestPayload, Class 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 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) codes.thischwa.cf.model.RecordSingleResponse.class); } - /** - * Sets the JSON payload for a request. - */ + /** Sets the JSON payload for a request. */ private 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) {} } diff --git a/src/main/java/codes/thischwa/cf/CfDnsClient.java b/src/main/java/codes/thischwa/cf/CfDnsClient.java index 580a411..41b485a 100644 --- a/src/main/java/codes/thischwa/cf/CfDnsClient.java +++ b/src/main/java/codes/thischwa/cf/CfDnsClient.java @@ -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; } diff --git a/src/main/java/codes/thischwa/cf/CfRequest.java b/src/main/java/codes/thischwa/cf/CfRequest.java index cebdcdf..93c19ef 100644 --- a/src/main/java/codes/thischwa/cf/CfRequest.java +++ b/src/main/java/codes/thischwa/cf/CfRequest.java @@ -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"); diff --git a/src/main/java/codes/thischwa/cf/CloudflareApiException.java b/src/main/java/codes/thischwa/cf/CloudflareApiException.java index 96bb579..b10833e 100644 --- a/src/main/java/codes/thischwa/cf/CloudflareApiException.java +++ b/src/main/java/codes/thischwa/cf/CloudflareApiException.java @@ -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); diff --git a/src/main/java/codes/thischwa/cf/CloudflareNotFoundException.java b/src/main/java/codes/thischwa/cf/CloudflareNotFoundException.java index f6f7f66..657e390 100644 --- a/src/main/java/codes/thischwa/cf/CloudflareNotFoundException.java +++ b/src/main/java/codes/thischwa/cf/CloudflareNotFoundException.java @@ -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); diff --git a/src/main/java/codes/thischwa/cf/model/PagingRequest.java b/src/main/java/codes/thischwa/cf/model/PagingRequest.java index 28c270d..fcdab6c 100644 --- a/src/main/java/codes/thischwa/cf/model/PagingRequest.java +++ b/src/main/java/codes/thischwa/cf/model/PagingRequest.java @@ -54,7 +54,7 @@ public class PagingRequest { * Retrieves the pagination parameters in a key-value map format. * * @return a map containing the pagination parameters, where the key "page" indicates the current - * page number and the key "perPage" indicates the number of items per page. + * page number and the key "perPage" indicates the number of items per page. */ public Map getPagingParams() { return Map.of("page", String.valueOf(page), "perPage", String.valueOf(perPage)); diff --git a/src/main/java/codes/thischwa/cf/model/RecordEntity.java b/src/main/java/codes/thischwa/cf/model/RecordEntity.java index cddef75..505e4fe 100644 --- a/src/main/java/codes/thischwa/cf/model/RecordEntity.java +++ b/src/main/java/codes/thischwa/cf/model/RecordEntity.java @@ -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(); diff --git a/src/main/java/codes/thischwa/cf/model/RecordType.java b/src/main/java/codes/thischwa/cf/model/RecordType.java index 8b0f28d..5fa911f 100644 --- a/src/main/java/codes/thischwa/cf/model/RecordType.java +++ b/src/main/java/codes/thischwa/cf/model/RecordType.java @@ -125,6 +125,7 @@ public enum RecordType { * encrypted or signed email exchanges. * *

Key features include: + * *

    *
  • Use in Secure/Multipurpose Internet Mail Extensions (S/MIME)-based messaging. *
  • Facilitating secure email communications by publishing certificates in DNS.