Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Feature] Added endpoint get order by id #2

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/main/java/com/mercadopago/client/order/OrderClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
public class OrderClient extends MercadoPagoClient {
private static final Logger LOGGER = Logger.getLogger(OrderClient.class.getName());

private static final String URL_WITH_ID = "/v1/orders/%s";

/** Default constructor. Uses the default http client used by the SDK. */
public OrderClient() {
this(MercadoPagoConfig.getHttpClient());
Expand Down Expand Up @@ -66,7 +68,7 @@ public Order create(OrderCreateRequest request, MPRequestOptions requestOptions)
}

/**
* Method responsible for creating order with request options
* Method responsible for creating order without request options
*
* @param request request
* @return order response
Expand All @@ -77,4 +79,37 @@ public Order create(OrderCreateRequest request) throws MPException, MPApiExcepti
return this.create(request, null);
}

/**
* Method responsible for obtaining order by id
*
* @param id orderId
* @return order response
* @throws MPException an error if the request fails
* @throws MPApiException an error if the request fails
*/
public Order get(String id) throws MPException, MPApiException {
return this.get(id, null);
}

/**
* Method responsible for obtaining order by id with request options
*
* @param id orderId
* @param requestOptions metadata to customize the request
* @return order response
* @throws MPException an error if the request fails
* @throws MPApiException an error if the request fails
*/
public Order get(String id, MPRequestOptions requestOptions) throws MPException, MPApiException {
LOGGER.info("Sending order get request");

MPResponse response =
send(String.format(URL_WITH_ID, id), HttpMethod.GET, null, null, requestOptions);

Order result = Serializer.deserializeFromJson(Order.class, response.getContent());
result.setResponse(response);

return result;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;
import java.util.Map;

public class createOrder {
public class CreateOrder {

public static void main(String[] args) {
MercadoPagoConfig.setAccessToken("{{ACCESS_TOKEN}}");
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/mercadopago/example/apis/order/GetOrderById.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mercadopago.example.apis.order;

import com.mercadopago.MercadoPagoConfig;
import com.mercadopago.client.order.OrderClient;
import com.mercadopago.core.MPRequestOptions;
import com.mercadopago.exceptions.MPApiException;
import com.mercadopago.exceptions.MPException;
import com.mercadopago.resources.order.Order;

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

public class GetOrderById {

public static void main(String[] args) {
MercadoPagoConfig.setAccessToken("{{ACCESS_TOKEN}}");

OrderClient client = new OrderClient();

Map<String, String> headers = new HashMap<>();
headers.put("X-Sandbox", "true");
MPRequestOptions requestOptions = MPRequestOptions.builder()
.customHeaders(headers)
.build();

try {
Order order = client.get("{{ORDER_ID}}", requestOptions);
System.out.println("Getting order: " + order.getId());
} catch (MPException | MPApiException e) {
System.out.println("Error getting order: " + e.getMessage());
}
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/mercadopago/client/order/OrderClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ private static OrderCreateRequest getMinimumOrderCreateRequest() {
.build())
.build();
}

@Test
void getSuccess() throws MPException, MPApiException, IOException {
HttpResponse response = MockHelper.generateHttpResponseFromFile(CREATE_ORDER_RESPONSE_FILE, HttpStatus.OK);
Mockito.doReturn(response).when(HTTP_CLIENT).execute(any(HttpRequestBase.class), any(HttpContext.class));

String orderId = "123";
Order order = client.get(orderId);

Assertions.assertNotNull(order);
Assertions.assertEquals(orderId, order.getId());

}
}