
com.squareup.square.V1TransactionsClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of square Show documentation
Show all versions of square Show documentation
Java client library for the Square API
The newest version!
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.squareup.square;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
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.types.V1ListOrdersRequest;
import com.squareup.square.types.V1Order;
import com.squareup.square.types.V1RetrieveOrderRequest;
import com.squareup.square.types.V1UpdateOrderRequest;
import java.io.IOException;
import java.util.List;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class V1TransactionsClient {
protected final ClientOptions clientOptions;
public V1TransactionsClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
/**
* Provides summary information for a merchant's online store orders.
*/
public List v1ListOrders(V1ListOrdersRequest request) {
return v1ListOrders(request, null);
}
/**
* Provides summary information for a merchant's online store orders.
*/
public List v1ListOrders(V1ListOrdersRequest request, RequestOptions requestOptions) {
HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v1")
.addPathSegment(request.getLocationId())
.addPathSegments("orders");
if (request.getOrder().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "order", request.getOrder().get().toString(), false);
}
if (request.getLimit().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "limit", request.getLimit().get().toString(), false);
}
if (request.getBatchToken().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "batch_token", request.getBatchToken().get(), 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);
}
try (Response response = client.newCall(okhttpRequest).execute()) {
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), new TypeReference>() {});
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
throw new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
} catch (IOException e) {
throw new SquareException("Network error executing HTTP request", e);
}
}
/**
* Provides comprehensive information for a single online store order, including the order's history.
*/
public V1Order v1RetrieveOrder(V1RetrieveOrderRequest request) {
return v1RetrieveOrder(request, null);
}
/**
* Provides comprehensive information for a single online store order, including the order's history.
*/
public V1Order v1RetrieveOrder(V1RetrieveOrderRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v1")
.addPathSegment(request.getLocationId())
.addPathSegments("orders")
.addPathSegment(request.getOrderId())
.build();
Request.Builder _requestBuilder = new Request.Builder()
.url(httpUrl)
.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);
}
try (Response response = client.newCall(okhttpRequest).execute()) {
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), V1Order.class);
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
throw new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
} catch (IOException e) {
throw new SquareException("Network error executing HTTP request", e);
}
}
/**
* Updates the details of an online store order. Every update you perform on an order corresponds to one of three actions:
*/
public V1Order v1UpdateOrder(V1UpdateOrderRequest request) {
return v1UpdateOrder(request, null);
}
/**
* Updates the details of an online store order. Every update you perform on an order corresponds to one of three actions:
*/
public V1Order v1UpdateOrder(V1UpdateOrderRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v1")
.addPathSegment(request.getLocationId())
.addPathSegments("orders")
.addPathSegment(request.getOrderId())
.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("PUT", 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);
}
try (Response response = client.newCall(okhttpRequest).execute()) {
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), V1Order.class);
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
throw new SquareApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
} catch (IOException e) {
throw new SquareException("Network error executing HTTP request", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy