Skip to content
Draft
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
236 changes: 236 additions & 0 deletions src/main/java/com/mercadopago/client/store/StoreClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package com.mercadopago.client.store;

import static com.mercadopago.MercadoPagoConfig.getStreamHandler;

import com.google.gson.JsonObject;
import com.mercadopago.MercadoPagoConfig;
import com.mercadopago.client.MercadoPagoClient;
import com.mercadopago.core.MPRequestOptions;
import com.mercadopago.exceptions.MPApiException;
import com.mercadopago.exceptions.MPException;
import com.mercadopago.net.HttpMethod;
import com.mercadopago.net.MPHttpClient;
import com.mercadopago.net.MPResponse;
import com.mercadopago.net.MPSearchRequest;
import com.mercadopago.resources.store.Store;
import com.mercadopago.serialization.Serializer;
import java.util.Map;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;

/**
* Client for the MercadoPago Stores API.
*
* <p>Supports creating, retrieving, searching, updating and deleting stores associated with a
* seller user:
* <ul>
* <li>POST /users/{user_id}/stores</li>
* <li>GET /stores/{id}</li>
* <li>GET /users/{user_id}/stores/search</li>
* <li>PUT /users/{user_id}/stores/{id}</li>
* <li>DELETE /users/{user_id}/stores/{id}</li>
* </ul>
*/
public class StoreClient extends MercadoPagoClient {

/** Class-level logger for store operations. */
private static final Logger LOGGER = Logger.getLogger(StoreClient.class.getName());

/** Default constructor. Uses the default HTTP client provided by {@link MercadoPagoConfig}. */
public StoreClient() {
this(MercadoPagoConfig.getHttpClient());
}

/**
* Constructs a {@code StoreClient} with a custom HTTP client.
*
* @param httpClient the {@link MPHttpClient} implementation used to execute HTTP requests
*/
public StoreClient(MPHttpClient httpClient) {
super(httpClient);
StreamHandler streamHandler = getStreamHandler();
streamHandler.setLevel(MercadoPagoConfig.getLoggingLevel());
LOGGER.addHandler(streamHandler);
LOGGER.setLevel(MercadoPagoConfig.getLoggingLevel());
}

/**
* Creates a store for the given user.
*
* @param userId identifier of the user (seller) that will own the store
* @param request the {@link StoreCreateRequest} with the store details
* @return the created {@link Store}
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public Store createStore(Long userId, StoreCreateRequest request)
throws MPException, MPApiException {
return this.createStore(userId, request, null);
}

/**
* Creates a store for the given user with custom request options.
*
* @param userId identifier of the user (seller) that will own the store
* @param request the {@link StoreCreateRequest} with the store details
* @param requestOptions optional {@link MPRequestOptions}; may be {@code null}
* @return the created {@link Store}
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public Store createStore(Long userId, StoreCreateRequest request, MPRequestOptions requestOptions)
throws MPException, MPApiException {
LOGGER.info("Sending create store request");
MPResponse response =
send(
String.format("/users/%s/stores", encodePathParam(userId)),
HttpMethod.POST,
Serializer.serializeToJson(request),
null,
requestOptions);
Store store = Serializer.deserializeFromJson(Store.class, response.getContent());
store.setResponse(response);
return store;
}

/**
* Retrieves a store by its identifier.
*
* @param id identifier of the store
* @return the {@link Store}
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public Store getStore(Long id) throws MPException, MPApiException {
return this.getStore(id, null);
}

/**
* Retrieves a store by its identifier with custom request options.
*
* @param id identifier of the store
* @param requestOptions optional {@link MPRequestOptions}; may be {@code null}
* @return the {@link Store}
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public Store getStore(Long id, MPRequestOptions requestOptions)
throws MPException, MPApiException {
LOGGER.info("Sending get store request");
MPResponse response =
send(String.format("/stores/%s", encodePathParam(id)), HttpMethod.GET, null, null, requestOptions);
Store store = Serializer.deserializeFromJson(Store.class, response.getContent());
store.setResponse(response);
return store;
}

/**
* Searches stores of the given user.
*
* @param userId identifier of the user (seller)
* @param searchRequest the {@link MPSearchRequest} with filters and pagination, or {@code null}
* @return the {@link MPResponse} with the search results
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public MPResponse searchStores(Long userId, MPSearchRequest searchRequest)
throws MPException, MPApiException {
return this.searchStores(userId, searchRequest, null);
}

/**
* Searches stores of the given user with custom request options.
*
* @param userId identifier of the user (seller)
* @param searchRequest the {@link MPSearchRequest} with filters and pagination, or {@code null}
* @param requestOptions optional {@link MPRequestOptions}; may be {@code null}
* @return the {@link MPResponse} with the search results
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public MPResponse searchStores(
Long userId, MPSearchRequest searchRequest, MPRequestOptions requestOptions)
throws MPException, MPApiException {
LOGGER.info("Sending search stores request");
return search(
String.format("/users/%s/stores/search", encodePathParam(userId)),
searchRequest,
requestOptions);
}

/**
* Updates a store of the given user.
*
* @param userId identifier of the user (seller) that owns the store
* @param storeId identifier of the store to update
* @param request the {@link StoreUpdateRequest} with the fields to update
* @return the updated {@link Store}
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public Store updateStore(Long userId, Long storeId, StoreUpdateRequest request)
throws MPException, MPApiException {
return this.updateStore(userId, storeId, request, null);
}

/**
* Updates a store of the given user with custom request options.
*
* @param userId identifier of the user (seller) that owns the store
* @param storeId identifier of the store to update
* @param request the {@link StoreUpdateRequest} with the fields to update
* @param requestOptions optional {@link MPRequestOptions}; may be {@code null}
* @return the updated {@link Store}
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public Store updateStore(
Long userId, Long storeId, StoreUpdateRequest request, MPRequestOptions requestOptions)
throws MPException, MPApiException {
LOGGER.info("Sending update store request");
MPResponse response =
send(
String.format("/users/%s/stores/%s", encodePathParam(userId), encodePathParam(storeId)),
HttpMethod.PUT,
Serializer.serializeToJson(request),
null,
requestOptions);
Store store = Serializer.deserializeFromJson(Store.class, response.getContent());
store.setResponse(response);
return store;
}

/**
* Deletes a store of the given user.
*
* @param userId identifier of the user (seller) that owns the store
* @param storeId identifier of the store to delete
* @return the {@link MPResponse} returned by the API
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public MPResponse deleteStore(Long userId, Long storeId) throws MPException, MPApiException {
return this.deleteStore(userId, storeId, null);
}

/**
* Deletes a store of the given user with custom request options.
*
* @param userId identifier of the user (seller) that owns the store
* @param storeId identifier of the store to delete
* @param requestOptions optional {@link MPRequestOptions}; may be {@code null}
* @return the {@link MPResponse} returned by the API
* @throws MPException if a transport-level or SDK-internal error occurs
* @throws MPApiException if the API returns a non-successful HTTP status code
*/
public MPResponse deleteStore(Long userId, Long storeId, MPRequestOptions requestOptions)
throws MPException, MPApiException {
LOGGER.info("Sending delete store request");
return send(
String.format("/users/%s/stores/%s", encodePathParam(userId), encodePathParam(storeId)),
HttpMethod.DELETE,
(JsonObject) null,
(Map<String, Object>) null,
requestOptions);
}
}
117 changes: 117 additions & 0 deletions src/main/java/com/mercadopago/client/store/StoreCreateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.mercadopago.client.store;

