Skip to content

Commit

Permalink
refactor send method
Browse files Browse the repository at this point in the history
  • Loading branch information
brunacamposxx committed Oct 20, 2023
1 parent 630a474 commit 2f4c2bc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
41 changes: 18 additions & 23 deletions src/main/java/com/mercadopago/client/MercadoPagoClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,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 +64,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> defaultHeader = addDefaultHeaders(request);
Map<String, String> addHeader = addCustomHeaders(defaultHeader, uri, requestOptions);

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

/**
Expand Down Expand Up @@ -204,7 +201,7 @@ 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))
Expand Down Expand Up @@ -247,9 +244,7 @@ private int addConnectionRequestTimeout(MPRequest request, MPRequestOptions requ
return MercadoPagoConfig.getConnectionRequestTimeout();
}

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

private Map<String, String> addCustomHeaders(Map<String, String> headers, String uri, 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());
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,25 @@ 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 2f4c2bc

Please sign in to comment.