
com.squareup.square.devices.CodesClient 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.devices;
import com.fasterxml.jackson.core.JsonProcessingException;
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.SyncPagingIterable;
import com.squareup.square.devices.types.CreateDeviceCodeRequest;
import com.squareup.square.devices.types.GetCodesRequest;
import com.squareup.square.devices.types.ListCodesRequest;
import com.squareup.square.types.CreateDeviceCodeResponse;
import com.squareup.square.types.DeviceCode;
import com.squareup.square.types.GetDeviceCodeResponse;
import com.squareup.square.types.ListDeviceCodesResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class CodesClient {
protected final ClientOptions clientOptions;
public CodesClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
/**
* Lists all DeviceCodes associated with the merchant.
*/
public SyncPagingIterable list() {
return list(ListCodesRequest.builder().build());
}
/**
* Lists all DeviceCodes associated with the merchant.
*/
public SyncPagingIterable list(ListCodesRequest request) {
return list(request, null);
}
/**
* Lists all DeviceCodes associated with the merchant.
*/
public SyncPagingIterable list(ListCodesRequest request, RequestOptions requestOptions) {
HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/devices/codes");
if (request.getCursor().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "cursor", request.getCursor().get(), false);
}
if (request.getLocationId().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "location_id", request.getLocationId().get(), false);
}
if (request.getProductType().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "product_type", request.getProductType().get(), false);
}
if (request.getStatus().isPresent()) {
QueryStringMapper.addQueryParameter(
httpUrl, "status", request.getStatus().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);
}
try (Response response = client.newCall(okhttpRequest).execute()) {
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
ListDeviceCodesResponse parsedResponse =
ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListDeviceCodesResponse.class);
Optional startingAfter = parsedResponse.getCursor();
ListCodesRequest nextRequest = ListCodesRequest.builder()
.from(request)
.cursor(startingAfter)
.build();
List result = parsedResponse.getDeviceCodes().orElse(Collections.emptyList());
return new SyncPagingIterable(
startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions));
}
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);
}
}
/**
* Creates a DeviceCode that can be used to login to a Square Terminal device to enter the connected
* terminal mode.
*/
public CreateDeviceCodeResponse create(CreateDeviceCodeRequest request) {
return create(request, null);
}
/**
* Creates a DeviceCode that can be used to login to a Square Terminal device to enter the connected
* terminal mode.
*/
public CreateDeviceCodeResponse create(CreateDeviceCodeRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/devices/codes")
.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);
}
try (Response response = client.newCall(okhttpRequest).execute()) {
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateDeviceCodeResponse.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);
}
}
/**
* Retrieves DeviceCode with the associated ID.
*/
public GetDeviceCodeResponse get(GetCodesRequest request) {
return get(request, null);
}
/**
* Retrieves DeviceCode with the associated ID.
*/
public GetDeviceCodeResponse get(GetCodesRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/devices/codes")
.addPathSegment(request.getId())
.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(), GetDeviceCodeResponse.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