import lombok.Builder;
import lombok.Getter;

/**
* Request payload for creating a store through the Stores API.
*
* @see StoreClient#createStore(Long, StoreCreateRequest)
*/
@Builder
@Getter
public class StoreCreateRequest {

/** Name of the store. */
private String name;

/** External identifier of the store, as provided by the integrator. */
private String externalId;

/** Location details of the store. */
private Location location;

/** Business hours of the store, grouped by day of the week. */
private BusinessHours businessHours;

/** Location details of a store. */
@Getter
public static class Location {

/** Address line of the store. */
private String addressLine;

/** Street name of the store address. */
private String streetName;

/** Street number of the store address. */
private String streetNumber;

/** Zip code of the store address. */
private String zipCode;

/** City of the store. */
private City city;

/** State of the store. */
private State state;

/** Reference information for the store location. */
private String reference;

/** Latitude of the store location. */
private Double latitude;

/** Longitude of the store location. */
private Double longitude;
}

/** City of a store location. */
@Getter
public static class City {

/** Identifier of the city. */
private String id;

/** Name of the city. */
private String name;
}

/** State of a store location. */
@Getter
public static class State {

/** Identifier of the state. */
private String id;

/** Name of the state. */
private String name;
}

/** Business hours of a store, grouped by day of the week. */
@Getter
public static class BusinessHours {

/** Business hours for Monday. */
private DayHours monday;

/** Business hours for Tuesday. */
private DayHours tuesday;

/** Business hours for Wednesday. */
private DayHours wednesday;

/** Business hours for Thursday. */
private DayHours thursday;

/** Business hours for Friday. */
private DayHours friday;

/** Business hours for Saturday. */
private DayHours saturday;

/** Business hours for Sunday. */
private DayHours sunday;
}

/** Opening and closing hours for a single day. */
@Getter
public static class DayHours {

/** Opening time (e.g. "08:00"). */
private String open;

/** Closing time (e.g. "18:00"). */
private String close;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mercadopago.client.store;

import com.google.gson.JsonObject;
import java.io.Serializable;
import lombok.Builder;
import lombok.Getter;

/**
* Request object for updating an existing store through the Stores API
* ({@code PUT /users/{user_id}/stores/{id}}).
*
* <p>Only fields evidenced by the Stores specification are modeled. All fields are optional in
* an update request; fields left unset are not sent to the API. The {@code location} and
* {@code business_hours} schemas are not resolved by the specification, so they are kept opaque
* as raw JSON objects.
*
* @see com.mercadopago.client.store.StoreClient
*/
@Getter
@Builder
public class StoreUpdateRequest implements Serializable {

/** Name of the store. */
private final String name;

/** Business hours of the store, kept opaque as a raw JSON object. */
private final JsonObject businessHours;

/** Geographic location of the store, kept opaque as a raw JSON object. */
private final JsonObject location;
}
Loading
Loading