Skip to content

Commit

Permalink
modify send method and addDefaultHeaders method
Browse files Browse the repository at this point in the history
  • Loading branch information
brunacamposxx committed Oct 24, 2023
1 parent 630a474 commit 6950845
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 47 deletions.
109 changes: 63 additions & 46 deletions src/main/java/com/mercadopago/client/MercadoPagoClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.mercadopago.client;

import static java.util.Objects.nonNull;
import static org.apache.commons.collections.MapUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import com.google.gson.JsonObject;
import com.mercadopago.MercadoPagoConfig;
import com.mercadopago.core.MPRequestOptions;
Expand All @@ -14,7 +18,6 @@
import com.mercadopago.net.UrlFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/** Mercado Pago client class. */
public abstract class MercadoPagoClient {
Expand Down Expand Up @@ -51,18 +54,7 @@ public MercadoPagoClient(MPHttpClient httpClient) {
* @throws MPException exception
*/
protected MPResponse send(MPRequest request) throws MPException, MPApiException {
String uri = UrlFormatter.format(request.getUri(), request.getQueryParams());

return httpClient.send(
MPRequest.builder()
.uri(uri)
.method(request.getMethod())
.headers(addDefaultHeaders(request))
.payload(request.getPayload())
.connectionRequestTimeout(addConnectionRequestTimeout(request, null))
.connectionTimeout(addConnectionTimeout(request, null))
.socketTimeout(addSocketTimeout(request, null))
.build());
return this.send(request, null);
}

/**
Expand All @@ -75,13 +67,21 @@ protected MPResponse send(MPRequest request) throws MPException, MPApiException
*/
protected MPResponse send(MPRequest request, MPRequestOptions requestOptions)
throws MPException, MPApiException {
return this.send(
this.buildRequest(
request.getUri(),
request.getMethod(),
request.getPayload(),
request.getQueryParams(),
requestOptions));
String uri = UrlFormatter.format(request.getUri(), request.getQueryParams());
Map<String, String> addHeadersByDefault = addDefaultHeaders(request, requestOptions); // esse metodo teria um melhor nome sendo addDefaultHeader?
Map<String, String> defaultAndCustomHeaders = addCustomHeaders(addHeadersByDefault, uri, requestOptions);

return httpClient.send(
MPRequest.builder()
.uri(uri)
.accessToken(getAccessToken(requestOptions))
.method(request.getMethod())
.headers(defaultAndCustomHeaders)
.payload(request.getPayload())
.connectionRequestTimeout(addConnectionRequestTimeout(request, requestOptions))
.connectionTimeout(addConnectionTimeout(request, requestOptions))
.socketTimeout(addSocketTimeout(request, requestOptions))
.build());
}

/**
Expand Down Expand Up @@ -150,7 +150,7 @@ protected MPResponse search(
String path, MPSearchRequest searchRequest, MPRequestOptions requestOptions)
throws MPException, MPApiException {
Map<String, Object> queryParams =
Objects.nonNull(searchRequest) ? searchRequest.getParameters() : null;
nonNull(searchRequest) ? searchRequest.getParameters() : null;

return this.send(path, HttpMethod.GET, null, queryParams, requestOptions);
}
Expand Down Expand Up @@ -204,70 +204,66 @@ private MPRequest buildRequest(
.payload(payload)
.method(method)
.queryParams(queryParams)
.headers(addCustomHeaders(path, requestOptions))
.headers(addCustomHeaders(defaultHeaders, path, requestOptions))
.connectionRequestTimeout(addConnectionRequestTimeout(null, requestOptions))
.connectionTimeout(addConnectionTimeout(null, requestOptions))
.socketTimeout(addSocketTimeout(null, requestOptions))
.build();
}

private int addSocketTimeout(MPRequest request, MPRequestOptions requestOptions) {
if (Objects.nonNull(requestOptions) && requestOptions.getSocketTimeout() > 0) {
if (nonNull(requestOptions) && requestOptions.getSocketTimeout() > 0) {
return requestOptions.getSocketTimeout();
}

if (Objects.nonNull(request) && request.getSocketTimeout() > 0) {
if (nonNull(request) && request.getSocketTimeout() > 0) {
return request.getSocketTimeout();
}

return MercadoPagoConfig.getSocketTimeout();
}

private int addConnectionTimeout(MPRequest request, MPRequestOptions requestOptions) {
if (Objects.nonNull(requestOptions) && requestOptions.getConnectionTimeout() > 0) {
if (nonNull(requestOptions) && requestOptions.getConnectionTimeout() > 0) {
return requestOptions.getConnectionTimeout();
}

if (Objects.nonNull(request) && request.getConnectionTimeout() > 0) {
if (nonNull(request) && request.getConnectionTimeout() > 0) {
return request.getConnectionTimeout();
}

return MercadoPagoConfig.getConnectionTimeout();
}

private int addConnectionRequestTimeout(MPRequest request, MPRequestOptions requestOptions) {
if (Objects.nonNull(requestOptions) && requestOptions.getConnectionRequestTimeout() > 0) {
if (nonNull(requestOptions) && requestOptions.getConnectionRequestTimeout() > 0) {
return requestOptions.getConnectionRequestTimeout();
}

if (Objects.nonNull(request) && request.getConnectionRequestTimeout() > 0) {
if (nonNull(request) && request.getConnectionRequestTimeout() > 0) {
return request.getConnectionRequestTimeout();
}

return MercadoPagoConfig.getConnectionRequestTimeout();
}

private Map<String, String> addCustomHeaders(String uri, MPRequestOptions requestOptions) {
Map<String, String> headers = new HashMap<>();

if (Objects.nonNull(requestOptions) && Objects.nonNull(requestOptions.getCustomHeaders())) {
for (Map.Entry<String, String> entry : requestOptions.getCustomHeaders().entrySet()) {
headers.put(entry.getKey().toLowerCase(), entry.getValue());
}
}
public Map<String, String> addDefaultHeaders(MPRequest request, MPRequestOptions requestOptions) {
Map<String, String> headers =
nonNull(request.getHeaders()) ? request.getHeaders() : new HashMap<>();

if (!uri.contains("/oauth/token")) {
headers.put(Headers.AUTHORIZATION, String.format("Bearer %s", getAccessToken(requestOptions)));
headers.putAll(defaultHeaders);

if (isNotBlank(MercadoPagoConfig.getCorporationId())) {
headers.put(Headers.CORPORATION_ID, MercadoPagoConfig.getCorporationId());
}
return headers;
}

private Map<String, String> addDefaultHeaders(MPRequest request) {
Map<String, String> headers =
Objects.nonNull(request.getHeaders()) ? request.getHeaders() : new HashMap<>();
if (isNotBlank(MercadoPagoConfig.getIntegratorId())) {
headers.put(Headers.INTEGRATOR_ID, MercadoPagoConfig.getIntegratorId());
}

for (Map.Entry<String, String> entry : defaultHeaders.entrySet()) {
headers.put(entry.getKey(), entry.getValue());
if (isNotBlank(MercadoPagoConfig.getPlatformId())) {
headers.put(Headers.PLATFORM_ID, MercadoPagoConfig.getPlatformId());
}

if (shouldAddIdempotencyKey(request, headers)) {
Expand All @@ -278,6 +274,27 @@ private Map<String, String> addDefaultHeaders(MPRequest request) {
headers.put(Headers.AUTHORIZATION, String.format("Bearer %s", getAccessToken(null)));
}

if (nonNull(requestOptions) && isNotEmpty(requestOptions.getCustomHeaders()) ) {
for (Map.Entry<String, String> header : requestOptions.getCustomHeaders().entrySet()) { // ver essa regra aqui
if (!headers.containsKey(header.getKey()) && !Headers.CONTENT_TYPE.equalsIgnoreCase(header.getKey())) {
headers.put(header.getKey().toLowerCase(), header.getValue());
}
}
}

return headers;
}

private Map<String, String> addCustomHeaders(Map<String, String> headers, String uri, MPRequestOptions requestOptions) {
if (nonNull(requestOptions) && nonNull(requestOptions.getCustomHeaders())) {
for (Map.Entry<String, String> entry : requestOptions.getCustomHeaders().entrySet()) {
headers.put(entry.getKey().toLowerCase(), entry.getValue());
}
}

if (!uri.contains("/oauth/token")) {
headers.put(Headers.AUTHORIZATION, String.format("Bearer %s", getAccessToken(requestOptions)));
}
return headers;
}

Expand All @@ -292,8 +309,8 @@ private boolean shouldAddIdempotencyKey(MPRequest request, Map headers) {
}

private String getAccessToken(MPRequestOptions requestOptions) {
return Objects.nonNull(requestOptions)
&& Objects.nonNull(requestOptions.getAccessToken())
return nonNull(requestOptions)
&& nonNull(requestOptions.getAccessToken())
&& !requestOptions.getAccessToken().isEmpty()
? requestOptions.getAccessToken()
: MercadoPagoConfig.getAccessToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
import com.mercadopago.client.common.AddressRequest;
import com.mercadopago.client.common.IdentificationRequest;
import com.mercadopago.client.common.PhoneRequest;
import com.mercadopago.core.MPRequestOptions;
import com.mercadopago.exceptions.MPApiException;
import com.mercadopago.exceptions.MPException;
import com.mercadopago.net.Headers;
import com.mercadopago.net.MPElementsResourcesPage;
import com.mercadopago.net.MPSearchRequest;
import com.mercadopago.resources.preference.Preference;
import com.mercadopago.resources.preference.PreferenceSearch;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;

/** PreferenceClientIT class. */
Expand Down Expand Up @@ -82,11 +87,24 @@ void createSuccess() {
void createWithRequestOptionsSuccess() {
try {
PreferenceRequest preferenceRequest = buildPreferenceRequest();
Preference preference = client.create(preferenceRequest, buildRequestOptions());

String idempotency = UUID.randomUUID().toString();
Map<String, String> idempotencyKey = new HashMap<>();
idempotencyKey.put(Headers.IDEMPOTENCY_KEY, idempotency);
MPRequestOptions mpRequestOptions = MPRequestOptions
.builder()
.customHeaders(idempotencyKey)
.build();

Preference preference = client.create(preferenceRequest, mpRequestOptions);

assertNotNull(preference.getResponse());
assertEquals(CREATED, preference.getResponse().getStatusCode());
assertNotNull(preference.getId());
System.out.println(idempotency);
System.out.println(idempotencyKey);
System.out.println(mpRequestOptions);
System.out.println("preference: " + preference.getResponse().getHeaders());
} catch (MPApiException mpApiException) {
fail(mpApiException.getApiResponse().getContent());
} catch (MPException mpException) {
Expand Down

0 comments on commit 6950845

Please sign in to comment.