All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squareup.square.merchants.CustomAttributeDefinitionsClient Maven / Gradle / Ivy

The newest version!
/**
 * This file was auto-generated by Fern from our API Definition.
 */
package com.squareup.square.merchants;

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.merchants.types.CreateMerchantCustomAttributeDefinitionRequest;
import com.squareup.square.merchants.types.DeleteCustomAttributeDefinitionsRequest;
import com.squareup.square.merchants.types.GetCustomAttributeDefinitionsRequest;
import com.squareup.square.merchants.types.ListCustomAttributeDefinitionsRequest;
import com.squareup.square.merchants.types.UpdateMerchantCustomAttributeDefinitionRequest;
import com.squareup.square.types.CreateMerchantCustomAttributeDefinitionResponse;
import com.squareup.square.types.CustomAttributeDefinition;
import com.squareup.square.types.DeleteMerchantCustomAttributeDefinitionResponse;
import com.squareup.square.types.ListMerchantCustomAttributeDefinitionsResponse;
import com.squareup.square.types.RetrieveMerchantCustomAttributeDefinitionResponse;
import com.squareup.square.types.UpdateMerchantCustomAttributeDefinitionResponse;
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 CustomAttributeDefinitionsClient {
    protected final ClientOptions clientOptions;

    public CustomAttributeDefinitionsClient(ClientOptions clientOptions) {
        this.clientOptions = clientOptions;
    }

    /**
     * Lists the merchant-related custom attribute definitions that belong to a Square seller account.
     * When all response pages are retrieved, the results include all custom attribute definitions
     * that are visible to the requesting application, including those that are created by other
     * applications and set to VISIBILITY_READ_ONLY or VISIBILITY_READ_WRITE_VALUES.
     */
    public SyncPagingIterable list() {
        return list(ListCustomAttributeDefinitionsRequest.builder().build());
    }

    /**
     * Lists the merchant-related custom attribute definitions that belong to a Square seller account.
     * When all response pages are retrieved, the results include all custom attribute definitions
     * that are visible to the requesting application, including those that are created by other
     * applications and set to VISIBILITY_READ_ONLY or VISIBILITY_READ_WRITE_VALUES.
     */
    public SyncPagingIterable list(ListCustomAttributeDefinitionsRequest request) {
        return list(request, null);
    }

    /**
     * Lists the merchant-related custom attribute definitions that belong to a Square seller account.
     * When all response pages are retrieved, the results include all custom attribute definitions
     * that are visible to the requesting application, including those that are created by other
     * applications and set to VISIBILITY_READ_ONLY or VISIBILITY_READ_WRITE_VALUES.
     */
    public SyncPagingIterable list(
            ListCustomAttributeDefinitionsRequest request, RequestOptions requestOptions) {
        HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/merchants/custom-attribute-definitions");
        if (request.getVisibilityFilter().isPresent()) {
            QueryStringMapper.addQueryParameter(
                    httpUrl,
                    "visibility_filter",
                    request.getVisibilityFilter().get().toString(),
                    false);
        }
        if (request.getLimit().isPresent()) {
            QueryStringMapper.addQueryParameter(
                    httpUrl, "limit", request.getLimit().get().toString(), false);
        }
        if (request.getCursor().isPresent()) {
            QueryStringMapper.addQueryParameter(
                    httpUrl, "cursor", request.getCursor().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()) {
                ListMerchantCustomAttributeDefinitionsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue(
                        responseBody.string(), ListMerchantCustomAttributeDefinitionsResponse.class);
                Optional startingAfter = parsedResponse.getCursor();
                ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder()
                        .from(request)
                        .cursor(startingAfter)
                        .build();
                List result =
                        parsedResponse.getCustomAttributeDefinitions().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 merchant-related custom attribute definition for a Square seller account.
     * Use this endpoint to define a custom attribute that can be associated with a merchant connecting to your application.
     * A custom attribute definition specifies the key, visibility, schema, and other properties
     * for a custom attribute. After the definition is created, you can call
     * UpsertMerchantCustomAttribute or
     * BulkUpsertMerchantCustomAttributes
     * to set the custom attribute for a merchant.
     */
    public CreateMerchantCustomAttributeDefinitionResponse create(
            CreateMerchantCustomAttributeDefinitionRequest request) {
        return create(request, null);
    }

    /**
     * Creates a merchant-related custom attribute definition for a Square seller account.
     * Use this endpoint to define a custom attribute that can be associated with a merchant connecting to your application.
     * A custom attribute definition specifies the key, visibility, schema, and other properties
     * for a custom attribute. After the definition is created, you can call
     * UpsertMerchantCustomAttribute or
     * BulkUpsertMerchantCustomAttributes
     * to set the custom attribute for a merchant.
     */
    public CreateMerchantCustomAttributeDefinitionResponse create(
            CreateMerchantCustomAttributeDefinitionRequest request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/merchants/custom-attribute-definitions")
                .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(), CreateMerchantCustomAttributeDefinitionResponse.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 a merchant-related custom attribute definition from a Square seller account.
     * To retrieve a custom attribute definition created by another application, the visibility
     * setting must be VISIBILITY_READ_ONLY or VISIBILITY_READ_WRITE_VALUES.
     */
    public RetrieveMerchantCustomAttributeDefinitionResponse get(GetCustomAttributeDefinitionsRequest request) {
        return get(request, null);
    }

    /**
     * Retrieves a merchant-related custom attribute definition from a Square seller account.
     * To retrieve a custom attribute definition created by another application, the visibility
     * setting must be VISIBILITY_READ_ONLY or VISIBILITY_READ_WRITE_VALUES.
     */
    public RetrieveMerchantCustomAttributeDefinitionResponse get(
            GetCustomAttributeDefinitionsRequest request, RequestOptions requestOptions) {
        HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/merchants/custom-attribute-definitions")
                .addPathSegment(request.getKey());
        if (request.getVersion().isPresent()) {
            QueryStringMapper.addQueryParameter(
                    httpUrl, "version", request.getVersion().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()) {
                return ObjectMappers.JSON_MAPPER.readValue(
                        responseBody.string(), RetrieveMerchantCustomAttributeDefinitionResponse.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 a merchant-related custom attribute definition for a Square seller account.
     * Use this endpoint to update the following fields: name, description, visibility, or the
     * schema for a Selection data type.
     * Only the definition owner can update a custom attribute definition.
     */
    public UpdateMerchantCustomAttributeDefinitionResponse update(
            UpdateMerchantCustomAttributeDefinitionRequest request) {
        return update(request, null);
    }

    /**
     * Updates a merchant-related custom attribute definition for a Square seller account.
     * Use this endpoint to update the following fields: name, description, visibility, or the
     * schema for a Selection data type.
     * Only the definition owner can update a custom attribute definition.
     */
    public UpdateMerchantCustomAttributeDefinitionResponse update(
            UpdateMerchantCustomAttributeDefinitionRequest request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/merchants/custom-attribute-definitions")
                .addPathSegment(request.getKey())
                .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(), UpdateMerchantCustomAttributeDefinitionResponse.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);
        }
    }

    /**
     * Deletes a merchant-related custom attribute definition from a Square seller account.
     * Deleting a custom attribute definition also deletes the corresponding custom attribute from
     * the merchant.
     * Only the definition owner can delete a custom attribute definition.
     */
    public DeleteMerchantCustomAttributeDefinitionResponse delete(DeleteCustomAttributeDefinitionsRequest request) {
        return delete(request, null);
    }

    /**
     * Deletes a merchant-related custom attribute definition from a Square seller account.
     * Deleting a custom attribute definition also deletes the corresponding custom attribute from
     * the merchant.
     * Only the definition owner can delete a custom attribute definition.
     */
    public DeleteMerchantCustomAttributeDefinitionResponse delete(
            DeleteCustomAttributeDefinitionsRequest request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/merchants/custom-attribute-definitions")
                .addPathSegment(request.getKey())
                .build();
        Request.Builder _requestBuilder = new Request.Builder()
                .url(httpUrl)
                .method("DELETE", 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(), DeleteMerchantCustomAttributeDefinitionResponse.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