diff --git a/src/main/java/io/codef/api/EasyCodef.java b/src/main/java/io/codef/api/EasyCodef.java index 6e9d991..5bace5c 100644 --- a/src/main/java/io/codef/api/EasyCodef.java +++ b/src/main/java/io/codef/api/EasyCodef.java @@ -54,6 +54,10 @@ protected EasyCodef(EasyCodefBuilder builder) { logInitializeSuccessfully(); } + public String encryptRSA(String requestParam) throws CodefException { + return RsaUtil.encryptRSA(requestParam, publicKey); + } + public EasyCodefResponse requestProduct(EasyCodefRequest request) throws CodefException { return singleReqFacade.requestProduct(request); } diff --git a/src/main/java/io/codef/api/dto/EasyCodefRequest.java b/src/main/java/io/codef/api/dto/EasyCodefRequest.java index 2486fb3..9c0ce10 100644 --- a/src/main/java/io/codef/api/dto/EasyCodefRequest.java +++ b/src/main/java/io/codef/api/dto/EasyCodefRequest.java @@ -1,10 +1,10 @@ package io.codef.api.dto; -import java.util.HashMap; +import java.util.Map; public record EasyCodefRequest( String path, - HashMap requestBody + Map requestBody ) { /** diff --git a/src/main/java/io/codef/api/dto/EasyCodefRequestBuilder.java b/src/main/java/io/codef/api/dto/EasyCodefRequestBuilder.java index 24e3d52..e01d296 100644 --- a/src/main/java/io/codef/api/dto/EasyCodefRequestBuilder.java +++ b/src/main/java/io/codef/api/dto/EasyCodefRequestBuilder.java @@ -1,32 +1,25 @@ package io.codef.api.dto; -import static io.codef.api.dto.EasyCodefRequest.EASY_CODEF_JAVA_FLAG; -import static io.codef.api.dto.EasyCodefRequest.ORGANIZATION; -import static io.codef.api.dto.EasyCodefRequest.PATH_PREFIX; - import io.codef.api.CodefValidator; -import io.codef.api.EasyCodef; import io.codef.api.error.CodefError; import io.codef.api.error.CodefException; -import io.codef.api.util.RsaUtil; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class EasyCodefRequestBuilder { +import java.util.*; +import java.util.function.Consumer; +import static io.codef.api.dto.EasyCodefRequest.EASY_CODEF_JAVA_FLAG; +import static io.codef.api.dto.EasyCodefRequest.PATH_PREFIX; + +public class EasyCodefRequestBuilder { private static final Logger log = LoggerFactory.getLogger(EasyCodefRequestBuilder.class); - private final HashMap generalRequestBody; - private final HashMap secureRequestBody; + private final Map requestBody; private String path; - private EasyCodef easyCodef; private EasyCodefRequestBuilder() { - this.generalRequestBody = new HashMap<>(); - this.secureRequestBody = new HashMap<>(); + this.requestBody = new HashMap<>(); } public static EasyCodefRequestBuilder builder() { @@ -35,71 +28,43 @@ public static EasyCodefRequestBuilder builder() { private static void requireValidPathElseThrow(String path) { Optional.of(path) - .filter(p -> p.startsWith(PATH_PREFIX)) - .orElseThrow(() -> CodefException.from(CodefError.INVALID_PATH_REQUESTED)); + .filter(p -> p.startsWith(PATH_PREFIX)) + .orElseThrow(() -> CodefException.from(CodefError.INVALID_PATH_REQUESTED)); } - public EasyCodefRequestBuilder organization(Object value) { - CodefValidator.requireNonNullElseThrow(value, CodefError.NULL_ORGANIZATION); - generalRequestBody.put(ORGANIZATION, value); - return this; - } public EasyCodefRequestBuilder path(String path) { this.path = path; requireValidPathElseThrow(path); - return this; } - public EasyCodefRequestBuilder requestBody( - String param, - Object value - ) { - generalRequestBody.put(param, value); + public EasyCodefRequestBuilder requestBody(String param, Object value) { + requestBody.put(param, value); return this; } - public EasyCodefRequestBuilder secureRequestBody( - String param, - String value + public EasyCodefRequestBuilder requestObject( + String key, + Consumer> objectBuilder ) { - secureRequestBody.put(param, value); - return this; - } + CodefValidator.requireNonNullElseThrow(key, CodefError.REQUEST_NULL); - public EasyCodefRequestBuilder secureWith(EasyCodef easyCodef) { - this.easyCodef = easyCodef; + Map object = new HashMap<>(); + objectBuilder.accept(object); + + @SuppressWarnings("unchecked") + List> objectList = + (List>) requestBody.computeIfAbsent(key, k -> new ArrayList<>()); + + objectList.add(object); return this; } public EasyCodefRequest build() { CodefValidator.requireNonNullElseThrow(path, CodefError.NEED_TO_PATH_METHOD); - CodefValidator.requireNonNullElseThrow( - generalRequestBody.get(ORGANIZATION), CodefError.NEED_TO_ORGANIZATION_METHOD - ); - - encryptSecureRequestBody(); - this.requestBody(EASY_CODEF_JAVA_FLAG, true); - this.generalRequestBody.putAll(secureRequestBody); - - return new EasyCodefRequest(path, generalRequestBody); - } - - private void encryptSecureRequestBody() { - Optional.of(secureRequestBody) - .filter(body -> !body.isEmpty()) - .ifPresent(body -> { - CodefValidator.requireNonNullElseThrow( - easyCodef, - CodefError.NEED_TO_SECURE_WITH_METHOD - ); - encryptRequestBodyValues(body); - }); - } - private void encryptRequestBodyValues(Map body) { - body.replaceAll((key, value) -> RsaUtil.encryptRSA(value, easyCodef.getPublicKey())); + return new EasyCodefRequest(path, requestBody); } -} +} \ No newline at end of file