diff --git a/.fern/metadata.json b/.fern/metadata.json new file mode 100644 index 00000000..3dc4b064 --- /dev/null +++ b/.fern/metadata.json @@ -0,0 +1,16 @@ +{ + "cliVersion": "2.2.5", + "generatorName": "fernapi/fern-java-sdk", + "generatorVersion": "3.18.6", + "generatorConfig": { + "base-api-exception-class-name": "SquareApiException", + "base-exception-class-name": "SquareException", + "client-class-name": "SquareClient", + "inline-path-parameters": true, + "inline-file-properties": true, + "package-prefix": "com.squareup.square", + "package-layout": "flat", + "enable-forward-compatible-enums": true, + "publish-to": "central" + } +} \ No newline at end of file diff --git a/README.md b/README.md index da5c11ec..fc01b818 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,31 @@ The Square Java library provides convenient access to the Square APIs from Java. +## Table of Contents + +- [Requirements](#requirements) +- [Installation](#installation) +- [Usage](#usage) +- [Instantiation](#instantiation) +- [Enums](#enums) +- [Versioning](#versioning) +- [Automatic Pagination](#automatic-pagination) +- [Retries](#retries) +- [Timeouts](#timeouts) +- [Environments](#environments) +- [Base Url](#base-url) +- [Exception Handling](#exception-handling) +- [Webhook Signature Verification](#webhook-signature-verification) +- [Reference](#reference) +- [Legacy Sdk](#legacy-sdk) +- [Advanced](#advanced) + - [Custom Client](#custom-client) + - [Retries](#retries) + - [Timeouts](#timeouts) + - [Custom Headers](#custom-headers) + - [Access Raw Response Data](#access-raw-response-data) +- [Contributing](#contributing) + ## Requirements Use of the Square Java SDK requires: @@ -31,7 +56,7 @@ Add the dependency in your `pom.xml` file: com.squareup square - 45.1.0.20251016 + 0.0.538 ``` @@ -278,9 +303,9 @@ When the API returns a non-success status code (4xx or 5xx response), an API exc ```java import com.squareup.square.core.SquareApiException; -try { +try{ client.payments().create(...); -} catch (SquareApiException e) { +} catch (SquareApiException e){ // Do something with the API exception... } ``` @@ -336,8 +361,8 @@ Gradle: ```groovy dependencies { - implementation 'com.squareup:square:45.1.0.20251016' - implementation 'com.squareup:square-legacy:45.1.0.20251016' + implementation 'com.squareup:square:0.0.538' + implementation 'com.squareup:square-legacy:0.0.538' } ``` @@ -347,12 +372,12 @@ Maven: com.squareup square - 45.1.0.20251016 + 0.0.538 com.squareup square-legacy - 45.1.0.20251016 + 0.0.538 ``` @@ -364,7 +389,7 @@ Maven: ### Custom Client -This SDK is built to work with any instance of `OkHttpClient`. By default, if no client is provided, the SDK will construct one. +This SDK is built to work with any instance of `OkHttpClient`. By default, if no client is provided, the SDK will construct one. However, you can pass your own client like so: ```java @@ -383,7 +408,9 @@ SquareClient client = SquareClient The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured -retry limit (default: 2). +retry limit (default: 2). Before defaulting to exponential backoff, the SDK will first attempt to respect +the `Retry-After` header (as either in seconds or as an HTTP date), and then the `X-RateLimit-Reset` header +(as a Unix timestamp in epoch seconds); failing both of those, it will fall back to exponential backoff. A request is deemed retryable when any of the following HTTP status codes is returned: @@ -452,6 +479,19 @@ client.payments().create( ); ``` +### Access Raw Response Data + +The SDK provides access to raw response data, including headers, through the `withRawResponse()` method. +The `withRawResponse()` method returns a raw client that wraps all responses with `body()` and `headers()` methods. +(A normal client's `response` is identical to a raw client's `response.body()`.) + +```java +CreateHttpResponse response = client.payments().withRawResponse().create(...); + +System.out.println(response.body()); +System.out.println(response.headers().get("X-My-Header")); +``` + ## Contributing While we value open-source contributions to this SDK, this library is generated programmatically. diff --git a/build.gradle b/build.gradle index ed6b345e..852502d2 100644 --- a/build.gradle +++ b/build.gradle @@ -14,10 +14,10 @@ repositories { } dependencies { - api 'com.squareup.okhttp3:okhttp:4.12.0' - api 'com.fasterxml.jackson.core:jackson-databind:2.17.2' - api 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.17.2' - api 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.2' + api 'com.squareup.okhttp3:okhttp:5.2.1' + api 'com.fasterxml.jackson.core:jackson-databind:2.18.2' + api 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.18.2' + api 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.18.2' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2' @@ -47,7 +47,7 @@ java { group = 'com.squareup' -version = '45.1.0.20251016' +version = '0.0.538' jar { dependsOn(":generatePomFileForMavenPublication") @@ -78,7 +78,7 @@ publishing { maven(MavenPublication) { groupId = 'com.squareup' artifactId = 'square' - version = '45.1.0.20251016' + version = '0.0.538' from components.java pom { name = 'square' diff --git a/reference.md b/reference.md index b341cd67..cad48580 100644 --- a/reference.md +++ b/reference.md @@ -817,7 +817,7 @@ client.applePay().registerDomain( ## BankAccounts -
client.bankAccounts.list() -> ListBankAccountsResponse +
client.bankAccounts.list() -> SyncPagingIterable<BankAccount>
@@ -1030,7 +1030,7 @@ client.bankAccounts().get(
## Bookings -
client.bookings.list() -> ListBookingsResponse +
client.bookings.list() -> SyncPagingIterable<Booking>
@@ -1759,7 +1759,7 @@ client.bookings().cancel(
## Cards -
client.cards.list() -> ListCardsResponse +
client.cards.list() -> SyncPagingIterable<Card>
@@ -2493,7 +2493,7 @@ client.catalog().info();
-
client.catalog.list() -> ListCatalogResponse +
client.catalog.list() -> SyncPagingIterable<CatalogObject>
@@ -3184,7 +3184,7 @@ At least one of `taxes_to_enable` or `taxes_to_disable` must be specified.
## Channels -
client.channels.list() -> ListChannelsResponse +
client.channels.list() -> SyncPagingIterable<Channel>
@@ -3403,7 +3403,7 @@ client.channels().get(
## Customers -
client.customers.list() -> ListCustomersResponse +
client.customers.list() -> SyncPagingIterable<Customer>
@@ -4575,7 +4575,7 @@ As a best practice, you should include this parameter to enable [optimistic conc
## Devices -
client.devices.list() -> ListDevicesResponse +
client.devices.list() -> SyncPagingIterable<Device>
@@ -4730,7 +4730,7 @@ client.devices().get(
## Disputes -
client.disputes.list() -> ListDisputesResponse +
client.disputes.list() -> SyncPagingIterable<Dispute>
@@ -5151,7 +5151,7 @@ client.disputes().submitEvidence(
## Employees -
client.employees.list() -> ListEmployeesResponse +
client.employees.list() -> SyncPagingIterable<Employee>
@@ -5521,7 +5521,7 @@ client.events().listEventTypes(
## GiftCards -
client.giftCards.list() -> ListGiftCardsResponse +
client.giftCards.list() -> SyncPagingIterable<GiftCard>
@@ -6502,7 +6502,7 @@ client.inventory().batchCreateChanges(
-
client.inventory.batchGetChanges(request) -> BatchGetInventoryChangesResponse +
client.inventory.batchGetChanges(request) -> SyncPagingIterable<InventoryChange>
@@ -6589,7 +6589,7 @@ client.inventory().batchGetChanges(
-
client.inventory.batchGetCounts(request) -> BatchGetInventoryCountsResponse +
client.inventory.batchGetCounts(request) -> SyncPagingIterable<InventoryCount>
@@ -6854,7 +6854,7 @@ client.inventory().getTransfer(
-
client.inventory.get(catalogObjectId) -> GetInventoryCountResponse +
client.inventory.get(catalogObjectId) -> SyncPagingIterable<InventoryCount>
@@ -6942,7 +6942,7 @@ See the [Pagination](https://developer.squareup.com/docs/working-with-apis/pagin
-
client.inventory.changes(catalogObjectId) -> GetInventoryChangesResponse +
client.inventory.changes(catalogObjectId) -> SyncPagingIterable<InventoryChange>
@@ -7040,7 +7040,7 @@ See the [Pagination](https://developer.squareup.com/docs/working-with-apis/pagin
## Invoices -
client.invoices.list() -> ListInvoicesResponse +
client.invoices.list() -> SyncPagingIterable<Invoice>
@@ -9738,7 +9738,7 @@ For more information, see [Pagination](https://developer.squareup.com/docs/build
## Merchants -
client.merchants.list() -> ListMerchantsResponse +
client.merchants.list() -> SyncPagingIterable<Merchant>
@@ -11040,7 +11040,7 @@ The payment total must match the order total.
## Payments -
client.payments.list() -> ListPaymentsResponse +
client.payments.list() -> SyncPagingIterable<Payment>
@@ -12053,7 +12053,7 @@ the update fails and a response with a VERSION_MISMATCH error is returned.
## Payouts -
client.payouts.list() -> ListPayoutsResponse +
client.payouts.list() -> SyncPagingIterable<Payout>
@@ -12248,7 +12248,7 @@ client.payouts().get(
-
client.payouts.listEntries(payoutId) -> ListPayoutEntriesResponse +
client.payouts.listEntries(payoutId) -> SyncPagingIterable<PayoutEntry>
@@ -12347,7 +12347,7 @@ Default: `100`
## Refunds -
client.refunds.list() -> ListPaymentRefundsResponse +
client.refunds.list() -> SyncPagingIterable<PaymentRefund>
@@ -13912,7 +13912,7 @@ client.subscriptions().cancel(
-
client.subscriptions.listEvents(subscriptionId) -> ListSubscriptionEventsResponse +
client.subscriptions.listEvents(subscriptionId) -> SyncPagingIterable<SubscriptionEvent>
@@ -15528,7 +15528,7 @@ any valid string but must be unique for every CreateTransferOrder request.
-
client.transferOrders.search(request) -> SearchTransferOrdersResponse +
client.transferOrders.search(request) -> SyncPagingIterable<TransferOrder>
@@ -16763,7 +16763,7 @@ client.vendors().update(
## Bookings CustomAttributeDefinitions -
client.bookings.customAttributeDefinitions.list() -> ListBookingCustomAttributeDefinitionsResponse +
client.bookings.customAttributeDefinitions.list() -> SyncPagingIterable<CustomAttributeDefinition>
@@ -17343,7 +17343,7 @@ information needed to create or update a custom attribute.
-
client.bookings.customAttributes.list(bookingId) -> ListBookingCustomAttributesResponse +
client.bookings.customAttributes.list(bookingId) -> SyncPagingIterable<CustomAttribute>
@@ -17736,7 +17736,7 @@ definition owner, you must use the qualified key.
## Bookings LocationProfiles -
client.bookings.locationProfiles.list() -> ListLocationBookingProfilesResponse +
client.bookings.locationProfiles.list() -> SyncPagingIterable<LocationBookingProfile>
@@ -17805,7 +17805,7 @@ client.bookings().locationProfiles().list(
## Bookings TeamMemberProfiles -
client.bookings.teamMemberProfiles.list() -> ListTeamMemberBookingProfilesResponse +
client.bookings.teamMemberProfiles.list() -> SyncPagingIterable<TeamMemberBookingProfile>
@@ -17951,7 +17951,7 @@ client.bookings().teamMemberProfiles().get(
## CashDrawers Shifts -
client.cashDrawers.shifts.list() -> ListCashDrawerShiftsResponse +
client.cashDrawers.shifts.list() -> SyncPagingIterable<CashDrawerShiftSummary>
@@ -18131,7 +18131,7 @@ client.cashDrawers().shifts().get(
-
client.cashDrawers.shifts.listEvents(shiftId) -> ListCashDrawerShiftEventsResponse +
client.cashDrawers.shifts.listEvents(shiftId) -> SyncPagingIterable<CashDrawerShiftEvent>
@@ -18617,7 +18617,7 @@ catalog item will delete its catalog item variations).
## Checkout PaymentLinks -
client.checkout.paymentLinks.list() -> ListPaymentLinksResponse +
client.checkout.paymentLinks.list() -> SyncPagingIterable<PaymentLink>
@@ -19044,7 +19044,7 @@ client.checkout().paymentLinks().delete(
## Customers CustomAttributeDefinitions -
client.customers.customAttributeDefinitions.list() -> ListCustomerCustomAttributeDefinitionsResponse +
client.customers.customAttributeDefinitions.list() -> SyncPagingIterable<CustomAttributeDefinition>
@@ -19611,7 +19611,7 @@ information needed to create or update a custom attribute.
## Customers Groups -
client.customers.groups.list() -> ListCustomerGroupsResponse +
client.customers.groups.list() -> SyncPagingIterable<CustomerGroup>
@@ -20097,7 +20097,7 @@ client.customers().groups().remove(
## Customers Segments -
client.customers.segments.list() -> ListCustomerSegmentsResponse +
client.customers.segments.list() -> SyncPagingIterable<CustomerSegment>
@@ -20431,7 +20431,7 @@ client.customers().cards().delete(
## Customers CustomAttributes -
client.customers.customAttributes.list(customerId) -> ListCustomerCustomAttributesResponse +
client.customers.customAttributes.list(customerId) -> SyncPagingIterable<CustomAttribute>
@@ -20833,7 +20833,7 @@ definition owner, you must use the qualified key.
## Devices Codes -
client.devices.codes.list() -> ListDeviceCodesResponse +
client.devices.codes.list() -> SyncPagingIterable<DeviceCode>
@@ -21074,7 +21074,7 @@ client.devices().codes().get(
## Disputes Evidence -
client.disputes.evidence.list(disputeId) -> ListDisputeEvidenceResponse +
client.disputes.evidence.list(disputeId) -> SyncPagingIterable<DisputeEvidence>
@@ -21286,7 +21286,7 @@ client.disputes().evidence().delete(
## GiftCards Activities -
client.giftCards.activities.list() -> ListGiftCardActivitiesResponse +
client.giftCards.activities.list() -> SyncPagingIterable<GiftCardActivity>
@@ -21527,7 +21527,7 @@ gift card, the `location_id` where the activity occurred, and the activity `type
## Labor BreakTypes -
client.labor.breakTypes.list() -> ListBreakTypesResponse +
client.labor.breakTypes.list() -> SyncPagingIterable<BreakType>
@@ -21897,7 +21897,7 @@ client.labor().breakTypes().delete(
## Labor EmployeeWages -
client.labor.employeeWages.list() -> ListEmployeeWagesResponse +
client.labor.employeeWages.list() -> SyncPagingIterable<EmployeeWage>
@@ -22517,7 +22517,7 @@ client.labor().shifts().delete(
## Labor TeamMemberWages -
client.labor.teamMemberWages.list() -> ListTeamMemberWagesResponse +
client.labor.teamMemberWages.list() -> SyncPagingIterable<TeamMemberWage>
@@ -22660,7 +22660,7 @@ client.labor().teamMemberWages().get(
## Labor WorkweekConfigs -
client.labor.workweekConfigs.list() -> ListWorkweekConfigsResponse +
client.labor.workweekConfigs.list() -> SyncPagingIterable<WorkweekConfig>
@@ -22804,7 +22804,7 @@ client.labor().workweekConfigs().get(
## Locations CustomAttributeDefinitions -
client.locations.customAttributeDefinitions.list() -> ListLocationCustomAttributeDefinitionsResponse +
client.locations.customAttributeDefinitions.list() -> SyncPagingIterable<CustomAttributeDefinition>
@@ -23418,7 +23418,7 @@ information needed to create or update a custom attribute.
-
client.locations.customAttributes.list(locationId) -> ListLocationCustomAttributesResponse +
client.locations.customAttributes.list(locationId) -> SyncPagingIterable<CustomAttribute>
@@ -25232,7 +25232,7 @@ Keys can be any valid string, but must be unique for every request.
## Loyalty Programs Promotions -
client.loyalty.programs.promotions.list(programId) -> ListLoyaltyPromotionsResponse +
client.loyalty.programs.promotions.list(programId) -> SyncPagingIterable<LoyaltyPromotion>
@@ -25615,7 +25615,7 @@ promotion that has an `ACTIVE` or `SCHEDULED` status.
## Merchants CustomAttributeDefinitions -
client.merchants.customAttributeDefinitions.list() -> ListMerchantCustomAttributeDefinitionsResponse +
client.merchants.customAttributeDefinitions.list() -> SyncPagingIterable<CustomAttributeDefinition>
@@ -26213,7 +26213,7 @@ information needed to create or update a custom attribute.
-
client.merchants.customAttributes.list(merchantId) -> ListMerchantCustomAttributesResponse +
client.merchants.customAttributes.list(merchantId) -> SyncPagingIterable<CustomAttribute>
@@ -26612,7 +26612,7 @@ definition owner, you must use the qualified key.
## Orders CustomAttributeDefinitions -
client.orders.customAttributeDefinitions.list() -> ListOrderCustomAttributeDefinitionsResponse +
client.orders.customAttributeDefinitions.list() -> SyncPagingIterable<CustomAttributeDefinition>
@@ -27214,7 +27214,7 @@ client.orders().customAttributes().batchUpsert(
-
client.orders.customAttributes.list(orderId) -> ListOrderCustomAttributesResponse +
client.orders.customAttributes.list(orderId) -> SyncPagingIterable<CustomAttribute>
@@ -28780,7 +28780,7 @@ client.webhooks().eventTypes().list(
## Webhooks Subscriptions -
client.webhooks.subscriptions.list() -> ListWebhookSubscriptionsResponse +
client.webhooks.subscriptions.list() -> SyncPagingIterable<WebhookSubscription>
diff --git a/src/main/java/com/squareup/square/AsyncDisputesClient.java b/src/main/java/com/squareup/square/AsyncDisputesClient.java index 614412a1..1469306c 100644 --- a/src/main/java/com/squareup/square/AsyncDisputesClient.java +++ b/src/main/java/com/squareup/square/AsyncDisputesClient.java @@ -20,8 +20,10 @@ import com.squareup.square.types.ListDisputesRequest; import com.squareup.square.types.SubmitEvidenceDisputesRequest; import com.squareup.square.types.SubmitEvidenceResponse; +import java.io.InputStream; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; +import okhttp3.MediaType; public class AsyncDisputesClient { protected final ClientOptions clientOptions; @@ -118,6 +120,30 @@ public CompletableFuture createEvidenceFile( return this.rawClient.createEvidenceFile(request, requestOptions).thenApply(response -> response.body()); } + public CompletableFuture createEvidenceFile( + InputStream stream, String filename) { + return this.rawClient.createEvidenceFile(stream, filename).thenApply(response -> response.body()); + } + + public CompletableFuture createEvidenceFile( + InputStream stream, String filename, MediaType mediaType) { + return this.rawClient.createEvidenceFile(stream, filename, mediaType).thenApply(response -> response.body()); + } + + public CompletableFuture createEvidenceFile( + InputStream stream, String filename, RequestOptions requestOptions) { + return this.rawClient + .createEvidenceFile(stream, filename, requestOptions) + .thenApply(response -> response.body()); + } + + public CompletableFuture createEvidenceFile( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + return this.rawClient + .createEvidenceFile(stream, filename, mediaType, requestOptions) + .thenApply(response -> response.body()); + } + /** * Uploads text to use as evidence for a dispute challenge. */ diff --git a/src/main/java/com/squareup/square/AsyncInvoicesClient.java b/src/main/java/com/squareup/square/AsyncInvoicesClient.java index 23eaef9c..64583bf4 100644 --- a/src/main/java/com/squareup/square/AsyncInvoicesClient.java +++ b/src/main/java/com/squareup/square/AsyncInvoicesClient.java @@ -26,7 +26,9 @@ import com.squareup.square.types.SearchInvoicesResponse; import com.squareup.square.types.UpdateInvoiceRequest; import com.squareup.square.types.UpdateInvoiceResponse; +import java.io.InputStream; import java.util.concurrent.CompletableFuture; +import okhttp3.MediaType; public class AsyncInvoicesClient { protected final ClientOptions clientOptions; @@ -190,6 +192,32 @@ public CompletableFuture createInvoiceAttachmen return this.rawClient.createInvoiceAttachment(request, requestOptions).thenApply(response -> response.body()); } + public CompletableFuture createInvoiceAttachment( + InputStream stream, String filename) { + return this.rawClient.createInvoiceAttachment(stream, filename).thenApply(response -> response.body()); + } + + public CompletableFuture createInvoiceAttachment( + InputStream stream, String filename, MediaType mediaType) { + return this.rawClient + .createInvoiceAttachment(stream, filename, mediaType) + .thenApply(response -> response.body()); + } + + public CompletableFuture createInvoiceAttachment( + InputStream stream, String filename, RequestOptions requestOptions) { + return this.rawClient + .createInvoiceAttachment(stream, filename, requestOptions) + .thenApply(response -> response.body()); + } + + public CompletableFuture createInvoiceAttachment( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + return this.rawClient + .createInvoiceAttachment(stream, filename, mediaType, requestOptions) + .thenApply(response -> response.body()); + } + /** * Removes an attachment from an invoice and permanently deletes the file. Attachments can be removed only * from invoices in the DRAFT, SCHEDULED, UNPAID, or PARTIALLY_PAID state. diff --git a/src/main/java/com/squareup/square/AsyncRawApplePayClient.java b/src/main/java/com/squareup/square/AsyncRawApplePayClient.java index dd2daec3..c7e7cf02 100644 --- a/src/main/java/com/squareup/square/AsyncRawApplePayClient.java +++ b/src/main/java/com/squareup/square/AsyncRawApplePayClient.java @@ -93,19 +93,16 @@ public CompletableFuture> regis @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RegisterDomainResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RegisterDomainResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawBankAccountsClient.java b/src/main/java/com/squareup/square/AsyncRawBankAccountsClient.java index f8723ab5..94ad3043 100644 --- a/src/main/java/com/squareup/square/AsyncRawBankAccountsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawBankAccountsClient.java @@ -91,9 +91,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListBankAccountsRequest nextRequest = ListBankAccountsRequest.builder() .from(request) @@ -102,24 +103,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getBankAccounts().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -167,19 +166,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetBankAccountByV1IdResponse.class), + responseBodyString, GetBankAccountByV1IdResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -228,19 +225,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetBankAccountResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetBankAccountResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawBookingsClient.java b/src/main/java/com/squareup/square/AsyncRawBookingsClient.java index 39a41c10..1216b65b 100644 --- a/src/main/java/com/squareup/square/AsyncRawBookingsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawBookingsClient.java @@ -128,9 +128,10 @@ public CompletableFuture>> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListBookingsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListBookingsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListBookingsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListBookingsRequest nextRequest = ListBookingsRequest.builder() .from(request) @@ -138,24 +139,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getBookings().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -233,18 +232,16 @@ public CompletableFuture> create @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateBookingResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateBookingResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -303,19 +300,17 @@ public CompletableFuture> s @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchAvailabilityResponse.class), + responseBodyString, SearchAvailabilityResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -374,19 +369,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkRetrieveBookingsResponse.class), + responseBodyString, BulkRetrieveBookingsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -433,19 +426,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetBusinessBookingProfileResponse.class), + responseBodyString, GetBusinessBookingProfileResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -495,19 +486,17 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) { @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveLocationBookingProfileResponse.class), + responseBodyString, RetrieveLocationBookingProfileResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -564,19 +553,17 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) { @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkRetrieveTeamMemberBookingProfilesResponse.class), + responseBodyString, BulkRetrieveTeamMemberBookingProfilesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -627,18 +614,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetBookingResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetBookingResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -701,18 +686,16 @@ public CompletableFuture> update @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateBookingResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateBookingResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -776,18 +759,16 @@ public CompletableFuture> cancel @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelBookingResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelBookingResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawCardsClient.java b/src/main/java/com/squareup/square/AsyncRawCardsClient.java index 26d9f3e3..b4368aad 100644 --- a/src/main/java/com/squareup/square/AsyncRawCardsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawCardsClient.java @@ -106,9 +106,10 @@ public CompletableFuture>> lis @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCardsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListCardsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListCardsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCardsRequest nextRequest = ListCardsRequest.builder() .from(request) @@ -116,7 +117,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getCards().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> { try { return list(nextRequest, requestOptions) .get() @@ -128,12 +129,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -187,18 +185,16 @@ public CompletableFuture> create( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateCardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -245,18 +241,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetCardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -306,18 +300,16 @@ public CompletableFuture> disable( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DisableCardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DisableCardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawCatalogClient.java b/src/main/java/com/squareup/square/AsyncRawCatalogClient.java index abf57df2..d65f7705 100644 --- a/src/main/java/com/squareup/square/AsyncRawCatalogClient.java +++ b/src/main/java/com/squareup/square/AsyncRawCatalogClient.java @@ -118,19 +118,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchDeleteCatalogObjectsResponse.class), + responseBodyString, BatchDeleteCatalogObjectsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -195,19 +193,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetCatalogObjectsResponse.class), + responseBodyString, BatchGetCatalogObjectsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -285,19 +281,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchUpsertCatalogObjectsResponse.class), + responseBodyString, BatchUpsertCatalogObjectsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -344,18 +338,16 @@ public CompletableFuture> info(Req @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CatalogInfoResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CatalogInfoResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -436,9 +428,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCatalogRequest nextRequest = ListCatalogRequest.builder() .from(request) @@ -446,24 +439,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getObjects().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -552,19 +543,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchCatalogObjectsResponse.class), + responseBodyString, SearchCatalogObjectsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -653,19 +642,17 @@ public CompletableFuture> s @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchCatalogItemsResponse.class), + responseBodyString, SearchCatalogItemsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -724,19 +711,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateItemModifierListsResponse.class), + responseBodyString, UpdateItemModifierListsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -795,19 +780,16 @@ public CompletableFuture> upda @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateItemTaxesResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateItemTaxesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawChannelsClient.java b/src/main/java/com/squareup/square/AsyncRawChannelsClient.java index 4f9f25b7..9ca8fc0b 100644 --- a/src/main/java/com/squareup/square/AsyncRawChannelsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawChannelsClient.java @@ -92,9 +92,10 @@ public CompletableFuture>> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListChannelsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListChannelsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListChannelsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListChannelsRequest nextRequest = ListChannelsRequest.builder() .from(request) @@ -102,24 +103,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getChannels().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -168,19 +167,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkRetrieveChannelsResponse.class), + responseBodyString, BulkRetrieveChannelsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -221,19 +218,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveChannelResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveChannelResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawCheckoutClient.java b/src/main/java/com/squareup/square/AsyncRawCheckoutClient.java index 720944b0..ac9c907b 100644 --- a/src/main/java/com/squareup/square/AsyncRawCheckoutClient.java +++ b/src/main/java/com/squareup/square/AsyncRawCheckoutClient.java @@ -72,19 +72,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveLocationSettingsResponse.class), + responseBodyString, RetrieveLocationSettingsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -140,19 +138,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateLocationSettingsResponse.class), + responseBodyString, UpdateLocationSettingsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -199,19 +195,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveMerchantSettingsResponse.class), + responseBodyString, RetrieveMerchantSettingsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -266,19 +260,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateMerchantSettingsResponse.class), + responseBodyString, UpdateMerchantSettingsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawCustomersClient.java b/src/main/java/com/squareup/square/AsyncRawCustomersClient.java index f13859f1..13a0b377 100644 --- a/src/main/java/com/squareup/square/AsyncRawCustomersClient.java +++ b/src/main/java/com/squareup/square/AsyncRawCustomersClient.java @@ -125,9 +125,10 @@ public CompletableFuture>> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCustomersResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListCustomersResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListCustomersResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomersRequest nextRequest = ListCustomersRequest.builder() .from(request) @@ -135,24 +136,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getCustomers().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -240,19 +239,16 @@ public CompletableFuture> creat @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateCustomerResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCustomerResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -325,19 +321,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkCreateCustomersResponse.class), + responseBodyString, BulkCreateCustomersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -394,19 +388,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkDeleteCustomersResponse.class), + responseBodyString, BulkDeleteCustomersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -463,19 +455,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkRetrieveCustomersResponse.class), + responseBodyString, BulkRetrieveCustomersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -532,19 +522,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkUpdateCustomersResponse.class), + responseBodyString, BulkUpdateCustomersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -623,19 +611,16 @@ public CompletableFuture> sear @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchCustomersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchCustomersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -682,18 +667,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetCustomerResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCustomerResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -752,19 +735,16 @@ public CompletableFuture> updat @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateCustomerResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateCustomerResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -816,19 +796,16 @@ public CompletableFuture> delet @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteCustomerResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteCustomerResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawDevicesClient.java b/src/main/java/com/squareup/square/AsyncRawDevicesClient.java index 6d201134..fbf38cb8 100644 --- a/src/main/java/com/squareup/square/AsyncRawDevicesClient.java +++ b/src/main/java/com/squareup/square/AsyncRawDevicesClient.java @@ -95,9 +95,10 @@ public CompletableFuture>> l @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListDevicesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListDevicesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListDevicesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListDevicesRequest nextRequest = ListDevicesRequest.builder() .from(request) @@ -105,24 +106,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getDevices().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -169,18 +168,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetDeviceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetDeviceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawDisputesClient.java b/src/main/java/com/squareup/square/AsyncRawDisputesClient.java index e38fe116..68bdcd68 100644 --- a/src/main/java/com/squareup/square/AsyncRawDisputesClient.java +++ b/src/main/java/com/squareup/square/AsyncRawDisputesClient.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.squareup.square.core.ClientOptions; +import com.squareup.square.core.FileStream; import com.squareup.square.core.MediaTypes; import com.squareup.square.core.ObjectMappers; import com.squareup.square.core.QueryStringMapper; @@ -27,6 +28,7 @@ import com.squareup.square.types.SubmitEvidenceDisputesRequest; import com.squareup.square.types.SubmitEvidenceResponse; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; import java.util.Collections; import java.util.List; @@ -102,9 +104,10 @@ public CompletableFuture>> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListDisputesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListDisputesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListDisputesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListDisputesRequest nextRequest = ListDisputesRequest.builder() .from(request) @@ -112,24 +115,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getDisputes().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -176,18 +177,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetDisputeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetDisputeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -241,18 +240,16 @@ public CompletableFuture> accept @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AcceptDisputeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, AcceptDisputeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -288,10 +285,10 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateDisputeEvidenceFileResponse.class), + responseBodyString, CreateDisputeEvidenceFileResponse.class), response)); return; } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> createEvidenceFile( + InputStream stream, String filename) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/disputes") + .addPathSegment(request.getDisputeId()) + .addPathSegments("evidence-files") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + 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()) { String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateDisputeEvidenceFileResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> createEvidenceFile( + InputStream stream, String filename, MediaType mediaType) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/disputes") + .addPathSegment(request.getDisputeId()) + .addPathSegments("evidence-files") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateDisputeEvidenceFileResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> createEvidenceFile( + InputStream stream, String filename, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/disputes") + .addPathSegment(request.getDisputeId()) + .addPathSegments("evidence-files") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateDisputeEvidenceFileResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> createEvidenceFile( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/disputes") + .addPathSegment(request.getDisputeId()) + .addPathSegments("evidence-files") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateDisputeEvidenceFileResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -395,19 +592,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateDisputeEvidenceTextResponse.class), + responseBodyString, CreateDisputeEvidenceTextResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -466,19 +661,16 @@ public CompletableFuture> submi @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SubmitEvidenceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SubmitEvidenceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawEmployeesClient.java b/src/main/java/com/squareup/square/AsyncRawEmployeesClient.java index 8f1ea66a..43f269f1 100644 --- a/src/main/java/com/squareup/square/AsyncRawEmployeesClient.java +++ b/src/main/java/com/squareup/square/AsyncRawEmployeesClient.java @@ -84,9 +84,10 @@ public CompletableFuture>> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListEmployeesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListEmployeesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListEmployeesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListEmployeesRequest nextRequest = ListEmployeesRequest.builder() .from(request) @@ -94,24 +95,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getEmployees().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -152,18 +151,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetEmployeeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetEmployeeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawEventsClient.java b/src/main/java/com/squareup/square/AsyncRawEventsClient.java index c6b880b8..dd4c3eb6 100644 --- a/src/main/java/com/squareup/square/AsyncRawEventsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawEventsClient.java @@ -84,18 +84,16 @@ public CompletableFuture> searchE @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchEventsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchEventsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -145,18 +143,16 @@ public CompletableFuture> disabl @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DisableEventsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DisableEventsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -202,18 +198,16 @@ public CompletableFuture> enableE @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), EnableEventsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, EnableEventsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -270,19 +264,16 @@ public CompletableFuture> listE @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListEventTypesResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListEventTypesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawGiftCardsClient.java b/src/main/java/com/squareup/square/AsyncRawGiftCardsClient.java index bf91dda6..00ae0015 100644 --- a/src/main/java/com/squareup/square/AsyncRawGiftCardsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawGiftCardsClient.java @@ -113,9 +113,10 @@ public CompletableFuture>> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListGiftCardsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListGiftCardsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListGiftCardsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListGiftCardsRequest nextRequest = ListGiftCardsRequest.builder() .from(request) @@ -123,24 +124,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getGiftCards().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -202,19 +201,16 @@ public CompletableFuture> creat @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateGiftCardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateGiftCardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -269,19 +265,17 @@ public CompletableFuture> g @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetGiftCardFromGanResponse.class), + responseBodyString, GetGiftCardFromGanResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -336,19 +330,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetGiftCardFromNonceResponse.class), + responseBodyString, GetGiftCardFromNonceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -405,19 +397,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), LinkCustomerToGiftCardResponse.class), + responseBodyString, LinkCustomerToGiftCardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -475,19 +465,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UnlinkCustomerFromGiftCardResponse.class), + responseBodyString, UnlinkCustomerFromGiftCardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -534,18 +522,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetGiftCardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetGiftCardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawInventoryClient.java b/src/main/java/com/squareup/square/AsyncRawInventoryClient.java index 44379820..6de8d935 100644 --- a/src/main/java/com/squareup/square/AsyncRawInventoryClient.java +++ b/src/main/java/com/squareup/square/AsyncRawInventoryClient.java @@ -92,19 +92,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetInventoryAdjustmentResponse.class), + responseBodyString, GetInventoryAdjustmentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -154,19 +152,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetInventoryAdjustmentResponse.class), + responseBodyString, GetInventoryAdjustmentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -223,19 +219,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchChangeInventoryResponse.class), + responseBodyString, BatchChangeInventoryResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -302,19 +296,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetInventoryChangesResponse.class), + responseBodyString, BatchGetInventoryChangesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -379,19 +371,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetInventoryCountsResponse.class), + responseBodyString, BatchGetInventoryCountsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -452,19 +442,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchChangeInventoryResponse.class), + responseBodyString, BatchChangeInventoryResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -542,9 +530,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); BatchRetrieveInventoryChangesRequest nextRequest = BatchRetrieveInventoryChangesRequest.builder() @@ -554,24 +543,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getChanges().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return batchGetChanges(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return batchGetChanges(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -658,9 +645,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); BatchGetInventoryCountsRequest nextRequest = BatchGetInventoryCountsRequest.builder() .from(request) @@ -668,24 +656,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getCounts().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return batchGetCounts(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return batchGetCounts(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -736,19 +722,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetInventoryPhysicalCountResponse.class), + responseBodyString, GetInventoryPhysicalCountResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -799,19 +783,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetInventoryPhysicalCountResponse.class), + responseBodyString, GetInventoryPhysicalCountResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -861,19 +843,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetInventoryTransferResponse.class), + responseBodyString, GetInventoryTransferResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -935,9 +915,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); GetInventoryRequest nextRequest = GetInventoryRequest.builder() .from(request) @@ -945,24 +926,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getCounts().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return get(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return get(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -1037,9 +1016,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ChangesInventoryRequest nextRequest = ChangesInventoryRequest.builder() .from(request) @@ -1048,24 +1028,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getChanges().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return changes(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return changes(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawInvoicesClient.java b/src/main/java/com/squareup/square/AsyncRawInvoicesClient.java index 68b3c5ad..00253cc2 100644 --- a/src/main/java/com/squareup/square/AsyncRawInvoicesClient.java +++ b/src/main/java/com/squareup/square/AsyncRawInvoicesClient.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.squareup.square.core.ClientOptions; +import com.squareup.square.core.FileStream; import com.squareup.square.core.MediaTypes; import com.squareup.square.core.ObjectMappers; import com.squareup.square.core.QueryStringMapper; @@ -35,6 +36,7 @@ import com.squareup.square.types.UpdateInvoiceRequest; import com.squareup.square.types.UpdateInvoiceResponse; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; import java.util.Collections; import java.util.List; @@ -104,9 +106,10 @@ public CompletableFuture>> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListInvoicesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListInvoicesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListInvoicesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListInvoicesRequest nextRequest = ListInvoicesRequest.builder() .from(request) @@ -114,24 +117,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getInvoices().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -191,18 +192,16 @@ public CompletableFuture> create @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateInvoiceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateInvoiceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -266,19 +265,16 @@ public CompletableFuture> searc @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchInvoicesResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchInvoicesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -325,18 +321,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetInvoiceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetInvoiceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -397,18 +391,16 @@ public CompletableFuture> update @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateInvoiceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateInvoiceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -462,18 +454,16 @@ public CompletableFuture> delete @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteInvoiceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteInvoiceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -517,10 +507,10 @@ public CompletableFuture( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateInvoiceAttachmentResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> createInvoiceAttachment( + InputStream stream, String filename) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/invoices") + .addPathSegment(request.getInvoiceId()) + .addPathSegments("attachments") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateInvoiceAttachmentResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> createInvoiceAttachment( + InputStream stream, String filename, MediaType mediaType) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/invoices") + .addPathSegment(request.getInvoiceId()) + .addPathSegments("attachments") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateInvoiceAttachmentResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> createInvoiceAttachment( + InputStream stream, String filename, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/invoices") + .addPathSegment(request.getInvoiceId()) + .addPathSegments("attachments") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateInvoiceAttachmentResponse.class), + responseBodyString, CreateInvoiceAttachmentResponse.class), response)); return; } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> createInvoiceAttachment( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/invoices") + .addPathSegment(request.getInvoiceId()) + .addPathSegments("attachments") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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()) { String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateInvoiceAttachmentResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -617,19 +803,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteInvoiceAttachmentResponse.class), + responseBodyString, DeleteInvoiceAttachmentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -689,18 +873,16 @@ public CompletableFuture> cancel @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelInvoiceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelInvoiceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -776,19 +958,16 @@ public CompletableFuture> publi @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), PublishInvoiceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, PublishInvoiceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawLaborClient.java b/src/main/java/com/squareup/square/AsyncRawLaborClient.java index 8e7656bb..83fe68d1 100644 --- a/src/main/java/com/squareup/square/AsyncRawLaborClient.java +++ b/src/main/java/com/squareup/square/AsyncRawLaborClient.java @@ -109,19 +109,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateScheduledShiftResponse.class), + responseBodyString, CreateScheduledShiftResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -185,19 +183,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkPublishScheduledShiftsResponse.class), + responseBodyString, BulkPublishScheduledShiftsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -262,19 +258,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchScheduledShiftsResponse.class), + responseBodyString, SearchScheduledShiftsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -322,19 +316,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveScheduledShiftResponse.class), + responseBodyString, RetrieveScheduledShiftResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -410,19 +402,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateScheduledShiftResponse.class), + responseBodyString, UpdateScheduledShiftResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -481,19 +471,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), PublishScheduledShiftResponse.class), + responseBodyString, PublishScheduledShiftResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -584,19 +572,16 @@ public CompletableFuture> creat @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateTimecardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateTimecardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -706,19 +691,16 @@ public CompletableFuture> sear @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchTimecardsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchTimecardsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -766,19 +748,16 @@ public CompletableFuture> ret @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveTimecardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveTimecardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -842,19 +821,16 @@ public CompletableFuture> updat @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateTimecardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateTimecardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -902,19 +878,16 @@ public CompletableFuture> delet @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteTimecardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteTimecardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawLocationsClient.java b/src/main/java/com/squareup/square/AsyncRawLocationsClient.java index 879beddd..553bd8b3 100644 --- a/src/main/java/com/squareup/square/AsyncRawLocationsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawLocationsClient.java @@ -72,18 +72,16 @@ public CompletableFuture> list(R @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListLocationsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListLocationsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -162,19 +160,16 @@ public CompletableFuture> creat @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateLocationResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateLocationResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -223,18 +218,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetLocationResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetLocationResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -289,19 +282,16 @@ public CompletableFuture> updat @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateLocationResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateLocationResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -366,19 +356,16 @@ public CompletableFuture> check @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateCheckoutResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCheckoutResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawLoyaltyClient.java b/src/main/java/com/squareup/square/AsyncRawLoyaltyClient.java index 2540f6f8..aa31a3d1 100644 --- a/src/main/java/com/squareup/square/AsyncRawLoyaltyClient.java +++ b/src/main/java/com/squareup/square/AsyncRawLoyaltyClient.java @@ -95,19 +95,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchLoyaltyEventsResponse.class), + responseBodyString, SearchLoyaltyEventsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawMerchantsClient.java b/src/main/java/com/squareup/square/AsyncRawMerchantsClient.java index aede5fe7..7683af5d 100644 --- a/src/main/java/com/squareup/square/AsyncRawMerchantsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawMerchantsClient.java @@ -102,9 +102,10 @@ public CompletableFuture>> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListMerchantsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListMerchantsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListMerchantsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListMerchantsRequest nextRequest = ListMerchantsRequest.builder() .from(request) @@ -112,24 +113,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getMerchant().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -176,18 +175,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetMerchantResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetMerchantResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawMobileClient.java b/src/main/java/com/squareup/square/AsyncRawMobileClient.java index aa10140d..c6a5831b 100644 --- a/src/main/java/com/squareup/square/AsyncRawMobileClient.java +++ b/src/main/java/com/squareup/square/AsyncRawMobileClient.java @@ -105,19 +105,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateMobileAuthorizationCodeResponse.class), + responseBodyString, CreateMobileAuthorizationCodeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawOAuthClient.java b/src/main/java/com/squareup/square/AsyncRawOAuthClient.java index 9d545ecd..f05fe7e6 100644 --- a/src/main/java/com/squareup/square/AsyncRawOAuthClient.java +++ b/src/main/java/com/squareup/square/AsyncRawOAuthClient.java @@ -106,18 +106,16 @@ public CompletableFuture> revokeTo @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RevokeTokenResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RevokeTokenResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -211,18 +209,16 @@ public CompletableFuture> obtainTo @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ObtainTokenResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ObtainTokenResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -282,19 +278,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveTokenStatusResponse.class), + responseBodyString, RetrieveTokenStatusResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -337,11 +331,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO return; } String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawOrdersClient.java b/src/main/java/com/squareup/square/AsyncRawOrdersClient.java index e0c695c7..990b8541 100644 --- a/src/main/java/com/squareup/square/AsyncRawOrdersClient.java +++ b/src/main/java/com/squareup/square/AsyncRawOrdersClient.java @@ -105,18 +105,16 @@ public CompletableFuture> create( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -172,19 +170,16 @@ public CompletableFuture> batch @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetOrdersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchGetOrdersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -239,19 +234,16 @@ public CompletableFuture> calcu @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CalculateOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CalculateOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -307,18 +299,16 @@ public CompletableFuture> clone( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CloneOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CloneOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -418,18 +408,16 @@ public CompletableFuture> search( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchOrdersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchOrdersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -476,18 +464,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -568,18 +554,16 @@ public CompletableFuture> update( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -659,18 +643,16 @@ public CompletableFuture> pay( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), PayOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, PayOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawPaymentsClient.java b/src/main/java/com/squareup/square/AsyncRawPaymentsClient.java index 020f5c14..7b191908 100644 --- a/src/main/java/com/squareup/square/AsyncRawPaymentsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawPaymentsClient.java @@ -164,9 +164,10 @@ public CompletableFuture>> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListPaymentsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListPaymentsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListPaymentsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListPaymentsRequest nextRequest = ListPaymentsRequest.builder() .from(request) @@ -174,24 +175,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getPayments().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -257,18 +256,16 @@ public CompletableFuture> create @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreatePaymentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreatePaymentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -340,19 +337,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelPaymentByIdempotencyKeyResponse.class), + responseBodyString, CancelPaymentByIdempotencyKeyResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -399,18 +394,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetPaymentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetPaymentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -467,18 +460,16 @@ public CompletableFuture> update @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdatePaymentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdatePaymentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -528,18 +519,16 @@ public CompletableFuture> cancel @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelPaymentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelPaymentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -600,19 +589,16 @@ public CompletableFuture> comp @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CompletePaymentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CompletePaymentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawPayoutsClient.java b/src/main/java/com/squareup/square/AsyncRawPayoutsClient.java index e0606eba..ff14e1d8 100644 --- a/src/main/java/com/squareup/square/AsyncRawPayoutsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawPayoutsClient.java @@ -113,9 +113,10 @@ public CompletableFuture>> l @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListPayoutsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListPayoutsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListPayoutsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListPayoutsRequest nextRequest = ListPayoutsRequest.builder() .from(request) @@ -123,24 +124,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getPayouts().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -189,18 +188,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetPayoutResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetPayoutResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -262,9 +259,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListEntriesPayoutsRequest nextRequest = ListEntriesPayoutsRequest.builder() .from(request) @@ -273,24 +271,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getPayoutEntries().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return listEntries(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return listEntries(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawRefundsClient.java b/src/main/java/com/squareup/square/AsyncRawRefundsClient.java index 0080920b..af462e63 100644 --- a/src/main/java/com/squareup/square/AsyncRawRefundsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawRefundsClient.java @@ -142,9 +142,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListRefundsRequest nextRequest = ListRefundsRequest.builder() .from(request) @@ -152,24 +153,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getRefunds().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -230,18 +229,16 @@ public CompletableFuture> refund @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RefundPaymentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RefundPaymentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -288,19 +285,16 @@ public CompletableFuture> get @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetPaymentRefundResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetPaymentRefundResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawSitesClient.java b/src/main/java/com/squareup/square/AsyncRawSitesClient.java index 815049af..c0b0f0d3 100644 --- a/src/main/java/com/squareup/square/AsyncRawSitesClient.java +++ b/src/main/java/com/squareup/square/AsyncRawSitesClient.java @@ -61,18 +61,16 @@ public CompletableFuture> list(Reque @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListSitesResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListSitesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawSnippetsClient.java b/src/main/java/com/squareup/square/AsyncRawSnippetsClient.java index f9a112f3..72ecbdcd 100644 --- a/src/main/java/com/squareup/square/AsyncRawSnippetsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawSnippetsClient.java @@ -74,18 +74,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetSnippetResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetSnippetResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -147,18 +145,16 @@ public CompletableFuture> upsert @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpsertSnippetResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpsertSnippetResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -210,18 +206,16 @@ public CompletableFuture> delete @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteSnippetResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteSnippetResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawSubscriptionsClient.java b/src/main/java/com/squareup/square/AsyncRawSubscriptionsClient.java index c2367a94..d43b19c2 100644 --- a/src/main/java/com/squareup/square/AsyncRawSubscriptionsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawSubscriptionsClient.java @@ -112,19 +112,17 @@ public CompletableFuture> c @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateSubscriptionResponse.class), + responseBodyString, CreateSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -180,18 +178,16 @@ public CompletableFuture> bulkSwa @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BulkSwapPlanResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BulkSwapPlanResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -286,19 +282,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchSubscriptionsResponse.class), + responseBodyString, SearchSubscriptionsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -348,19 +342,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetSubscriptionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -418,19 +409,17 @@ public CompletableFuture> u @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateSubscriptionResponse.class), + responseBodyString, UpdateSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -481,19 +470,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteSubscriptionActionResponse.class), + responseBodyString, DeleteSubscriptionActionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -552,19 +539,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ChangeBillingAnchorDateResponse.class), + responseBodyString, ChangeBillingAnchorDateResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -617,19 +602,17 @@ public CompletableFuture> c @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelSubscriptionResponse.class), + responseBodyString, CancelSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -686,9 +669,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListEventsSubscriptionsRequest nextRequest = ListEventsSubscriptionsRequest.builder() .from(request) @@ -697,24 +681,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getSubscriptionEvents().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return listEvents(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return listEvents(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -771,19 +753,17 @@ public CompletableFuture> pa @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), PauseSubscriptionResponse.class), + responseBodyString, PauseSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -840,19 +820,17 @@ public CompletableFuture> r @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ResumeSubscriptionResponse.class), + responseBodyString, ResumeSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -910,18 +888,16 @@ public CompletableFuture> swapPlan( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SwapPlanResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SwapPlanResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawTeamClient.java b/src/main/java/com/squareup/square/AsyncRawTeamClient.java index 31796e04..ac794200 100644 --- a/src/main/java/com/squareup/square/AsyncRawTeamClient.java +++ b/src/main/java/com/squareup/square/AsyncRawTeamClient.java @@ -81,18 +81,16 @@ public CompletableFuture> listJobs( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListJobsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListJobsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -148,18 +146,16 @@ public CompletableFuture> createJob( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateJobResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateJobResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -206,18 +202,16 @@ public CompletableFuture> retrieve @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RetrieveJobResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveJobResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -276,18 +270,16 @@ public CompletableFuture> updateJob( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateJobResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateJobResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawTeamMembersClient.java b/src/main/java/com/squareup/square/AsyncRawTeamMembersClient.java index 0f8ae545..ee010008 100644 --- a/src/main/java/com/squareup/square/AsyncRawTeamMembersClient.java +++ b/src/main/java/com/squareup/square/AsyncRawTeamMembersClient.java @@ -108,19 +108,16 @@ public CompletableFuture> cre @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateTeamMemberResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateTeamMemberResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -183,19 +180,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchCreateTeamMembersResponse.class), + responseBodyString, BatchCreateTeamMembersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -258,19 +253,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchUpdateTeamMembersResponse.class), + responseBodyString, BatchUpdateTeamMembersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -338,19 +331,17 @@ public CompletableFuture> se @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchTeamMembersResponse.class), + responseBodyString, SearchTeamMembersResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -399,18 +390,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetTeamMemberResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetTeamMemberResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -468,19 +457,16 @@ public CompletableFuture> upd @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateTeamMemberResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateTeamMemberResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawTerminalClient.java b/src/main/java/com/squareup/square/AsyncRawTerminalClient.java index db65a6c7..79919945 100644 --- a/src/main/java/com/squareup/square/AsyncRawTerminalClient.java +++ b/src/main/java/com/squareup/square/AsyncRawTerminalClient.java @@ -71,19 +71,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DismissTerminalActionResponse.class), + responseBodyString, DismissTerminalActionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -132,19 +130,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DismissTerminalCheckoutResponse.class), + responseBodyString, DismissTerminalCheckoutResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -193,19 +189,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DismissTerminalRefundResponse.class), + responseBodyString, DismissTerminalRefundResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawTransferOrdersClient.java b/src/main/java/com/squareup/square/AsyncRawTransferOrdersClient.java index 6fca85bd..467fc380 100644 --- a/src/main/java/com/squareup/square/AsyncRawTransferOrdersClient.java +++ b/src/main/java/com/squareup/square/AsyncRawTransferOrdersClient.java @@ -132,19 +132,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateTransferOrderResponse.class), + responseBodyString, CreateTransferOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -228,9 +226,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); SearchTransferOrdersRequest nextRequest = SearchTransferOrdersRequest.builder() .from(request) @@ -239,24 +238,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getTransferOrders().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return search(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return search(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -318,19 +315,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveTransferOrderResponse.class), + responseBodyString, RetrieveTransferOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -390,19 +385,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateTransferOrderResponse.class), + responseBodyString, UpdateTransferOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -459,19 +452,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteTransferOrderResponse.class), + responseBodyString, DeleteTransferOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -546,19 +537,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelTransferOrderResponse.class), + responseBodyString, CancelTransferOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -641,19 +630,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ReceiveTransferOrderResponse.class), + responseBodyString, ReceiveTransferOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -720,19 +707,17 @@ public CompletableFuture> s @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), StartTransferOrderResponse.class), + responseBodyString, StartTransferOrderResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawV1TransactionsClient.java b/src/main/java/com/squareup/square/AsyncRawV1TransactionsClient.java index 87313777..e70ae07e 100644 --- a/src/main/java/com/squareup/square/AsyncRawV1TransactionsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawV1TransactionsClient.java @@ -82,19 +82,17 @@ public CompletableFuture>> v1ListOrders( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), new TypeReference>() {}), + responseBodyString, new TypeReference>() {}), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -143,17 +141,15 @@ public CompletableFuture> v1RetrieveOrder( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), V1Order.class), response)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, V1Order.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -210,17 +206,15 @@ public CompletableFuture> v1UpdateOrder( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), V1Order.class), response)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, V1Order.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/AsyncRawVendorsClient.java b/src/main/java/com/squareup/square/AsyncRawVendorsClient.java index fd6cf515..349d95d6 100644 --- a/src/main/java/com/squareup/square/AsyncRawVendorsClient.java +++ b/src/main/java/com/squareup/square/AsyncRawVendorsClient.java @@ -85,19 +85,17 @@ public CompletableFuture> b @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchCreateVendorsResponse.class), + responseBodyString, BatchCreateVendorsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -159,19 +157,16 @@ public CompletableFuture> batc @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetVendorsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchGetVendorsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -226,19 +221,17 @@ public CompletableFuture> b @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchUpdateVendorsResponse.class), + responseBodyString, BatchUpdateVendorsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -292,18 +285,16 @@ public CompletableFuture> create( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateVendorResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateVendorResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -364,18 +355,16 @@ public CompletableFuture> search @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchVendorsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchVendorsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -422,18 +411,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetVendorResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetVendorResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -488,18 +475,16 @@ public CompletableFuture> update( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateVendorResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateVendorResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/DisputesClient.java b/src/main/java/com/squareup/square/DisputesClient.java index 81cf2821..ad1e3783 100644 --- a/src/main/java/com/squareup/square/DisputesClient.java +++ b/src/main/java/com/squareup/square/DisputesClient.java @@ -20,7 +20,9 @@ import com.squareup.square.types.ListDisputesRequest; import com.squareup.square.types.SubmitEvidenceDisputesRequest; import com.squareup.square.types.SubmitEvidenceResponse; +import java.io.InputStream; import java.util.function.Supplier; +import okhttp3.MediaType; public class DisputesClient { protected final ClientOptions clientOptions; @@ -114,6 +116,29 @@ public CreateDisputeEvidenceFileResponse createEvidenceFile( return this.rawClient.createEvidenceFile(request, requestOptions).body(); } + public CreateDisputeEvidenceFileResponse createEvidenceFile(InputStream stream, String filename) { + return this.rawClient.createEvidenceFile(stream, filename).body(); + } + + public CreateDisputeEvidenceFileResponse createEvidenceFile( + InputStream stream, String filename, MediaType mediaType) { + return this.rawClient.createEvidenceFile(stream, filename, mediaType).body(); + } + + public CreateDisputeEvidenceFileResponse createEvidenceFile( + InputStream stream, String filename, RequestOptions requestOptions) { + return this.rawClient + .createEvidenceFile(stream, filename, requestOptions) + .body(); + } + + public CreateDisputeEvidenceFileResponse createEvidenceFile( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + return this.rawClient + .createEvidenceFile(stream, filename, mediaType, requestOptions) + .body(); + } + /** * Uploads text to use as evidence for a dispute challenge. */ diff --git a/src/main/java/com/squareup/square/InvoicesClient.java b/src/main/java/com/squareup/square/InvoicesClient.java index 9216285e..1c4ce005 100644 --- a/src/main/java/com/squareup/square/InvoicesClient.java +++ b/src/main/java/com/squareup/square/InvoicesClient.java @@ -26,6 +26,8 @@ import com.squareup.square.types.SearchInvoicesResponse; import com.squareup.square.types.UpdateInvoiceRequest; import com.squareup.square.types.UpdateInvoiceResponse; +import java.io.InputStream; +import okhttp3.MediaType; public class InvoicesClient { protected final ClientOptions clientOptions; @@ -183,6 +185,31 @@ public CreateInvoiceAttachmentResponse createInvoiceAttachment( return this.rawClient.createInvoiceAttachment(request, requestOptions).body(); } + public CreateInvoiceAttachmentResponse createInvoiceAttachment(InputStream stream, String filename) { + return this.rawClient.createInvoiceAttachment(stream, filename).body(); + } + + public CreateInvoiceAttachmentResponse createInvoiceAttachment( + InputStream stream, String filename, MediaType mediaType) { + return this.rawClient + .createInvoiceAttachment(stream, filename, mediaType) + .body(); + } + + public CreateInvoiceAttachmentResponse createInvoiceAttachment( + InputStream stream, String filename, RequestOptions requestOptions) { + return this.rawClient + .createInvoiceAttachment(stream, filename, requestOptions) + .body(); + } + + public CreateInvoiceAttachmentResponse createInvoiceAttachment( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + return this.rawClient + .createInvoiceAttachment(stream, filename, mediaType, requestOptions) + .body(); + } + /** * Removes an attachment from an invoice and permanently deletes the file. Attachments can be removed only * from invoices in the DRAFT, SCHEDULED, UNPAID, or PARTIALLY_PAID state. diff --git a/src/main/java/com/squareup/square/RawApplePayClient.java b/src/main/java/com/squareup/square/RawApplePayClient.java index 863ef5d1..79cef207 100644 --- a/src/main/java/com/squareup/square/RawApplePayClient.java +++ b/src/main/java/com/squareup/square/RawApplePayClient.java @@ -85,17 +85,15 @@ public SquareClientHttpResponse registerDomain( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RegisterDomainResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RegisterDomainResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawBankAccountsClient.java b/src/main/java/com/squareup/square/RawBankAccountsClient.java index bf164777..8e79642c 100644 --- a/src/main/java/com/squareup/square/RawBankAccountsClient.java +++ b/src/main/java/com/squareup/square/RawBankAccountsClient.java @@ -82,9 +82,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListBankAccountsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListBankAccountsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListBankAccountsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListBankAccountsRequest nextRequest = ListBankAccountsRequest.builder() .from(request) @@ -93,16 +94,14 @@ public SquareClientHttpResponse> list( List result = parsedResponse.getBankAccounts().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -137,17 +136,15 @@ public SquareClientHttpResponse getByV1Id( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetBankAccountByV1IdResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetBankAccountByV1IdResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -184,17 +181,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetBankAccountResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetBankAccountResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawBookingsClient.java b/src/main/java/com/squareup/square/RawBookingsClient.java index eff90ec7..c02a19d7 100644 --- a/src/main/java/com/squareup/square/RawBookingsClient.java +++ b/src/main/java/com/squareup/square/RawBookingsClient.java @@ -120,9 +120,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListBookingsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListBookingsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListBookingsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListBookingsRequest nextRequest = ListBookingsRequest.builder() .from(request) @@ -130,17 +131,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getBookings().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -206,17 +204,14 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateBookingResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateBookingResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -262,17 +257,15 @@ public SquareClientHttpResponse searchAvailability( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchAvailabilityResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchAvailabilityResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -319,17 +312,15 @@ public SquareClientHttpResponse bulkRetrieveBookin } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BulkRetrieveBookingsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BulkRetrieveBookingsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -363,18 +354,16 @@ public SquareClientHttpResponse getBusinessPr } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetBusinessBookingProfileResponse.class), + responseBodyString, GetBusinessBookingProfileResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -410,18 +399,16 @@ public SquareClientHttpResponse retrieve } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveLocationBookingProfileResponse.class), + responseBodyString, RetrieveLocationBookingProfileResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -465,18 +452,16 @@ public SquareClientHttpResponse retrieve } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkRetrieveTeamMemberBookingProfilesResponse.class), + responseBodyString, BulkRetrieveTeamMemberBookingProfilesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -514,16 +499,14 @@ public SquareClientHttpResponse get(GetBookingsRequest reque } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetBookingResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetBookingResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -574,17 +557,14 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateBookingResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateBookingResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -636,17 +616,14 @@ public SquareClientHttpResponse cancel( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelBookingResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelBookingResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawCardsClient.java b/src/main/java/com/squareup/square/RawCardsClient.java index 90cc30d1..d5c28c01 100644 --- a/src/main/java/com/squareup/square/RawCardsClient.java +++ b/src/main/java/com/squareup/square/RawCardsClient.java @@ -98,9 +98,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCardsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListCardsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListCardsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCardsRequest nextRequest = ListCardsRequest.builder() .from(request) @@ -108,17 +109,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getCards().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -160,16 +158,14 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateCardResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -203,16 +199,14 @@ public SquareClientHttpResponse get(GetCardsRequest request, Re } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetCardResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -250,17 +244,14 @@ public SquareClientHttpResponse disable( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DisableCardResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DisableCardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawCatalogClient.java b/src/main/java/com/squareup/square/RawCatalogClient.java index ba0faa9e..9fdd259f 100644 --- a/src/main/java/com/squareup/square/RawCatalogClient.java +++ b/src/main/java/com/squareup/square/RawCatalogClient.java @@ -109,18 +109,16 @@ public SquareClientHttpResponse batchDelete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchDeleteCatalogObjectsResponse.class), + responseBodyString, BatchDeleteCatalogObjectsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -172,18 +170,15 @@ public SquareClientHttpResponse batchGet( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetCatalogObjectsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchGetCatalogObjectsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -248,18 +243,16 @@ public SquareClientHttpResponse batchUpsert( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchUpsertCatalogObjectsResponse.class), + responseBodyString, BatchUpsertCatalogObjectsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -294,17 +287,14 @@ public SquareClientHttpResponse info(RequestOptions request } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CatalogInfoResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CatalogInfoResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -371,9 +361,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCatalogResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListCatalogResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListCatalogResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCatalogRequest nextRequest = ListCatalogRequest.builder() .from(request) @@ -382,16 +373,14 @@ public SquareClientHttpResponse> list( List result = parsedResponse.getObjects().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -467,17 +456,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchCatalogObjectsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchCatalogObjectsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -553,17 +540,15 @@ public SquareClientHttpResponse searchItems( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchCatalogItemsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchCatalogItemsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -610,18 +595,15 @@ public SquareClientHttpResponse updateItemModif } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateItemModifierListsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateItemModifierListsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -667,17 +649,15 @@ public SquareClientHttpResponse updateItemTaxes( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateItemTaxesResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateItemTaxesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawChannelsClient.java b/src/main/java/com/squareup/square/RawChannelsClient.java index 3dcc2b29..f30fe1a7 100644 --- a/src/main/java/com/squareup/square/RawChannelsClient.java +++ b/src/main/java/com/squareup/square/RawChannelsClient.java @@ -84,9 +84,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListChannelsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListChannelsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListChannelsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListChannelsRequest nextRequest = ListChannelsRequest.builder() .from(request) @@ -94,17 +95,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getChannels().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -140,17 +138,15 @@ public SquareClientHttpResponse bulkRetrieve( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BulkRetrieveChannelsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BulkRetrieveChannelsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -179,17 +175,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RetrieveChannelResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveChannelResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawCheckoutClient.java b/src/main/java/com/squareup/square/RawCheckoutClient.java index 72f4dab6..5c7b0468 100644 --- a/src/main/java/com/squareup/square/RawCheckoutClient.java +++ b/src/main/java/com/squareup/square/RawCheckoutClient.java @@ -64,18 +64,15 @@ public SquareClientHttpResponse retrieveLocati } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveLocationSettingsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveLocationSettingsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -119,18 +116,15 @@ public SquareClientHttpResponse updateLocationSe } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateLocationSettingsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateLocationSettingsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -164,18 +158,15 @@ public SquareClientHttpResponse retrieveMercha } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveMerchantSettingsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveMerchantSettingsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -218,18 +209,15 @@ public SquareClientHttpResponse updateMerchantSe } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateMerchantSettingsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateMerchantSettingsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawCustomersClient.java b/src/main/java/com/squareup/square/RawCustomersClient.java index 7ea67c8a..39ab3d55 100644 --- a/src/main/java/com/squareup/square/RawCustomersClient.java +++ b/src/main/java/com/squareup/square/RawCustomersClient.java @@ -116,9 +116,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCustomersResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListCustomersResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListCustomersResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomersRequest nextRequest = ListCustomersRequest.builder() .from(request) @@ -126,17 +127,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getCustomers().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -212,17 +210,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateCustomerResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCustomerResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -282,17 +278,15 @@ public SquareClientHttpResponse batchCreate( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BulkCreateCustomersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BulkCreateCustomersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -337,17 +331,15 @@ public SquareClientHttpResponse bulkDeleteCustomers } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BulkDeleteCustomersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BulkDeleteCustomersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -392,17 +384,15 @@ public SquareClientHttpResponse bulkRetrieveCusto } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BulkRetrieveCustomersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BulkRetrieveCustomersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -447,17 +437,15 @@ public SquareClientHttpResponse bulkUpdateCustomers } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BulkUpdateCustomersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BulkUpdateCustomersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -524,17 +512,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchCustomersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchCustomersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -569,17 +555,14 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetCustomerResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCustomerResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -626,17 +609,15 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateCustomerResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateCustomerResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -676,17 +657,15 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteCustomerResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteCustomerResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawDevicesClient.java b/src/main/java/com/squareup/square/RawDevicesClient.java index 7b0abb7a..cf23c6c8 100644 --- a/src/main/java/com/squareup/square/RawDevicesClient.java +++ b/src/main/java/com/squareup/square/RawDevicesClient.java @@ -87,9 +87,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListDevicesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListDevicesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListDevicesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListDevicesRequest nextRequest = ListDevicesRequest.builder() .from(request) @@ -97,17 +98,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getDevices().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -141,16 +139,14 @@ public SquareClientHttpResponse get(GetDevicesRequest request } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetDeviceResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetDeviceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawDisputesClient.java b/src/main/java/com/squareup/square/RawDisputesClient.java index 049d2f5b..6d3a94d6 100644 --- a/src/main/java/com/squareup/square/RawDisputesClient.java +++ b/src/main/java/com/squareup/square/RawDisputesClient.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.squareup.square.core.ClientOptions; +import com.squareup.square.core.FileStream; import com.squareup.square.core.MediaTypes; import com.squareup.square.core.ObjectMappers; import com.squareup.square.core.QueryStringMapper; @@ -27,6 +28,7 @@ import com.squareup.square.types.SubmitEvidenceDisputesRequest; import com.squareup.square.types.SubmitEvidenceResponse; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; import java.util.Collections; import java.util.List; @@ -94,9 +96,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListDisputesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListDisputesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListDisputesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListDisputesRequest nextRequest = ListDisputesRequest.builder() .from(request) @@ -104,17 +107,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getDisputes().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -148,16 +148,14 @@ public SquareClientHttpResponse get(GetDisputesRequest reque } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetDisputeResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetDisputeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -199,17 +197,14 @@ public SquareClientHttpResponse accept( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AcceptDisputeResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, AcceptDisputeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -236,10 +231,10 @@ public SquareClientHttpResponse createEvidenc .addPathSegment(request.getDisputeId()) .addPathSegments("evidence-files") .build(); - MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); try { if (request.getRequest().isPresent()) { - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "request", ObjectMappers.JSON_MAPPER.writeValueAsString( request.getRequest().get())); @@ -249,7 +244,7 @@ public SquareClientHttpResponse createEvidenc Files.probeContentType(request.getImageFile().get().toPath()); MediaType imageFileMimeTypeMediaType = imageFileMimeType != null ? MediaType.parse(imageFileMimeType) : null; - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "image_file", request.getImageFile().get().getName(), RequestBody.create(request.getImageFile().get(), imageFileMimeTypeMediaType)); @@ -259,7 +254,7 @@ public SquareClientHttpResponse createEvidenc } Request.Builder _requestBuilder = new Request.Builder() .url(httpUrl) - .method("POST", body.build()) + .method("POST", multipartBodyBuilder.build()) .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Accept", "application/json"); Request okhttpRequest = _requestBuilder.build(); @@ -269,18 +264,162 @@ public SquareClientHttpResponse createEvidenc } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateDisputeEvidenceFileResponse.class), + responseBodyString, CreateDisputeEvidenceFileResponse.class), response); } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse createEvidenceFile( + InputStream stream, String filename) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/disputes") + .addPathSegment(request.getDisputeId()) + .addPathSegments("evidence-files") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateDisputeEvidenceFileResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse createEvidenceFile( + InputStream stream, String filename, MediaType mediaType) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/disputes") + .addPathSegment(request.getDisputeId()) + .addPathSegments("evidence-files") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateDisputeEvidenceFileResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse createEvidenceFile( + InputStream stream, String filename, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/disputes") + .addPathSegment(request.getDisputeId()) + .addPathSegments("evidence-files") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateDisputeEvidenceFileResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse createEvidenceFile( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/disputes") + .addPathSegment(request.getDisputeId()) + .addPathSegments("evidence-files") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateDisputeEvidenceFileResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -325,18 +464,16 @@ public SquareClientHttpResponse createEvidenc } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateDisputeEvidenceTextResponse.class), + responseBodyString, CreateDisputeEvidenceTextResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -382,17 +519,15 @@ public SquareClientHttpResponse submitEvidence( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SubmitEvidenceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SubmitEvidenceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawEmployeesClient.java b/src/main/java/com/squareup/square/RawEmployeesClient.java index 27eeab18..ad7e5e60 100644 --- a/src/main/java/com/squareup/square/RawEmployeesClient.java +++ b/src/main/java/com/squareup/square/RawEmployeesClient.java @@ -75,9 +75,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListEmployeesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListEmployeesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListEmployeesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListEmployeesRequest nextRequest = ListEmployeesRequest.builder() .from(request) @@ -85,17 +86,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getEmployees().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -124,17 +122,14 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetEmployeeResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetEmployeeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawEventsClient.java b/src/main/java/com/squareup/square/RawEventsClient.java index a5be6e11..ffa362a3 100644 --- a/src/main/java/com/squareup/square/RawEventsClient.java +++ b/src/main/java/com/squareup/square/RawEventsClient.java @@ -77,17 +77,14 @@ public SquareClientHttpResponse searchEvents( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchEventsResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchEventsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -124,17 +121,14 @@ public SquareClientHttpResponse disableEvents(RequestOpti } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DisableEventsResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DisableEventsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -167,17 +161,14 @@ public SquareClientHttpResponse enableEvents(RequestOption } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), EnableEventsResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, EnableEventsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -221,17 +212,15 @@ public SquareClientHttpResponse listEventTypes( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListEventTypesResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListEventTypesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawGiftCardsClient.java b/src/main/java/com/squareup/square/RawGiftCardsClient.java index 9dfb29ac..e95c4a7f 100644 --- a/src/main/java/com/squareup/square/RawGiftCardsClient.java +++ b/src/main/java/com/squareup/square/RawGiftCardsClient.java @@ -104,9 +104,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListGiftCardsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListGiftCardsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListGiftCardsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListGiftCardsRequest nextRequest = ListGiftCardsRequest.builder() .from(request) @@ -114,17 +115,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getGiftCards().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -174,17 +172,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateGiftCardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateGiftCardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -226,17 +222,15 @@ public SquareClientHttpResponse getFromGan( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetGiftCardFromGanResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetGiftCardFromGanResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -278,17 +272,15 @@ public SquareClientHttpResponse getFromNonce( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetGiftCardFromNonceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetGiftCardFromNonceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -333,18 +325,15 @@ public SquareClientHttpResponse linkCustomer( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), LinkCustomerToGiftCardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, LinkCustomerToGiftCardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -389,18 +378,16 @@ public SquareClientHttpResponse unlinkCustom } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UnlinkCustomerFromGiftCardResponse.class), + responseBodyString, UnlinkCustomerFromGiftCardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -435,17 +422,14 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetGiftCardResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetGiftCardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawInventoryClient.java b/src/main/java/com/squareup/square/RawInventoryClient.java index 0e63c408..d2983e99 100644 --- a/src/main/java/com/squareup/square/RawInventoryClient.java +++ b/src/main/java/com/squareup/square/RawInventoryClient.java @@ -84,18 +84,15 @@ public SquareClientHttpResponse deprecatedGetAdj } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetInventoryAdjustmentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetInventoryAdjustmentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -133,18 +130,15 @@ public SquareClientHttpResponse getAdjustment( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetInventoryAdjustmentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetInventoryAdjustmentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -189,17 +183,15 @@ public SquareClientHttpResponse deprecatedBatchCha } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BatchChangeInventoryResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchChangeInventoryResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -253,18 +245,15 @@ public SquareClientHttpResponse deprecatedBatc } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetInventoryChangesResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchGetInventoryChangesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -317,18 +306,15 @@ public SquareClientHttpResponse deprecatedBatch } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetInventoryCountsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchGetInventoryCountsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -377,17 +363,15 @@ public SquareClientHttpResponse batchCreateChanges } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BatchChangeInventoryResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchChangeInventoryResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -452,9 +436,10 @@ public SquareClientHttpResponse> batchGetCha } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { - BatchGetInventoryChangesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetInventoryChangesResponse.class); + BatchGetInventoryChangesResponse parsedResponse = + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchGetInventoryChangesResponse.class); Optional startingAfter = parsedResponse.getCursor(); BatchRetrieveInventoryChangesRequest nextRequest = BatchRetrieveInventoryChangesRequest.builder() .from(request) @@ -463,16 +448,14 @@ public SquareClientHttpResponse> batchGetCha List result = parsedResponse.getChanges().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> batchGetChanges(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> batchGetChanges( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -546,9 +529,10 @@ public SquareClientHttpResponse> batchGetCoun } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { - BatchGetInventoryCountsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchGetInventoryCountsResponse.class); + BatchGetInventoryCountsResponse parsedResponse = + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchGetInventoryCountsResponse.class); Optional startingAfter = parsedResponse.getCursor(); BatchGetInventoryCountsRequest nextRequest = BatchGetInventoryCountsRequest.builder() .from(request) @@ -557,16 +541,14 @@ public SquareClientHttpResponse> batchGetCoun List result = parsedResponse.getCounts().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> batchGetCounts(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> batchGetCounts( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -604,18 +586,16 @@ public SquareClientHttpResponse deprecatedGet } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetInventoryPhysicalCountResponse.class), + responseBodyString, GetInventoryPhysicalCountResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -653,18 +633,16 @@ public SquareClientHttpResponse getPhysicalCo } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetInventoryPhysicalCountResponse.class), + responseBodyString, GetInventoryPhysicalCountResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -701,17 +679,15 @@ public SquareClientHttpResponse getTransfer( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetInventoryTransferResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetInventoryTransferResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -759,9 +735,10 @@ public SquareClientHttpResponse> get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { GetInventoryCountResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetInventoryCountResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetInventoryCountResponse.class); Optional startingAfter = parsedResponse.getCursor(); GetInventoryRequest nextRequest = GetInventoryRequest.builder() .from(request) @@ -770,16 +747,14 @@ public SquareClientHttpResponse> get( List result = parsedResponse.getCounts().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> get(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> get( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -840,9 +815,10 @@ public SquareClientHttpResponse> changes( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { GetInventoryChangesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetInventoryChangesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetInventoryChangesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ChangesInventoryRequest nextRequest = ChangesInventoryRequest.builder() .from(request) @@ -851,16 +827,14 @@ public SquareClientHttpResponse> changes( List result = parsedResponse.getChanges().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> changes(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> changes( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawInvoicesClient.java b/src/main/java/com/squareup/square/RawInvoicesClient.java index a5a7509d..b732a5aa 100644 --- a/src/main/java/com/squareup/square/RawInvoicesClient.java +++ b/src/main/java/com/squareup/square/RawInvoicesClient.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.squareup.square.core.ClientOptions; +import com.squareup.square.core.FileStream; import com.squareup.square.core.MediaTypes; import com.squareup.square.core.ObjectMappers; import com.squareup.square.core.QueryStringMapper; @@ -35,6 +36,7 @@ import com.squareup.square.types.UpdateInvoiceRequest; import com.squareup.square.types.UpdateInvoiceResponse; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; import java.util.Collections; import java.util.List; @@ -96,9 +98,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListInvoicesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListInvoicesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListInvoicesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListInvoicesRequest nextRequest = ListInvoicesRequest.builder() .from(request) @@ -106,17 +109,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getInvoices().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -164,17 +164,14 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateInvoiceResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateInvoiceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -226,17 +223,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchInvoicesResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchInvoicesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -270,16 +265,14 @@ public SquareClientHttpResponse get(GetInvoicesRequest reque } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetInvoiceResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetInvoiceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -328,17 +321,14 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateInvoiceResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateInvoiceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -380,17 +370,14 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteInvoiceResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteInvoiceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -425,10 +412,10 @@ public SquareClientHttpResponse createInvoiceAt .addPathSegment(request.getInvoiceId()) .addPathSegments("attachments") .build(); - MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); try { if (request.getRequest().isPresent()) { - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "request", ObjectMappers.JSON_MAPPER.writeValueAsString( request.getRequest().get())); @@ -438,7 +425,7 @@ public SquareClientHttpResponse createInvoiceAt Files.probeContentType(request.getImageFile().get().toPath()); MediaType imageFileMimeTypeMediaType = imageFileMimeType != null ? MediaType.parse(imageFileMimeType) : null; - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "image_file", request.getImageFile().get().getName(), RequestBody.create(request.getImageFile().get(), imageFileMimeTypeMediaType)); @@ -448,7 +435,7 @@ public SquareClientHttpResponse createInvoiceAt } Request.Builder _requestBuilder = new Request.Builder() .url(httpUrl) - .method("POST", body.build()) + .method("POST", multipartBodyBuilder.build()) .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Accept", "application/json"); Request okhttpRequest = _requestBuilder.build(); @@ -458,18 +445,157 @@ public SquareClientHttpResponse createInvoiceAt } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateInvoiceAttachmentResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse createInvoiceAttachment( + InputStream stream, String filename) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/invoices") + .addPathSegment(request.getInvoiceId()) + .addPathSegments("attachments") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateInvoiceAttachmentResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse createInvoiceAttachment( + InputStream stream, String filename, MediaType mediaType) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/invoices") + .addPathSegment(request.getInvoiceId()) + .addPathSegments("attachments") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateInvoiceAttachmentResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse createInvoiceAttachment( + InputStream stream, String filename, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/invoices") + .addPathSegment(request.getInvoiceId()) + .addPathSegments("attachments") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateInvoiceAttachmentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateInvoiceAttachmentResponse.class), response); } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse createInvoiceAttachment( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/invoices") + .addPathSegment(request.getInvoiceId()) + .addPathSegments("attachments") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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(); String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateInvoiceAttachmentResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -509,18 +635,15 @@ public SquareClientHttpResponse deleteInvoiceAt } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteInvoiceAttachmentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteInvoiceAttachmentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -568,17 +691,14 @@ public SquareClientHttpResponse cancel( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelInvoiceResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelInvoiceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -642,17 +762,15 @@ public SquareClientHttpResponse publish( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), PublishInvoiceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, PublishInvoiceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawLaborClient.java b/src/main/java/com/squareup/square/RawLaborClient.java index b1b117b2..e6d18d31 100644 --- a/src/main/java/com/squareup/square/RawLaborClient.java +++ b/src/main/java/com/squareup/square/RawLaborClient.java @@ -102,17 +102,15 @@ public SquareClientHttpResponse createScheduledShi } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateScheduledShiftResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateScheduledShiftResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -163,18 +161,16 @@ public SquareClientHttpResponse bulkPublishS } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkPublishScheduledShiftsResponse.class), + responseBodyString, BulkPublishScheduledShiftsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -227,17 +223,15 @@ public SquareClientHttpResponse searchScheduledSh } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchScheduledShiftsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchScheduledShiftsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -273,18 +267,15 @@ public SquareClientHttpResponse retrieveSchedule } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveScheduledShiftResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveScheduledShiftResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -348,17 +339,15 @@ public SquareClientHttpResponse updateScheduledShi } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateScheduledShiftResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateScheduledShiftResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -405,17 +394,15 @@ public SquareClientHttpResponse publishScheduledS } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), PublishScheduledShiftResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, PublishScheduledShiftResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -493,17 +480,15 @@ public SquareClientHttpResponse createTimecard( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateTimecardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateTimecardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -600,17 +585,15 @@ public SquareClientHttpResponse searchTimecards( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchTimecardsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchTimecardsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -645,17 +628,15 @@ public SquareClientHttpResponse retrieveTimecard( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RetrieveTimecardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveTimecardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -706,17 +687,15 @@ public SquareClientHttpResponse updateTimecard( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateTimecardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateTimecardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -751,17 +730,15 @@ public SquareClientHttpResponse deleteTimecard( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteTimecardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteTimecardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawLocationsClient.java b/src/main/java/com/squareup/square/RawLocationsClient.java index a3221722..8d3f20ed 100644 --- a/src/main/java/com/squareup/square/RawLocationsClient.java +++ b/src/main/java/com/squareup/square/RawLocationsClient.java @@ -65,17 +65,14 @@ public SquareClientHttpResponse list(RequestOptions reque } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListLocationsResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListLocationsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -142,17 +139,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateLocationResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateLocationResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -189,17 +184,14 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetLocationResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetLocationResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -242,17 +234,15 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateLocationResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateLocationResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -304,17 +294,15 @@ public SquareClientHttpResponse checkouts( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateCheckoutResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCheckoutResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawLoyaltyClient.java b/src/main/java/com/squareup/square/RawLoyaltyClient.java index f73e37c5..3d467f15 100644 --- a/src/main/java/com/squareup/square/RawLoyaltyClient.java +++ b/src/main/java/com/squareup/square/RawLoyaltyClient.java @@ -87,17 +87,15 @@ public SquareClientHttpResponse searchEvents( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchLoyaltyEventsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchLoyaltyEventsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawMerchantsClient.java b/src/main/java/com/squareup/square/RawMerchantsClient.java index c6bfdc94..50024a97 100644 --- a/src/main/java/com/squareup/square/RawMerchantsClient.java +++ b/src/main/java/com/squareup/square/RawMerchantsClient.java @@ -93,9 +93,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListMerchantsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListMerchantsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListMerchantsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListMerchantsRequest nextRequest = ListMerchantsRequest.builder() .from(request) @@ -103,17 +104,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getMerchant().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -148,17 +146,14 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetMerchantResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetMerchantResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawMobileClient.java b/src/main/java/com/squareup/square/RawMobileClient.java index 9f49c7e8..f4692fbc 100644 --- a/src/main/java/com/squareup/square/RawMobileClient.java +++ b/src/main/java/com/squareup/square/RawMobileClient.java @@ -97,18 +97,16 @@ public SquareClientHttpResponse authoriza } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateMobileAuthorizationCodeResponse.class), + responseBodyString, CreateMobileAuthorizationCodeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawOAuthClient.java b/src/main/java/com/squareup/square/RawOAuthClient.java index abefb5c8..c6db14a8 100644 --- a/src/main/java/com/squareup/square/RawOAuthClient.java +++ b/src/main/java/com/squareup/square/RawOAuthClient.java @@ -99,17 +99,14 @@ public SquareClientHttpResponse revokeToken( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RevokeTokenResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RevokeTokenResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -191,17 +188,14 @@ public SquareClientHttpResponse obtainToken( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ObtainTokenResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ObtainTokenResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -248,17 +242,15 @@ public SquareClientHttpResponse retrieveTokenStatus } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RetrieveTokenStatusResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveTokenStatusResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -288,11 +280,9 @@ public SquareClientHttpResponse authorize(RequestOptions requestOptions) { return new SquareClientHttpResponse<>(null, response); } String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawOrdersClient.java b/src/main/java/com/squareup/square/RawOrdersClient.java index 4eaa353f..4669f70c 100644 --- a/src/main/java/com/squareup/square/RawOrdersClient.java +++ b/src/main/java/com/squareup/square/RawOrdersClient.java @@ -98,17 +98,14 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateOrderResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -152,17 +149,15 @@ public SquareClientHttpResponse batchGet( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BatchGetOrdersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchGetOrdersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -204,17 +199,15 @@ public SquareClientHttpResponse calculate( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CalculateOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CalculateOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -258,16 +251,14 @@ public SquareClientHttpResponse clone( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CloneOrderResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CloneOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -355,17 +346,14 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchOrdersResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchOrdersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -399,16 +387,14 @@ public SquareClientHttpResponse get(GetOrdersRequest request, } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetOrderResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -477,17 +463,14 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateOrderResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -554,16 +537,14 @@ public SquareClientHttpResponse pay(PayOrderRequest request, R } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), PayOrderResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, PayOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawPaymentsClient.java b/src/main/java/com/squareup/square/RawPaymentsClient.java index cc44b906..8a8ef0b0 100644 --- a/src/main/java/com/squareup/square/RawPaymentsClient.java +++ b/src/main/java/com/squareup/square/RawPaymentsClient.java @@ -156,9 +156,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListPaymentsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListPaymentsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListPaymentsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListPaymentsRequest nextRequest = ListPaymentsRequest.builder() .from(request) @@ -166,17 +167,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getPayments().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -230,17 +228,14 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreatePaymentResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreatePaymentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -299,18 +294,16 @@ public SquareClientHttpResponse cancelByI } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelPaymentByIdempotencyKeyResponse.class), + responseBodyString, CancelPaymentByIdempotencyKeyResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -344,16 +337,14 @@ public SquareClientHttpResponse get(GetPaymentsRequest reque } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetPaymentResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetPaymentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -398,17 +389,14 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdatePaymentResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdatePaymentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -446,17 +434,14 @@ public SquareClientHttpResponse cancel( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelPaymentResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelPaymentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -504,17 +489,15 @@ public SquareClientHttpResponse complete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CompletePaymentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CompletePaymentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawPayoutsClient.java b/src/main/java/com/squareup/square/RawPayoutsClient.java index 8408d8ee..19ac9900 100644 --- a/src/main/java/com/squareup/square/RawPayoutsClient.java +++ b/src/main/java/com/squareup/square/RawPayoutsClient.java @@ -105,9 +105,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListPayoutsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListPayoutsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListPayoutsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListPayoutsRequest nextRequest = ListPayoutsRequest.builder() .from(request) @@ -115,17 +116,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getPayouts().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -161,16 +159,14 @@ public SquareClientHttpResponse get(GetPayoutsRequest request } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetPayoutResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetPayoutResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -219,9 +215,10 @@ public SquareClientHttpResponse> listEntries( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListPayoutEntriesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListPayoutEntriesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListPayoutEntriesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListEntriesPayoutsRequest nextRequest = ListEntriesPayoutsRequest.builder() .from(request) @@ -230,16 +227,14 @@ public SquareClientHttpResponse> listEntries( List result = parsedResponse.getPayoutEntries().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> listEntries(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> listEntries( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawRefundsClient.java b/src/main/java/com/squareup/square/RawRefundsClient.java index 288de687..822d340d 100644 --- a/src/main/java/com/squareup/square/RawRefundsClient.java +++ b/src/main/java/com/squareup/square/RawRefundsClient.java @@ -132,9 +132,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListPaymentRefundsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListPaymentRefundsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListPaymentRefundsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListRefundsRequest nextRequest = ListRefundsRequest.builder() .from(request) @@ -143,16 +144,14 @@ public SquareClientHttpResponse> list( List result = parsedResponse.getRefunds().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -200,17 +199,14 @@ public SquareClientHttpResponse refundPayment( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RefundPaymentResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RefundPaymentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -245,17 +241,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetPaymentRefundResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetPaymentRefundResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawSitesClient.java b/src/main/java/com/squareup/square/RawSitesClient.java index e649d7da..fb361c27 100644 --- a/src/main/java/com/squareup/square/RawSitesClient.java +++ b/src/main/java/com/squareup/square/RawSitesClient.java @@ -54,16 +54,14 @@ public SquareClientHttpResponse list(RequestOptions requestOp } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListSitesResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListSitesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawSnippetsClient.java b/src/main/java/com/squareup/square/RawSnippetsClient.java index e4beb7cd..f4d3f8b4 100644 --- a/src/main/java/com/squareup/square/RawSnippetsClient.java +++ b/src/main/java/com/squareup/square/RawSnippetsClient.java @@ -66,16 +66,14 @@ public SquareClientHttpResponse get(GetSnippetsRequest reque } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetSnippetResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetSnippetResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -125,17 +123,14 @@ public SquareClientHttpResponse upsert( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpsertSnippetResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpsertSnippetResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -175,17 +170,14 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteSnippetResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteSnippetResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawSubscriptionsClient.java b/src/main/java/com/squareup/square/RawSubscriptionsClient.java index 3c7da78d..23846067 100644 --- a/src/main/java/com/squareup/square/RawSubscriptionsClient.java +++ b/src/main/java/com/squareup/square/RawSubscriptionsClient.java @@ -103,17 +103,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateSubscriptionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -157,17 +155,14 @@ public SquareClientHttpResponse bulkSwapPlan( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BulkSwapPlanResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BulkSwapPlanResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -249,17 +244,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchSubscriptionsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchSubscriptionsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -297,17 +290,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetSubscriptionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -352,17 +343,15 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateSubscriptionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -400,18 +389,15 @@ public SquareClientHttpResponse deleteAction( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteSubscriptionActionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteSubscriptionActionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -458,18 +444,15 @@ public SquareClientHttpResponse changeBillingAn } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ChangeBillingAnchorDateResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ChangeBillingAnchorDateResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -509,17 +492,15 @@ public SquareClientHttpResponse cancel( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelSubscriptionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -563,9 +544,10 @@ public SquareClientHttpResponse> listEvent } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { - ListSubscriptionEventsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListSubscriptionEventsResponse.class); + ListSubscriptionEventsResponse parsedResponse = + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListSubscriptionEventsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListEventsSubscriptionsRequest nextRequest = ListEventsSubscriptionsRequest.builder() .from(request) @@ -575,16 +557,14 @@ public SquareClientHttpResponse> listEvent parsedResponse.getSubscriptionEvents().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> listEvents(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> listEvents( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -628,17 +608,15 @@ public SquareClientHttpResponse pause( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), PauseSubscriptionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, PauseSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -682,17 +660,15 @@ public SquareClientHttpResponse resume( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ResumeSubscriptionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ResumeSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -737,16 +713,14 @@ public SquareClientHttpResponse swapPlan(SwapPlanRequest reque } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SwapPlanResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SwapPlanResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawTeamClient.java b/src/main/java/com/squareup/square/RawTeamClient.java index cae7bb95..c8319aa4 100644 --- a/src/main/java/com/squareup/square/RawTeamClient.java +++ b/src/main/java/com/squareup/square/RawTeamClient.java @@ -73,16 +73,14 @@ public SquareClientHttpResponse listJobs(ListJobsRequest reque } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListJobsResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListJobsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -126,16 +124,14 @@ public SquareClientHttpResponse createJob( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateJobResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateJobResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -170,17 +166,14 @@ public SquareClientHttpResponse retrieveJob( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RetrieveJobResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveJobResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -227,16 +220,14 @@ public SquareClientHttpResponse updateJob( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateJobResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateJobResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawTeamMembersClient.java b/src/main/java/com/squareup/square/RawTeamMembersClient.java index 5b79aaff..fda92c9d 100644 --- a/src/main/java/com/squareup/square/RawTeamMembersClient.java +++ b/src/main/java/com/squareup/square/RawTeamMembersClient.java @@ -100,17 +100,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateTeamMemberResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateTeamMemberResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -160,18 +158,15 @@ public SquareClientHttpResponse batchCreate( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchCreateTeamMembersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchCreateTeamMembersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -221,18 +216,15 @@ public SquareClientHttpResponse batchUpdate( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchUpdateTeamMembersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchUpdateTeamMembersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -287,17 +279,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchTeamMembersResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchTeamMembersResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -334,17 +324,14 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetTeamMemberResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetTeamMemberResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -389,17 +376,15 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateTeamMemberResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateTeamMemberResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawTerminalClient.java b/src/main/java/com/squareup/square/RawTerminalClient.java index 211663b9..5c641f12 100644 --- a/src/main/java/com/squareup/square/RawTerminalClient.java +++ b/src/main/java/com/squareup/square/RawTerminalClient.java @@ -64,17 +64,15 @@ public SquareClientHttpResponse dismissTerminalAc } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DismissTerminalActionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DismissTerminalActionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -111,18 +109,15 @@ public SquareClientHttpResponse dismissTerminal } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DismissTerminalCheckoutResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DismissTerminalCheckoutResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -159,17 +154,15 @@ public SquareClientHttpResponse dismissTerminalRe } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DismissTerminalRefundResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DismissTerminalRefundResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawTransferOrdersClient.java b/src/main/java/com/squareup/square/RawTransferOrdersClient.java index 466b9d48..ebb22fd7 100644 --- a/src/main/java/com/squareup/square/RawTransferOrdersClient.java +++ b/src/main/java/com/squareup/square/RawTransferOrdersClient.java @@ -123,17 +123,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateTransferOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateTransferOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -203,9 +201,10 @@ public SquareClientHttpResponse> search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { SearchTransferOrdersResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchTransferOrdersResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchTransferOrdersResponse.class); Optional startingAfter = parsedResponse.getCursor(); SearchTransferOrdersRequest nextRequest = SearchTransferOrdersRequest.builder() .from(request) @@ -214,16 +213,14 @@ public SquareClientHttpResponse> search( List result = parsedResponse.getTransferOrders().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> search(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> search( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -272,17 +269,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RetrieveTransferOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RetrieveTransferOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -329,17 +324,15 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateTransferOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateTransferOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -383,17 +376,15 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteTransferOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteTransferOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -455,17 +446,15 @@ public SquareClientHttpResponse cancel( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelTransferOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelTransferOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -535,17 +524,15 @@ public SquareClientHttpResponse receive( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ReceiveTransferOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ReceiveTransferOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -599,17 +586,15 @@ public SquareClientHttpResponse start( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), StartTransferOrderResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, StartTransferOrderResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawV1TransactionsClient.java b/src/main/java/com/squareup/square/RawV1TransactionsClient.java index bd478dd3..32c8efa7 100644 --- a/src/main/java/com/squareup/square/RawV1TransactionsClient.java +++ b/src/main/java/com/squareup/square/RawV1TransactionsClient.java @@ -75,18 +75,15 @@ public SquareClientHttpResponse> v1ListOrders( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), new TypeReference>() {}), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, new TypeReference>() {}), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -123,16 +120,14 @@ public SquareClientHttpResponse v1RetrieveOrder( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), V1Order.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, V1Order.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -177,16 +172,14 @@ public SquareClientHttpResponse v1UpdateOrder( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), V1Order.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, V1Order.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/RawVendorsClient.java b/src/main/java/com/squareup/square/RawVendorsClient.java index eee42cf9..fd640751 100644 --- a/src/main/java/com/squareup/square/RawVendorsClient.java +++ b/src/main/java/com/squareup/square/RawVendorsClient.java @@ -77,17 +77,15 @@ public SquareClientHttpResponse batchCreate( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BatchCreateVendorsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchCreateVendorsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -136,17 +134,15 @@ public SquareClientHttpResponse batchGet( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BatchGetVendorsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchGetVendorsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -188,17 +184,15 @@ public SquareClientHttpResponse batchUpdate( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), BatchUpdateVendorsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, BatchUpdateVendorsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -240,17 +234,14 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateVendorResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateVendorResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -299,17 +290,14 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchVendorsResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchVendorsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -343,16 +331,14 @@ public SquareClientHttpResponse get(GetVendorsRequest request } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetVendorResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetVendorResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -395,17 +381,14 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateVendorResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateVendorResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/bookings/AsyncRawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/bookings/AsyncRawCustomAttributeDefinitionsClient.java index 3e11e97c..fad95255 100644 --- a/src/main/java/com/squareup/square/bookings/AsyncRawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/bookings/AsyncRawCustomAttributeDefinitionsClient.java @@ -101,10 +101,11 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() @@ -115,7 +116,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO parsedResponse.getCustomAttributeDefinitions().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> { + startingAfter.isPresent(), result, parsedResponse, () -> { try { return list(nextRequest, requestOptions) .get() @@ -127,12 +128,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -196,19 +194,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateBookingCustomAttributeDefinitionResponse.class), + responseBodyString, CreateBookingCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -264,19 +260,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveBookingCustomAttributeDefinitionResponse.class), + responseBodyString, RetrieveBookingCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -341,19 +335,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateBookingCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateBookingCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -410,19 +402,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteBookingCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteBookingCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/bookings/AsyncRawCustomAttributesClient.java b/src/main/java/com/squareup/square/bookings/AsyncRawCustomAttributesClient.java index 4c932ae9..eec7a770 100644 --- a/src/main/java/com/squareup/square/bookings/AsyncRawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/bookings/AsyncRawCustomAttributesClient.java @@ -99,19 +99,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkDeleteBookingCustomAttributesResponse.class), + responseBodyString, BulkDeleteBookingCustomAttributesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -175,19 +173,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkUpsertBookingCustomAttributesResponse.class), + responseBodyString, BulkUpsertBookingCustomAttributesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -252,9 +248,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -263,24 +260,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -342,19 +337,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveBookingCustomAttributeResponse.class), + responseBodyString, RetrieveBookingCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -421,19 +414,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertBookingCustomAttributeResponse.class), + responseBodyString, UpsertBookingCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -492,19 +483,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteBookingCustomAttributeResponse.class), + responseBodyString, DeleteBookingCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/bookings/AsyncRawLocationProfilesClient.java b/src/main/java/com/squareup/square/bookings/AsyncRawLocationProfilesClient.java index 01d288c4..94c126be 100644 --- a/src/main/java/com/squareup/square/bookings/AsyncRawLocationProfilesClient.java +++ b/src/main/java/com/squareup/square/bookings/AsyncRawLocationProfilesClient.java @@ -84,9 +84,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListLocationProfilesRequest nextRequest = ListLocationProfilesRequest.builder() .from(request) @@ -96,7 +97,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO parsedResponse.getLocationBookingProfiles().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> { + startingAfter.isPresent(), result, parsedResponse, () -> { try { return list(nextRequest, requestOptions) .get() @@ -108,12 +109,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/bookings/AsyncRawTeamMemberProfilesClient.java b/src/main/java/com/squareup/square/bookings/AsyncRawTeamMemberProfilesClient.java index e9b6a0aa..a8805904 100644 --- a/src/main/java/com/squareup/square/bookings/AsyncRawTeamMemberProfilesClient.java +++ b/src/main/java/com/squareup/square/bookings/AsyncRawTeamMemberProfilesClient.java @@ -94,9 +94,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListTeamMemberProfilesRequest nextRequest = ListTeamMemberProfilesRequest.builder() .from(request) @@ -106,7 +107,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO parsedResponse.getTeamMemberBookingProfiles().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> { + startingAfter.isPresent(), result, parsedResponse, () -> { try { return list(nextRequest, requestOptions) .get() @@ -118,12 +119,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -172,19 +170,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetTeamMemberBookingProfileResponse.class), + responseBodyString, GetTeamMemberBookingProfileResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/bookings/RawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/bookings/RawCustomAttributeDefinitionsClient.java index a7087547..1d294a28 100644 --- a/src/main/java/com/squareup/square/bookings/RawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/bookings/RawCustomAttributeDefinitionsClient.java @@ -92,9 +92,10 @@ public SquareClientHttpResponse> l } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListBookingCustomAttributeDefinitionsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListBookingCustomAttributeDefinitionsResponse.class); + responseBodyString, ListBookingCustomAttributeDefinitionsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() .from(request) @@ -104,16 +105,14 @@ public SquareClientHttpResponse> l parsedResponse.getCustomAttributeDefinitions().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -164,18 +163,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateBookingCustomAttributeDefinitionResponse.class), + responseBodyString, CreateBookingCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -218,18 +215,16 @@ public SquareClientHttpResponse( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveBookingCustomAttributeDefinitionResponse.class), + responseBodyString, RetrieveBookingCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -281,18 +276,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateBookingCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateBookingCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -336,18 +329,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteBookingCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteBookingCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/bookings/RawCustomAttributesClient.java b/src/main/java/com/squareup/square/bookings/RawCustomAttributesClient.java index a9741eed..047723d7 100644 --- a/src/main/java/com/squareup/square/bookings/RawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/bookings/RawCustomAttributesClient.java @@ -90,18 +90,16 @@ public SquareClientHttpResponse batch } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkDeleteBookingCustomAttributesResponse.class), + responseBodyString, BulkDeleteBookingCustomAttributesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -152,18 +150,16 @@ public SquareClientHttpResponse batch } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkUpsertBookingCustomAttributesResponse.class), + responseBodyString, BulkUpsertBookingCustomAttributesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -214,9 +210,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListBookingCustomAttributesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListBookingCustomAttributesResponse.class); + responseBodyString, ListBookingCustomAttributesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -226,16 +223,14 @@ public SquareClientHttpResponse> list( parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -283,18 +278,16 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveBookingCustomAttributeResponse.class), + responseBodyString, RetrieveBookingCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -348,18 +341,16 @@ public SquareClientHttpResponse upsert( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertBookingCustomAttributeResponse.class), + responseBodyString, UpsertBookingCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -405,18 +396,16 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteBookingCustomAttributeResponse.class), + responseBodyString, DeleteBookingCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/bookings/RawLocationProfilesClient.java b/src/main/java/com/squareup/square/bookings/RawLocationProfilesClient.java index 3ef54e6c..a886c2cc 100644 --- a/src/main/java/com/squareup/square/bookings/RawLocationProfilesClient.java +++ b/src/main/java/com/squareup/square/bookings/RawLocationProfilesClient.java @@ -75,9 +75,10 @@ public SquareClientHttpResponse> list } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListLocationBookingProfilesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListLocationBookingProfilesResponse.class); + responseBodyString, ListLocationBookingProfilesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListLocationProfilesRequest nextRequest = ListLocationProfilesRequest.builder() .from(request) @@ -87,16 +88,14 @@ public SquareClientHttpResponse> list parsedResponse.getLocationBookingProfiles().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/bookings/RawTeamMemberProfilesClient.java b/src/main/java/com/squareup/square/bookings/RawTeamMemberProfilesClient.java index 0d2d0c54..e5549cff 100644 --- a/src/main/java/com/squareup/square/bookings/RawTeamMemberProfilesClient.java +++ b/src/main/java/com/squareup/square/bookings/RawTeamMemberProfilesClient.java @@ -85,9 +85,10 @@ public SquareClientHttpResponse> li } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListTeamMemberBookingProfilesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListTeamMemberBookingProfilesResponse.class); + responseBodyString, ListTeamMemberBookingProfilesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListTeamMemberProfilesRequest nextRequest = ListTeamMemberProfilesRequest.builder() .from(request) @@ -97,16 +98,14 @@ public SquareClientHttpResponse> li parsedResponse.getTeamMemberBookingProfiles().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -141,18 +140,16 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetTeamMemberBookingProfileResponse.class), + responseBodyString, GetTeamMemberBookingProfileResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/bookings/types/BulkDeleteBookingCustomAttributesRequest.java b/src/main/java/com/squareup/square/bookings/types/BulkDeleteBookingCustomAttributesRequest.java index 38f717b2..d55726c9 100644 --- a/src/main/java/com/squareup/square/bookings/types/BulkDeleteBookingCustomAttributesRequest.java +++ b/src/main/java/com/squareup/square/bookings/types/BulkDeleteBookingCustomAttributesRequest.java @@ -93,7 +93,9 @@ public Builder from(BulkDeleteBookingCustomAttributesRequest other) { @JsonSetter(value = "values", nulls = Nulls.SKIP) public Builder values(Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/bookings/types/BulkUpsertBookingCustomAttributesRequest.java b/src/main/java/com/squareup/square/bookings/types/BulkUpsertBookingCustomAttributesRequest.java index 6fe086e7..9cae6c18 100644 --- a/src/main/java/com/squareup/square/bookings/types/BulkUpsertBookingCustomAttributesRequest.java +++ b/src/main/java/com/squareup/square/bookings/types/BulkUpsertBookingCustomAttributesRequest.java @@ -93,7 +93,9 @@ public Builder from(BulkUpsertBookingCustomAttributesRequest other) { @JsonSetter(value = "values", nulls = Nulls.SKIP) public Builder values(Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/cashdrawers/AsyncRawShiftsClient.java b/src/main/java/com/squareup/square/cashdrawers/AsyncRawShiftsClient.java index de8a0961..182c9b14 100644 --- a/src/main/java/com/squareup/square/cashdrawers/AsyncRawShiftsClient.java +++ b/src/main/java/com/squareup/square/cashdrawers/AsyncRawShiftsClient.java @@ -97,9 +97,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListShiftsRequest nextRequest = ListShiftsRequest.builder() .from(request) @@ -109,7 +110,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO parsedResponse.getCashDrawerShifts().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> { + startingAfter.isPresent(), result, parsedResponse, () -> { try { return list(nextRequest, requestOptions) .get() @@ -121,12 +122,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -175,19 +173,17 @@ public CompletableFuture> g @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetCashDrawerShiftResponse.class), + responseBodyString, GetCashDrawerShiftResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -245,9 +241,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListEventsShiftsRequest nextRequest = ListEventsShiftsRequest.builder() .from(request) @@ -256,24 +253,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getCashDrawerShiftEvents().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return listEvents(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return listEvents(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/cashdrawers/RawShiftsClient.java b/src/main/java/com/squareup/square/cashdrawers/RawShiftsClient.java index 6a823624..886a5671 100644 --- a/src/main/java/com/squareup/square/cashdrawers/RawShiftsClient.java +++ b/src/main/java/com/squareup/square/cashdrawers/RawShiftsClient.java @@ -87,9 +87,10 @@ public SquareClientHttpResponse> list } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCashDrawerShiftsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListCashDrawerShiftsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListCashDrawerShiftsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListShiftsRequest nextRequest = ListShiftsRequest.builder() .from(request) @@ -99,16 +100,14 @@ public SquareClientHttpResponse> list parsedResponse.getCashDrawerShifts().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -145,17 +144,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetCashDrawerShiftResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCashDrawerShiftResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -200,9 +197,10 @@ public SquareClientHttpResponse> listEv } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCashDrawerShiftEventsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListCashDrawerShiftEventsResponse.class); + responseBodyString, ListCashDrawerShiftEventsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListEventsShiftsRequest nextRequest = ListEventsShiftsRequest.builder() .from(request) @@ -212,16 +210,14 @@ public SquareClientHttpResponse> listEv parsedResponse.getCashDrawerShiftEvents().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> listEvents(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> listEvents( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/catalog/AsyncImagesClient.java b/src/main/java/com/squareup/square/catalog/AsyncImagesClient.java index 4f9859c0..f57c8149 100644 --- a/src/main/java/com/squareup/square/catalog/AsyncImagesClient.java +++ b/src/main/java/com/squareup/square/catalog/AsyncImagesClient.java @@ -9,7 +9,9 @@ import com.squareup.square.core.RequestOptions; import com.squareup.square.types.CreateCatalogImageResponse; import com.squareup.square.types.UpdateCatalogImageResponse; +import java.io.InputStream; import java.util.concurrent.CompletableFuture; +import okhttp3.MediaType; public class AsyncImagesClient { protected final ClientOptions clientOptions; @@ -62,6 +64,27 @@ public CompletableFuture create( return this.rawClient.create(request, requestOptions).thenApply(response -> response.body()); } + public CompletableFuture create(InputStream stream, String filename) { + return this.rawClient.create(stream, filename).thenApply(response -> response.body()); + } + + public CompletableFuture create( + InputStream stream, String filename, MediaType mediaType) { + return this.rawClient.create(stream, filename, mediaType).thenApply(response -> response.body()); + } + + public CompletableFuture create( + InputStream stream, String filename, RequestOptions requestOptions) { + return this.rawClient.create(stream, filename, requestOptions).thenApply(response -> response.body()); + } + + public CompletableFuture create( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + return this.rawClient + .create(stream, filename, mediaType, requestOptions) + .thenApply(response -> response.body()); + } + /** * Uploads a new image file to replace the existing one in the specified CatalogImage object. *

This UpdateCatalogImage endpoint accepts HTTP multipart/form-data requests with a JSON part and an image file part in @@ -80,4 +103,25 @@ public CompletableFuture update( UpdateImagesRequest request, RequestOptions requestOptions) { return this.rawClient.update(request, requestOptions).thenApply(response -> response.body()); } + + public CompletableFuture update(InputStream stream, String filename) { + return this.rawClient.update(stream, filename).thenApply(response -> response.body()); + } + + public CompletableFuture update( + InputStream stream, String filename, MediaType mediaType) { + return this.rawClient.update(stream, filename, mediaType).thenApply(response -> response.body()); + } + + public CompletableFuture update( + InputStream stream, String filename, RequestOptions requestOptions) { + return this.rawClient.update(stream, filename, requestOptions).thenApply(response -> response.body()); + } + + public CompletableFuture update( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + return this.rawClient + .update(stream, filename, mediaType, requestOptions) + .thenApply(response -> response.body()); + } } diff --git a/src/main/java/com/squareup/square/catalog/AsyncRawImagesClient.java b/src/main/java/com/squareup/square/catalog/AsyncRawImagesClient.java index 93f34bfa..e72588ce 100644 --- a/src/main/java/com/squareup/square/catalog/AsyncRawImagesClient.java +++ b/src/main/java/com/squareup/square/catalog/AsyncRawImagesClient.java @@ -6,6 +6,7 @@ import com.squareup.square.catalog.types.CreateImagesRequest; import com.squareup.square.catalog.types.UpdateImagesRequest; import com.squareup.square.core.ClientOptions; +import com.squareup.square.core.FileStream; import com.squareup.square.core.ObjectMappers; import com.squareup.square.core.RequestOptions; import com.squareup.square.core.SquareApiException; @@ -14,6 +15,7 @@ import com.squareup.square.types.CreateCatalogImageResponse; import com.squareup.square.types.UpdateCatalogImageResponse; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; import java.util.concurrent.CompletableFuture; import okhttp3.Call; @@ -71,10 +73,10 @@ public CompletableFuture> c .newBuilder() .addPathSegments("v2/catalog/images") .build(); - MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); try { if (request.getRequest().isPresent()) { - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "request", ObjectMappers.JSON_MAPPER.writeValueAsString( request.getRequest().get())); @@ -84,7 +86,7 @@ public CompletableFuture> c Files.probeContentType(request.getImageFile().get().toPath()); MediaType imageFileMimeTypeMediaType = imageFileMimeType != null ? MediaType.parse(imageFileMimeType) : null; - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "image_file", request.getImageFile().get().getName(), RequestBody.create(request.getImageFile().get(), imageFileMimeTypeMediaType)); @@ -94,7 +96,7 @@ public CompletableFuture> c } Request.Builder _requestBuilder = new Request.Builder() .url(httpUrl) - .method("POST", body.build()) + .method("POST", multipartBodyBuilder.build()) .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Accept", "application/json"); Request okhttpRequest = _requestBuilder.build(); @@ -107,19 +109,207 @@ public CompletableFuture> c @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateCatalogImageResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> create( + InputStream stream, String filename) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateCatalogImageResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> create( + InputStream stream, String filename, MediaType mediaType) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateCatalogImageResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> create( + InputStream stream, String filename, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateCatalogImageResponse.class), + responseBodyString, CreateCatalogImageResponse.class), response)); return; } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> create( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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()) { String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, CreateCatalogImageResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -155,10 +345,10 @@ public CompletableFuture> u .addPathSegments("v2/catalog/images") .addPathSegment(request.getImageId()) .build(); - MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); try { if (request.getRequest().isPresent()) { - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "request", ObjectMappers.JSON_MAPPER.writeValueAsString( request.getRequest().get())); @@ -168,7 +358,7 @@ public CompletableFuture> u Files.probeContentType(request.getImageFile().get().toPath()); MediaType imageFileMimeTypeMediaType = imageFileMimeType != null ? MediaType.parse(imageFileMimeType) : null; - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "image_file", request.getImageFile().get().getName(), RequestBody.create(request.getImageFile().get(), imageFileMimeTypeMediaType)); @@ -178,7 +368,7 @@ public CompletableFuture> u } Request.Builder _requestBuilder = new Request.Builder() .url(httpUrl) - .method("PUT", body.build()) + .method("PUT", multipartBodyBuilder.build()) .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Accept", "application/json"); Request okhttpRequest = _requestBuilder.build(); @@ -191,19 +381,211 @@ public CompletableFuture> u @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UpdateCatalogImageResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> update( + InputStream stream, String filename) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .addPathSegment(request.getImageId()) + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("PUT", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UpdateCatalogImageResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> update( + InputStream stream, String filename, MediaType mediaType) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .addPathSegment(request.getImageId()) + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("PUT", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UpdateCatalogImageResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> update( + InputStream stream, String filename, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .addPathSegment(request.getImageId()) + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("PUT", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateCatalogImageResponse.class), + responseBodyString, UpdateCatalogImageResponse.class), response)); return; } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + future.completeExceptionally(new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response)); + 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 CompletableFuture> update( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .addPathSegment(request.getImageId()) + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("PUT", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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()) { String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + future.complete(new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UpdateCatalogImageResponse.class), + response)); + return; + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/catalog/AsyncRawObjectClient.java b/src/main/java/com/squareup/square/catalog/AsyncRawObjectClient.java index 7e998617..24891fe2 100644 --- a/src/main/java/com/squareup/square/catalog/AsyncRawObjectClient.java +++ b/src/main/java/com/squareup/square/catalog/AsyncRawObjectClient.java @@ -84,19 +84,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertCatalogObjectResponse.class), + responseBodyString, UpsertCatalogObjectResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -172,19 +170,16 @@ public CompletableFuture> get @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetCatalogObjectResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCatalogObjectResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -248,19 +243,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteCatalogObjectResponse.class), + responseBodyString, DeleteCatalogObjectResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/catalog/ImagesClient.java b/src/main/java/com/squareup/square/catalog/ImagesClient.java index 77878d0d..5e80d3a7 100644 --- a/src/main/java/com/squareup/square/catalog/ImagesClient.java +++ b/src/main/java/com/squareup/square/catalog/ImagesClient.java @@ -9,6 +9,8 @@ import com.squareup.square.core.RequestOptions; import com.squareup.square.types.CreateCatalogImageResponse; import com.squareup.square.types.UpdateCatalogImageResponse; +import java.io.InputStream; +import okhttp3.MediaType; public class ImagesClient { protected final ClientOptions clientOptions; @@ -60,6 +62,25 @@ public CreateCatalogImageResponse create(CreateImagesRequest request, RequestOpt return this.rawClient.create(request, requestOptions).body(); } + public CreateCatalogImageResponse create(InputStream stream, String filename) { + return this.rawClient.create(stream, filename).body(); + } + + public CreateCatalogImageResponse create(InputStream stream, String filename, MediaType mediaType) { + return this.rawClient.create(stream, filename, mediaType).body(); + } + + public CreateCatalogImageResponse create(InputStream stream, String filename, RequestOptions requestOptions) { + return this.rawClient.create(stream, filename, requestOptions).body(); + } + + public CreateCatalogImageResponse create( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + return this.rawClient + .create(stream, filename, mediaType, requestOptions) + .body(); + } + /** * Uploads a new image file to replace the existing one in the specified CatalogImage object. *

This UpdateCatalogImage endpoint accepts HTTP multipart/form-data requests with a JSON part and an image file part in @@ -77,4 +98,23 @@ public UpdateCatalogImageResponse update(UpdateImagesRequest request) { public UpdateCatalogImageResponse update(UpdateImagesRequest request, RequestOptions requestOptions) { return this.rawClient.update(request, requestOptions).body(); } + + public UpdateCatalogImageResponse update(InputStream stream, String filename) { + return this.rawClient.update(stream, filename).body(); + } + + public UpdateCatalogImageResponse update(InputStream stream, String filename, MediaType mediaType) { + return this.rawClient.update(stream, filename, mediaType).body(); + } + + public UpdateCatalogImageResponse update(InputStream stream, String filename, RequestOptions requestOptions) { + return this.rawClient.update(stream, filename, requestOptions).body(); + } + + public UpdateCatalogImageResponse update( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + return this.rawClient + .update(stream, filename, mediaType, requestOptions) + .body(); + } } diff --git a/src/main/java/com/squareup/square/catalog/RawImagesClient.java b/src/main/java/com/squareup/square/catalog/RawImagesClient.java index 98d52191..d4c34d7f 100644 --- a/src/main/java/com/squareup/square/catalog/RawImagesClient.java +++ b/src/main/java/com/squareup/square/catalog/RawImagesClient.java @@ -6,6 +6,7 @@ import com.squareup.square.catalog.types.CreateImagesRequest; import com.squareup.square.catalog.types.UpdateImagesRequest; import com.squareup.square.core.ClientOptions; +import com.squareup.square.core.FileStream; import com.squareup.square.core.ObjectMappers; import com.squareup.square.core.RequestOptions; import com.squareup.square.core.SquareApiException; @@ -14,6 +15,7 @@ import com.squareup.square.types.CreateCatalogImageResponse; import com.squareup.square.types.UpdateCatalogImageResponse; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; import okhttp3.Headers; import okhttp3.HttpUrl; @@ -67,10 +69,10 @@ public SquareClientHttpResponse create( .newBuilder() .addPathSegments("v2/catalog/images") .build(); - MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); try { if (request.getRequest().isPresent()) { - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "request", ObjectMappers.JSON_MAPPER.writeValueAsString( request.getRequest().get())); @@ -80,7 +82,7 @@ public SquareClientHttpResponse create( Files.probeContentType(request.getImageFile().get().toPath()); MediaType imageFileMimeTypeMediaType = imageFileMimeType != null ? MediaType.parse(imageFileMimeType) : null; - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "image_file", request.getImageFile().get().getName(), RequestBody.create(request.getImageFile().get(), imageFileMimeTypeMediaType)); @@ -90,7 +92,7 @@ public SquareClientHttpResponse create( } Request.Builder _requestBuilder = new Request.Builder() .url(httpUrl) - .method("POST", body.build()) + .method("POST", multipartBodyBuilder.build()) .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Accept", "application/json"); Request okhttpRequest = _requestBuilder.build(); @@ -100,17 +102,148 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCatalogImageResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse create(InputStream stream, String filename) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateCatalogImageResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCatalogImageResponse.class), response); } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse create( + InputStream stream, String filename, MediaType mediaType) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCatalogImageResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse create( + InputStream stream, String filename, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCatalogImageResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse create( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("POST", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCatalogImageResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -137,10 +270,10 @@ public SquareClientHttpResponse update( .addPathSegments("v2/catalog/images") .addPathSegment(request.getImageId()) .build(); - MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); try { if (request.getRequest().isPresent()) { - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "request", ObjectMappers.JSON_MAPPER.writeValueAsString( request.getRequest().get())); @@ -150,7 +283,7 @@ public SquareClientHttpResponse update( Files.probeContentType(request.getImageFile().get().toPath()); MediaType imageFileMimeTypeMediaType = imageFileMimeType != null ? MediaType.parse(imageFileMimeType) : null; - body.addFormDataPart( + multipartBodyBuilder.addFormDataPart( "image_file", request.getImageFile().get().getName(), RequestBody.create(request.getImageFile().get(), imageFileMimeTypeMediaType)); @@ -160,7 +293,7 @@ public SquareClientHttpResponse update( } Request.Builder _requestBuilder = new Request.Builder() .url(httpUrl) - .method("PUT", body.build()) + .method("PUT", multipartBodyBuilder.build()) .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Accept", "application/json"); Request okhttpRequest = _requestBuilder.build(); @@ -170,17 +303,152 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateCatalogImageResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse update(InputStream stream, String filename) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .addPathSegment(request.getImageId()) + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("PUT", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateCatalogImageResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse update( + InputStream stream, String filename, MediaType mediaType) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .addPathSegment(request.getImageId()) + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("PUT", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(null))); + Request okhttpRequest = _requestBuilder.build(); + OkHttpClient client = clientOptions.httpClient(); + try (Response response = client.newCall(okhttpRequest).execute()) { + ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateCatalogImageResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse update( + InputStream stream, String filename, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .addPathSegment(request.getImageId()) + .build(); + FileStream fs = new FileStream(stream, filename, null); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("PUT", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateCatalogImageResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateCatalogImageResponse.class), response); } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); + throw new SquareApiException( + "Error with status code " + response.code(), response.code(), errorBody, response); + } catch (IOException e) { + throw new SquareException("Network error executing HTTP request", e); + } + } + + public SquareClientHttpResponse update( + InputStream stream, String filename, MediaType mediaType, RequestOptions requestOptions) { + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) + .newBuilder() + .addPathSegments("v2/catalog/images") + .addPathSegment(request.getImageId()) + .build(); + FileStream fs = new FileStream(stream, filename, mediaType); + MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); + multipartBodyBuilder.addFormDataPart("image_file", filename, fs.toRequestBody()); + RequestBody body = multipartBodyBuilder.build(); + Request.Builder _requestBuilder = new Request.Builder(); + _requestBuilder.url(httpUrl); + _requestBuilder.method("PUT", body); + _requestBuilder.headers(Headers.of(this.clientOptions.headers(requestOptions))); + 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(); String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + if (response.isSuccessful()) { + return new SquareClientHttpResponse<>( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateCatalogImageResponse.class), + response); + } + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/catalog/RawObjectClient.java b/src/main/java/com/squareup/square/catalog/RawObjectClient.java index 519fef4e..fe3cfe08 100644 --- a/src/main/java/com/squareup/square/catalog/RawObjectClient.java +++ b/src/main/java/com/squareup/square/catalog/RawObjectClient.java @@ -76,17 +76,15 @@ public SquareClientHttpResponse upsert( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpsertCatalogObjectResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpsertCatalogObjectResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -150,17 +148,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetCatalogObjectResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCatalogObjectResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -211,17 +207,15 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteCatalogObjectResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteCatalogObjectResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/checkout/AsyncRawPaymentLinksClient.java b/src/main/java/com/squareup/square/checkout/AsyncRawPaymentLinksClient.java index 9cc8dfaa..7af04e93 100644 --- a/src/main/java/com/squareup/square/checkout/AsyncRawPaymentLinksClient.java +++ b/src/main/java/com/squareup/square/checkout/AsyncRawPaymentLinksClient.java @@ -94,9 +94,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListPaymentLinksRequest nextRequest = ListPaymentLinksRequest.builder() .from(request) @@ -105,24 +106,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getPaymentLinks().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -184,19 +183,17 @@ public CompletableFuture> cr @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreatePaymentLinkResponse.class), + responseBodyString, CreatePaymentLinkResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -243,19 +240,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetPaymentLinkResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetPaymentLinkResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -315,19 +309,17 @@ public CompletableFuture> up @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdatePaymentLinkResponse.class), + responseBodyString, UpdatePaymentLinkResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -375,19 +367,17 @@ public CompletableFuture> de @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeletePaymentLinkResponse.class), + responseBodyString, DeletePaymentLinkResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/checkout/RawPaymentLinksClient.java b/src/main/java/com/squareup/square/checkout/RawPaymentLinksClient.java index 65cf5b5c..15df312a 100644 --- a/src/main/java/com/squareup/square/checkout/RawPaymentLinksClient.java +++ b/src/main/java/com/squareup/square/checkout/RawPaymentLinksClient.java @@ -85,9 +85,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListPaymentLinksResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListPaymentLinksResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListPaymentLinksResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListPaymentLinksRequest nextRequest = ListPaymentLinksRequest.builder() .from(request) @@ -96,16 +97,14 @@ public SquareClientHttpResponse> list( List result = parsedResponse.getPaymentLinks().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -154,17 +153,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreatePaymentLinkResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreatePaymentLinkResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -199,17 +196,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetPaymentLinkResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetPaymentLinkResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -256,17 +251,15 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdatePaymentLinkResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdatePaymentLinkResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -301,17 +294,15 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeletePaymentLinkResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeletePaymentLinkResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/core/AsyncCustomPager.java b/src/main/java/com/squareup/square/core/AsyncCustomPager.java new file mode 100644 index 00000000..74085142 --- /dev/null +++ b/src/main/java/com/squareup/square/core/AsyncCustomPager.java @@ -0,0 +1,164 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.squareup.square.core; + +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; + +/** + * Skeleton implementation for custom asynchronous bidirectional pagination. + * + * THIS CLASS MUST BE IMPLEMENTED BY THE USER. + * + * This file is added to .fernignore and will not be regenerated. + * Replace this skeleton implementation with your custom async pagination logic + * that handles your API's specific pagination structure (e.g., HATEOAS links). + * + * Example implementation for HATEOAS-style async pagination: + *

{@code
+ * public class AsyncCustomPager implements BiDirectionalPage {
+ *     private final List items;
+ *     private final String nextUrl;
+ *     private final String previousUrl;
+ *     private final AsyncHttpClient client;
+ *
+ *     public AsyncCustomPager(Response response, AsyncHttpClient client, ...) {
+ *         this.items = response.getData();
+ *         this.nextUrl = response.getLinks().getNext();
+ *         this.previousUrl = response.getLinks().getPrevious();
+ *         // ... store other needed context
+ *     }
+ *
+ *     @Override
+ *     public boolean hasNext() {
+ *         return nextUrl != null;
+ *     }
+ *
+ *     @Override
+ *     public CompletableFuture> nextPageAsync() {
+ *         if (!hasNext()) {
+ *             CompletableFuture> future = new CompletableFuture<>();
+ *             future.completeExceptionally(new NoSuchElementException("No next page available"));
+ *             return future;
+ *         }
+ *         // Make async HTTP request to nextUrl
+ *         return client.getAsync(nextUrl)
+ *             .thenApply(response -> new AsyncCustomPager<>(response, client, ...));
+ *     }
+ *
+ *     // ... implement other methods
+ * }
+ * }
+ * + * @param The type of items in the page + */ +public class AsyncCustomPager implements BiDirectionalPage { + + /** + * Create an AsyncCustomPager from an initial response. + * + * @param initialResponse The first page response from the API + * @param client The async HTTP client to use for subsequent requests + * @param requestOptions Request options for authentication, headers, etc. + * @return A CompletableFuture containing the new AsyncCustomPager instance + */ + public static CompletableFuture> createAsync( + Object initialResponse, okhttp3.OkHttpClient client, Object requestOptions) { + throw new UnsupportedOperationException("AsyncCustomPager must be implemented. " + + "Please implement this class in core/AsyncCustomPager.java to define your async pagination logic. " + + "This file has been added to .fernignore and will not be overwritten. " + + "See the class documentation for implementation examples."); + } + + @Override + public boolean hasNext() { + throw new UnsupportedOperationException("AsyncCustomPager.hasNext() must be implemented. " + + "This method should return true if a next page is available."); + } + + @Override + public boolean hasPrevious() { + throw new UnsupportedOperationException("AsyncCustomPager.hasPrevious() must be implemented. " + + "This method should return true if a previous page is available."); + } + + /** + * Asynchronously fetch the next page. + * + * @return A CompletableFuture that completes with the next page + * @throws java.util.NoSuchElementException if no next page exists (wrapped in CompletableFuture) + */ + public CompletableFuture> nextPageAsync() { + CompletableFuture> future = new CompletableFuture<>(); + future.completeExceptionally( + new UnsupportedOperationException("AsyncCustomPager.nextPageAsync() must be implemented. " + + "This method should asynchronously fetch and return the next page of results.")); + return future; + } + + /** + * Asynchronously fetch the previous page. + * + * @return A CompletableFuture that completes with the previous page + * @throws java.util.NoSuchElementException if no previous page exists (wrapped in CompletableFuture) + */ + public CompletableFuture> previousPageAsync() { + CompletableFuture> future = new CompletableFuture<>(); + future.completeExceptionally( + new UnsupportedOperationException("AsyncCustomPager.previousPageAsync() must be implemented. " + + "This method should asynchronously fetch and return the previous page of results.")); + return future; + } + + @Override + public BiDirectionalPage nextPage() throws IOException { + throw new UnsupportedOperationException("AsyncCustomPager.nextPage() must be implemented. " + + "Consider using nextPageAsync() for async operations, or implement synchronous blocking version."); + } + + @Override + public BiDirectionalPage previousPage() throws IOException { + throw new UnsupportedOperationException( + "AsyncCustomPager.previousPage() must be implemented. " + + "Consider using previousPageAsync() for async operations, or implement synchronous blocking version."); + } + + @Override + public List getItems() { + throw new UnsupportedOperationException("AsyncCustomPager.getItems() must be implemented. " + + "This method should return the items in the current page."); + } + + @Override + public Optional getResponse() { + throw new UnsupportedOperationException("AsyncCustomPager.getResponse() must be implemented. " + + "This method should return the full response object for accessing pagination metadata."); + } + + /** + * Asynchronously iterate through all pages starting from current. + * Returns a CompletableFuture that completes with all items from all pages. + * + * @return CompletableFuture containing all items across all pages + */ + public CompletableFuture> getAllItemsAsync() { + throw new UnsupportedOperationException("AsyncCustomPager.getAllItemsAsync() must be implemented. " + + "This method should asynchronously fetch all pages and return all items."); + } + + /** + * Process each page asynchronously as it arrives. + * + * @param pageProcessor Function to process each page + * @return CompletableFuture that completes when all pages are processed + */ + public CompletableFuture forEachPageAsync( + java.util.function.Function, CompletionStage> pageProcessor) { + throw new UnsupportedOperationException("AsyncCustomPager.forEachPageAsync() must be implemented. " + + "This method should asynchronously process each page with the given function."); + } +} diff --git a/src/main/java/com/squareup/square/core/BasePage.java b/src/main/java/com/squareup/square/core/BasePage.java index 1ed43f1c..4fb16b04 100644 --- a/src/main/java/com/squareup/square/core/BasePage.java +++ b/src/main/java/com/squareup/square/core/BasePage.java @@ -4,14 +4,17 @@ package com.squareup.square.core; import java.util.List; +import java.util.Optional; public abstract class BasePage { private final boolean hasNext; private final List items; + private final Object response; - public BasePage(boolean hasNext, List items) { + public BasePage(boolean hasNext, List items, Object response) { this.hasNext = hasNext; this.items = items; + this.response = response; } public boolean hasNext() { @@ -21,4 +24,15 @@ public boolean hasNext() { public List getItems() { return items; } + + /** + * Returns the full response object for accessing pagination metadata like cursor tokens. + * + * @return Optional containing the response, or empty if unavailable + */ + public Optional getResponse() { + @SuppressWarnings("unchecked") + R typedResponse = (R) response; + return Optional.ofNullable(typedResponse); + } } diff --git a/src/main/java/com/squareup/square/core/BiDirectionalPage.java b/src/main/java/com/squareup/square/core/BiDirectionalPage.java new file mode 100644 index 00000000..bf982cfd --- /dev/null +++ b/src/main/java/com/squareup/square/core/BiDirectionalPage.java @@ -0,0 +1,60 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.squareup.square.core; + +import java.util.List; + +/** + * Interface for pages that support bidirectional pagination (both forward and backward navigation). + * This is used for custom pagination scenarios where the API provides both next and previous page links. + * + * @param The type of items in the page + */ +public interface BiDirectionalPage { + /** + * Returns whether there is a next page available. + * + * @return true if next page exists and can be fetched + */ + boolean hasNext(); + + /** + * Returns whether there is a previous page available. + * + * @return true if previous page exists and can be fetched + */ + boolean hasPrevious(); + + /** + * Fetches and returns the next page. + * + * @return the next page + * @throws java.util.NoSuchElementException if no next page exists + * @throws java.io.IOException if the HTTP request fails + */ + BiDirectionalPage nextPage() throws java.io.IOException; + + /** + * Fetches and returns the previous page. + * + * @return the previous page + * @throws java.util.NoSuchElementException if no previous page exists + * @throws java.io.IOException if the HTTP request fails + */ + BiDirectionalPage previousPage() throws java.io.IOException; + + /** + * Returns the items in the current page. + * + * @return list of items in this page + */ + List getItems(); + + /** + * Returns the full response object for accessing pagination metadata. + * + * @return Optional containing the response, or empty if unavailable + */ + java.util.Optional getResponse(); +} diff --git a/src/main/java/com/squareup/square/core/ClientOptions.java b/src/main/java/com/squareup/square/core/ClientOptions.java index 0960b301..dd11ddbc 100644 --- a/src/main/java/com/squareup/square/core/ClientOptions.java +++ b/src/main/java/com/squareup/square/core/ClientOptions.java @@ -21,26 +21,29 @@ public final class ClientOptions { private final int timeout; + private final int maxRetries; + private ClientOptions( Environment environment, Map headers, Map> headerSuppliers, OkHttpClient httpClient, - int timeout) { + int timeout, + int maxRetries) { this.environment = environment; this.headers = new HashMap<>(); this.headers.putAll(headers); this.headers.putAll(new HashMap() { { - put("User-Agent", "com.squareup:square/45.1.0.20251016"); put("X-Fern-Language", "JAVA"); put("X-Fern-SDK-Name", "com.square.fern:api-sdk"); - put("X-Fern-SDK-Version", "45.1.0.20251016"); + put("X-Fern-SDK-Version", "0.0.538"); } }); this.headerSuppliers = headerSuppliers; this.httpClient = httpClient; this.timeout = timeout; + this.maxRetries = maxRetries; } public Environment environment() { @@ -82,6 +85,10 @@ public OkHttpClient httpClientWithTimeout(RequestOptions requestOptions) { .build(); } + public int maxRetries() { + return this.maxRetries; + } + public static Builder builder() { return new Builder(); } @@ -165,7 +172,8 @@ public ClientOptions build() { this.httpClient = httpClientBuilder.build(); this.timeout = Optional.of(httpClient.callTimeoutMillis() / 1000); - return new ClientOptions(environment, headers, headerSuppliers, httpClient, this.timeout.get()); + return new ClientOptions( + environment, headers, headerSuppliers, httpClient, this.timeout.get(), this.maxRetries); } /** diff --git a/src/main/java/com/squareup/square/core/CustomPager.java b/src/main/java/com/squareup/square/core/CustomPager.java new file mode 100644 index 00000000..892f86a1 --- /dev/null +++ b/src/main/java/com/squareup/square/core/CustomPager.java @@ -0,0 +1,117 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.squareup.square.core; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; + +/** + * Skeleton implementation for custom bidirectional pagination. + * + * THIS CLASS MUST BE IMPLEMENTED BY THE USER. + * + * This file is added to .fernignore and will not be regenerated. + * Replace this skeleton implementation with your custom pagination logic + * that handles your API's specific pagination structure (e.g., HATEOAS links). + * + * Example implementation for HATEOAS-style pagination: + *
{@code
+ * public class CustomPager implements BiDirectionalPage, Iterable {
+ *     private final List items;
+ *     private final String nextUrl;
+ *     private final String previousUrl;
+ *     private final OkHttpClient client;
+ *     private final TypeReference> responseType;
+ *
+ *     public CustomPager(Response response, OkHttpClient client, ...) {
+ *         this.items = response.getData();
+ *         this.nextUrl = response.getLinks().getNext();
+ *         this.previousUrl = response.getLinks().getPrevious();
+ *         // ... store other needed context
+ *     }
+ *
+ *     @Override
+ *     public boolean hasNext() {
+ *         return nextUrl != null;
+ *     }
+ *
+ *     @Override
+ *     public CustomPager nextPage() throws IOException {
+ *         if (!hasNext()) {
+ *             throw new NoSuchElementException("No next page available");
+ *         }
+ *         // Make HTTP request to nextUrl
+ *         // Parse response
+ *         // Return new CustomPager instance
+ *     }
+ *
+ *     // ... implement other methods
+ * }
+ * }
+ * + * @param The type of items in the page + */ +public class CustomPager implements BiDirectionalPage, Iterable { + + /** + * Create a CustomPager from an initial response. + * + * @param initialResponse The first page response from the API + * @param client The HTTP client to use for subsequent requests + * @param requestOptions Request options for authentication, headers, etc. + * @return A new CustomPager instance + * @throws IOException if the request fails + */ + public static CustomPager create(Object initialResponse, okhttp3.OkHttpClient client, Object requestOptions) + throws IOException { + throw new UnsupportedOperationException("CustomPager must be implemented. " + + "Please implement this class in core/CustomPager.java to define your pagination logic. " + + "This file has been added to .fernignore and will not be overwritten. " + + "See the class documentation for implementation examples."); + } + + @Override + public boolean hasNext() { + throw new UnsupportedOperationException("CustomPager.hasNext() must be implemented. " + + "This method should return true if a next page is available."); + } + + @Override + public boolean hasPrevious() { + throw new UnsupportedOperationException("CustomPager.hasPrevious() must be implemented. " + + "This method should return true if a previous page is available."); + } + + @Override + public BiDirectionalPage nextPage() throws IOException { + throw new UnsupportedOperationException("CustomPager.nextPage() must be implemented. " + + "This method should fetch and return the next page of results."); + } + + @Override + public BiDirectionalPage previousPage() throws IOException { + throw new UnsupportedOperationException("CustomPager.previousPage() must be implemented. " + + "This method should fetch and return the previous page of results."); + } + + @Override + public List getItems() { + throw new UnsupportedOperationException("CustomPager.getItems() must be implemented. " + + "This method should return the items in the current page."); + } + + @Override + public Optional getResponse() { + throw new UnsupportedOperationException("CustomPager.getResponse() must be implemented. " + + "This method should return the full response object for accessing pagination metadata."); + } + + @Override + public Iterator iterator() { + throw new UnsupportedOperationException("CustomPager.iterator() must be implemented. " + + "This method should return an iterator that traverses all items across all pages."); + } +} diff --git a/src/main/java/com/squareup/square/core/NullableNonemptyFilter.java b/src/main/java/com/squareup/square/core/NullableNonemptyFilter.java index 4f8493b6..f55bba89 100644 --- a/src/main/java/com/squareup/square/core/NullableNonemptyFilter.java +++ b/src/main/java/com/squareup/square/core/NullableNonemptyFilter.java @@ -14,6 +14,9 @@ public boolean equals(Object o) { } private boolean isOptionalEmpty(Object o) { - return o instanceof Optional && !((Optional) o).isPresent(); + if (o instanceof Optional) { + return !((Optional) o).isPresent(); + } + return false; } } diff --git a/src/main/java/com/squareup/square/core/ObjectMappers.java b/src/main/java/com/squareup/square/core/ObjectMappers.java index f0fc54b1..6cb27074 100644 --- a/src/main/java/com/squareup/square/core/ObjectMappers.java +++ b/src/main/java/com/squareup/square/core/ObjectMappers.java @@ -4,6 +4,7 @@ package com.squareup.square.core; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -33,4 +34,12 @@ public static String stringify(Object o) { return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode()); } } + + public static Object parseErrorBody(String responseBodyString) { + try { + return JSON_MAPPER.readValue(responseBodyString, Object.class); + } catch (JsonProcessingException ignored) { + return responseBodyString; + } + } } diff --git a/src/main/java/com/squareup/square/core/RetryInterceptor.java b/src/main/java/com/squareup/square/core/RetryInterceptor.java index 728c0bcc..30d4daa8 100644 --- a/src/main/java/com/squareup/square/core/RetryInterceptor.java +++ b/src/main/java/com/squareup/square/core/RetryInterceptor.java @@ -5,6 +5,9 @@ import java.io.IOException; import java.time.Duration; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Optional; import java.util.Random; import okhttp3.Interceptor; @@ -12,7 +15,10 @@ public class RetryInterceptor implements Interceptor { - private static final Duration ONE_SECOND = Duration.ofSeconds(1); + private static final Duration INITIAL_RETRY_DELAY = Duration.ofMillis(1000); + private static final Duration MAX_RETRY_DELAY = Duration.ofMillis(60000); + private static final double JITTER_FACTOR = 0.2; + private final ExponentialBackoff backoff; private final Random random = new Random(); @@ -32,7 +38,7 @@ public Response intercept(Chain chain) throws IOException { } private Response retryChain(Response response, Chain chain) throws IOException { - Optional nextBackoff = this.backoff.nextBackoff(); + Optional nextBackoff = this.backoff.nextBackoff(response); while (nextBackoff.isPresent()) { try { Thread.sleep(nextBackoff.get().toMillis()); @@ -42,7 +48,7 @@ private Response retryChain(Response response, Chain chain) throws IOException { response.close(); response = chain.proceed(chain.request()); if (shouldRetry(response.code())) { - nextBackoff = this.backoff.nextBackoff(); + nextBackoff = this.backoff.nextBackoff(response); } else { return response; } @@ -51,6 +57,102 @@ private Response retryChain(Response response, Chain chain) throws IOException { return response; } + /** + * Calculates the retry delay from response headers, with fallback to exponential backoff. + * Priority: Retry-After > X-RateLimit-Reset > Exponential Backoff + */ + private Duration getRetryDelayFromHeaders(Response response, int retryAttempt) { + // Check for Retry-After header first (RFC 7231), with no jitter + String retryAfter = response.header("Retry-After"); + if (retryAfter != null) { + // Parse as number of seconds... + Optional secondsDelay = tryParseLong(retryAfter) + .map(seconds -> seconds * 1000) + .filter(delayMs -> delayMs > 0) + .map(delayMs -> Math.min(delayMs, MAX_RETRY_DELAY.toMillis())) + .map(Duration::ofMillis); + if (secondsDelay.isPresent()) { + return secondsDelay.get(); + } + + // ...or as an HTTP date; both are valid + Optional dateDelay = tryParseHttpDate(retryAfter) + .map(resetTime -> resetTime.toInstant().toEpochMilli() - System.currentTimeMillis()) + .filter(delayMs -> delayMs > 0) + .map(delayMs -> Math.min(delayMs, MAX_RETRY_DELAY.toMillis())) + .map(Duration::ofMillis); + if (dateDelay.isPresent()) { + return dateDelay.get(); + } + } + + // Then check for industry-standard X-RateLimit-Reset header, with positive jitter + String rateLimitReset = response.header("X-RateLimit-Reset"); + if (rateLimitReset != null) { + // Assume Unix timestamp in epoch seconds + Optional rateLimitDelay = tryParseLong(rateLimitReset) + .map(resetTimeSeconds -> (resetTimeSeconds * 1000) - System.currentTimeMillis()) + .filter(delayMs -> delayMs > 0) + .map(delayMs -> Math.min(delayMs, MAX_RETRY_DELAY.toMillis())) + .map(this::addPositiveJitter) + .map(Duration::ofMillis); + if (rateLimitDelay.isPresent()) { + return rateLimitDelay.get(); + } + } + + // Fall back to exponential backoff, with symmetric jitter + long baseDelay = INITIAL_RETRY_DELAY.toMillis() * (1L << retryAttempt); // 2^retryAttempt + long cappedDelay = Math.min(baseDelay, MAX_RETRY_DELAY.toMillis()); + return Duration.ofMillis(addSymmetricJitter(cappedDelay)); + } + + /** + * Attempts to parse a string as a long, returning empty Optional on failure. + */ + private Optional tryParseLong(String value) { + if (value == null) { + return Optional.empty(); + } + try { + return Optional.of(Long.parseLong(value)); + } catch (NumberFormatException e) { + return Optional.empty(); + } + } + + /** + * Attempts to parse a string as an HTTP date (RFC 1123), returning empty Optional on failure. + */ + private Optional tryParseHttpDate(String value) { + if (value == null) { + return Optional.empty(); + } + try { + return Optional.of(ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME)); + } catch (DateTimeParseException e) { + return Optional.empty(); + } + } + + /** + * Adds positive jitter (100-120% of original value) to prevent thundering herd. + * Used for X-RateLimit-Reset header delays. + */ + private long addPositiveJitter(long delayMs) { + double jitterMultiplier = 1.0 + (random.nextDouble() * JITTER_FACTOR); + return (long) (delayMs * jitterMultiplier); + } + + /** + * Adds symmetric jitter (90-110% of original value) to prevent thundering herd. + * Used for exponential backoff delays. + */ + private long addSymmetricJitter(long delayMs) { + double jitterMultiplier = 1.0 + ((random.nextDouble() - 0.5) * JITTER_FACTOR); + return (long) (delayMs * jitterMultiplier); + } + private static boolean shouldRetry(int statusCode) { return statusCode == 408 || statusCode == 429 || statusCode >= 500; } @@ -65,14 +167,14 @@ private final class ExponentialBackoff { this.maxNumRetries = maxNumRetries; } - public Optional nextBackoff() { - retryNumber += 1; - if (retryNumber > maxNumRetries) { + public Optional nextBackoff(Response response) { + if (retryNumber >= maxNumRetries) { return Optional.empty(); } - int upperBound = (int) Math.pow(2, retryNumber); - return Optional.of(ONE_SECOND.multipliedBy(random.nextInt(upperBound))); + Duration delay = getRetryDelayFromHeaders(response, retryNumber); + retryNumber += 1; + return Optional.of(delay); } } } diff --git a/src/main/java/com/squareup/square/core/Stream.java b/src/main/java/com/squareup/square/core/Stream.java index 7ea39a0c..39ae566e 100644 --- a/src/main/java/com/squareup/square/core/Stream.java +++ b/src/main/java/com/squareup/square/core/Stream.java @@ -174,8 +174,8 @@ private final class SSEIterator implements Iterator { private T nextItem; private boolean hasNextItem = false; private boolean endOfStream = false; - private StringBuilder buffer = new StringBuilder(); - private boolean prefixSeen = false; + private StringBuilder eventDataBuffer = new StringBuilder(); + private String currentEventType = null; private SSEIterator() { if (sseReader != null && !isStreamClosed()) { @@ -223,39 +223,69 @@ private boolean readNextMessage() { try { while (sseScanner.hasNextLine()) { - String chunk = sseScanner.nextLine(); - buffer.append(chunk).append(NEWLINE); - - int terminatorIndex; - while ((terminatorIndex = buffer.indexOf(messageTerminator)) >= 0) { - String line = buffer.substring(0, terminatorIndex + messageTerminator.length()); - buffer.delete(0, terminatorIndex + messageTerminator.length()); - - line = line.trim(); - if (line.isEmpty()) { - continue; + String line = sseScanner.nextLine(); + + if (line.trim().isEmpty()) { + if (eventDataBuffer.length() > 0) { + try { + nextItem = ObjectMappers.JSON_MAPPER.readValue(eventDataBuffer.toString(), valueType); + hasNextItem = true; + eventDataBuffer.setLength(0); + currentEventType = null; + return true; + } catch (Exception parseEx) { + System.err.println("Failed to parse SSE event: " + parseEx.getMessage()); + eventDataBuffer.setLength(0); + currentEventType = null; + continue; + } } + continue; + } - if (!prefixSeen && line.startsWith(DATA_PREFIX)) { - prefixSeen = true; - line = line.substring(DATA_PREFIX.length()).trim(); - } else if (!prefixSeen) { - continue; + if (line.startsWith(DATA_PREFIX)) { + String dataContent = line.substring(DATA_PREFIX.length()); + if (dataContent.startsWith(" ")) { + dataContent = dataContent.substring(1); } - if (streamTerminator != null && line.contains(streamTerminator)) { + if (eventDataBuffer.length() == 0 + && streamTerminator != null + && dataContent.trim().equals(streamTerminator)) { endOfStream = true; return false; } - try { - nextItem = ObjectMappers.JSON_MAPPER.readValue(line, valueType); - hasNextItem = true; - prefixSeen = false; - return true; - } catch (Exception parseEx) { - continue; + if (eventDataBuffer.length() > 0) { + eventDataBuffer.append('\n'); + } + eventDataBuffer.append(dataContent); + } else if (line.startsWith("event:")) { + String eventValue = line.length() > 6 ? line.substring(6) : ""; + if (eventValue.startsWith(" ")) { + eventValue = eventValue.substring(1); } + currentEventType = eventValue; + } else if (line.startsWith("id:")) { + // Event ID field (ignored) + } else if (line.startsWith("retry:")) { + // Retry field (ignored) + } else if (line.startsWith(":")) { + // Comment line (ignored) + } + } + + if (eventDataBuffer.length() > 0) { + try { + nextItem = ObjectMappers.JSON_MAPPER.readValue(eventDataBuffer.toString(), valueType); + hasNextItem = true; + eventDataBuffer.setLength(0); + currentEventType = null; + return true; + } catch (Exception parseEx) { + System.err.println("Failed to parse final SSE event: " + parseEx.getMessage()); + eventDataBuffer.setLength(0); + currentEventType = null; } } diff --git a/src/main/java/com/squareup/square/core/SyncPage.java b/src/main/java/com/squareup/square/core/SyncPage.java index c652b585..5689efe8 100644 --- a/src/main/java/com/squareup/square/core/SyncPage.java +++ b/src/main/java/com/squareup/square/core/SyncPage.java @@ -10,8 +10,8 @@ public class SyncPage extends BasePage { protected final Supplier> nextSupplier; - public SyncPage(boolean hasNext, List items, Supplier> nextSupplier) { - super(hasNext, items); + public SyncPage(boolean hasNext, List items, Object response, Supplier> nextSupplier) { + super(hasNext, items, response); this.nextSupplier = nextSupplier; } diff --git a/src/main/java/com/squareup/square/core/SyncPagingIterable.java b/src/main/java/com/squareup/square/core/SyncPagingIterable.java index 105667a5..c870d8a1 100644 --- a/src/main/java/com/squareup/square/core/SyncPagingIterable.java +++ b/src/main/java/com/squareup/square/core/SyncPagingIterable.java @@ -14,12 +14,14 @@ public class SyncPagingIterable extends SyncPage implements Iterable { - public SyncPagingIterable(boolean hasNext, List items, Supplier> getNext) { - super(hasNext, items, getNext); + public SyncPagingIterable( + boolean hasNext, List items, Object response, Supplier> getNext) { + super(hasNext, items, response, getNext); } - public SyncPagingIterable(boolean hasNext, Optional> items, Supplier> getNext) { - super(hasNext, items.orElse(new ArrayList<>()), getNext); + public SyncPagingIterable( + boolean hasNext, Optional> items, Object response, Supplier> getNext) { + super(hasNext, items.orElse(new ArrayList<>()), response, getNext); } public Stream streamItems() { diff --git a/src/main/java/com/squareup/square/customers/AsyncRawCardsClient.java b/src/main/java/com/squareup/square/customers/AsyncRawCardsClient.java index fdf5824f..24cf6ccf 100644 --- a/src/main/java/com/squareup/square/customers/AsyncRawCardsClient.java +++ b/src/main/java/com/squareup/square/customers/AsyncRawCardsClient.java @@ -83,19 +83,17 @@ public CompletableFuture> c @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateCustomerCardResponse.class), + responseBodyString, CreateCustomerCardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -144,19 +142,17 @@ public CompletableFuture> d @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteCustomerCardResponse.class), + responseBodyString, DeleteCustomerCardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/customers/AsyncRawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/customers/AsyncRawCustomAttributeDefinitionsClient.java index 2db99d7a..96e6620c 100644 --- a/src/main/java/com/squareup/square/customers/AsyncRawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/customers/AsyncRawCustomAttributeDefinitionsClient.java @@ -109,10 +109,11 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() @@ -123,7 +124,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO parsedResponse.getCustomAttributeDefinitions().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> { + startingAfter.isPresent(), result, parsedResponse, () -> { try { return list(nextRequest, requestOptions) .get() @@ -135,12 +136,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -212,19 +210,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateCustomerCustomAttributeDefinitionResponse.class), + responseBodyString, CreateCustomerCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -282,19 +278,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetCustomerCustomAttributeDefinitionResponse.class), + responseBodyString, GetCustomerCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -359,19 +353,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateCustomerCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateCustomerCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -426,19 +418,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteCustomerCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteCustomerCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -514,19 +504,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchUpsertCustomerCustomAttributesResponse.class), + responseBodyString, BatchUpsertCustomerCustomAttributesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/customers/AsyncRawCustomAttributesClient.java b/src/main/java/com/squareup/square/customers/AsyncRawCustomAttributesClient.java index ffedc450..a0d965fa 100644 --- a/src/main/java/com/squareup/square/customers/AsyncRawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/customers/AsyncRawCustomAttributesClient.java @@ -102,9 +102,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -113,24 +114,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -198,19 +197,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetCustomerCustomAttributeResponse.class), + responseBodyString, GetCustomerCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -281,19 +278,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertCustomerCustomAttributeResponse.class), + responseBodyString, UpsertCustomerCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -350,19 +345,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteCustomerCustomAttributeResponse.class), + responseBodyString, DeleteCustomerCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/customers/AsyncRawGroupsClient.java b/src/main/java/com/squareup/square/customers/AsyncRawGroupsClient.java index 48cd7c17..78d823b6 100644 --- a/src/main/java/com/squareup/square/customers/AsyncRawGroupsClient.java +++ b/src/main/java/com/squareup/square/customers/AsyncRawGroupsClient.java @@ -99,9 +99,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListGroupsRequest nextRequest = ListGroupsRequest.builder() .from(request) @@ -109,24 +110,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getGroups().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -183,19 +182,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateCustomerGroupResponse.class), + responseBodyString, CreateCustomerGroupResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -242,19 +239,16 @@ public CompletableFuture> get @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetCustomerGroupResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCustomerGroupResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -310,19 +304,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateCustomerGroupResponse.class), + responseBodyString, UpdateCustomerGroupResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -370,19 +362,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteCustomerGroupResponse.class), + responseBodyString, DeleteCustomerGroupResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -435,19 +425,17 @@ public CompletableFuture> a @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), AddGroupToCustomerResponse.class), + responseBodyString, AddGroupToCustomerResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -501,19 +489,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RemoveGroupFromCustomerResponse.class), + responseBodyString, RemoveGroupFromCustomerResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/customers/AsyncRawSegmentsClient.java b/src/main/java/com/squareup/square/customers/AsyncRawSegmentsClient.java index 38364954..29705d68 100644 --- a/src/main/java/com/squareup/square/customers/AsyncRawSegmentsClient.java +++ b/src/main/java/com/squareup/square/customers/AsyncRawSegmentsClient.java @@ -86,9 +86,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListSegmentsRequest nextRequest = ListSegmentsRequest.builder() .from(request) @@ -97,24 +98,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getSegments().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -161,19 +160,17 @@ public CompletableFuture> g @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetCustomerSegmentResponse.class), + responseBodyString, GetCustomerSegmentResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/customers/RawCardsClient.java b/src/main/java/com/squareup/square/customers/RawCardsClient.java index d61c9b82..00c8ff52 100644 --- a/src/main/java/com/squareup/square/customers/RawCardsClient.java +++ b/src/main/java/com/squareup/square/customers/RawCardsClient.java @@ -75,17 +75,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateCustomerCardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCustomerCardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -122,17 +120,15 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteCustomerCardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteCustomerCardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/customers/RawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/customers/RawCustomAttributeDefinitionsClient.java index 75aecdd7..5e862fa1 100644 --- a/src/main/java/com/squareup/square/customers/RawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/customers/RawCustomAttributeDefinitionsClient.java @@ -100,9 +100,10 @@ public SquareClientHttpResponse> l } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCustomerCustomAttributeDefinitionsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListCustomerCustomAttributeDefinitionsResponse.class); + responseBodyString, ListCustomerCustomAttributeDefinitionsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() .from(request) @@ -112,16 +113,14 @@ public SquareClientHttpResponse> l parsedResponse.getCustomAttributeDefinitions().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -180,18 +179,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateCustomerCustomAttributeDefinitionResponse.class), + responseBodyString, CreateCustomerCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -236,18 +233,16 @@ public SquareClientHttpResponse ge } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetCustomerCustomAttributeDefinitionResponse.class), + responseBodyString, GetCustomerCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -299,18 +294,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateCustomerCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateCustomerCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -352,18 +345,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteCustomerCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteCustomerCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -426,18 +417,16 @@ public SquareClientHttpResponse bat } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BatchUpsertCustomerCustomAttributesResponse.class), + responseBodyString, BatchUpsertCustomerCustomAttributesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/customers/RawCustomAttributesClient.java b/src/main/java/com/squareup/square/customers/RawCustomAttributesClient.java index 6154be8c..4a288a47 100644 --- a/src/main/java/com/squareup/square/customers/RawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/customers/RawCustomAttributesClient.java @@ -92,9 +92,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCustomerCustomAttributesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListCustomerCustomAttributesResponse.class); + responseBodyString, ListCustomerCustomAttributesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -104,16 +105,14 @@ public SquareClientHttpResponse> list( parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -167,18 +166,16 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetCustomerCustomAttributeResponse.class), + responseBodyString, GetCustomerCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -236,18 +233,16 @@ public SquareClientHttpResponse upsert( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertCustomerCustomAttributeResponse.class), + responseBodyString, UpsertCustomerCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -291,18 +286,16 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteCustomerCustomAttributeResponse.class), + responseBodyString, DeleteCustomerCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/customers/RawGroupsClient.java b/src/main/java/com/squareup/square/customers/RawGroupsClient.java index 151a1c5f..f04ff5a3 100644 --- a/src/main/java/com/squareup/square/customers/RawGroupsClient.java +++ b/src/main/java/com/squareup/square/customers/RawGroupsClient.java @@ -89,9 +89,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCustomerGroupsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListCustomerGroupsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListCustomerGroupsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListGroupsRequest nextRequest = ListGroupsRequest.builder() .from(request) @@ -100,16 +101,14 @@ public SquareClientHttpResponse> list( List result = parsedResponse.getGroups().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -153,17 +152,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateCustomerGroupResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateCustomerGroupResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -198,17 +195,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetCustomerGroupResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCustomerGroupResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -251,17 +246,15 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateCustomerGroupResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateCustomerGroupResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -296,17 +289,15 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteCustomerGroupResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteCustomerGroupResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -347,17 +338,15 @@ public SquareClientHttpResponse add( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AddGroupToCustomerResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, AddGroupToCustomerResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -398,18 +387,15 @@ public SquareClientHttpResponse remove( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RemoveGroupFromCustomerResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RemoveGroupFromCustomerResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/customers/RawSegmentsClient.java b/src/main/java/com/squareup/square/customers/RawSegmentsClient.java index 443ff20a..2475d474 100644 --- a/src/main/java/com/squareup/square/customers/RawSegmentsClient.java +++ b/src/main/java/com/squareup/square/customers/RawSegmentsClient.java @@ -76,9 +76,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListCustomerSegmentsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListCustomerSegmentsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListCustomerSegmentsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListSegmentsRequest nextRequest = ListSegmentsRequest.builder() .from(request) @@ -87,16 +88,14 @@ public SquareClientHttpResponse> list( List result = parsedResponse.getSegments().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -131,17 +130,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetCustomerSegmentResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetCustomerSegmentResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/customers/types/BatchUpsertCustomerCustomAttributesRequest.java b/src/main/java/com/squareup/square/customers/types/BatchUpsertCustomerCustomAttributesRequest.java index dfcab7d7..76d956f3 100644 --- a/src/main/java/com/squareup/square/customers/types/BatchUpsertCustomerCustomAttributesRequest.java +++ b/src/main/java/com/squareup/square/customers/types/BatchUpsertCustomerCustomAttributesRequest.java @@ -96,7 +96,9 @@ public Builder from(BatchUpsertCustomerCustomAttributesRequest other) { public Builder values( Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/devices/AsyncRawCodesClient.java b/src/main/java/com/squareup/square/devices/AsyncRawCodesClient.java index 789026e7..d0f18a14 100644 --- a/src/main/java/com/squareup/square/devices/AsyncRawCodesClient.java +++ b/src/main/java/com/squareup/square/devices/AsyncRawCodesClient.java @@ -97,9 +97,10 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { - ListDeviceCodesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListDeviceCodesResponse.class); + ListDeviceCodesResponse parsedResponse = + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListDeviceCodesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCodesRequest nextRequest = ListCodesRequest.builder() .from(request) @@ -108,24 +109,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getDeviceCodes().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -182,19 +181,16 @@ public CompletableFuture> cre @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateDeviceCodeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateDeviceCodeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -241,18 +237,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetDeviceCodeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetDeviceCodeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/devices/RawCodesClient.java b/src/main/java/com/squareup/square/devices/RawCodesClient.java index 7703d3b4..b29096d8 100644 --- a/src/main/java/com/squareup/square/devices/RawCodesClient.java +++ b/src/main/java/com/squareup/square/devices/RawCodesClient.java @@ -89,9 +89,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListDeviceCodesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListDeviceCodesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListDeviceCodesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCodesRequest nextRequest = ListCodesRequest.builder() .from(request) @@ -100,16 +101,14 @@ public SquareClientHttpResponse> list( List result = parsedResponse.getDeviceCodes().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -153,17 +152,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateDeviceCodeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateDeviceCodeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -197,17 +194,14 @@ public SquareClientHttpResponse get(GetCodesRequest reque } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetDeviceCodeResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetDeviceCodeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/disputes/AsyncRawEvidenceClient.java b/src/main/java/com/squareup/square/disputes/AsyncRawEvidenceClient.java index 1b025396..5c100178 100644 --- a/src/main/java/com/squareup/square/disputes/AsyncRawEvidenceClient.java +++ b/src/main/java/com/squareup/square/disputes/AsyncRawEvidenceClient.java @@ -79,9 +79,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListEvidenceRequest nextRequest = ListEvidenceRequest.builder() .from(request) @@ -90,24 +91,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getEvidence().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -158,19 +157,17 @@ public CompletableFuture> g @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetDisputeEvidenceResponse.class), + responseBodyString, GetDisputeEvidenceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -222,19 +219,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteDisputeEvidenceResponse.class), + responseBodyString, DeleteDisputeEvidenceResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/disputes/RawEvidenceClient.java b/src/main/java/com/squareup/square/disputes/RawEvidenceClient.java index 3ad65ee9..7b1aebda 100644 --- a/src/main/java/com/squareup/square/disputes/RawEvidenceClient.java +++ b/src/main/java/com/squareup/square/disputes/RawEvidenceClient.java @@ -69,9 +69,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListDisputeEvidenceResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListDisputeEvidenceResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListDisputeEvidenceResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListEvidenceRequest nextRequest = ListEvidenceRequest.builder() .from(request) @@ -80,16 +81,14 @@ public SquareClientHttpResponse> list( List result = parsedResponse.getEvidence().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -128,17 +127,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetDisputeEvidenceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetDisputeEvidenceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -177,17 +174,15 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteDisputeEvidenceResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteDisputeEvidenceResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/giftcards/AsyncRawActivitiesClient.java b/src/main/java/com/squareup/square/giftcards/AsyncRawActivitiesClient.java index 2ee0cb90..a8e21fb2 100644 --- a/src/main/java/com/squareup/square/giftcards/AsyncRawActivitiesClient.java +++ b/src/main/java/com/squareup/square/giftcards/AsyncRawActivitiesClient.java @@ -122,9 +122,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListActivitiesRequest nextRequest = ListActivitiesRequest.builder() .from(request) @@ -133,24 +134,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getGiftCardActivities().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -207,19 +206,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateGiftCardActivityResponse.class), + responseBodyString, CreateGiftCardActivityResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/giftcards/RawActivitiesClient.java b/src/main/java/com/squareup/square/giftcards/RawActivitiesClient.java index 0402d6c6..82709420 100644 --- a/src/main/java/com/squareup/square/giftcards/RawActivitiesClient.java +++ b/src/main/java/com/squareup/square/giftcards/RawActivitiesClient.java @@ -112,9 +112,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { - ListGiftCardActivitiesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListGiftCardActivitiesResponse.class); + ListGiftCardActivitiesResponse parsedResponse = + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListGiftCardActivitiesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListActivitiesRequest nextRequest = ListActivitiesRequest.builder() .from(request) @@ -124,16 +125,14 @@ public SquareClientHttpResponse> list( parsedResponse.getGiftCardActivities().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -177,18 +176,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateGiftCardActivityResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateGiftCardActivityResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/labor/AsyncRawBreakTypesClient.java b/src/main/java/com/squareup/square/labor/AsyncRawBreakTypesClient.java index d5f5e80d..fcdcb2b6 100644 --- a/src/main/java/com/squareup/square/labor/AsyncRawBreakTypesClient.java +++ b/src/main/java/com/squareup/square/labor/AsyncRawBreakTypesClient.java @@ -98,9 +98,10 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { - ListBreakTypesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListBreakTypesResponse.class); + ListBreakTypesResponse parsedResponse = + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListBreakTypesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListBreakTypesRequest nextRequest = ListBreakTypesRequest.builder() .from(request) @@ -108,24 +109,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO .build(); List result = parsedResponse.getBreakTypes().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -203,19 +202,16 @@ public CompletableFuture> crea @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateBreakTypeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateBreakTypeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -262,18 +258,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetBreakTypeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetBreakTypeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -328,19 +322,16 @@ public CompletableFuture> upda @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateBreakTypeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateBreakTypeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -390,19 +381,16 @@ public CompletableFuture> dele @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteBreakTypeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteBreakTypeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/labor/AsyncRawEmployeeWagesClient.java b/src/main/java/com/squareup/square/labor/AsyncRawEmployeeWagesClient.java index 610d8227..a922889a 100644 --- a/src/main/java/com/squareup/square/labor/AsyncRawEmployeeWagesClient.java +++ b/src/main/java/com/squareup/square/labor/AsyncRawEmployeeWagesClient.java @@ -90,9 +90,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListEmployeeWagesRequest nextRequest = ListEmployeeWagesRequest.builder() .from(request) @@ -101,24 +102,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getEmployeeWages().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -165,19 +164,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetEmployeeWageResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetEmployeeWageResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/labor/AsyncRawShiftsClient.java b/src/main/java/com/squareup/square/labor/AsyncRawShiftsClient.java index a617eaa5..95e51805 100644 --- a/src/main/java/com/squareup/square/labor/AsyncRawShiftsClient.java +++ b/src/main/java/com/squareup/square/labor/AsyncRawShiftsClient.java @@ -116,18 +116,16 @@ public CompletableFuture> create( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateShiftResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateShiftResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -236,18 +234,16 @@ public CompletableFuture> search( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchShiftsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchShiftsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -294,18 +290,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetShiftResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetShiftResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -368,18 +362,16 @@ public CompletableFuture> update( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateShiftResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateShiftResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -426,18 +418,16 @@ public CompletableFuture> delete( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteShiftResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteShiftResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/labor/AsyncRawTeamMemberWagesClient.java b/src/main/java/com/squareup/square/labor/AsyncRawTeamMemberWagesClient.java index 6e1930ae..03359b33 100644 --- a/src/main/java/com/squareup/square/labor/AsyncRawTeamMemberWagesClient.java +++ b/src/main/java/com/squareup/square/labor/AsyncRawTeamMemberWagesClient.java @@ -90,9 +90,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListTeamMemberWagesRequest nextRequest = ListTeamMemberWagesRequest.builder() .from(request) @@ -101,24 +102,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getTeamMemberWages().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -166,19 +165,17 @@ public CompletableFuture> ge @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetTeamMemberWageResponse.class), + responseBodyString, GetTeamMemberWageResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/labor/AsyncRawWorkweekConfigsClient.java b/src/main/java/com/squareup/square/labor/AsyncRawWorkweekConfigsClient.java index 7af5ef52..3c9a16b7 100644 --- a/src/main/java/com/squareup/square/labor/AsyncRawWorkweekConfigsClient.java +++ b/src/main/java/com/squareup/square/labor/AsyncRawWorkweekConfigsClient.java @@ -89,9 +89,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListWorkweekConfigsRequest nextRequest = ListWorkweekConfigsRequest.builder() .from(request) @@ -100,24 +101,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getWorkweekConfigs().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -173,19 +172,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateWorkweekConfigResponse.class), + responseBodyString, UpdateWorkweekConfigResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/labor/RawBreakTypesClient.java b/src/main/java/com/squareup/square/labor/RawBreakTypesClient.java index ad569e48..e93e2a74 100644 --- a/src/main/java/com/squareup/square/labor/RawBreakTypesClient.java +++ b/src/main/java/com/squareup/square/labor/RawBreakTypesClient.java @@ -89,9 +89,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListBreakTypesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListBreakTypesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListBreakTypesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListBreakTypesRequest nextRequest = ListBreakTypesRequest.builder() .from(request) @@ -99,17 +100,14 @@ public SquareClientHttpResponse> list( .build(); List result = parsedResponse.getBreakTypes().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( - new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) - .body()), + new SyncPagingIterable(startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) + .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -175,17 +173,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateBreakTypeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateBreakTypeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -220,17 +216,14 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetBreakTypeResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetBreakTypeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -273,17 +266,15 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateBreakTypeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateBreakTypeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -320,17 +311,15 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteBreakTypeResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteBreakTypeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/labor/RawEmployeeWagesClient.java b/src/main/java/com/squareup/square/labor/RawEmployeeWagesClient.java index d4f3347a..8188ca44 100644 --- a/src/main/java/com/squareup/square/labor/RawEmployeeWagesClient.java +++ b/src/main/java/com/squareup/square/labor/RawEmployeeWagesClient.java @@ -80,9 +80,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListEmployeeWagesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListEmployeeWagesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListEmployeeWagesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListEmployeeWagesRequest nextRequest = ListEmployeeWagesRequest.builder() .from(request) @@ -91,16 +92,14 @@ public SquareClientHttpResponse> list( List result = parsedResponse.getEmployeeWages().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -135,17 +134,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetEmployeeWageResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetEmployeeWageResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/labor/RawShiftsClient.java b/src/main/java/com/squareup/square/labor/RawShiftsClient.java index cb7387e1..383270e8 100644 --- a/src/main/java/com/squareup/square/labor/RawShiftsClient.java +++ b/src/main/java/com/squareup/square/labor/RawShiftsClient.java @@ -109,17 +109,14 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateShiftResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateShiftResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -216,17 +213,14 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchShiftsResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchShiftsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -260,16 +254,14 @@ public SquareClientHttpResponse get(GetShiftsRequest request, } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetShiftResponse.class), response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetShiftResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -320,17 +312,14 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateShiftResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateShiftResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -365,17 +354,14 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteShiftResponse.class), - response); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteShiftResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/labor/RawTeamMemberWagesClient.java b/src/main/java/com/squareup/square/labor/RawTeamMemberWagesClient.java index 1945ca7d..648b3ed4 100644 --- a/src/main/java/com/squareup/square/labor/RawTeamMemberWagesClient.java +++ b/src/main/java/com/squareup/square/labor/RawTeamMemberWagesClient.java @@ -80,9 +80,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListTeamMemberWagesResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListTeamMemberWagesResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListTeamMemberWagesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListTeamMemberWagesRequest nextRequest = ListTeamMemberWagesRequest.builder() .from(request) @@ -92,16 +93,14 @@ public SquareClientHttpResponse> list( parsedResponse.getTeamMemberWages().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -136,17 +135,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetTeamMemberWageResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetTeamMemberWageResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/labor/RawWorkweekConfigsClient.java b/src/main/java/com/squareup/square/labor/RawWorkweekConfigsClient.java index 0cf4a7b0..04847c13 100644 --- a/src/main/java/com/squareup/square/labor/RawWorkweekConfigsClient.java +++ b/src/main/java/com/squareup/square/labor/RawWorkweekConfigsClient.java @@ -79,9 +79,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListWorkweekConfigsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListWorkweekConfigsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListWorkweekConfigsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListWorkweekConfigsRequest nextRequest = ListWorkweekConfigsRequest.builder() .from(request) @@ -91,16 +92,14 @@ public SquareClientHttpResponse> list( parsedResponse.getWorkweekConfigs().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -143,17 +142,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateWorkweekConfigResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateWorkweekConfigResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/locations/AsyncRawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/locations/AsyncRawCustomAttributeDefinitionsClient.java index 502dc30c..bc39b243 100644 --- a/src/main/java/com/squareup/square/locations/AsyncRawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/locations/AsyncRawCustomAttributeDefinitionsClient.java @@ -108,10 +108,11 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() @@ -122,7 +123,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO parsedResponse.getCustomAttributeDefinitions().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> { + startingAfter.isPresent(), result, parsedResponse, () -> { try { return list(nextRequest, requestOptions) .get() @@ -134,12 +135,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -207,19 +205,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateLocationCustomAttributeDefinitionResponse.class), + responseBodyString, CreateLocationCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -275,19 +271,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveLocationCustomAttributeDefinitionResponse.class), + responseBodyString, RetrieveLocationCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -350,19 +344,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateLocationCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateLocationCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -417,19 +409,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteLocationCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteLocationCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/locations/AsyncRawCustomAttributesClient.java b/src/main/java/com/squareup/square/locations/AsyncRawCustomAttributesClient.java index e7faf18b..5631cef8 100644 --- a/src/main/java/com/squareup/square/locations/AsyncRawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/locations/AsyncRawCustomAttributesClient.java @@ -95,19 +95,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkDeleteLocationCustomAttributesResponse.class), + responseBodyString, BulkDeleteLocationCustomAttributesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -181,19 +179,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkUpsertLocationCustomAttributesResponse.class), + responseBodyString, BulkUpsertLocationCustomAttributesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -268,9 +264,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -279,24 +276,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -362,19 +357,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveLocationCustomAttributeResponse.class), + responseBodyString, RetrieveLocationCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -443,19 +436,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertLocationCustomAttributeResponse.class), + responseBodyString, UpsertLocationCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -510,19 +501,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteLocationCustomAttributeResponse.class), + responseBodyString, DeleteLocationCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/locations/AsyncRawTransactionsClient.java b/src/main/java/com/squareup/square/locations/AsyncRawTransactionsClient.java index 3f2d63ce..da07e208 100644 --- a/src/main/java/com/squareup/square/locations/AsyncRawTransactionsClient.java +++ b/src/main/java/com/squareup/square/locations/AsyncRawTransactionsClient.java @@ -92,19 +92,16 @@ public CompletableFuture> lis @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListTransactionsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListTransactionsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -153,19 +150,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetTransactionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetTransactionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -222,19 +216,17 @@ public CompletableFuture> c @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CaptureTransactionResponse.class), + responseBodyString, CaptureTransactionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -290,19 +282,16 @@ public CompletableFuture> void @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), VoidTransactionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, VoidTransactionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/locations/RawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/locations/RawCustomAttributeDefinitionsClient.java index c4b37159..1d0c4170 100644 --- a/src/main/java/com/squareup/square/locations/RawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/locations/RawCustomAttributeDefinitionsClient.java @@ -99,9 +99,10 @@ public SquareClientHttpResponse> l } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListLocationCustomAttributeDefinitionsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListLocationCustomAttributeDefinitionsResponse.class); + responseBodyString, ListLocationCustomAttributeDefinitionsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() .from(request) @@ -111,16 +112,14 @@ public SquareClientHttpResponse> l parsedResponse.getCustomAttributeDefinitions().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -175,18 +174,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateLocationCustomAttributeDefinitionResponse.class), + responseBodyString, CreateLocationCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -229,18 +226,16 @@ public SquareClientHttpResponse( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveLocationCustomAttributeDefinitionResponse.class), + responseBodyString, RetrieveLocationCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -290,18 +285,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateLocationCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateLocationCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -343,18 +336,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteLocationCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteLocationCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/locations/RawCustomAttributesClient.java b/src/main/java/com/squareup/square/locations/RawCustomAttributesClient.java index 8ed3a68d..f6053b8d 100644 --- a/src/main/java/com/squareup/square/locations/RawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/locations/RawCustomAttributesClient.java @@ -86,18 +86,16 @@ public SquareClientHttpResponse batc } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkDeleteLocationCustomAttributesResponse.class), + responseBodyString, BulkDeleteLocationCustomAttributesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -158,18 +156,16 @@ public SquareClientHttpResponse batc } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkUpsertLocationCustomAttributesResponse.class), + responseBodyString, BulkUpsertLocationCustomAttributesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -230,9 +226,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListLocationCustomAttributesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListLocationCustomAttributesResponse.class); + responseBodyString, ListLocationCustomAttributesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -242,16 +239,14 @@ public SquareClientHttpResponse> list( parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -303,18 +298,16 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveLocationCustomAttributeResponse.class), + responseBodyString, RetrieveLocationCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -370,18 +363,16 @@ public SquareClientHttpResponse upsert( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertLocationCustomAttributeResponse.class), + responseBodyString, UpsertLocationCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -423,18 +414,16 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteLocationCustomAttributeResponse.class), + responseBodyString, DeleteLocationCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/locations/RawTransactionsClient.java b/src/main/java/com/squareup/square/locations/RawTransactionsClient.java index 5f7893c8..bcd6ee37 100644 --- a/src/main/java/com/squareup/square/locations/RawTransactionsClient.java +++ b/src/main/java/com/squareup/square/locations/RawTransactionsClient.java @@ -85,17 +85,15 @@ public SquareClientHttpResponse list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListTransactionsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListTransactionsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -132,17 +130,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetTransactionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetTransactionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -186,17 +182,15 @@ public SquareClientHttpResponse capture( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CaptureTransactionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CaptureTransactionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -240,17 +234,15 @@ public SquareClientHttpResponse void_( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), VoidTransactionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, VoidTransactionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/locations/types/BulkDeleteLocationCustomAttributesRequest.java b/src/main/java/com/squareup/square/locations/types/BulkDeleteLocationCustomAttributesRequest.java index adbac244..e71da171 100644 --- a/src/main/java/com/squareup/square/locations/types/BulkDeleteLocationCustomAttributesRequest.java +++ b/src/main/java/com/squareup/square/locations/types/BulkDeleteLocationCustomAttributesRequest.java @@ -94,7 +94,9 @@ public Builder from(BulkDeleteLocationCustomAttributesRequest other) { public Builder values( Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/locations/types/BulkUpsertLocationCustomAttributesRequest.java b/src/main/java/com/squareup/square/locations/types/BulkUpsertLocationCustomAttributesRequest.java index b7295708..c4b508ce 100644 --- a/src/main/java/com/squareup/square/locations/types/BulkUpsertLocationCustomAttributesRequest.java +++ b/src/main/java/com/squareup/square/locations/types/BulkUpsertLocationCustomAttributesRequest.java @@ -96,7 +96,9 @@ public Builder from(BulkUpsertLocationCustomAttributesRequest other) { public Builder values( Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/loyalty/AsyncRawAccountsClient.java b/src/main/java/com/squareup/square/loyalty/AsyncRawAccountsClient.java index d59c6aaa..65c791ef 100644 --- a/src/main/java/com/squareup/square/loyalty/AsyncRawAccountsClient.java +++ b/src/main/java/com/squareup/square/loyalty/AsyncRawAccountsClient.java @@ -81,19 +81,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateLoyaltyAccountResponse.class), + responseBodyString, CreateLoyaltyAccountResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -161,19 +159,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchLoyaltyAccountsResponse.class), + responseBodyString, SearchLoyaltyAccountsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -220,19 +216,17 @@ public CompletableFuture> ge @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetLoyaltyAccountResponse.class), + responseBodyString, GetLoyaltyAccountResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -323,19 +317,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), AccumulateLoyaltyPointsResponse.class), + responseBodyString, AccumulateLoyaltyPointsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -398,19 +390,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), AdjustLoyaltyPointsResponse.class), + responseBodyString, AdjustLoyaltyPointsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/loyalty/AsyncRawProgramsClient.java b/src/main/java/com/squareup/square/loyalty/AsyncRawProgramsClient.java index 7c9ab89b..d2ba8fa3 100644 --- a/src/main/java/com/squareup/square/loyalty/AsyncRawProgramsClient.java +++ b/src/main/java/com/squareup/square/loyalty/AsyncRawProgramsClient.java @@ -71,19 +71,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListLoyaltyProgramsResponse.class), + responseBodyString, ListLoyaltyProgramsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -132,19 +130,17 @@ public CompletableFuture> ge @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetLoyaltyProgramResponse.class), + responseBodyString, GetLoyaltyProgramResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -237,19 +233,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CalculateLoyaltyPointsResponse.class), + responseBodyString, CalculateLoyaltyPointsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/loyalty/AsyncRawRewardsClient.java b/src/main/java/com/squareup/square/loyalty/AsyncRawRewardsClient.java index 6cfc17da..40311854 100644 --- a/src/main/java/com/squareup/square/loyalty/AsyncRawRewardsClient.java +++ b/src/main/java/com/squareup/square/loyalty/AsyncRawRewardsClient.java @@ -95,19 +95,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateLoyaltyRewardResponse.class), + responseBodyString, CreateLoyaltyRewardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -181,19 +179,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchLoyaltyRewardsResponse.class), + responseBodyString, SearchLoyaltyRewardsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -240,19 +236,16 @@ public CompletableFuture> get @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetLoyaltyRewardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetLoyaltyRewardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -316,19 +309,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteLoyaltyRewardResponse.class), + responseBodyString, DeleteLoyaltyRewardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -399,19 +390,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RedeemLoyaltyRewardResponse.class), + responseBodyString, RedeemLoyaltyRewardResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/loyalty/RawAccountsClient.java b/src/main/java/com/squareup/square/loyalty/RawAccountsClient.java index 739c3cbc..a96aa327 100644 --- a/src/main/java/com/squareup/square/loyalty/RawAccountsClient.java +++ b/src/main/java/com/squareup/square/loyalty/RawAccountsClient.java @@ -73,17 +73,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateLoyaltyAccountResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateLoyaltyAccountResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -138,17 +136,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchLoyaltyAccountsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchLoyaltyAccountsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -183,17 +179,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetLoyaltyAccountResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetLoyaltyAccountResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -272,18 +266,15 @@ public SquareClientHttpResponse accumulatePoint } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), AccumulateLoyaltyPointsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, AccumulateLoyaltyPointsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -333,17 +324,15 @@ public SquareClientHttpResponse adjust( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), AdjustLoyaltyPointsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, AdjustLoyaltyPointsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/loyalty/RawProgramsClient.java b/src/main/java/com/squareup/square/loyalty/RawProgramsClient.java index 2169f723..7917c7c0 100644 --- a/src/main/java/com/squareup/square/loyalty/RawProgramsClient.java +++ b/src/main/java/com/squareup/square/loyalty/RawProgramsClient.java @@ -63,17 +63,15 @@ public SquareClientHttpResponse list(RequestOptions } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListLoyaltyProgramsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListLoyaltyProgramsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -110,17 +108,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetLoyaltyProgramResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetLoyaltyProgramResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -200,18 +196,15 @@ public SquareClientHttpResponse calculate( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CalculateLoyaltyPointsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CalculateLoyaltyPointsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/loyalty/RawRewardsClient.java b/src/main/java/com/squareup/square/loyalty/RawRewardsClient.java index 6e023c40..ab724f8f 100644 --- a/src/main/java/com/squareup/square/loyalty/RawRewardsClient.java +++ b/src/main/java/com/squareup/square/loyalty/RawRewardsClient.java @@ -87,17 +87,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateLoyaltyRewardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateLoyaltyRewardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -158,17 +156,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchLoyaltyRewardsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchLoyaltyRewardsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -203,17 +199,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetLoyaltyRewardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetLoyaltyRewardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -264,17 +258,15 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DeleteLoyaltyRewardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, DeleteLoyaltyRewardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -332,17 +324,15 @@ public SquareClientHttpResponse redeem( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RedeemLoyaltyRewardResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, RedeemLoyaltyRewardResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/loyalty/programs/AsyncRawPromotionsClient.java b/src/main/java/com/squareup/square/loyalty/programs/AsyncRawPromotionsClient.java index 5980e12e..2f386f83 100644 --- a/src/main/java/com/squareup/square/loyalty/programs/AsyncRawPromotionsClient.java +++ b/src/main/java/com/squareup/square/loyalty/programs/AsyncRawPromotionsClient.java @@ -94,9 +94,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListPromotionsRequest nextRequest = ListPromotionsRequest.builder() .from(request) @@ -105,24 +106,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getLoyaltyPromotions().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -187,19 +186,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateLoyaltyPromotionResponse.class), + responseBodyString, CreateLoyaltyPromotionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -248,19 +245,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetLoyaltyPromotionResponse.class), + responseBodyString, GetLoyaltyPromotionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -319,19 +314,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelLoyaltyPromotionResponse.class), + responseBodyString, CancelLoyaltyPromotionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/loyalty/programs/RawPromotionsClient.java b/src/main/java/com/squareup/square/loyalty/programs/RawPromotionsClient.java index d7ebf070..bf2b637e 100644 --- a/src/main/java/com/squareup/square/loyalty/programs/RawPromotionsClient.java +++ b/src/main/java/com/squareup/square/loyalty/programs/RawPromotionsClient.java @@ -84,9 +84,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListLoyaltyPromotionsResponse parsedResponse = - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListLoyaltyPromotionsResponse.class); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListLoyaltyPromotionsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListPromotionsRequest nextRequest = ListPromotionsRequest.builder() .from(request) @@ -96,16 +97,14 @@ public SquareClientHttpResponse> list( parsedResponse.getLoyaltyPromotions().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -157,18 +156,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateLoyaltyPromotionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateLoyaltyPromotionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -205,17 +201,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetLoyaltyPromotionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetLoyaltyPromotionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -261,18 +255,15 @@ public SquareClientHttpResponse cancel( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelLoyaltyPromotionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelLoyaltyPromotionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/merchants/AsyncRawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/merchants/AsyncRawCustomAttributeDefinitionsClient.java index 70cc729b..f7ce62e1 100644 --- a/src/main/java/com/squareup/square/merchants/AsyncRawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/merchants/AsyncRawCustomAttributeDefinitionsClient.java @@ -108,10 +108,11 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() @@ -122,7 +123,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO parsedResponse.getCustomAttributeDefinitions().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> { + startingAfter.isPresent(), result, parsedResponse, () -> { try { return list(nextRequest, requestOptions) .get() @@ -134,12 +135,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -207,19 +205,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateMerchantCustomAttributeDefinitionResponse.class), + responseBodyString, CreateMerchantCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -275,19 +271,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveMerchantCustomAttributeDefinitionResponse.class), + responseBodyString, RetrieveMerchantCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -350,19 +344,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateMerchantCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateMerchantCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -417,19 +409,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteMerchantCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteMerchantCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/merchants/AsyncRawCustomAttributesClient.java b/src/main/java/com/squareup/square/merchants/AsyncRawCustomAttributesClient.java index ac3693e2..2288581c 100644 --- a/src/main/java/com/squareup/square/merchants/AsyncRawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/merchants/AsyncRawCustomAttributesClient.java @@ -95,19 +95,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkDeleteMerchantCustomAttributesResponse.class), + responseBodyString, BulkDeleteMerchantCustomAttributesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -181,19 +179,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkUpsertMerchantCustomAttributesResponse.class), + responseBodyString, BulkUpsertMerchantCustomAttributesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -268,9 +264,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -279,24 +276,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -362,19 +357,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveMerchantCustomAttributeResponse.class), + responseBodyString, RetrieveMerchantCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -443,19 +436,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertMerchantCustomAttributeResponse.class), + responseBodyString, UpsertMerchantCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -510,19 +501,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteMerchantCustomAttributeResponse.class), + responseBodyString, DeleteMerchantCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/merchants/RawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/merchants/RawCustomAttributeDefinitionsClient.java index e267ca5a..74e2cfb5 100644 --- a/src/main/java/com/squareup/square/merchants/RawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/merchants/RawCustomAttributeDefinitionsClient.java @@ -99,9 +99,10 @@ public SquareClientHttpResponse> l } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListMerchantCustomAttributeDefinitionsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListMerchantCustomAttributeDefinitionsResponse.class); + responseBodyString, ListMerchantCustomAttributeDefinitionsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() .from(request) @@ -111,16 +112,14 @@ public SquareClientHttpResponse> l parsedResponse.getCustomAttributeDefinitions().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -175,18 +174,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateMerchantCustomAttributeDefinitionResponse.class), + responseBodyString, CreateMerchantCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -229,18 +226,16 @@ public SquareClientHttpResponse( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveMerchantCustomAttributeDefinitionResponse.class), + responseBodyString, RetrieveMerchantCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -290,18 +285,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateMerchantCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateMerchantCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -343,18 +336,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteMerchantCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteMerchantCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/merchants/RawCustomAttributesClient.java b/src/main/java/com/squareup/square/merchants/RawCustomAttributesClient.java index ecbfefe2..31f56220 100644 --- a/src/main/java/com/squareup/square/merchants/RawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/merchants/RawCustomAttributesClient.java @@ -86,18 +86,16 @@ public SquareClientHttpResponse batc } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkDeleteMerchantCustomAttributesResponse.class), + responseBodyString, BulkDeleteMerchantCustomAttributesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -158,18 +156,16 @@ public SquareClientHttpResponse batc } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkUpsertMerchantCustomAttributesResponse.class), + responseBodyString, BulkUpsertMerchantCustomAttributesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -230,9 +226,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListMerchantCustomAttributesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListMerchantCustomAttributesResponse.class); + responseBodyString, ListMerchantCustomAttributesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -242,16 +239,14 @@ public SquareClientHttpResponse> list( parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -303,18 +298,16 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveMerchantCustomAttributeResponse.class), + responseBodyString, RetrieveMerchantCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -370,18 +363,16 @@ public SquareClientHttpResponse upsert( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertMerchantCustomAttributeResponse.class), + responseBodyString, UpsertMerchantCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -423,18 +414,16 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteMerchantCustomAttributeResponse.class), + responseBodyString, DeleteMerchantCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/merchants/types/BulkDeleteMerchantCustomAttributesRequest.java b/src/main/java/com/squareup/square/merchants/types/BulkDeleteMerchantCustomAttributesRequest.java index 27fa41d1..54170ddf 100644 --- a/src/main/java/com/squareup/square/merchants/types/BulkDeleteMerchantCustomAttributesRequest.java +++ b/src/main/java/com/squareup/square/merchants/types/BulkDeleteMerchantCustomAttributesRequest.java @@ -94,7 +94,9 @@ public Builder from(BulkDeleteMerchantCustomAttributesRequest other) { public Builder values( Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/merchants/types/BulkUpsertMerchantCustomAttributesRequest.java b/src/main/java/com/squareup/square/merchants/types/BulkUpsertMerchantCustomAttributesRequest.java index 44298902..938c708a 100644 --- a/src/main/java/com/squareup/square/merchants/types/BulkUpsertMerchantCustomAttributesRequest.java +++ b/src/main/java/com/squareup/square/merchants/types/BulkUpsertMerchantCustomAttributesRequest.java @@ -96,7 +96,9 @@ public Builder from(BulkUpsertMerchantCustomAttributesRequest other) { public Builder values( Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/orders/AsyncRawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/orders/AsyncRawCustomAttributeDefinitionsClient.java index eceac8ab..eeea4042 100644 --- a/src/main/java/com/squareup/square/orders/AsyncRawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/orders/AsyncRawCustomAttributeDefinitionsClient.java @@ -110,10 +110,11 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() @@ -123,7 +124,7 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getCustomAttributeDefinitions(); future.complete(new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> { + startingAfter.isPresent(), result, parsedResponse, () -> { try { return list(nextRequest, requestOptions) .get() @@ -135,12 +136,9 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -202,19 +200,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateOrderCustomAttributeDefinitionResponse.class), + responseBodyString, CreateOrderCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -272,19 +268,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveOrderCustomAttributeDefinitionResponse.class), + responseBodyString, RetrieveOrderCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -343,19 +337,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateOrderCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateOrderCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -406,19 +398,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteOrderCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteOrderCustomAttributeDefinitionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/orders/AsyncRawCustomAttributesClient.java b/src/main/java/com/squareup/square/orders/AsyncRawCustomAttributesClient.java index 853a9f40..b2bc51ab 100644 --- a/src/main/java/com/squareup/square/orders/AsyncRawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/orders/AsyncRawCustomAttributesClient.java @@ -111,19 +111,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkDeleteOrderCustomAttributesResponse.class), + responseBodyString, BulkDeleteOrderCustomAttributesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -199,19 +197,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkUpsertOrderCustomAttributesResponse.class), + responseBodyString, BulkUpsertOrderCustomAttributesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -286,9 +282,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -297,24 +294,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -382,19 +377,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveOrderCustomAttributeResponse.class), + responseBodyString, RetrieveOrderCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -465,19 +458,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertOrderCustomAttributeResponse.class), + responseBodyString, UpsertOrderCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -534,19 +525,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteOrderCustomAttributeResponse.class), + responseBodyString, DeleteOrderCustomAttributeResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/orders/RawCustomAttributeDefinitionsClient.java b/src/main/java/com/squareup/square/orders/RawCustomAttributeDefinitionsClient.java index 1c1e2189..83eb4339 100644 --- a/src/main/java/com/squareup/square/orders/RawCustomAttributeDefinitionsClient.java +++ b/src/main/java/com/squareup/square/orders/RawCustomAttributeDefinitionsClient.java @@ -101,9 +101,10 @@ public SquareClientHttpResponse> l } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListOrderCustomAttributeDefinitionsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListOrderCustomAttributeDefinitionsResponse.class); + responseBodyString, ListOrderCustomAttributeDefinitionsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributeDefinitionsRequest nextRequest = ListCustomAttributeDefinitionsRequest.builder() .from(request) @@ -112,16 +113,14 @@ public SquareClientHttpResponse> l List result = parsedResponse.getCustomAttributeDefinitions(); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -170,18 +169,16 @@ public SquareClientHttpResponse cr } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateOrderCustomAttributeDefinitionResponse.class), + responseBodyString, CreateOrderCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -226,18 +223,16 @@ public SquareClientHttpResponse } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveOrderCustomAttributeDefinitionResponse.class), + responseBodyString, RetrieveOrderCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -283,18 +278,16 @@ public SquareClientHttpResponse up } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateOrderCustomAttributeDefinitionResponse.class), + responseBodyString, UpdateOrderCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -332,18 +325,16 @@ public SquareClientHttpResponse de } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteOrderCustomAttributeDefinitionResponse.class), + responseBodyString, DeleteOrderCustomAttributeDefinitionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/orders/RawCustomAttributesClient.java b/src/main/java/com/squareup/square/orders/RawCustomAttributesClient.java index be51835d..d5f62c0c 100644 --- a/src/main/java/com/squareup/square/orders/RawCustomAttributesClient.java +++ b/src/main/java/com/squareup/square/orders/RawCustomAttributesClient.java @@ -102,18 +102,16 @@ public SquareClientHttpResponse batchDe } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkDeleteOrderCustomAttributesResponse.class), + responseBodyString, BulkDeleteOrderCustomAttributesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -176,18 +174,16 @@ public SquareClientHttpResponse batchUp } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), BulkUpsertOrderCustomAttributesResponse.class), + responseBodyString, BulkUpsertOrderCustomAttributesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -248,9 +244,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { ListOrderCustomAttributesResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListOrderCustomAttributesResponse.class); + responseBodyString, ListOrderCustomAttributesResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListCustomAttributesRequest nextRequest = ListCustomAttributesRequest.builder() .from(request) @@ -260,16 +257,14 @@ public SquareClientHttpResponse> list( parsedResponse.getCustomAttributes().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -323,18 +318,16 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), RetrieveOrderCustomAttributeResponse.class), + responseBodyString, RetrieveOrderCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -392,18 +385,16 @@ public SquareClientHttpResponse upsert( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpsertOrderCustomAttributeResponse.class), + responseBodyString, UpsertOrderCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -446,18 +437,16 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteOrderCustomAttributeResponse.class), + responseBodyString, DeleteOrderCustomAttributeResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/orders/types/BulkDeleteOrderCustomAttributesRequest.java b/src/main/java/com/squareup/square/orders/types/BulkDeleteOrderCustomAttributesRequest.java index 2cc04122..2d1e890c 100644 --- a/src/main/java/com/squareup/square/orders/types/BulkDeleteOrderCustomAttributesRequest.java +++ b/src/main/java/com/squareup/square/orders/types/BulkDeleteOrderCustomAttributesRequest.java @@ -90,7 +90,9 @@ public Builder from(BulkDeleteOrderCustomAttributesRequest other) { @JsonSetter(value = "values", nulls = Nulls.SKIP) public Builder values(Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/orders/types/BulkUpsertOrderCustomAttributesRequest.java b/src/main/java/com/squareup/square/orders/types/BulkUpsertOrderCustomAttributesRequest.java index e3398e6a..38f2c257 100644 --- a/src/main/java/com/squareup/square/orders/types/BulkUpsertOrderCustomAttributesRequest.java +++ b/src/main/java/com/squareup/square/orders/types/BulkUpsertOrderCustomAttributesRequest.java @@ -90,7 +90,9 @@ public Builder from(BulkUpsertOrderCustomAttributesRequest other) { @JsonSetter(value = "values", nulls = Nulls.SKIP) public Builder values(Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/teammembers/AsyncRawWageSettingClient.java b/src/main/java/com/squareup/square/teammembers/AsyncRawWageSettingClient.java index ab9534bc..ecaa1bfb 100644 --- a/src/main/java/com/squareup/square/teammembers/AsyncRawWageSettingClient.java +++ b/src/main/java/com/squareup/square/teammembers/AsyncRawWageSettingClient.java @@ -76,19 +76,16 @@ public CompletableFuture> get( @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetWageSettingResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetWageSettingResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -157,19 +154,17 @@ public CompletableFuture> up @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateWageSettingResponse.class), + responseBodyString, UpdateWageSettingResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/teammembers/RawWageSettingClient.java b/src/main/java/com/squareup/square/teammembers/RawWageSettingClient.java index e36e7b55..f8de4da6 100644 --- a/src/main/java/com/squareup/square/teammembers/RawWageSettingClient.java +++ b/src/main/java/com/squareup/square/teammembers/RawWageSettingClient.java @@ -69,17 +69,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetWageSettingResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetWageSettingResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -135,17 +133,15 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateWageSettingResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, UpdateWageSettingResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/terminal/AsyncRawActionsClient.java b/src/main/java/com/squareup/square/terminal/AsyncRawActionsClient.java index 3a4921e5..d48a5855 100644 --- a/src/main/java/com/squareup/square/terminal/AsyncRawActionsClient.java +++ b/src/main/java/com/squareup/square/terminal/AsyncRawActionsClient.java @@ -79,19 +79,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateTerminalActionResponse.class), + responseBodyString, CreateTerminalActionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -153,19 +151,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchTerminalActionsResponse.class), + responseBodyString, SearchTerminalActionsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -212,19 +208,17 @@ public CompletableFuture> ge @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetTerminalActionResponse.class), + responseBodyString, GetTerminalActionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -273,19 +267,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelTerminalActionResponse.class), + responseBodyString, CancelTerminalActionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/terminal/AsyncRawCheckoutsClient.java b/src/main/java/com/squareup/square/terminal/AsyncRawCheckoutsClient.java index cc5d98a8..acc75dfd 100644 --- a/src/main/java/com/squareup/square/terminal/AsyncRawCheckoutsClient.java +++ b/src/main/java/com/squareup/square/terminal/AsyncRawCheckoutsClient.java @@ -81,19 +81,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateTerminalCheckoutResponse.class), + responseBodyString, CreateTerminalCheckoutResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -155,19 +153,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchTerminalCheckoutsResponse.class), + responseBodyString, SearchTerminalCheckoutsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -214,19 +210,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetTerminalCheckoutResponse.class), + responseBodyString, GetTerminalCheckoutResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -275,19 +269,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelTerminalCheckoutResponse.class), + responseBodyString, CancelTerminalCheckoutResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/terminal/AsyncRawRefundsClient.java b/src/main/java/com/squareup/square/terminal/AsyncRawRefundsClient.java index af289d51..3d031fa0 100644 --- a/src/main/java/com/squareup/square/terminal/AsyncRawRefundsClient.java +++ b/src/main/java/com/squareup/square/terminal/AsyncRawRefundsClient.java @@ -79,19 +79,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateTerminalRefundResponse.class), + responseBodyString, CreateTerminalRefundResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -153,19 +151,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchTerminalRefundsResponse.class), + responseBodyString, SearchTerminalRefundsResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -212,19 +208,17 @@ public CompletableFuture> ge @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetTerminalRefundResponse.class), + responseBodyString, GetTerminalRefundResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -273,19 +267,17 @@ public CompletableFuture> @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelTerminalRefundResponse.class), + responseBodyString, CancelTerminalRefundResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/terminal/RawActionsClient.java b/src/main/java/com/squareup/square/terminal/RawActionsClient.java index 928149b2..5268ac08 100644 --- a/src/main/java/com/squareup/square/terminal/RawActionsClient.java +++ b/src/main/java/com/squareup/square/terminal/RawActionsClient.java @@ -71,17 +71,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateTerminalActionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateTerminalActionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -130,17 +128,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchTerminalActionsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchTerminalActionsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -175,17 +171,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetTerminalActionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetTerminalActionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -221,17 +215,15 @@ public SquareClientHttpResponse cancel( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelTerminalActionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelTerminalActionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/terminal/RawCheckoutsClient.java b/src/main/java/com/squareup/square/terminal/RawCheckoutsClient.java index 6e30ccd4..132c05c6 100644 --- a/src/main/java/com/squareup/square/terminal/RawCheckoutsClient.java +++ b/src/main/java/com/squareup/square/terminal/RawCheckoutsClient.java @@ -73,18 +73,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateTerminalCheckoutResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateTerminalCheckoutResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -133,18 +130,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), SearchTerminalCheckoutsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchTerminalCheckoutsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -179,17 +173,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetTerminalCheckoutResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetTerminalCheckoutResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -225,18 +217,15 @@ public SquareClientHttpResponse cancel( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CancelTerminalCheckoutResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelTerminalCheckoutResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/terminal/RawRefundsClient.java b/src/main/java/com/squareup/square/terminal/RawRefundsClient.java index 0962cf47..0ccafc2c 100644 --- a/src/main/java/com/squareup/square/terminal/RawRefundsClient.java +++ b/src/main/java/com/squareup/square/terminal/RawRefundsClient.java @@ -71,17 +71,15 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateTerminalRefundResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CreateTerminalRefundResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -130,17 +128,15 @@ public SquareClientHttpResponse search( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SearchTerminalRefundsResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, SearchTerminalRefundsResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -175,17 +171,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetTerminalRefundResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetTerminalRefundResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -221,17 +215,15 @@ public SquareClientHttpResponse cancel( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CancelTerminalRefundResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, CancelTerminalRefundResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/types/BatchCreateTeamMembersRequest.java b/src/main/java/com/squareup/square/types/BatchCreateTeamMembersRequest.java index 59f24297..da498ff0 100644 --- a/src/main/java/com/squareup/square/types/BatchCreateTeamMembersRequest.java +++ b/src/main/java/com/squareup/square/types/BatchCreateTeamMembersRequest.java @@ -93,7 +93,9 @@ public Builder from(BatchCreateTeamMembersRequest other) { @JsonSetter(value = "team_members", nulls = Nulls.SKIP) public Builder teamMembers(Map teamMembers) { this.teamMembers.clear(); - this.teamMembers.putAll(teamMembers); + if (teamMembers != null) { + this.teamMembers.putAll(teamMembers); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BatchCreateVendorsRequest.java b/src/main/java/com/squareup/square/types/BatchCreateVendorsRequest.java index 5b3c3887..c1ddbc7e 100644 --- a/src/main/java/com/squareup/square/types/BatchCreateVendorsRequest.java +++ b/src/main/java/com/squareup/square/types/BatchCreateVendorsRequest.java @@ -86,7 +86,9 @@ public Builder from(BatchCreateVendorsRequest other) { @JsonSetter(value = "vendors", nulls = Nulls.SKIP) public Builder vendors(Map vendors) { this.vendors.clear(); - this.vendors.putAll(vendors); + if (vendors != null) { + this.vendors.putAll(vendors); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BatchDeleteCatalogObjectsRequest.java b/src/main/java/com/squareup/square/types/BatchDeleteCatalogObjectsRequest.java index 036a81cc..55025c3f 100644 --- a/src/main/java/com/squareup/square/types/BatchDeleteCatalogObjectsRequest.java +++ b/src/main/java/com/squareup/square/types/BatchDeleteCatalogObjectsRequest.java @@ -91,7 +91,9 @@ public Builder from(BatchDeleteCatalogObjectsRequest other) { @JsonSetter(value = "object_ids", nulls = Nulls.SKIP) public Builder objectIds(List objectIds) { this.objectIds.clear(); - this.objectIds.addAll(objectIds); + if (objectIds != null) { + this.objectIds.addAll(objectIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BatchGetCatalogObjectsRequest.java b/src/main/java/com/squareup/square/types/BatchGetCatalogObjectsRequest.java index 9786cb3b..0e11ebf7 100644 --- a/src/main/java/com/squareup/square/types/BatchGetCatalogObjectsRequest.java +++ b/src/main/java/com/squareup/square/types/BatchGetCatalogObjectsRequest.java @@ -215,7 +215,9 @@ public Builder from(BatchGetCatalogObjectsRequest other) { @JsonSetter(value = "object_ids", nulls = Nulls.SKIP) public Builder objectIds(List objectIds) { this.objectIds.clear(); - this.objectIds.addAll(objectIds); + if (objectIds != null) { + this.objectIds.addAll(objectIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BatchGetOrdersRequest.java b/src/main/java/com/squareup/square/types/BatchGetOrdersRequest.java index 5271fa1a..f0f05773 100644 --- a/src/main/java/com/squareup/square/types/BatchGetOrdersRequest.java +++ b/src/main/java/com/squareup/square/types/BatchGetOrdersRequest.java @@ -142,7 +142,9 @@ public Builder locationId(Nullable locationId) { @JsonSetter(value = "order_ids", nulls = Nulls.SKIP) public Builder orderIds(List orderIds) { this.orderIds.clear(); - this.orderIds.addAll(orderIds); + if (orderIds != null) { + this.orderIds.addAll(orderIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BatchUpdateTeamMembersRequest.java b/src/main/java/com/squareup/square/types/BatchUpdateTeamMembersRequest.java index d7da6c1a..0aabc1de 100644 --- a/src/main/java/com/squareup/square/types/BatchUpdateTeamMembersRequest.java +++ b/src/main/java/com/squareup/square/types/BatchUpdateTeamMembersRequest.java @@ -95,7 +95,9 @@ public Builder from(BatchUpdateTeamMembersRequest other) { @JsonSetter(value = "team_members", nulls = Nulls.SKIP) public Builder teamMembers(Map teamMembers) { this.teamMembers.clear(); - this.teamMembers.putAll(teamMembers); + if (teamMembers != null) { + this.teamMembers.putAll(teamMembers); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BatchUpdateVendorsRequest.java b/src/main/java/com/squareup/square/types/BatchUpdateVendorsRequest.java index a4333e60..a13343bf 100644 --- a/src/main/java/com/squareup/square/types/BatchUpdateVendorsRequest.java +++ b/src/main/java/com/squareup/square/types/BatchUpdateVendorsRequest.java @@ -89,7 +89,9 @@ public Builder from(BatchUpdateVendorsRequest other) { @JsonSetter(value = "vendors", nulls = Nulls.SKIP) public Builder vendors(Map vendors) { this.vendors.clear(); - this.vendors.putAll(vendors); + if (vendors != null) { + this.vendors.putAll(vendors); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BatchUpsertCatalogObjectsRequest.java b/src/main/java/com/squareup/square/types/BatchUpsertCatalogObjectsRequest.java index 05fb7e82..821c0b01 100644 --- a/src/main/java/com/squareup/square/types/BatchUpsertCatalogObjectsRequest.java +++ b/src/main/java/com/squareup/square/types/BatchUpsertCatalogObjectsRequest.java @@ -264,7 +264,9 @@ public _FinalStage addBatches(CatalogObjectBatch batches) { @JsonSetter(value = "batches", nulls = Nulls.SKIP) public _FinalStage batches(List batches) { this.batches.clear(); - this.batches.addAll(batches); + if (batches != null) { + this.batches.addAll(batches); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkCreateCustomersRequest.java b/src/main/java/com/squareup/square/types/BulkCreateCustomersRequest.java index 91aa73d1..3662ef66 100644 --- a/src/main/java/com/squareup/square/types/BulkCreateCustomersRequest.java +++ b/src/main/java/com/squareup/square/types/BulkCreateCustomersRequest.java @@ -95,7 +95,9 @@ public Builder from(BulkCreateCustomersRequest other) { @JsonSetter(value = "customers", nulls = Nulls.SKIP) public Builder customers(Map customers) { this.customers.clear(); - this.customers.putAll(customers); + if (customers != null) { + this.customers.putAll(customers); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkDeleteCustomersRequest.java b/src/main/java/com/squareup/square/types/BulkDeleteCustomersRequest.java index d23c295c..94a6db7a 100644 --- a/src/main/java/com/squareup/square/types/BulkDeleteCustomersRequest.java +++ b/src/main/java/com/squareup/square/types/BulkDeleteCustomersRequest.java @@ -87,7 +87,9 @@ public Builder from(BulkDeleteCustomersRequest other) { @JsonSetter(value = "customer_ids", nulls = Nulls.SKIP) public Builder customerIds(List customerIds) { this.customerIds.clear(); - this.customerIds.addAll(customerIds); + if (customerIds != null) { + this.customerIds.addAll(customerIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkDeleteLocationCustomAttributesResponse.java b/src/main/java/com/squareup/square/types/BulkDeleteLocationCustomAttributesResponse.java index e3acb8b2..c5dc0297 100644 --- a/src/main/java/com/squareup/square/types/BulkDeleteLocationCustomAttributesResponse.java +++ b/src/main/java/com/squareup/square/types/BulkDeleteLocationCustomAttributesResponse.java @@ -110,7 +110,9 @@ public Builder from(BulkDeleteLocationCustomAttributesResponse other) { public Builder values( Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkDeleteMerchantCustomAttributesResponse.java b/src/main/java/com/squareup/square/types/BulkDeleteMerchantCustomAttributesResponse.java index e82dcffa..0bf80b50 100644 --- a/src/main/java/com/squareup/square/types/BulkDeleteMerchantCustomAttributesResponse.java +++ b/src/main/java/com/squareup/square/types/BulkDeleteMerchantCustomAttributesResponse.java @@ -110,7 +110,9 @@ public Builder from(BulkDeleteMerchantCustomAttributesResponse other) { public Builder values( Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkDeleteOrderCustomAttributesResponse.java b/src/main/java/com/squareup/square/types/BulkDeleteOrderCustomAttributesResponse.java index 1c9d4ff5..216aa37a 100644 --- a/src/main/java/com/squareup/square/types/BulkDeleteOrderCustomAttributesResponse.java +++ b/src/main/java/com/squareup/square/types/BulkDeleteOrderCustomAttributesResponse.java @@ -122,7 +122,9 @@ public Builder errors(List errors) { @JsonSetter(value = "values", nulls = Nulls.SKIP) public Builder values(Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkPublishScheduledShiftsRequest.java b/src/main/java/com/squareup/square/types/BulkPublishScheduledShiftsRequest.java index fb2a523c..4e6f26da 100644 --- a/src/main/java/com/squareup/square/types/BulkPublishScheduledShiftsRequest.java +++ b/src/main/java/com/squareup/square/types/BulkPublishScheduledShiftsRequest.java @@ -118,7 +118,9 @@ public Builder from(BulkPublishScheduledShiftsRequest other) { @JsonSetter(value = "scheduled_shifts", nulls = Nulls.SKIP) public Builder scheduledShifts(Map scheduledShifts) { this.scheduledShifts.clear(); - this.scheduledShifts.putAll(scheduledShifts); + if (scheduledShifts != null) { + this.scheduledShifts.putAll(scheduledShifts); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkRetrieveBookingsRequest.java b/src/main/java/com/squareup/square/types/BulkRetrieveBookingsRequest.java index d2fe499d..36a52782 100644 --- a/src/main/java/com/squareup/square/types/BulkRetrieveBookingsRequest.java +++ b/src/main/java/com/squareup/square/types/BulkRetrieveBookingsRequest.java @@ -87,7 +87,9 @@ public Builder from(BulkRetrieveBookingsRequest other) { @JsonSetter(value = "booking_ids", nulls = Nulls.SKIP) public Builder bookingIds(List bookingIds) { this.bookingIds.clear(); - this.bookingIds.addAll(bookingIds); + if (bookingIds != null) { + this.bookingIds.addAll(bookingIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkRetrieveChannelsRequest.java b/src/main/java/com/squareup/square/types/BulkRetrieveChannelsRequest.java index 7555331f..c2fe07e4 100644 --- a/src/main/java/com/squareup/square/types/BulkRetrieveChannelsRequest.java +++ b/src/main/java/com/squareup/square/types/BulkRetrieveChannelsRequest.java @@ -81,7 +81,9 @@ public Builder from(BulkRetrieveChannelsRequest other) { @JsonSetter(value = "channel_ids", nulls = Nulls.SKIP) public Builder channelIds(List channelIds) { this.channelIds.clear(); - this.channelIds.addAll(channelIds); + if (channelIds != null) { + this.channelIds.addAll(channelIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkRetrieveCustomersRequest.java b/src/main/java/com/squareup/square/types/BulkRetrieveCustomersRequest.java index 84ef9595..d09d15dd 100644 --- a/src/main/java/com/squareup/square/types/BulkRetrieveCustomersRequest.java +++ b/src/main/java/com/squareup/square/types/BulkRetrieveCustomersRequest.java @@ -87,7 +87,9 @@ public Builder from(BulkRetrieveCustomersRequest other) { @JsonSetter(value = "customer_ids", nulls = Nulls.SKIP) public Builder customerIds(List customerIds) { this.customerIds.clear(); - this.customerIds.addAll(customerIds); + if (customerIds != null) { + this.customerIds.addAll(customerIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkRetrieveTeamMemberBookingProfilesRequest.java b/src/main/java/com/squareup/square/types/BulkRetrieveTeamMemberBookingProfilesRequest.java index a7e39d48..599ebeb7 100644 --- a/src/main/java/com/squareup/square/types/BulkRetrieveTeamMemberBookingProfilesRequest.java +++ b/src/main/java/com/squareup/square/types/BulkRetrieveTeamMemberBookingProfilesRequest.java @@ -89,7 +89,9 @@ public Builder from(BulkRetrieveTeamMemberBookingProfilesRequest other) { @JsonSetter(value = "team_member_ids", nulls = Nulls.SKIP) public Builder teamMemberIds(List teamMemberIds) { this.teamMemberIds.clear(); - this.teamMemberIds.addAll(teamMemberIds); + if (teamMemberIds != null) { + this.teamMemberIds.addAll(teamMemberIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkUpdateCustomersRequest.java b/src/main/java/com/squareup/square/types/BulkUpdateCustomersRequest.java index 8cb205d1..1d20a960 100644 --- a/src/main/java/com/squareup/square/types/BulkUpdateCustomersRequest.java +++ b/src/main/java/com/squareup/square/types/BulkUpdateCustomersRequest.java @@ -97,7 +97,9 @@ public Builder from(BulkUpdateCustomersRequest other) { @JsonSetter(value = "customers", nulls = Nulls.SKIP) public Builder customers(Map customers) { this.customers.clear(); - this.customers.putAll(customers); + if (customers != null) { + this.customers.putAll(customers); + } return this; } diff --git a/src/main/java/com/squareup/square/types/BulkUpsertOrderCustomAttributesResponse.java b/src/main/java/com/squareup/square/types/BulkUpsertOrderCustomAttributesResponse.java index 91a370e1..117b3b68 100644 --- a/src/main/java/com/squareup/square/types/BulkUpsertOrderCustomAttributesResponse.java +++ b/src/main/java/com/squareup/square/types/BulkUpsertOrderCustomAttributesResponse.java @@ -120,7 +120,9 @@ public Builder errors(List errors) { @JsonSetter(value = "values", nulls = Nulls.SKIP) public Builder values(Map values) { this.values.clear(); - this.values.putAll(values); + if (values != null) { + this.values.putAll(values); + } return this; } diff --git a/src/main/java/com/squareup/square/types/CatalogCustomAttributeDefinition.java b/src/main/java/com/squareup/square/types/CatalogCustomAttributeDefinition.java index a3ed69c8..a6f1f2b1 100644 --- a/src/main/java/com/squareup/square/types/CatalogCustomAttributeDefinition.java +++ b/src/main/java/com/squareup/square/types/CatalogCustomAttributeDefinition.java @@ -674,7 +674,9 @@ public _FinalStage addAllowedObjectTypes(CatalogObjectType allowedObjectTypes) { @JsonSetter(value = "allowed_object_types", nulls = Nulls.SKIP) public _FinalStage allowedObjectTypes(List allowedObjectTypes) { this.allowedObjectTypes.clear(); - this.allowedObjectTypes.addAll(allowedObjectTypes); + if (allowedObjectTypes != null) { + this.allowedObjectTypes.addAll(allowedObjectTypes); + } return this; } diff --git a/src/main/java/com/squareup/square/types/CatalogObjectBatch.java b/src/main/java/com/squareup/square/types/CatalogObjectBatch.java index 462a8c53..932980fa 100644 --- a/src/main/java/com/squareup/square/types/CatalogObjectBatch.java +++ b/src/main/java/com/squareup/square/types/CatalogObjectBatch.java @@ -87,7 +87,9 @@ public Builder from(CatalogObjectBatch other) { @JsonSetter(value = "objects", nulls = Nulls.SKIP) public Builder objects(List objects) { this.objects.clear(); - this.objects.addAll(objects); + if (objects != null) { + this.objects.addAll(objects); + } return this; } diff --git a/src/main/java/com/squareup/square/types/CatalogObjectCategory.java b/src/main/java/com/squareup/square/types/CatalogObjectCategory.java index a3b25071..f761ff86 100644 --- a/src/main/java/com/squareup/square/types/CatalogObjectCategory.java +++ b/src/main/java/com/squareup/square/types/CatalogObjectCategory.java @@ -28,6 +28,8 @@ public final class CatalogObjectCategory { private final Optional ordinal; + private final Optional type; + private final Optional categoryData; private final Optional updatedAt; @@ -53,6 +55,7 @@ public final class CatalogObjectCategory { private CatalogObjectCategory( Optional id, Optional ordinal, + Optional type, Optional categoryData, Optional updatedAt, Optional version, @@ -66,6 +69,7 @@ private CatalogObjectCategory( Map additionalProperties) { this.id = id; this.ordinal = ordinal; + this.type = type; this.categoryData = categoryData; this.updatedAt = updatedAt; this.version = version; @@ -98,6 +102,11 @@ public Optional getOrdinal() { return ordinal; } + @JsonProperty("type") + public Optional getType() { + return type; + } + /** * @return Structured data for a CatalogCategory, set for CatalogObjects of type CATEGORY. */ @@ -220,6 +229,7 @@ public Map getAdditionalProperties() { private boolean equalTo(CatalogObjectCategory other) { return id.equals(other.id) && ordinal.equals(other.ordinal) + && type.equals(other.type) && categoryData.equals(other.categoryData) && updatedAt.equals(other.updatedAt) && version.equals(other.version) @@ -237,6 +247,7 @@ public int hashCode() { return Objects.hash( this.id, this.ordinal, + this.type, this.categoryData, this.updatedAt, this.version, @@ -264,6 +275,8 @@ public static final class Builder { private Optional ordinal = Optional.empty(); + private Optional type = Optional.empty(); + private Optional categoryData = Optional.empty(); private Optional updatedAt = Optional.empty(); @@ -292,6 +305,7 @@ private Builder() {} public Builder from(CatalogObjectCategory other) { id(other.getId()); ordinal(other.getOrdinal()); + type(other.getType()); categoryData(other.getCategoryData()); updatedAt(other.getUpdatedAt()); version(other.getVersion()); @@ -344,6 +358,17 @@ public Builder ordinal(Nullable ordinal) { return this; } + @JsonSetter(value = "type", nulls = Nulls.SKIP) + public Builder type(Optional type) { + this.type = type; + return this; + } + + public Builder type(String type) { + this.type = Optional.ofNullable(type); + return this; + } + /** *

Structured data for a CatalogCategory, set for CatalogObjects of type CATEGORY.

*/ @@ -510,6 +535,7 @@ public CatalogObjectCategory build() { return new CatalogObjectCategory( id, ordinal, + type, categoryData, updatedAt, version, diff --git a/src/main/java/com/squareup/square/types/CatalogQueryItemsForModifierList.java b/src/main/java/com/squareup/square/types/CatalogQueryItemsForModifierList.java index 48dd389e..725f492f 100644 --- a/src/main/java/com/squareup/square/types/CatalogQueryItemsForModifierList.java +++ b/src/main/java/com/squareup/square/types/CatalogQueryItemsForModifierList.java @@ -87,7 +87,9 @@ public Builder from(CatalogQueryItemsForModifierList other) { @JsonSetter(value = "modifier_list_ids", nulls = Nulls.SKIP) public Builder modifierListIds(List modifierListIds) { this.modifierListIds.clear(); - this.modifierListIds.addAll(modifierListIds); + if (modifierListIds != null) { + this.modifierListIds.addAll(modifierListIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/CatalogQueryItemsForTax.java b/src/main/java/com/squareup/square/types/CatalogQueryItemsForTax.java index b608323b..0023fa4e 100644 --- a/src/main/java/com/squareup/square/types/CatalogQueryItemsForTax.java +++ b/src/main/java/com/squareup/square/types/CatalogQueryItemsForTax.java @@ -87,7 +87,9 @@ public Builder from(CatalogQueryItemsForTax other) { @JsonSetter(value = "tax_ids", nulls = Nulls.SKIP) public Builder taxIds(List taxIds) { this.taxIds.clear(); - this.taxIds.addAll(taxIds); + if (taxIds != null) { + this.taxIds.addAll(taxIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/CatalogQuerySet.java b/src/main/java/com/squareup/square/types/CatalogQuerySet.java index 47bb2459..ebf9d589 100644 --- a/src/main/java/com/squareup/square/types/CatalogQuerySet.java +++ b/src/main/java/com/squareup/square/types/CatalogQuerySet.java @@ -166,7 +166,9 @@ public _FinalStage addAttributeValues(String attributeValues) { @JsonSetter(value = "attribute_values", nulls = Nulls.SKIP) public _FinalStage attributeValues(List attributeValues) { this.attributeValues.clear(); - this.attributeValues.addAll(attributeValues); + if (attributeValues != null) { + this.attributeValues.addAll(attributeValues); + } return this; } diff --git a/src/main/java/com/squareup/square/types/CatalogQueryText.java b/src/main/java/com/squareup/square/types/CatalogQueryText.java index efd5ea15..37cb61a8 100644 --- a/src/main/java/com/squareup/square/types/CatalogQueryText.java +++ b/src/main/java/com/squareup/square/types/CatalogQueryText.java @@ -87,7 +87,9 @@ public Builder from(CatalogQueryText other) { @JsonSetter(value = "keywords", nulls = Nulls.SKIP) public Builder keywords(List keywords) { this.keywords.clear(); - this.keywords.addAll(keywords); + if (keywords != null) { + this.keywords.addAll(keywords); + } return this; } diff --git a/src/main/java/com/squareup/square/types/CatalogSubscriptionPlanVariation.java b/src/main/java/com/squareup/square/types/CatalogSubscriptionPlanVariation.java index 4f18f98b..a1706694 100644 --- a/src/main/java/com/squareup/square/types/CatalogSubscriptionPlanVariation.java +++ b/src/main/java/com/squareup/square/types/CatalogSubscriptionPlanVariation.java @@ -464,7 +464,9 @@ public _FinalStage addPhases(SubscriptionPhase phases) { @JsonSetter(value = "phases", nulls = Nulls.SKIP) public _FinalStage phases(List phases) { this.phases.clear(); - this.phases.addAll(phases); + if (phases != null) { + this.phases.addAll(phases); + } return this; } diff --git a/src/main/java/com/squareup/square/types/InvoiceFilter.java b/src/main/java/com/squareup/square/types/InvoiceFilter.java index 06ed030f..c87dff96 100644 --- a/src/main/java/com/squareup/square/types/InvoiceFilter.java +++ b/src/main/java/com/squareup/square/types/InvoiceFilter.java @@ -119,7 +119,9 @@ public Builder from(InvoiceFilter other) { @JsonSetter(value = "location_ids", nulls = Nulls.SKIP) public Builder locationIds(List locationIds) { this.locationIds.clear(); - this.locationIds.addAll(locationIds); + if (locationIds != null) { + this.locationIds.addAll(locationIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/ListOrderCustomAttributeDefinitionsResponse.java b/src/main/java/com/squareup/square/types/ListOrderCustomAttributeDefinitionsResponse.java index 30525cb7..358c5b45 100644 --- a/src/main/java/com/squareup/square/types/ListOrderCustomAttributeDefinitionsResponse.java +++ b/src/main/java/com/squareup/square/types/ListOrderCustomAttributeDefinitionsResponse.java @@ -125,7 +125,9 @@ public Builder from(ListOrderCustomAttributeDefinitionsResponse other) { @JsonSetter(value = "custom_attribute_definitions", nulls = Nulls.SKIP) public Builder customAttributeDefinitions(List customAttributeDefinitions) { this.customAttributeDefinitions.clear(); - this.customAttributeDefinitions.addAll(customAttributeDefinitions); + if (customAttributeDefinitions != null) { + this.customAttributeDefinitions.addAll(customAttributeDefinitions); + } return this; } diff --git a/src/main/java/com/squareup/square/types/LoyaltyEventLocationFilter.java b/src/main/java/com/squareup/square/types/LoyaltyEventLocationFilter.java index eb7d56ed..b1b5aa5f 100644 --- a/src/main/java/com/squareup/square/types/LoyaltyEventLocationFilter.java +++ b/src/main/java/com/squareup/square/types/LoyaltyEventLocationFilter.java @@ -91,7 +91,9 @@ public Builder from(LoyaltyEventLocationFilter other) { @JsonSetter(value = "location_ids", nulls = Nulls.SKIP) public Builder locationIds(List locationIds) { this.locationIds.clear(); - this.locationIds.addAll(locationIds); + if (locationIds != null) { + this.locationIds.addAll(locationIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/LoyaltyEventTypeFilter.java b/src/main/java/com/squareup/square/types/LoyaltyEventTypeFilter.java index 747b583a..ebefd4b0 100644 --- a/src/main/java/com/squareup/square/types/LoyaltyEventTypeFilter.java +++ b/src/main/java/com/squareup/square/types/LoyaltyEventTypeFilter.java @@ -93,7 +93,9 @@ public Builder from(LoyaltyEventTypeFilter other) { @JsonSetter(value = "types", nulls = Nulls.SKIP) public Builder types(List types) { this.types.clear(); - this.types.addAll(types); + if (types != null) { + this.types.addAll(types); + } return this; } diff --git a/src/main/java/com/squareup/square/types/LoyaltyPromotionAvailableTimeData.java b/src/main/java/com/squareup/square/types/LoyaltyPromotionAvailableTimeData.java index 887710a2..d216ac83 100644 --- a/src/main/java/com/squareup/square/types/LoyaltyPromotionAvailableTimeData.java +++ b/src/main/java/com/squareup/square/types/LoyaltyPromotionAvailableTimeData.java @@ -172,7 +172,9 @@ public Builder endDate(String endDate) { @JsonSetter(value = "time_periods", nulls = Nulls.SKIP) public Builder timePeriods(List timePeriods) { this.timePeriods.clear(); - this.timePeriods.addAll(timePeriods); + if (timePeriods != null) { + this.timePeriods.addAll(timePeriods); + } return this; } diff --git a/src/main/java/com/squareup/square/types/SearchOrdersStateFilter.java b/src/main/java/com/squareup/square/types/SearchOrdersStateFilter.java index 9a322945..914f1669 100644 --- a/src/main/java/com/squareup/square/types/SearchOrdersStateFilter.java +++ b/src/main/java/com/squareup/square/types/SearchOrdersStateFilter.java @@ -89,7 +89,9 @@ public Builder from(SearchOrdersStateFilter other) { @JsonSetter(value = "states", nulls = Nulls.SKIP) public Builder states(List states) { this.states.clear(); - this.states.addAll(states); + if (states != null) { + this.states.addAll(states); + } return this; } diff --git a/src/main/java/com/squareup/square/types/SelectOptions.java b/src/main/java/com/squareup/square/types/SelectOptions.java index 630355b8..5a352e1d 100644 --- a/src/main/java/com/squareup/square/types/SelectOptions.java +++ b/src/main/java/com/squareup/square/types/SelectOptions.java @@ -243,7 +243,9 @@ public _FinalStage addOptions(SelectOption options) { @JsonSetter(value = "options", nulls = Nulls.SKIP) public _FinalStage options(List options) { this.options.clear(); - this.options.addAll(options); + if (options != null) { + this.options.addAll(options); + } return this; } diff --git a/src/main/java/com/squareup/square/types/UpdateItemModifierListsRequest.java b/src/main/java/com/squareup/square/types/UpdateItemModifierListsRequest.java index ff285264..e522ccbb 100644 --- a/src/main/java/com/squareup/square/types/UpdateItemModifierListsRequest.java +++ b/src/main/java/com/squareup/square/types/UpdateItemModifierListsRequest.java @@ -145,7 +145,9 @@ public Builder from(UpdateItemModifierListsRequest other) { @JsonSetter(value = "item_ids", nulls = Nulls.SKIP) public Builder itemIds(List itemIds) { this.itemIds.clear(); - this.itemIds.addAll(itemIds); + if (itemIds != null) { + this.itemIds.addAll(itemIds); + } return this; } diff --git a/src/main/java/com/squareup/square/types/UpdateItemTaxesRequest.java b/src/main/java/com/squareup/square/types/UpdateItemTaxesRequest.java index 0cdcbeea..f329fc23 100644 --- a/src/main/java/com/squareup/square/types/UpdateItemTaxesRequest.java +++ b/src/main/java/com/squareup/square/types/UpdateItemTaxesRequest.java @@ -147,7 +147,9 @@ public Builder from(UpdateItemTaxesRequest other) { @JsonSetter(value = "item_ids", nulls = Nulls.SKIP) public Builder itemIds(List itemIds) { this.itemIds.clear(); - this.itemIds.addAll(itemIds); + if (itemIds != null) { + this.itemIds.addAll(itemIds); + } return this; } diff --git a/src/main/java/com/squareup/square/webhooks/AsyncRawEventTypesClient.java b/src/main/java/com/squareup/square/webhooks/AsyncRawEventTypesClient.java index 7cbe8421..1d5df07c 100644 --- a/src/main/java/com/squareup/square/webhooks/AsyncRawEventTypesClient.java +++ b/src/main/java/com/squareup/square/webhooks/AsyncRawEventTypesClient.java @@ -73,19 +73,17 @@ public CompletableFuture @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListWebhookEventTypesResponse.class), + responseBodyString, ListWebhookEventTypesResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/webhooks/AsyncRawSubscriptionsClient.java b/src/main/java/com/squareup/square/webhooks/AsyncRawSubscriptionsClient.java index 7afa5f80..4199a25a 100644 --- a/src/main/java/com/squareup/square/webhooks/AsyncRawSubscriptionsClient.java +++ b/src/main/java/com/squareup/square/webhooks/AsyncRawSubscriptionsClient.java @@ -107,9 +107,10 @@ public CompletableFuture startingAfter = parsedResponse.getCursor(); ListSubscriptionsRequest nextRequest = ListSubscriptionsRequest.builder() .from(request) @@ -118,24 +119,22 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO List result = parsedResponse.getSubscriptions().orElse(Collections.emptyList()); future.complete(new SquareClientHttpResponse<>( - new SyncPagingIterable(startingAfter.isPresent(), result, () -> { - try { - return list(nextRequest, requestOptions) - .get() - .body(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - }), + new SyncPagingIterable( + startingAfter.isPresent(), result, parsedResponse, () -> { + try { + return list(nextRequest, requestOptions) + .get() + .body(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -191,19 +190,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateWebhookSubscriptionResponse.class), + responseBodyString, CreateWebhookSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -251,19 +248,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetWebhookSubscriptionResponse.class), + responseBodyString, GetWebhookSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -320,19 +315,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateWebhookSubscriptionResponse.class), + responseBodyString, UpdateWebhookSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -381,19 +374,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteWebhookSubscriptionResponse.class), + responseBodyString, DeleteWebhookSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -451,19 +442,17 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) { @Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { try (ResponseBody responseBody = response.body()) { + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { future.complete(new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateWebhookSubscriptionSignatureKeyResponse.class), + responseBodyString, UpdateWebhookSubscriptionSignatureKeyResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); @@ -520,19 +509,17 @@ public CompletableFuture( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), TestWebhookSubscriptionResponse.class), + responseBodyString, TestWebhookSubscriptionResponse.class), response)); return; } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); future.completeExceptionally(new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response)); + "Error with status code " + response.code(), response.code(), errorBody, response)); return; } catch (IOException e) { future.completeExceptionally(new SquareException("Network error executing HTTP request", e)); diff --git a/src/main/java/com/squareup/square/webhooks/RawEventTypesClient.java b/src/main/java/com/squareup/square/webhooks/RawEventTypesClient.java index 352befa9..5b84df24 100644 --- a/src/main/java/com/squareup/square/webhooks/RawEventTypesClient.java +++ b/src/main/java/com/squareup/square/webhooks/RawEventTypesClient.java @@ -65,17 +65,15 @@ public SquareClientHttpResponse list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListWebhookEventTypesResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListWebhookEventTypesResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } diff --git a/src/main/java/com/squareup/square/webhooks/RawSubscriptionsClient.java b/src/main/java/com/squareup/square/webhooks/RawSubscriptionsClient.java index 20bc345d..110636eb 100644 --- a/src/main/java/com/squareup/square/webhooks/RawSubscriptionsClient.java +++ b/src/main/java/com/squareup/square/webhooks/RawSubscriptionsClient.java @@ -97,9 +97,10 @@ public SquareClientHttpResponse> list( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { - ListWebhookSubscriptionsResponse parsedResponse = ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), ListWebhookSubscriptionsResponse.class); + ListWebhookSubscriptionsResponse parsedResponse = + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ListWebhookSubscriptionsResponse.class); Optional startingAfter = parsedResponse.getCursor(); ListSubscriptionsRequest nextRequest = ListSubscriptionsRequest.builder() .from(request) @@ -109,16 +110,14 @@ public SquareClientHttpResponse> list( parsedResponse.getSubscriptions().orElse(Collections.emptyList()); return new SquareClientHttpResponse<>( new SyncPagingIterable( - startingAfter.isPresent(), result, () -> list(nextRequest, requestOptions) + startingAfter.isPresent(), result, parsedResponse, () -> list( + nextRequest, requestOptions) .body()), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -161,18 +160,16 @@ public SquareClientHttpResponse create( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), CreateWebhookSubscriptionResponse.class), + responseBodyString, CreateWebhookSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -207,18 +204,15 @@ public SquareClientHttpResponse get( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), GetWebhookSubscriptionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GetWebhookSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -262,18 +256,16 @@ public SquareClientHttpResponse update( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateWebhookSubscriptionResponse.class), + responseBodyString, UpdateWebhookSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -308,18 +300,16 @@ public SquareClientHttpResponse delete( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), DeleteWebhookSubscriptionResponse.class), + responseBodyString, DeleteWebhookSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -364,18 +354,16 @@ public SquareClientHttpResponse u } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), UpdateWebhookSubscriptionSignatureKeyResponse.class), + responseBodyString, UpdateWebhookSubscriptionSignatureKeyResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); } @@ -419,18 +407,15 @@ public SquareClientHttpResponse test( } try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; if (response.isSuccessful()) { return new SquareClientHttpResponse<>( - ObjectMappers.JSON_MAPPER.readValue( - responseBody.string(), TestWebhookSubscriptionResponse.class), + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, TestWebhookSubscriptionResponse.class), response); } - String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); throw new SquareApiException( - "Error with status code " + response.code(), - response.code(), - ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), - response); + "Error with status code " + response.code(), response.code(), errorBody, response); } catch (IOException e) { throw new SquareException("Network error executing HTTP request", e); }