
com.squareup.square.AsyncCatalogClient Maven / Gradle / Ivy
Show all versions of square Show documentation
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.squareup.square;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.squareup.square.catalog.AsyncImagesClient;
import com.squareup.square.catalog.AsyncObjectClient;
import com.squareup.square.core.ClientOptions;
import com.squareup.square.core.MediaTypes;
import com.squareup.square.core.ObjectMappers;
import com.squareup.square.core.QueryStringMapper;
import com.squareup.square.core.RequestOptions;
import com.squareup.square.core.SquareApiException;
import com.squareup.square.core.SquareException;
import com.squareup.square.core.Suppliers;
import com.squareup.square.core.SyncPagingIterable;
import com.squareup.square.types.BatchDeleteCatalogObjectsRequest;
import com.squareup.square.types.BatchDeleteCatalogObjectsResponse;
import com.squareup.square.types.BatchGetCatalogObjectsRequest;
import com.squareup.square.types.BatchGetCatalogObjectsResponse;
import com.squareup.square.types.BatchUpsertCatalogObjectsRequest;
import com.squareup.square.types.BatchUpsertCatalogObjectsResponse;
import com.squareup.square.types.CatalogInfoResponse;
import com.squareup.square.types.CatalogObject;
import com.squareup.square.types.ListCatalogRequest;
import com.squareup.square.types.ListCatalogResponse;
import com.squareup.square.types.SearchCatalogItemsRequest;
import com.squareup.square.types.SearchCatalogItemsResponse;
import com.squareup.square.types.SearchCatalogObjectsRequest;
import com.squareup.square.types.SearchCatalogObjectsResponse;
import com.squareup.square.types.UpdateItemModifierListsRequest;
import com.squareup.square.types.UpdateItemModifierListsResponse;
import com.squareup.square.types.UpdateItemTaxesRequest;
import com.squareup.square.types.UpdateItemTaxesResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
public class AsyncCatalogClient {
protected final ClientOptions clientOptions;
protected final Supplier imagesClient;
protected final Supplier objectClient;
public AsyncCatalogClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
this.imagesClient = Suppliers.memoize(() -> new AsyncImagesClient(clientOptions));
this.objectClient = Suppliers.memoize(() -> new AsyncObjectClient(clientOptions));
}
/**
* Deletes a set of CatalogItems based on the
* provided list of target IDs and returns a set of successfully deleted IDs in
* the response. Deletion is a cascading event such that all children of the
* targeted object are also deleted. For example, deleting a CatalogItem will
* also delete all of its CatalogItemVariation
* children.
* BatchDeleteCatalogObjects
succeeds even if only a portion of the targeted
* IDs can be deleted. The response will only include IDs that were
* actually deleted.
* To ensure consistency, only one delete request is processed at a time per seller account.
* While one (batch or non-batch) delete request is being processed, other (batched and non-batched)
* delete requests are rejected with the 429
error code.
*/
public CompletableFuture batchDelete(BatchDeleteCatalogObjectsRequest request) {
return batchDelete(request, null);
}
/**
* Deletes a set of CatalogItems based on the
* provided list of target IDs and returns a set of successfully deleted IDs in
* the response. Deletion is a cascading event such that all children of the
* targeted object are also deleted. For example, deleting a CatalogItem will
* also delete all of its CatalogItemVariation
* children.
* BatchDeleteCatalogObjects
succeeds even if only a portion of the targeted
* IDs can be deleted. The response will only include IDs that were
* actually deleted.
* To ensure consistency, only one delete request is processed at a time per seller account.
* While one (batch or non-batch) delete request is being processed, other (batched and non-batched)
* delete requests are rejected with the 429
error code.
*/
public CompletableFuture batchDelete(
BatchDeleteCatalogObjectsRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/catalog/batch-delete")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new SquareException("Failed to serialize request", e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
future.complete(ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), BatchDeleteCatalogObjectsResponse.class));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
future.completeExceptionally(new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
return;
} catch (IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
});
return future;
}
/**
* Returns a set of objects based on the provided ID.
* Each CatalogItem returned in the set includes all of its
* child information including: all of its
* CatalogItemVariation objects, references to
* its CatalogModifierList objects, and the ids of
* any CatalogTax objects that apply to it.
*/
public CompletableFuture batchGet(BatchGetCatalogObjectsRequest request) {
return batchGet(request, null);
}
/**
* Returns a set of objects based on the provided ID.
* Each CatalogItem returned in the set includes all of its
* child information including: all of its
* CatalogItemVariation objects, references to
* its CatalogModifierList objects, and the ids of
* any CatalogTax objects that apply to it.
*/
public CompletableFuture batchGet(
BatchGetCatalogObjectsRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/catalog/batch-retrieve")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new SquareException("Failed to serialize request", e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
future.complete(ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), BatchGetCatalogObjectsResponse.class));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
future.completeExceptionally(new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
return;
} catch (IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
});
return future;
}
/**
* Creates or updates up to 10,000 target objects based on the provided
* list of objects. The target objects are grouped into batches and each batch is
* inserted/updated in an all-or-nothing manner. If an object within a batch is
* malformed in some way, or violates a database constraint, the entire batch
* containing that item will be disregarded. However, other batches in the same
* request may still succeed. Each batch may contain up to 1,000 objects, and
* batches will be processed in order as long as the total object count for the
* request (items, variations, modifier lists, discounts, and taxes) is no more
* than 10,000.
* To ensure consistency, only one update request is processed at a time per seller account.
* While one (batch or non-batch) update request is being processed, other (batched and non-batched)
* update requests are rejected with the 429
error code.
*/
public CompletableFuture batchUpsert(BatchUpsertCatalogObjectsRequest request) {
return batchUpsert(request, null);
}
/**
* Creates or updates up to 10,000 target objects based on the provided
* list of objects. The target objects are grouped into batches and each batch is
* inserted/updated in an all-or-nothing manner. If an object within a batch is
* malformed in some way, or violates a database constraint, the entire batch
* containing that item will be disregarded. However, other batches in the same
* request may still succeed. Each batch may contain up to 1,000 objects, and
* batches will be processed in order as long as the total object count for the
* request (items, variations, modifier lists, discounts, and taxes) is no more
* than 10,000.
* To ensure consistency, only one update request is processed at a time per seller account.
* While one (batch or non-batch) update request is being processed, other (batched and non-batched)
* update requests are rejected with the 429
error code.
*/
public CompletableFuture batchUpsert(
BatchUpsertCatalogObjectsRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/catalog/batch-upsert")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new SquareException("Failed to serialize request", e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
future.complete(ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), BatchUpsertCatalogObjectsResponse.class));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
future.completeExceptionally(new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
return;
} catch (IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
});
return future;
}
/**
* Retrieves information about the Square Catalog API, such as batch size
* limits that can be used by the BatchUpsertCatalogObjects
endpoint.
*/
public CompletableFuture info() {
return info(null);
}
/**
* Retrieves information about the Square Catalog API, such as batch size
* limits that can be used by the BatchUpsertCatalogObjects
endpoint.
*/
public CompletableFuture info(RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/catalog/info")
.build();
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("GET", null)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
future.complete(
ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CatalogInfoResponse.class));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
future.completeExceptionally(new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
return;
} catch (IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
});
return future;
}
/**
* Returns a list of all CatalogObjects of the specified types in the catalog.
* The types
parameter is specified as a comma-separated list of the CatalogObjectType values,
* for example, "ITEM
, ITEM_VARIATION
, MODIFIER
, MODIFIER_LIST
, CATEGORY
, DISCOUNT
, TAX
, IMAGE
".
* Important: ListCatalog does not return deleted catalog items. To retrieve
* deleted catalog items, use SearchCatalogObjects
* and set the include_deleted_objects
attribute value to true
.
*/
public CompletableFuture> list() {
return list(ListCatalogRequest.builder().build());
}
/**
* Returns a list of all CatalogObjects of the specified types in the catalog.
* The types
parameter is specified as a comma-separated list of the CatalogObjectType values,
* for example, "ITEM
, ITEM_VARIATION
, MODIFIER
, MODIFIER_LIST
, CATEGORY
, DISCOUNT
, TAX
, IMAGE
".
* Important: ListCatalog does not return deleted catalog items. To retrieve
* deleted catalog items, use SearchCatalogObjects
* and set the include_deleted_objects
attribute value to true
.
*/
public CompletableFuture> list(ListCatalogRequest request) {
return list(request, null);
}
/**
* Returns a list of all CatalogObjects of the specified types in the catalog.
* The types
parameter is specified as a comma-separated list of the CatalogObjectType values,
* for example, "ITEM
, ITEM_VARIATION
, MODIFIER
, MODIFIER_LIST
, CATEGORY
, DISCOUNT
, TAX
, IMAGE
".
* Important: ListCatalog does not return deleted catalog items. To retrieve
* deleted catalog items, use SearchCatalogObjects
* and set the include_deleted_objects
attribute value to true
.
*/
public CompletableFuture> list(
ListCatalogRequest request, RequestOptions requestOptions) {
HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/catalog/list");
if (request.getCursor().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "cursor", request.getCursor().get(), false);
}
if (request.getTypes().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "types", request.getTypes().get(), false);
}
if (request.getCatalogVersion().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl,
"catalog_version",
request.getCatalogVersion().get().toString(),
false);
}
Request.Builder _requestBuilder = new Request.Builder()
.url(httpUrl.build())
.method("GET", null)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json");
Request okhttpRequest = _requestBuilder.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture> future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
ListCatalogResponse parsedResponse =
ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListCatalogResponse.class);
Optional startingAfter = parsedResponse.getCursor();
ListCatalogRequest nextRequest = ListCatalogRequest.builder()
.from(request)
.cursor(startingAfter)
.build();
List result = parsedResponse.getObjects().orElse(Collections.emptyList());
future.complete(new SyncPagingIterable(startingAfter.isPresent(), result, () -> {
try {
return list(nextRequest, requestOptions).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
future.completeExceptionally(new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
return;
} catch (IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
});
return future;
}
/**
* Searches for CatalogObject of any type by matching supported search attribute values,
* excluding custom attribute values on items or item variations, against one or more of the specified query filters.
* This (SearchCatalogObjects
) endpoint differs from the SearchCatalogItems
* endpoint in the following aspects:
*
* SearchCatalogItems
can only search for items or item variations, whereas SearchCatalogObjects
can search for any type of catalog objects.
* SearchCatalogItems
supports the custom attribute query filters to return items or item variations that contain custom attribute values, where SearchCatalogObjects
does not.
* SearchCatalogItems
does not support the include_deleted_objects
filter to search for deleted items or item variations, whereas SearchCatalogObjects
does.
* - The both endpoints have different call conventions, including the query filter formats.
*
*/
public CompletableFuture search() {
return search(SearchCatalogObjectsRequest.builder().build());
}
/**
* Searches for CatalogObject of any type by matching supported search attribute values,
* excluding custom attribute values on items or item variations, against one or more of the specified query filters.
* This (SearchCatalogObjects
) endpoint differs from the SearchCatalogItems
* endpoint in the following aspects:
*
* SearchCatalogItems
can only search for items or item variations, whereas SearchCatalogObjects
can search for any type of catalog objects.
* SearchCatalogItems
supports the custom attribute query filters to return items or item variations that contain custom attribute values, where SearchCatalogObjects
does not.
* SearchCatalogItems
does not support the include_deleted_objects
filter to search for deleted items or item variations, whereas SearchCatalogObjects
does.
* - The both endpoints have different call conventions, including the query filter formats.
*
*/
public CompletableFuture search(SearchCatalogObjectsRequest request) {
return search(request, null);
}
/**
* Searches for CatalogObject of any type by matching supported search attribute values,
* excluding custom attribute values on items or item variations, against one or more of the specified query filters.
* This (SearchCatalogObjects
) endpoint differs from the SearchCatalogItems
* endpoint in the following aspects:
*
* SearchCatalogItems
can only search for items or item variations, whereas SearchCatalogObjects
can search for any type of catalog objects.
* SearchCatalogItems
supports the custom attribute query filters to return items or item variations that contain custom attribute values, where SearchCatalogObjects
does not.
* SearchCatalogItems
does not support the include_deleted_objects
filter to search for deleted items or item variations, whereas SearchCatalogObjects
does.
* - The both endpoints have different call conventions, including the query filter formats.
*
*/
public CompletableFuture search(
SearchCatalogObjectsRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/catalog/search")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new SquareException("Failed to serialize request", e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
future.complete(ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), SearchCatalogObjectsResponse.class));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
future.completeExceptionally(new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
return;
} catch (IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
});
return future;
}
/**
* Searches for catalog items or item variations by matching supported search attribute values, including
* custom attribute values, against one or more of the specified query filters.
* This (SearchCatalogItems
) endpoint differs from the SearchCatalogObjects
* endpoint in the following aspects:
*
* SearchCatalogItems
can only search for items or item variations, whereas SearchCatalogObjects
can search for any type of catalog objects.
* SearchCatalogItems
supports the custom attribute query filters to return items or item variations that contain custom attribute values, where SearchCatalogObjects
does not.
* SearchCatalogItems
does not support the include_deleted_objects
filter to search for deleted items or item variations, whereas SearchCatalogObjects
does.
* - The both endpoints use different call conventions, including the query filter formats.
*
*/
public CompletableFuture searchItems() {
return searchItems(SearchCatalogItemsRequest.builder().build());
}
/**
* Searches for catalog items or item variations by matching supported search attribute values, including
* custom attribute values, against one or more of the specified query filters.
* This (SearchCatalogItems
) endpoint differs from the SearchCatalogObjects
* endpoint in the following aspects:
*
* SearchCatalogItems
can only search for items or item variations, whereas SearchCatalogObjects
can search for any type of catalog objects.
* SearchCatalogItems
supports the custom attribute query filters to return items or item variations that contain custom attribute values, where SearchCatalogObjects
does not.
* SearchCatalogItems
does not support the include_deleted_objects
filter to search for deleted items or item variations, whereas SearchCatalogObjects
does.
* - The both endpoints use different call conventions, including the query filter formats.
*
*/
public CompletableFuture searchItems(SearchCatalogItemsRequest request) {
return searchItems(request, null);
}
/**
* Searches for catalog items or item variations by matching supported search attribute values, including
* custom attribute values, against one or more of the specified query filters.
* This (SearchCatalogItems
) endpoint differs from the SearchCatalogObjects
* endpoint in the following aspects:
*
* SearchCatalogItems
can only search for items or item variations, whereas SearchCatalogObjects
can search for any type of catalog objects.
* SearchCatalogItems
supports the custom attribute query filters to return items or item variations that contain custom attribute values, where SearchCatalogObjects
does not.
* SearchCatalogItems
does not support the include_deleted_objects
filter to search for deleted items or item variations, whereas SearchCatalogObjects
does.
* - The both endpoints use different call conventions, including the query filter formats.
*
*/
public CompletableFuture searchItems(
SearchCatalogItemsRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/catalog/search-catalog-items")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new SquareException("Failed to serialize request", e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
future.complete(ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), SearchCatalogItemsResponse.class));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
future.completeExceptionally(new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
return;
} catch (IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
});
return future;
}
/**
* Updates the CatalogModifierList objects
* that apply to the targeted CatalogItem without having
* to perform an upsert on the entire item.
*/
public CompletableFuture updateItemModifierLists(
UpdateItemModifierListsRequest request) {
return updateItemModifierLists(request, null);
}
/**
* Updates the CatalogModifierList objects
* that apply to the targeted CatalogItem without having
* to perform an upsert on the entire item.
*/
public CompletableFuture updateItemModifierLists(
UpdateItemModifierListsRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/catalog/update-item-modifier-lists")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new SquareException("Failed to serialize request", e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
future.complete(ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), UpdateItemModifierListsResponse.class));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
future.completeExceptionally(new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
return;
} catch (IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
});
return future;
}
/**
* Updates the CatalogTax objects that apply to the
* targeted CatalogItem without having to perform an
* upsert on the entire item.
*/
public CompletableFuture updateItemTaxes(UpdateItemTaxesRequest request) {
return updateItemTaxes(request, null);
}
/**
* Updates the CatalogTax objects that apply to the
* targeted CatalogItem without having to perform an
* upsert on the entire item.
*/
public CompletableFuture updateItemTaxes(
UpdateItemTaxesRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/catalog/update-item-taxes")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new SquareException("Failed to serialize request", e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
CompletableFuture future = new CompletableFuture<>();
client.newCall(okhttpRequest).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (response.isSuccessful()) {
future.complete(ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), UpdateItemTaxesResponse.class));
return;
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
future.completeExceptionally(new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)));
return;
} catch (IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
future.completeExceptionally(new SquareException("Network error executing HTTP request", e));
}
});
return future;
}
public AsyncImagesClient images() {
return this.imagesClient.get();
}
public AsyncObjectClient object() {
return this.objectClient.get();
}
}