Skip to content

Commit

Permalink
Merge pull request #13 from PAYONE-GmbH/refactor/initial-sonarcloud-a…
Browse files Browse the repository at this point in the history
…nalysis

refactor: fix issues from initial sonarcloud analysis
  • Loading branch information
lrosenfeldt authored Aug 7, 2024
2 parents 2681352 + 28b6a8d commit a3954c0
Show file tree
Hide file tree
Showing 32 changed files with 1,005 additions and 689 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Welcome to the Java SDK for the PayOne PCP platform! This repository contains a
### TODOS

- [ ] Setup changelog
- [ ] Setup sonarcloud

## Table of Contents

Expand Down
596 changes: 309 additions & 287 deletions app/src/main/java/com/payone/commerce/platform/app/App.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.payone.commerce.platform.lib;

import com.payone.commerce.platform.lib.utils.ServerMetaInfo;

public class CommunicatorConfiguration {
private final String apiKey;
private final String apiSecret;
private final String host;
private final ServerMetaInfo serverMetaInfo;

public CommunicatorConfiguration(String apiKey, String apiSecret, String host) {
public CommunicatorConfiguration(String apiKey, String apiSecret, String host, String integrator) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.host = host;
this.serverMetaInfo = ServerMetaInfo.withDefaults(integrator);
}

public String getApiKey() {
Expand All @@ -22,4 +26,8 @@ public String getApiSecret() {
public String getHost() {
return host;
}

public ServerMetaInfo getServerMetaInfo() {
return serverMetaInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public class RequestHeaderGenerator {

public static final String SERVER_META_INFO_HEADER_NAME = "X-GCS-ServerMetaInfo";
public static final String CLIENT_META_INFO_HEADER_NAME = "X-GCS-ClientMetaInfo";
private static final String DATE_HEADER_NAME = "Date";
private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
private static final String CONTENT_TYPE_HEADER_NAME = "Content-Type";

private static final String ALGORITHM = "HmacSHA256";
private static final String WHITESPACE_REGEX = "\\r?\\n[\\h]*";
private final String DATE_HEADER_NAME = "Date";
private final String AUTHORIZATION_HEADER_NAME = "Authorization";

private final CommunicatorConfiguration config;
private final Mac mac;
Expand All @@ -38,7 +39,7 @@ public RequestHeaderGenerator(CommunicatorConfiguration config) throws InvalidKe
ALGORITHM);
this.mac.init(secretKeySpec);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("HmacSHA256 must be available to use the PCP Java SDK", e);
throw new AssertionError("HmacSHA256 must be available to use the PCP Java SDK", e);
}
}

Expand Down Expand Up @@ -69,8 +70,8 @@ private String getAuthHeader(Request request, Builder headersBuilder) {
StringBuilder stringToSign = new StringBuilder(request.method());
stringToSign.append("\n");
// 2. Content-Type
if (headersBuilder.get("Content-Type") != null) {
stringToSign.append(headersBuilder.get("Content-Type"));
if (headersBuilder.get(CONTENT_TYPE_HEADER_NAME) != null) {
stringToSign.append(headersBuilder.get(CONTENT_TYPE_HEADER_NAME));
}
stringToSign.append("\n");
// 3. Date
Expand All @@ -90,7 +91,7 @@ private String getAuthHeader(Request request, Builder headersBuilder) {
.append("\n");
}
// 5. Canonicalized Resource (has to include query parameters)
stringToSign.append(request.url().encodedPath().toString());
stringToSign.append(request.url().encodedPath());
if (request.url().encodedQuery() != null) {
stringToSign.append("?")
.append(request.url().encodedQuery());
Expand All @@ -106,14 +107,14 @@ private String sign(String target) {
}

private String getServerMetaInfo() {
ServerMetaInfo meta = new ServerMetaInfo();
ServerMetaInfo meta = config.getServerMetaInfo();
String jsonString;

try {
jsonString = JsonSerializer.serializeToJson(meta);
return Base64.getEncoder().encodeToString(jsonString.getBytes(StandardCharsets.UTF_8));
} catch (JsonProcessingException e) {
throw new RuntimeException(
throw new AssertionError(
"Server Meta Info must be encodable as JSON, this is likely an internal bug of the java PCP SDK",
e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@
import okhttp3.Response;

public class BaseApiClient {
private final OkHttpClient client = new OkHttpClient();
private final String JSON_PARSE_ERROR = "Excepted valid JSON response, but failed to parse";
protected final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final String JSON_PARSE_ERROR = "Excepted valid JSON response, but failed to parse";
protected static final String MERCHANT_ID_REQUIRED_ERROR = "Merchant ID is required";
protected static final String COMMERCE_CASE_ID_REQUIRED_ERROR = "Commerce Case ID is required";
protected static final String CHECKOUT_ID_REQUIRED_ERROR = "Checkout ID is required";
protected static final String PAYLOAD_REQUIRED_ERROR = "Payload is required";

protected static final String HTTPS_SCHEME = "https";
protected static final String PCP_PATH_SEGMENT_VERSION = "v1";
protected static final String PCP_PATH_SEGMENT_COMMERCE_CASES = "commerce-cases";
protected static final String PCP_PATH_SEGMENT_CHECKOUTS = "checkouts";

protected static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

private final OkHttpClient client = new OkHttpClient();
private final RequestHeaderGenerator requestHeaderGenerator;
private final CommunicatorConfiguration config;

Expand Down Expand Up @@ -63,7 +73,7 @@ protected <T> T makeApiCall(Request request, TypeReference<T> valueTypeRef)
try {
return JsonSerializer.deserializeFromJson(response.body().string(), valueTypeRef);
} catch (JsonMappingException e) {
throw new RuntimeException(JSON_PARSE_ERROR, e);
throw new AssertionError(JSON_PARSE_ERROR, e);
}
}

Expand All @@ -75,7 +85,7 @@ protected <T> T makeApiCall(Request request, Class<T> clazz)
try {
return JsonSerializer.deserializeFromJson(response.body().string(), clazz);
} catch (JsonMappingException e) {
throw new RuntimeException(JSON_PARSE_ERROR, e);
throw new AssertionError(JSON_PARSE_ERROR, e);
}
}

Expand All @@ -86,7 +96,7 @@ private void handleError(Response response)
}

String responseBody = response.body().string();
if (responseBody == null || responseBody.isEmpty()) {
if (responseBody.isEmpty()) {
throw new ApiResponseRetrievalException(response.code(), responseBody);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ public CreateCheckoutResponse createCheckoutRequest(String merchantId, String co
CreateCheckoutRequest payload)
throws ApiErrorResponseException, ApiResponseRetrievalException, IOException {
if (merchantId == null) {
throw new IllegalArgumentException("Merchant ID is required");
throw new IllegalArgumentException(MERCHANT_ID_REQUIRED_ERROR);
}
if (commerceCaseId == null) {
throw new IllegalArgumentException("Commerce Case ID is required");
throw new IllegalArgumentException(COMMERCE_CASE_ID_REQUIRED_ERROR);
}
if (payload == null) {
throw new IllegalArgumentException("Payload is required");
throw new IllegalArgumentException(PAYLOAD_REQUIRED_ERROR);
}

HttpUrl url = new HttpUrl.Builder()
.scheme("https")
.scheme(HTTPS_SCHEME)
.host(this.getConfig().getHost())
.addPathSegment("v1")
.addPathSegment(PCP_PATH_SEGMENT_VERSION)
.addPathSegment(merchantId)
.addPathSegment("commerce-cases")
.addPathSegment(PCP_PATH_SEGMENT_COMMERCE_CASES)
.addPathSegment(commerceCaseId)
.addPathSegment("checkouts")
.addPathSegment(PCP_PATH_SEGMENT_CHECKOUTS)
.build();

String jsonString = null;
Expand All @@ -68,23 +68,23 @@ public CreateCheckoutResponse createCheckoutRequest(String merchantId, String co
public CheckoutResponse getCheckoutRequest(String merchantId, String commerceCaseId, String checkoutId)
throws ApiErrorResponseException, ApiResponseRetrievalException, IOException {
if (merchantId == null) {
throw new IllegalArgumentException("Merchant ID is required");
throw new IllegalArgumentException(MERCHANT_ID_REQUIRED_ERROR);
}
if (commerceCaseId == null) {
throw new IllegalArgumentException("Commerce Case ID is required");
throw new IllegalArgumentException(COMMERCE_CASE_ID_REQUIRED_ERROR);
}
if (checkoutId == null) {
throw new IllegalArgumentException("Checkout ID is required");
throw new IllegalArgumentException(CHECKOUT_ID_REQUIRED_ERROR);
}

HttpUrl url = new HttpUrl.Builder()
.scheme("https")
.scheme(HTTPS_SCHEME)
.host(this.getConfig().getHost())
.addPathSegment("v1")
.addPathSegment(PCP_PATH_SEGMENT_VERSION)
.addPathSegment(merchantId)
.addPathSegment("commerce-cases")
.addPathSegment(PCP_PATH_SEGMENT_COMMERCE_CASES)
.addPathSegment(commerceCaseId)
.addPathSegment("checkouts")
.addPathSegment(PCP_PATH_SEGMENT_CHECKOUTS)
.addPathSegment(checkoutId)
.build();

Expand All @@ -105,15 +105,15 @@ public CheckoutsResponse getCheckoutsRequest(String merchantID)
public CheckoutsResponse getCheckoutsRequest(String merchantId, GetCheckoutsQuery queryParams)
throws ApiErrorResponseException, ApiResponseRetrievalException, IOException {
if (merchantId == null) {
throw new IllegalArgumentException("Merchant ID is required");
throw new IllegalArgumentException(MERCHANT_ID_REQUIRED_ERROR);
}

HttpUrl.Builder urlBuilder = new HttpUrl.Builder()
.scheme("https")
.scheme(HTTPS_SCHEME)
.host(this.getConfig().getHost())
.addPathSegment("v1")
.addPathSegment(PCP_PATH_SEGMENT_VERSION)
.addPathSegment(merchantId)
.addPathSegment("checkouts");
.addPathSegment(PCP_PATH_SEGMENT_CHECKOUTS);

if (queryParams != null) {
for (Map.Entry<String, String> entry : queryParams.toQueryMap().entrySet()) {
Expand All @@ -134,26 +134,26 @@ public void updateCheckoutRequest(String merchantId, String commerceCaseId, Stri
PatchCheckoutRequest payload)
throws ApiErrorResponseException, ApiResponseRetrievalException, IOException {
if (merchantId == null) {
throw new IllegalArgumentException("Merchant ID is required");
throw new IllegalArgumentException(MERCHANT_ID_REQUIRED_ERROR);
}
if (commerceCaseId == null) {
throw new IllegalArgumentException("Commerce Case ID is required");
throw new IllegalArgumentException(COMMERCE_CASE_ID_REQUIRED_ERROR);
}
if (checkoutId == null) {
throw new IllegalArgumentException("Checkout ID is required");
throw new IllegalArgumentException(CHECKOUT_ID_REQUIRED_ERROR);
}
if (payload == null) {
throw new IllegalArgumentException("Payload is required");
throw new IllegalArgumentException(PAYLOAD_REQUIRED_ERROR);
}

HttpUrl url = new HttpUrl.Builder()
.scheme("https")
.scheme(HTTPS_SCHEME)
.host(this.getConfig().getHost())
.addPathSegment("v1")
.addPathSegment(PCP_PATH_SEGMENT_VERSION)
.addPathSegment(merchantId)
.addPathSegment("commerce-cases")
.addPathSegment(PCP_PATH_SEGMENT_COMMERCE_CASES)
.addPathSegment(commerceCaseId)
.addPathSegment("checkouts")
.addPathSegment(PCP_PATH_SEGMENT_CHECKOUTS)
.addPathSegment(checkoutId)
.build();

Expand All @@ -174,23 +174,23 @@ public void updateCheckoutRequest(String merchantId, String commerceCaseId, Stri
public void removeCheckoutRequest(String merchantId, String commerceCaseId, String checkoutId)
throws ApiErrorResponseException, ApiResponseRetrievalException, IOException {
if (merchantId == null) {
throw new IllegalArgumentException("Merchant ID is required");
throw new IllegalArgumentException(MERCHANT_ID_REQUIRED_ERROR);
}
if (commerceCaseId == null) {
throw new IllegalArgumentException("Commerce Case ID is required");
throw new IllegalArgumentException(COMMERCE_CASE_ID_REQUIRED_ERROR);
}
if (checkoutId == null) {
throw new IllegalArgumentException("Checkout ID is required");
throw new IllegalArgumentException(CHECKOUT_ID_REQUIRED_ERROR);
}

HttpUrl url = new HttpUrl.Builder()
.scheme("https")
.scheme(HTTPS_SCHEME)
.host(this.getConfig().getHost())
.addPathSegment("v1")
.addPathSegment(PCP_PATH_SEGMENT_VERSION)
.addPathSegment(merchantId)
.addPathSegment("commerce-cases")
.addPathSegment(PCP_PATH_SEGMENT_COMMERCE_CASES)
.addPathSegment(commerceCaseId)
.addPathSegment("checkouts")
.addPathSegment(PCP_PATH_SEGMENT_CHECKOUTS)
.addPathSegment(checkoutId)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ public CommerceCaseApiClient(CommunicatorConfiguration config) throws InvalidKey
public CreateCommerceCaseResponse createCommerceCaseRequest(String merchantId, CreateCommerceCaseRequest payload)
throws ApiErrorResponseException, ApiResponseRetrievalException, IOException {
if (merchantId == null) {
throw new IllegalArgumentException("Merchant ID is required");
throw new IllegalArgumentException(MERCHANT_ID_REQUIRED_ERROR);
}
if (payload == null) {
throw new IllegalArgumentException("Payload is required");
throw new IllegalArgumentException(PAYLOAD_REQUIRED_ERROR);
}

HttpUrl url = new HttpUrl.Builder()
.scheme("https")
.scheme(HTTPS_SCHEME)
.host(this.getConfig().getHost())
.addPathSegment("v1")
.addPathSegment(PCP_PATH_SEGMENT_VERSION)
.addPathSegment(merchantId)
.addPathSegment("commerce-cases")
.addPathSegment(PCP_PATH_SEGMENT_COMMERCE_CASES)
.build();

String jsonString = JsonSerializer.serializeToJson(payload);
Expand All @@ -63,18 +63,18 @@ public CreateCommerceCaseResponse createCommerceCaseRequest(String merchantId, C
public CommerceCaseResponse getCommerceCaseRequest(String merchantId,
String commerceCaseId) throws ApiErrorResponseException, ApiResponseRetrievalException, IOException {
if (merchantId == null) {
throw new IllegalArgumentException("Merchant ID is required");
throw new IllegalArgumentException(MERCHANT_ID_REQUIRED_ERROR);
}
if (commerceCaseId == null) {
throw new IllegalArgumentException("Commerce Case ID is required");
throw new IllegalArgumentException(COMMERCE_CASE_ID_REQUIRED_ERROR);
}

HttpUrl.Builder urlBuilder = new HttpUrl.Builder()
.scheme("https")
.scheme(HTTPS_SCHEME)
.host(this.getConfig().getHost())
.addPathSegment("v1")
.addPathSegment(PCP_PATH_SEGMENT_VERSION)
.addPathSegment(merchantId)
.addPathSegment("commerce-cases")
.addPathSegment(PCP_PATH_SEGMENT_COMMERCE_CASES)
.addPathSegment(commerceCaseId);

HttpUrl url = urlBuilder.build();
Expand All @@ -97,15 +97,15 @@ public List<CommerceCaseResponse> getCommerceCasesRequest(String merchantId,
GetCommerceCasesQuery queryParams)
throws ApiErrorResponseException, ApiResponseRetrievalException, IOException {
if (merchantId == null) {
throw new IllegalArgumentException("Merchant ID is required");
throw new IllegalArgumentException(MERCHANT_ID_REQUIRED_ERROR);
}

HttpUrl.Builder urlBuilder = new HttpUrl.Builder()
.scheme("https")
.scheme(HTTPS_SCHEME)
.host(this.getConfig().getHost())
.addPathSegment("v1")
.addPathSegment(PCP_PATH_SEGMENT_VERSION)
.addPathSegment(merchantId)
.addPathSegment("commerce-cases");
.addPathSegment(PCP_PATH_SEGMENT_COMMERCE_CASES);

if (queryParams != null) {
for (Map.Entry<String, String> entry : queryParams.toQueryMap().entrySet()) {
Expand All @@ -127,21 +127,21 @@ public List<CommerceCaseResponse> getCommerceCasesRequest(String merchantId,
public void updateCommerceCaseRequest(String merchantId, String commerceCaseId,
Customer payload) throws ApiErrorResponseException, ApiResponseRetrievalException, IOException {
if (merchantId == null) {
throw new IllegalArgumentException("Merchant ID is required");
throw new IllegalArgumentException(MERCHANT_ID_REQUIRED_ERROR);
}
if (commerceCaseId == null) {
throw new IllegalArgumentException("Commerce Case ID is required");
throw new IllegalArgumentException(COMMERCE_CASE_ID_REQUIRED_ERROR);
}
if (payload == null) {
throw new IllegalArgumentException("Payload is required");
throw new IllegalArgumentException(PAYLOAD_REQUIRED_ERROR);
}

HttpUrl url = new HttpUrl.Builder()
.scheme("https")
.scheme(HTTPS_SCHEME)
.host(this.getConfig().getHost())
.addPathSegment("v1")
.addPathSegment(PCP_PATH_SEGMENT_VERSION)
.addPathSegment(merchantId)
.addPathSegment("commerce-cases")
.addPathSegment(PCP_PATH_SEGMENT_COMMERCE_CASES)
.addPathSegment(commerceCaseId)
.build();

Expand Down
Loading

0 comments on commit a3954c0

Please sign in to comment.