Skip to content

Commit

Permalink
feat: requestObject 메소드 기능 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
h-beeen committed Nov 29, 2024
1 parent 721f566 commit 5a626d8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 63 deletions.
4 changes: 4 additions & 0 deletions src/main/java/io/codef/api/EasyCodef.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/codef/api/dto/EasyCodefRequest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.codef.api.dto;

import java.util.HashMap;
import java.util.Map;

public record EasyCodefRequest(
String path,
HashMap<String, Object> requestBody
Map<String, Object> requestBody
) {

/**
Expand Down
87 changes: 26 additions & 61 deletions src/main/java/io/codef/api/dto/EasyCodefRequestBuilder.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> generalRequestBody;
private final HashMap<String, String> secureRequestBody;
private final Map<String, Object> 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() {
Expand All @@ -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<Map<String, Object>> objectBuilder
) {
secureRequestBody.put(param, value);
return this;
}
CodefValidator.requireNonNullElseThrow(key, CodefError.REQUEST_NULL);

public EasyCodefRequestBuilder secureWith(EasyCodef easyCodef) {
this.easyCodef = easyCodef;
Map<String, Object> object = new HashMap<>();
objectBuilder.accept(object);

@SuppressWarnings("unchecked")
List<Map<String, Object>> objectList =
(List<Map<String, Object>>) 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<String, String> body) {
body.replaceAll((key, value) -> RsaUtil.encryptRSA(value, easyCodef.getPublicKey()));
return new EasyCodefRequest(path, requestBody);
}
}
}

0 comments on commit 5a626d8

Please sign in to comment.