reducing code smells

This commit is contained in:
2026-01-23 18:32:54 +01:00
parent 9e09b12dc3
commit 4a3ec42fa5
3 changed files with 37 additions and 22 deletions
@@ -15,8 +15,10 @@ import codes.thischwa.cf.model.ZoneMultipleResponse;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -297,9 +299,9 @@ public class CfDnsClient extends CfBasicHttpClient {
RecordSingleResponse resp = postRequest(endpoint, rec, RecordSingleResponse.class); RecordSingleResponse resp = postRequest(endpoint, rec, RecordSingleResponse.class);
checkResponse(resp); checkResponse(resp);
log.info("Record {} of type {} successful created.", rec.getSld(), rec.getType()); log.info("Record {} of type {} successful created.", rec.getSld(), rec.getType());
RecordEntity record = resp.getResult(); RecordEntity retRec = resp.getResult();
record.setZoneId(zone.getId()); retRec.setZoneId(zone.getId());
return record; return retRec;
} }
/** /**
@@ -433,7 +435,10 @@ public class CfDnsClient extends CfBasicHttpClient {
throws CloudflareNotFoundException { throws CloudflareNotFoundException {
List<RecordEntity> filtered; List<RecordEntity> filtered;
if (types != null && types.length > 0) { if (types != null && types.length > 0) {
filtered = recs.stream().filter(rec -> Arrays.asList(types).contains(RecordType.valueOf(rec.getType()))).collect(Collectors.toList()); Set<RecordType> allowedTypes = new HashSet<>(Arrays.asList(types));
filtered = recs.stream()
.filter(rec -> allowedTypes.contains(RecordType.valueOf(rec.getType())))
.collect(Collectors.toList());;
} else { } else {
filtered = new ArrayList<>(recs); filtered = new ArrayList<>(recs);
} }
@@ -46,17 +46,27 @@ class ResponseValidator {
private void validateResultCount(AbstractResponse resp, boolean singleResultExpected) private void validateResultCount(AbstractResponse resp, boolean singleResultExpected)
throws CloudflareApiException { throws CloudflareApiException {
if (resp instanceof RecordMultipleResponse respMulti) { if (resp instanceof RecordMultipleResponse respMulti) {
if (singleResultExpected && respMulti.getResultInfo().totalCount() > 1) { validateMultipleResponse(respMulti, singleResultExpected);
throw new CloudflareApiException(
"Unexpected result count: " + respMulti.getResultInfo().totalCount());
}
if (emptyResultThrowsException && respMulti.getResultInfo().totalCount() == 0) {
throw new CloudflareNotFoundException("No result found");
}
} else if (resp instanceof AbstractSingleResponse<?> respSingle) { } else if (resp instanceof AbstractSingleResponse<?> respSingle) {
if (emptyResultThrowsException && respSingle.getResult() == null) { validateSingleResponse(respSingle);
throw new CloudflareNotFoundException("No result found"); }
} }
private void validateMultipleResponse(RecordMultipleResponse response, boolean singleResultExpected)
throws CloudflareApiException {
int totalCount = response.getResultInfo().totalCount();
if (singleResultExpected && totalCount > 1) {
throw new CloudflareApiException("Unexpected result count: " + totalCount);
}
if (emptyResultThrowsException && totalCount == 0) {
throw new CloudflareNotFoundException("No result found");
}
}
private void validateSingleResponse(AbstractSingleResponse<?> response)
throws CloudflareNotFoundException {
if (emptyResultThrowsException && response.getResult() == null) {
throw new CloudflareNotFoundException("No result found");
} }
} }
@@ -45,16 +45,16 @@ public class RecordOperationsImpl implements RecordOperations {
@Override @Override
public RecordEntity update(String newContent) throws CloudflareApiException { public RecordEntity update(String newContent) throws CloudflareApiException {
List<RecordEntity> records = get(); List<RecordEntity> recs = get();
if (records.isEmpty()) { if (recs.isEmpty()) {
throw new CloudflareApiException("No records found to update for subdomain: " + sld); throw new CloudflareApiException("No recs found to update for subdomain: " + sld);
} }
if (records.size() > 1) { if (recs.size() > 1) {
throw new CloudflareApiException("Multiple records found. Please use recordUpdate() directly for precise control."); throw new CloudflareApiException("Multiple recs found. Please use recordUpdate() directly for precise control.");
} }
RecordEntity record = records.get(0); RecordEntity rec = recs.get(0);
record.setContent(newContent); rec.setContent(newContent);
return client.recordUpdate(zone, record); return client.recordUpdate(zone, rec);
} }
@Override @Override