Skip to content

Commit

Permalink
Merge pull request #266 from mercadopago/feature/methods-not-sending-…
Browse files Browse the repository at this point in the history
…access-token

refactor send method
  • Loading branch information
brunacamposxx authored Oct 30, 2023
2 parents 630a474 + b8d43d0 commit a3ecb32
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 48 deletions.
122 changes: 75 additions & 47 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,13 +18,14 @@
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 {
private static final String ACCEPT_HEADER_VALUE = "application/json";

private static final String CONTENT_TYPE_HEADER_VALUE = "application/json; charset=UTF-8";
private static final String BEARER = "Bearer %s";
private static final String OAUTH_TOKEN = "/oauth/token";

protected final MPHttpClient httpClient;

Expand Down Expand Up @@ -51,18 +56,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 +69,19 @@ 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());

return httpClient.send(
MPRequest.builder()
.uri(uri)
.accessToken(getAccessToken(requestOptions))
.method(request.getMethod())
.headers(addRequestHeaders(request, requestOptions))
.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 @@ -212,77 +212,105 @@ private MPRequest buildRequest(
}

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<>();
private Map<String, String> addRequestHeaders(MPRequest request, MPRequestOptions requestOptions) {

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

headers.putAll(addDefaultHeaders(request));

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

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

if (isNotBlank(MercadoPagoConfig.getPlatformId())) {
headers.put(Headers.PLATFORM_ID, MercadoPagoConfig.getPlatformId());
}

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

headers.putAll(addCustomHeaders(request.getUri(), requestOptions));
return headers;
}

private Map<String, String> addDefaultHeaders(MPRequest request) {
Map<String, String> headers =
Objects.nonNull(request.getHeaders()) ? request.getHeaders() : new HashMap<>();
Map<String, String> headers = new HashMap<>(defaultHeaders);
if (shouldAddIdempotencyKey(request)) {
headers.put(Headers.IDEMPOTENCY_KEY, request.createIdempotencyKey());
}

for (Map.Entry<String, String> entry : defaultHeaders.entrySet()) {
headers.put(entry.getKey(), entry.getValue());
if (!request.getUri().contains(OAUTH_TOKEN) && !headers.containsKey(Headers.AUTHORIZATION)) {
headers.put(Headers.AUTHORIZATION, String.format(BEARER, getAccessToken(null)));
}
return headers;
}

if (shouldAddIdempotencyKey(request, headers)) {
headers.put(Headers.IDEMPOTENCY_KEY, request.createIdempotencyKey());
private Map<String, String> addCustomHeaders(String uri, MPRequestOptions requestOptions) {
Map<String, String> headers = new HashMap<>();
if (nonNull(requestOptions) && nonNull(requestOptions.getCustomHeaders())) {
for (Map.Entry<String, String> entry : requestOptions.getCustomHeaders().entrySet()) {
headers.put(entry.getKey().toLowerCase(), entry.getValue());
}
}

if (!request.getUri().contains("/oauth/token") && !headers.containsKey(Headers.AUTHORIZATION)) {
headers.put(Headers.AUTHORIZATION, String.format("Bearer %s", getAccessToken(null)));
if (!uri.contains(OAUTH_TOKEN)) {
headers.put(Headers.AUTHORIZATION, String.format(BEARER, getAccessToken(requestOptions)));
}

return headers;
}

private boolean shouldAddIdempotencyKey(MPRequest request, Map headers) {
boolean containsIdempotency = headers.containsKey(Headers.IDEMPOTENCY_KEY.toLowerCase());
private boolean shouldAddIdempotencyKey(MPRequest request) {

boolean containsIdempotency = false;
if (nonNull(request) && nonNull(request.getHeaders())) {
containsIdempotency = request.getHeaders().containsKey(Headers.IDEMPOTENCY_KEY.toLowerCase());
}

if (containsIdempotency) return false;

Expand All @@ -292,8 +320,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,7 +87,16 @@ 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());
Expand Down

0 comments on commit a3ecb32

Please sign in to comment.