From a2367751e07b26b9c4fbb9c80ca18df601db4085 Mon Sep 17 00:00:00 2001 From: "fern-api[bot]" <115122769+fern-api[bot]@users.noreply.github.com> Date: Fri, 30 Aug 2024 15:02:03 -0400 Subject: [PATCH] SDK regeneration (#28) Co-authored-by: fern-api <115122769+fern-api[bot]@users.noreply.github.com> --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- gradlew | 2 +- src/main/java/com/cohere/api/Cohere.java | 709 ++++++++++++++---- .../java/com/cohere/api/core/ApiError.java | 28 - .../com/cohere/api/core/ClientOptions.java | 15 +- .../cohere/api/core/CohereApiApiError.java | 45 ++ .../com/cohere/api/core/CohereApiError.java | 17 + .../com/cohere/api/core/RequestOptions.java | 2 +- .../api/errors/CohereApiBadRequestError.java | 26 + .../CohereApiClientClosedRequestError.java | 27 + .../api/errors/CohereApiForbiddenError.java | 26 + .../errors/CohereApiGatewayTimeoutError.java | 27 + .../errors/CohereApiInternalServerError.java | 26 + .../api/errors/CohereApiNotFoundError.java | 26 + .../errors/CohereApiNotImplementedError.java | 27 + .../CohereApiServiceUnavailableError.java | 26 + .../errors/CohereApiTooManyRequestsError.java | 27 + .../errors/CohereApiUnauthorizedError.java | 26 + .../CohereApiUnprocessableEntityError.java | 27 + .../connectors/ConnectorsClient.java | 382 ++++++++-- .../resources/datasets/DatasetsClient.java | 326 ++++++-- .../resources/embedjobs/EmbedJobsClient.java | 260 +++++-- .../finetuning/FinetuningClient.java | 326 ++++++-- .../api/resources/models/ModelsClient.java | 138 +++- .../com/cohere/api/types/ChatStreamEvent.java | 27 + .../cohere/api/types/GenerateStreamEvent.java | 27 + .../cohere/api/types/TextResponseFormat.java | 27 + 28 files changed, 2196 insertions(+), 430 deletions(-) delete mode 100644 src/main/java/com/cohere/api/core/ApiError.java create mode 100644 src/main/java/com/cohere/api/core/CohereApiApiError.java create mode 100644 src/main/java/com/cohere/api/core/CohereApiError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiBadRequestError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiClientClosedRequestError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiForbiddenError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiGatewayTimeoutError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiInternalServerError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiNotFoundError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiNotImplementedError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiServiceUnavailableError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiTooManyRequestsError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiUnauthorizedError.java create mode 100644 src/main/java/com/cohere/api/errors/CohereApiUnprocessableEntityError.java diff --git a/build.gradle b/build.gradle index f2378cb..301d49e 100644 --- a/build.gradle +++ b/build.gradle @@ -50,7 +50,7 @@ publishing { maven(MavenPublication) { groupId = 'com.cohere' artifactId = 'cohere-java' - version = '1.3.0' + version = '1.3.1' from components.java pom { name = 'cohere' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b82aa23..a441313 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew index 1aa94a4..b740cf1 100755 --- a/gradlew +++ b/gradlew @@ -55,7 +55,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. diff --git a/src/main/java/com/cohere/api/Cohere.java b/src/main/java/com/cohere/api/Cohere.java index 052952e..362a79c 100644 --- a/src/main/java/com/cohere/api/Cohere.java +++ b/src/main/java/com/cohere/api/Cohere.java @@ -3,13 +3,25 @@ */ package com.cohere.api; -import com.cohere.api.core.ApiError; import com.cohere.api.core.ClientOptions; +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.core.CohereApiError; import com.cohere.api.core.MediaTypes; import com.cohere.api.core.ObjectMappers; import com.cohere.api.core.RequestOptions; import com.cohere.api.core.Stream; import com.cohere.api.core.Suppliers; +import com.cohere.api.errors.CohereApiBadRequestError; +import com.cohere.api.errors.CohereApiClientClosedRequestError; +import com.cohere.api.errors.CohereApiForbiddenError; +import com.cohere.api.errors.CohereApiGatewayTimeoutError; +import com.cohere.api.errors.CohereApiInternalServerError; +import com.cohere.api.errors.CohereApiNotFoundError; +import com.cohere.api.errors.CohereApiNotImplementedError; +import com.cohere.api.errors.CohereApiServiceUnavailableError; +import com.cohere.api.errors.CohereApiTooManyRequestsError; +import com.cohere.api.errors.CohereApiUnauthorizedError; +import com.cohere.api.errors.CohereApiUnprocessableEntityError; import com.cohere.api.requests.ChatRequest; import com.cohere.api.requests.ChatStreamRequest; import com.cohere.api.requests.ClassifyRequest; @@ -27,15 +39,21 @@ import com.cohere.api.resources.models.ModelsClient; import com.cohere.api.types.CheckApiKeyResponse; import com.cohere.api.types.ClassifyResponse; +import com.cohere.api.types.ClientClosedRequestErrorBody; import com.cohere.api.types.DetokenizeResponse; import com.cohere.api.types.EmbedResponse; +import com.cohere.api.types.GatewayTimeoutErrorBody; import com.cohere.api.types.GenerateStreamedResponse; import com.cohere.api.types.Generation; import com.cohere.api.types.NonStreamedChatResponse; +import com.cohere.api.types.NotImplementedErrorBody; import com.cohere.api.types.RerankResponse; import com.cohere.api.types.StreamedChatResponse; import com.cohere.api.types.SummarizeResponse; import com.cohere.api.types.TokenizeResponse; +import com.cohere.api.types.TooManyRequestsErrorBody; +import com.cohere.api.types.UnprocessableEntityErrorBody; +import com.fasterxml.jackson.core.JsonProcessingException; import java.io.IOException; import java.util.function.Supplier; import okhttp3.Headers; @@ -89,8 +107,8 @@ public Iterable chatStream(ChatStreamRequest request, Requ try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -98,22 +116,61 @@ public Iterable chatStream(ChatStreamRequest request, Requ .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return new Stream(StreamedChatResponse.class, responseBody.charStream(), "\n"); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -138,8 +195,8 @@ public NonStreamedChatResponse chat(ChatRequest request, RequestOptions requestO try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -147,22 +204,61 @@ public NonStreamedChatResponse chat(ChatRequest request, RequestOptions requestO .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), NonStreamedChatResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -192,8 +288,8 @@ public Iterable generateStream( try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -201,23 +297,62 @@ public Iterable generateStream( .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return new Stream( GenerateStreamedResponse.class, responseBody.charStream(), "\n"); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -246,8 +381,8 @@ public Generation generate(GenerateRequest request, RequestOptions requestOption try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -255,22 +390,61 @@ public Generation generate(GenerateRequest request, RequestOptions requestOption .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Generation.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -297,8 +471,8 @@ public EmbedResponse embed(EmbedRequest request, RequestOptions requestOptions) try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -306,22 +480,61 @@ public EmbedResponse embed(EmbedRequest request, RequestOptions requestOptions) .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), EmbedResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -344,8 +557,8 @@ public RerankResponse rerank(RerankRequest request, RequestOptions requestOption try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -353,22 +566,61 @@ public RerankResponse rerank(RerankRequest request, RequestOptions requestOption .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RerankResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -393,8 +645,8 @@ public ClassifyResponse classify(ClassifyRequest request, RequestOptions request try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -402,22 +654,61 @@ public ClassifyResponse classify(ClassifyRequest request, RequestOptions request .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ClassifyResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -446,8 +737,8 @@ public SummarizeResponse summarize(SummarizeRequest request, RequestOptions requ try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -455,22 +746,61 @@ public SummarizeResponse summarize(SummarizeRequest request, RequestOptions requ .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SummarizeResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -493,8 +823,8 @@ public TokenizeResponse tokenize(TokenizeRequest request, RequestOptions request try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -502,22 +832,61 @@ public TokenizeResponse tokenize(TokenizeRequest request, RequestOptions request .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), TokenizeResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -540,8 +909,8 @@ public DetokenizeResponse detokenize(DetokenizeRequest request, RequestOptions r try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -549,22 +918,61 @@ public DetokenizeResponse detokenize(DetokenizeRequest request, RequestOptions r .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DetokenizeResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -589,22 +997,61 @@ public CheckApiKeyResponse checkApiKey(RequestOptions requestOptions) { .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CheckApiKeyResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } diff --git a/src/main/java/com/cohere/api/core/ApiError.java b/src/main/java/com/cohere/api/core/ApiError.java deleted file mode 100644 index 3692156..0000000 --- a/src/main/java/com/cohere/api/core/ApiError.java +++ /dev/null @@ -1,28 +0,0 @@ -/** - * This file was auto-generated by Fern from our API Definition. - */ -package com.cohere.api.core; - -public final class ApiError extends RuntimeException { - private final int statusCode; - - private final Object body; - - public ApiError(int statusCode, Object body) { - this.statusCode = statusCode; - this.body = body; - } - - public int statusCode() { - return this.statusCode; - } - - public Object body() { - return this.body; - } - - @java.lang.Override - public String toString() { - return "ApiError{" + "statusCode: " + statusCode + ", body: " + body + "}"; - } -} diff --git a/src/main/java/com/cohere/api/core/ClientOptions.java b/src/main/java/com/cohere/api/core/ClientOptions.java index af7afeb..f51ecd7 100644 --- a/src/main/java/com/cohere/api/core/ClientOptions.java +++ b/src/main/java/com/cohere/api/core/ClientOptions.java @@ -26,16 +26,15 @@ private ClientOptions( this.environment = environment; this.headers = new HashMap<>(); this.headers.putAll(headers); - this.headers.putAll(Map.of( - "X-Fern-SDK-Name", - "com.cohere.fern:api-sdk", - "X-Fern-SDK-Version", - "1.3.0", - "X-Fern-Language", - "JAVA")); + this.headers.putAll(new HashMap() { + { + put("X-Fern-Language", "JAVA"); + put("X-Fern-SDK-Name", "com.cohere.fern:api-sdk"); + put("X-Fern-SDK-Version", "1.3.1"); + } + }); this.headerSuppliers = headerSuppliers; this.httpClient = httpClient; - ; } public Environment environment() { diff --git a/src/main/java/com/cohere/api/core/CohereApiApiError.java b/src/main/java/com/cohere/api/core/CohereApiApiError.java new file mode 100644 index 0000000..c0a8166 --- /dev/null +++ b/src/main/java/com/cohere/api/core/CohereApiApiError.java @@ -0,0 +1,45 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.core; + +/** + * This exception type will be thrown for any non-2XX API responses. + */ +public class CohereApiApiError extends CohereApiError { + /** + * The error code of the response that triggered the exception. + */ + private final int statusCode; + + /** + * The body of the response that triggered the exception. + */ + private final Object body; + + public CohereApiApiError(String message, int statusCode, Object body) { + super(message); + this.statusCode = statusCode; + this.body = body; + } + + /** + * @return the statusCode + */ + public int statusCode() { + return this.statusCode; + } + + /** + * @return the body + */ + public Object body() { + return this.body; + } + + @java.lang.Override + public String toString() { + return "CohereApiApiError{" + "message: " + getMessage() + ", statusCode: " + statusCode + ", body: " + body + + "}"; + } +} diff --git a/src/main/java/com/cohere/api/core/CohereApiError.java b/src/main/java/com/cohere/api/core/CohereApiError.java new file mode 100644 index 0000000..b587058 --- /dev/null +++ b/src/main/java/com/cohere/api/core/CohereApiError.java @@ -0,0 +1,17 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.core; + +/** + * This class serves as the base exception for all errors in the SDK. + */ +public class CohereApiError extends RuntimeException { + public CohereApiError(String message) { + super(message); + } + + public CohereApiError(String message, Exception e) { + super(message, e); + } +} diff --git a/src/main/java/com/cohere/api/core/RequestOptions.java b/src/main/java/com/cohere/api/core/RequestOptions.java index 414b7cf..d88c8e9 100644 --- a/src/main/java/com/cohere/api/core/RequestOptions.java +++ b/src/main/java/com/cohere/api/core/RequestOptions.java @@ -52,7 +52,7 @@ public static final class Builder { private String clientName = null; - private Optional timeout = null; + private Optional timeout = Optional.empty(); private TimeUnit timeoutTimeUnit = TimeUnit.SECONDS; diff --git a/src/main/java/com/cohere/api/errors/CohereApiBadRequestError.java b/src/main/java/com/cohere/api/errors/CohereApiBadRequestError.java new file mode 100644 index 0000000..5934a09 --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiBadRequestError.java @@ -0,0 +1,26 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; + +public final class CohereApiBadRequestError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final Object body; + + public CohereApiBadRequestError(Object body) { + super("BadRequestError", 400, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public Object body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiClientClosedRequestError.java b/src/main/java/com/cohere/api/errors/CohereApiClientClosedRequestError.java new file mode 100644 index 0000000..120cadd --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiClientClosedRequestError.java @@ -0,0 +1,27 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.types.ClientClosedRequestErrorBody; + +public final class CohereApiClientClosedRequestError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final ClientClosedRequestErrorBody body; + + public CohereApiClientClosedRequestError(ClientClosedRequestErrorBody body) { + super("ClientClosedRequestError", 499, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public ClientClosedRequestErrorBody body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiForbiddenError.java b/src/main/java/com/cohere/api/errors/CohereApiForbiddenError.java new file mode 100644 index 0000000..ca15614 --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiForbiddenError.java @@ -0,0 +1,26 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; + +public final class CohereApiForbiddenError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final Object body; + + public CohereApiForbiddenError(Object body) { + super("ForbiddenError", 403, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public Object body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiGatewayTimeoutError.java b/src/main/java/com/cohere/api/errors/CohereApiGatewayTimeoutError.java new file mode 100644 index 0000000..3ad8338 --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiGatewayTimeoutError.java @@ -0,0 +1,27 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.types.GatewayTimeoutErrorBody; + +public final class CohereApiGatewayTimeoutError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final GatewayTimeoutErrorBody body; + + public CohereApiGatewayTimeoutError(GatewayTimeoutErrorBody body) { + super("GatewayTimeoutError", 504, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public GatewayTimeoutErrorBody body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiInternalServerError.java b/src/main/java/com/cohere/api/errors/CohereApiInternalServerError.java new file mode 100644 index 0000000..f653437 --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiInternalServerError.java @@ -0,0 +1,26 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; + +public final class CohereApiInternalServerError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final Object body; + + public CohereApiInternalServerError(Object body) { + super("InternalServerError", 500, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public Object body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiNotFoundError.java b/src/main/java/com/cohere/api/errors/CohereApiNotFoundError.java new file mode 100644 index 0000000..6420838 --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiNotFoundError.java @@ -0,0 +1,26 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; + +public final class CohereApiNotFoundError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final Object body; + + public CohereApiNotFoundError(Object body) { + super("NotFoundError", 404, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public Object body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiNotImplementedError.java b/src/main/java/com/cohere/api/errors/CohereApiNotImplementedError.java new file mode 100644 index 0000000..7c4c81a --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiNotImplementedError.java @@ -0,0 +1,27 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.types.NotImplementedErrorBody; + +public final class CohereApiNotImplementedError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final NotImplementedErrorBody body; + + public CohereApiNotImplementedError(NotImplementedErrorBody body) { + super("NotImplementedError", 501, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public NotImplementedErrorBody body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiServiceUnavailableError.java b/src/main/java/com/cohere/api/errors/CohereApiServiceUnavailableError.java new file mode 100644 index 0000000..7601763 --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiServiceUnavailableError.java @@ -0,0 +1,26 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; + +public final class CohereApiServiceUnavailableError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final Object body; + + public CohereApiServiceUnavailableError(Object body) { + super("ServiceUnavailableError", 503, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public Object body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiTooManyRequestsError.java b/src/main/java/com/cohere/api/errors/CohereApiTooManyRequestsError.java new file mode 100644 index 0000000..2b401c6 --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiTooManyRequestsError.java @@ -0,0 +1,27 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.types.TooManyRequestsErrorBody; + +public final class CohereApiTooManyRequestsError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final TooManyRequestsErrorBody body; + + public CohereApiTooManyRequestsError(TooManyRequestsErrorBody body) { + super("TooManyRequestsError", 429, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public TooManyRequestsErrorBody body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiUnauthorizedError.java b/src/main/java/com/cohere/api/errors/CohereApiUnauthorizedError.java new file mode 100644 index 0000000..784500f --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiUnauthorizedError.java @@ -0,0 +1,26 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; + +public final class CohereApiUnauthorizedError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final Object body; + + public CohereApiUnauthorizedError(Object body) { + super("UnauthorizedError", 401, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public Object body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/errors/CohereApiUnprocessableEntityError.java b/src/main/java/com/cohere/api/errors/CohereApiUnprocessableEntityError.java new file mode 100644 index 0000000..cc1280d --- /dev/null +++ b/src/main/java/com/cohere/api/errors/CohereApiUnprocessableEntityError.java @@ -0,0 +1,27 @@ +/** + * This file was auto-generated by Fern from our API Definition. + */ +package com.cohere.api.errors; + +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.types.UnprocessableEntityErrorBody; + +public final class CohereApiUnprocessableEntityError extends CohereApiApiError { + /** + * The body of the response that triggered the exception. + */ + private final UnprocessableEntityErrorBody body; + + public CohereApiUnprocessableEntityError(UnprocessableEntityErrorBody body) { + super("UnprocessableEntityError", 422, body); + this.body = body; + } + + /** + * @return the body + */ + @java.lang.Override + public UnprocessableEntityErrorBody body() { + return this.body; + } +} diff --git a/src/main/java/com/cohere/api/resources/connectors/ConnectorsClient.java b/src/main/java/com/cohere/api/resources/connectors/ConnectorsClient.java index a50c95b..0f0a3a7 100644 --- a/src/main/java/com/cohere/api/resources/connectors/ConnectorsClient.java +++ b/src/main/java/com/cohere/api/resources/connectors/ConnectorsClient.java @@ -3,20 +3,38 @@ */ package com.cohere.api.resources.connectors; -import com.cohere.api.core.ApiError; import com.cohere.api.core.ClientOptions; +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.core.CohereApiError; import com.cohere.api.core.MediaTypes; import com.cohere.api.core.ObjectMappers; import com.cohere.api.core.RequestOptions; +import com.cohere.api.errors.CohereApiBadRequestError; +import com.cohere.api.errors.CohereApiClientClosedRequestError; +import com.cohere.api.errors.CohereApiForbiddenError; +import com.cohere.api.errors.CohereApiGatewayTimeoutError; +import com.cohere.api.errors.CohereApiInternalServerError; +import com.cohere.api.errors.CohereApiNotFoundError; +import com.cohere.api.errors.CohereApiNotImplementedError; +import com.cohere.api.errors.CohereApiServiceUnavailableError; +import com.cohere.api.errors.CohereApiTooManyRequestsError; +import com.cohere.api.errors.CohereApiUnauthorizedError; +import com.cohere.api.errors.CohereApiUnprocessableEntityError; import com.cohere.api.resources.connectors.requests.ConnectorsListRequest; import com.cohere.api.resources.connectors.requests.ConnectorsOAuthAuthorizeRequest; import com.cohere.api.resources.connectors.requests.CreateConnectorRequest; import com.cohere.api.resources.connectors.requests.UpdateConnectorRequest; +import com.cohere.api.types.ClientClosedRequestErrorBody; import com.cohere.api.types.CreateConnectorResponse; +import com.cohere.api.types.GatewayTimeoutErrorBody; import com.cohere.api.types.GetConnectorResponse; import com.cohere.api.types.ListConnectorsResponse; +import com.cohere.api.types.NotImplementedErrorBody; import com.cohere.api.types.OAuthAuthorizeResponse; +import com.cohere.api.types.TooManyRequestsErrorBody; +import com.cohere.api.types.UnprocessableEntityErrorBody; import com.cohere.api.types.UpdateConnectorResponse; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import java.io.IOException; import java.util.Map; @@ -68,22 +86,61 @@ public ListConnectorsResponse list(ConnectorsListRequest request, RequestOptions .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json"); Request okhttpRequest = _requestBuilder.build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListConnectorsResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -106,8 +163,8 @@ public CreateConnectorResponse create(CreateConnectorRequest request, RequestOpt try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -115,22 +172,61 @@ public CreateConnectorResponse create(CreateConnectorRequest request, RequestOpt .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateConnectorResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -156,22 +252,61 @@ public GetConnectorResponse get(String id, RequestOptions requestOptions) { .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetConnectorResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -197,23 +332,62 @@ public Map delete(String id, RequestOptions requestOptions) { .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue( responseBody.string(), new TypeReference>() {}); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -244,8 +418,8 @@ public UpdateConnectorResponse update(String id, UpdateConnectorRequest request, try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -253,22 +427,61 @@ public UpdateConnectorResponse update(String id, UpdateConnectorRequest request, .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateConnectorResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -306,22 +519,61 @@ public OAuthAuthorizeResponse oAuthAuthorize( .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json"); Request okhttpRequest = _requestBuilder.build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), OAuthAuthorizeResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } } diff --git a/src/main/java/com/cohere/api/resources/datasets/DatasetsClient.java b/src/main/java/com/cohere/api/resources/datasets/DatasetsClient.java index a591d41..946a884 100644 --- a/src/main/java/com/cohere/api/resources/datasets/DatasetsClient.java +++ b/src/main/java/com/cohere/api/resources/datasets/DatasetsClient.java @@ -3,16 +3,34 @@ */ package com.cohere.api.resources.datasets; -import com.cohere.api.core.ApiError; import com.cohere.api.core.ClientOptions; +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.core.CohereApiError; import com.cohere.api.core.ObjectMappers; import com.cohere.api.core.RequestOptions; +import com.cohere.api.errors.CohereApiBadRequestError; +import com.cohere.api.errors.CohereApiClientClosedRequestError; +import com.cohere.api.errors.CohereApiForbiddenError; +import com.cohere.api.errors.CohereApiGatewayTimeoutError; +import com.cohere.api.errors.CohereApiInternalServerError; +import com.cohere.api.errors.CohereApiNotFoundError; +import com.cohere.api.errors.CohereApiNotImplementedError; +import com.cohere.api.errors.CohereApiServiceUnavailableError; +import com.cohere.api.errors.CohereApiTooManyRequestsError; +import com.cohere.api.errors.CohereApiUnauthorizedError; +import com.cohere.api.errors.CohereApiUnprocessableEntityError; import com.cohere.api.resources.datasets.requests.DatasetsCreateRequest; import com.cohere.api.resources.datasets.requests.DatasetsListRequest; import com.cohere.api.resources.datasets.types.DatasetsCreateResponse; import com.cohere.api.resources.datasets.types.DatasetsGetResponse; import com.cohere.api.resources.datasets.types.DatasetsGetUsageResponse; import com.cohere.api.resources.datasets.types.DatasetsListResponse; +import com.cohere.api.types.ClientClosedRequestErrorBody; +import com.cohere.api.types.GatewayTimeoutErrorBody; +import com.cohere.api.types.NotImplementedErrorBody; +import com.cohere.api.types.TooManyRequestsErrorBody; +import com.cohere.api.types.UnprocessableEntityErrorBody; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import java.io.File; import java.io.IOException; @@ -82,22 +100,61 @@ public DatasetsListResponse list(DatasetsListRequest request, RequestOptions req .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json"); Request okhttpRequest = _requestBuilder.build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DatasetsListResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -147,13 +204,16 @@ public DatasetsCreateResponse create( MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM); try { String dataMimeType = Files.probeContentType(data.toPath()); - MediaType dataMediaType = dataMimeType != null ? MediaType.parse(dataMimeType) : null; - body.addFormDataPart("data", data.getName(), RequestBody.create(dataMediaType, data)); + MediaType dataMimeTypeMediaType = dataMimeType != null ? MediaType.parse(dataMimeType) : null; + body.addFormDataPart("data", data.getName(), RequestBody.create(dataMimeTypeMediaType, data)); if (evalData.isPresent()) { String evalDataMimeType = Files.probeContentType(evalData.get().toPath()); - MediaType evalDataMediaType = evalDataMimeType != null ? MediaType.parse(evalDataMimeType) : null; + MediaType evalDataMimeTypeMediaType = + evalDataMimeType != null ? MediaType.parse(evalDataMimeType) : null; body.addFormDataPart( - "eval_data", evalData.get().getName(), RequestBody.create(evalDataMediaType, evalData.get())); + "eval_data", + evalData.get().getName(), + RequestBody.create(evalDataMimeTypeMediaType, evalData.get())); } } catch (Exception e) { throw new RuntimeException(e); @@ -163,22 +223,61 @@ public DatasetsCreateResponse create( .method("POST", body.build()) .headers(Headers.of(clientOptions.headers(requestOptions))); Request okhttpRequest = _requestBuilder.build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DatasetsCreateResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -203,22 +302,61 @@ public DatasetsGetUsageResponse getUsage(RequestOptions requestOptions) { .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DatasetsGetUsageResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -244,22 +382,61 @@ public DatasetsGetResponse get(String id, RequestOptions requestOptions) { .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DatasetsGetResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -285,23 +462,62 @@ public Map delete(String id, RequestOptions requestOptions) { .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue( responseBody.string(), new TypeReference>() {}); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } } diff --git a/src/main/java/com/cohere/api/resources/embedjobs/EmbedJobsClient.java b/src/main/java/com/cohere/api/resources/embedjobs/EmbedJobsClient.java index 6a8bb10..464ebe9 100644 --- a/src/main/java/com/cohere/api/resources/embedjobs/EmbedJobsClient.java +++ b/src/main/java/com/cohere/api/resources/embedjobs/EmbedJobsClient.java @@ -3,15 +3,33 @@ */ package com.cohere.api.resources.embedjobs; -import com.cohere.api.core.ApiError; import com.cohere.api.core.ClientOptions; +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.core.CohereApiError; import com.cohere.api.core.MediaTypes; import com.cohere.api.core.ObjectMappers; import com.cohere.api.core.RequestOptions; +import com.cohere.api.errors.CohereApiBadRequestError; +import com.cohere.api.errors.CohereApiClientClosedRequestError; +import com.cohere.api.errors.CohereApiForbiddenError; +import com.cohere.api.errors.CohereApiGatewayTimeoutError; +import com.cohere.api.errors.CohereApiInternalServerError; +import com.cohere.api.errors.CohereApiNotFoundError; +import com.cohere.api.errors.CohereApiNotImplementedError; +import com.cohere.api.errors.CohereApiServiceUnavailableError; +import com.cohere.api.errors.CohereApiTooManyRequestsError; +import com.cohere.api.errors.CohereApiUnauthorizedError; +import com.cohere.api.errors.CohereApiUnprocessableEntityError; import com.cohere.api.resources.embedjobs.requests.CreateEmbedJobRequest; +import com.cohere.api.types.ClientClosedRequestErrorBody; import com.cohere.api.types.CreateEmbedJobResponse; import com.cohere.api.types.EmbedJob; +import com.cohere.api.types.GatewayTimeoutErrorBody; import com.cohere.api.types.ListEmbedJobResponse; +import com.cohere.api.types.NotImplementedErrorBody; +import com.cohere.api.types.TooManyRequestsErrorBody; +import com.cohere.api.types.UnprocessableEntityErrorBody; +import com.fasterxml.jackson.core.JsonProcessingException; import java.io.IOException; import okhttp3.Headers; import okhttp3.HttpUrl; @@ -49,22 +67,61 @@ public ListEmbedJobResponse list(RequestOptions requestOptions) { .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListEmbedJobResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -87,8 +144,8 @@ public CreateEmbedJobResponse create(CreateEmbedJobRequest request, RequestOptio try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -96,22 +153,61 @@ public CreateEmbedJobResponse create(CreateEmbedJobRequest request, RequestOptio .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateEmbedJobResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -137,22 +233,61 @@ public EmbedJob get(String id, RequestOptions requestOptions) { .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), EmbedJob.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -178,22 +313,61 @@ public void cancel(String id, RequestOptions requestOptions) { .method("POST", RequestBody.create("", null)) .headers(Headers.of(clientOptions.headers(requestOptions))) .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return; } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } } diff --git a/src/main/java/com/cohere/api/resources/finetuning/FinetuningClient.java b/src/main/java/com/cohere/api/resources/finetuning/FinetuningClient.java index 8ae5c8a..793ded1 100644 --- a/src/main/java/com/cohere/api/resources/finetuning/FinetuningClient.java +++ b/src/main/java/com/cohere/api/resources/finetuning/FinetuningClient.java @@ -3,11 +3,18 @@ */ package com.cohere.api.resources.finetuning; -import com.cohere.api.core.ApiError; import com.cohere.api.core.ClientOptions; +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.core.CohereApiError; import com.cohere.api.core.MediaTypes; import com.cohere.api.core.ObjectMappers; import com.cohere.api.core.RequestOptions; +import com.cohere.api.errors.CohereApiBadRequestError; +import com.cohere.api.errors.CohereApiForbiddenError; +import com.cohere.api.errors.CohereApiInternalServerError; +import com.cohere.api.errors.CohereApiNotFoundError; +import com.cohere.api.errors.CohereApiServiceUnavailableError; +import com.cohere.api.errors.CohereApiUnauthorizedError; import com.cohere.api.resources.finetuning.finetuning.types.CreateFinetunedModelResponse; import com.cohere.api.resources.finetuning.finetuning.types.FinetunedModel; import com.cohere.api.resources.finetuning.finetuning.types.GetFinetunedModelResponse; @@ -19,6 +26,7 @@ import com.cohere.api.resources.finetuning.requests.FinetuningListFinetunedModelsRequest; import com.cohere.api.resources.finetuning.requests.FinetuningListTrainingStepMetricsRequest; import com.cohere.api.resources.finetuning.requests.FinetuningUpdateFinetunedModelRequest; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import java.io.IOException; import java.util.Map; @@ -66,22 +74,46 @@ public ListFinetunedModelsResponse listFinetunedModels( .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json"); Request okhttpRequest = _requestBuilder.build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListFinetunedModelsResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -98,8 +130,8 @@ public CreateFinetunedModelResponse createFinetunedModel(FinetunedModel request, try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -107,22 +139,46 @@ public CreateFinetunedModelResponse createFinetunedModel(FinetunedModel request, .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateFinetunedModelResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -142,22 +198,46 @@ public GetFinetunedModelResponse getFinetunedModel(String id, RequestOptions req .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetFinetunedModelResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -177,23 +257,47 @@ public Map deleteFinetunedModel(String id, RequestOptions reques .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue( responseBody.string(), new TypeReference>() {}); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -212,8 +316,8 @@ public UpdateFinetunedModelResponse updateFinetunedModel( try { body = RequestBody.create( ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw new CohereApiError("Failed to serialize request", e); } Request okhttpRequest = new Request.Builder() .url(httpUrl) @@ -221,22 +325,46 @@ public UpdateFinetunedModelResponse updateFinetunedModel( .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UpdateFinetunedModelResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -271,22 +399,46 @@ public ListEventsResponse listEvents( .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json"); Request okhttpRequest = _requestBuilder.build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListEventsResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -320,23 +472,47 @@ public ListTrainingStepMetricsResponse listTrainingStepMetrics( .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json"); Request okhttpRequest = _requestBuilder.build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue( responseBody.string(), ListTrainingStepMetricsResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } } diff --git a/src/main/java/com/cohere/api/resources/models/ModelsClient.java b/src/main/java/com/cohere/api/resources/models/ModelsClient.java index 5694c2f..16cb824 100644 --- a/src/main/java/com/cohere/api/resources/models/ModelsClient.java +++ b/src/main/java/com/cohere/api/resources/models/ModelsClient.java @@ -3,13 +3,31 @@ */ package com.cohere.api.resources.models; -import com.cohere.api.core.ApiError; import com.cohere.api.core.ClientOptions; +import com.cohere.api.core.CohereApiApiError; +import com.cohere.api.core.CohereApiError; import com.cohere.api.core.ObjectMappers; import com.cohere.api.core.RequestOptions; +import com.cohere.api.errors.CohereApiBadRequestError; +import com.cohere.api.errors.CohereApiClientClosedRequestError; +import com.cohere.api.errors.CohereApiForbiddenError; +import com.cohere.api.errors.CohereApiGatewayTimeoutError; +import com.cohere.api.errors.CohereApiInternalServerError; +import com.cohere.api.errors.CohereApiNotFoundError; +import com.cohere.api.errors.CohereApiNotImplementedError; +import com.cohere.api.errors.CohereApiServiceUnavailableError; +import com.cohere.api.errors.CohereApiTooManyRequestsError; +import com.cohere.api.errors.CohereApiUnauthorizedError; +import com.cohere.api.errors.CohereApiUnprocessableEntityError; import com.cohere.api.resources.models.requests.ModelsListRequest; +import com.cohere.api.types.ClientClosedRequestErrorBody; +import com.cohere.api.types.GatewayTimeoutErrorBody; import com.cohere.api.types.GetModelResponse; import com.cohere.api.types.ListModelsResponse; +import com.cohere.api.types.NotImplementedErrorBody; +import com.cohere.api.types.TooManyRequestsErrorBody; +import com.cohere.api.types.UnprocessableEntityErrorBody; +import com.fasterxml.jackson.core.JsonProcessingException; import java.io.IOException; import okhttp3.Headers; import okhttp3.HttpUrl; @@ -47,22 +65,61 @@ public GetModelResponse get(String model, RequestOptions requestOptions) { .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json") .build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetModelResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } @@ -106,22 +163,61 @@ public ListModelsResponse list(ModelsListRequest request, RequestOptions request .headers(Headers.of(clientOptions.headers(requestOptions))) .addHeader("Content-Type", "application/json"); Request okhttpRequest = _requestBuilder.build(); - try { - OkHttpClient client = clientOptions.httpClient(); - if (requestOptions != null && requestOptions.getTimeout().isPresent()) { - client = clientOptions.httpClientWithTimeout(requestOptions); - } - Response response = client.newCall(okhttpRequest).execute(); + OkHttpClient client = clientOptions.httpClient(); + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { + client = clientOptions.httpClientWithTimeout(requestOptions); + } + try (Response response = client.newCall(okhttpRequest).execute()) { ResponseBody responseBody = response.body(); if (response.isSuccessful()) { return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListModelsResponse.class); } - throw new ApiError( + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; + try { + switch (response.code()) { + case 400: + throw new CohereApiBadRequestError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 401: + throw new CohereApiUnauthorizedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 403: + throw new CohereApiForbiddenError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 404: + throw new CohereApiNotFoundError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 422: + throw new CohereApiUnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, UnprocessableEntityErrorBody.class)); + case 429: + throw new CohereApiTooManyRequestsError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, TooManyRequestsErrorBody.class)); + case 499: + throw new CohereApiClientClosedRequestError(ObjectMappers.JSON_MAPPER.readValue( + responseBodyString, ClientClosedRequestErrorBody.class)); + case 500: + throw new CohereApiInternalServerError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 501: + throw new CohereApiNotImplementedError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, NotImplementedErrorBody.class)); + case 503: + throw new CohereApiServiceUnavailableError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); + case 504: + throw new CohereApiGatewayTimeoutError( + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, GatewayTimeoutErrorBody.class)); + } + } catch (JsonProcessingException ignored) { + // unable to map error response, throwing generic error + } + throw new CohereApiApiError( + "Error with status code " + response.code(), response.code(), - ObjectMappers.JSON_MAPPER.readValue( - responseBody != null ? responseBody.string() : "{}", Object.class)); + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); } catch (IOException e) { - throw new RuntimeException(e); + throw new CohereApiError("Network error executing HTTP request", e); } } } diff --git a/src/main/java/com/cohere/api/types/ChatStreamEvent.java b/src/main/java/com/cohere/api/types/ChatStreamEvent.java index b9d7f22..a9a1758 100644 --- a/src/main/java/com/cohere/api/types/ChatStreamEvent.java +++ b/src/main/java/com/cohere/api/types/ChatStreamEvent.java @@ -5,8 +5,15 @@ import com.cohere.api.core.ObjectMappers; import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import java.util.HashMap; import java.util.Map; +@JsonInclude(JsonInclude.Include.NON_EMPTY) +@JsonDeserialize(builder = ChatStreamEvent.Builder.class) public final class ChatStreamEvent implements IChatStreamEvent { private final Map additionalProperties; @@ -29,4 +36,24 @@ public Map getAdditionalProperties() { public String toString() { return ObjectMappers.stringify(this); } + + public static Builder builder() { + return new Builder(); + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static final class Builder { + @JsonAnySetter + private Map additionalProperties = new HashMap<>(); + + private Builder() {} + + public Builder from(ChatStreamEvent other) { + return this; + } + + public ChatStreamEvent build() { + return new ChatStreamEvent(additionalProperties); + } + } } diff --git a/src/main/java/com/cohere/api/types/GenerateStreamEvent.java b/src/main/java/com/cohere/api/types/GenerateStreamEvent.java index cd36550..f61602b 100644 --- a/src/main/java/com/cohere/api/types/GenerateStreamEvent.java +++ b/src/main/java/com/cohere/api/types/GenerateStreamEvent.java @@ -5,8 +5,15 @@ import com.cohere.api.core.ObjectMappers; import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import java.util.HashMap; import java.util.Map; +@JsonInclude(JsonInclude.Include.NON_EMPTY) +@JsonDeserialize(builder = GenerateStreamEvent.Builder.class) public final class GenerateStreamEvent implements IGenerateStreamEvent { private final Map additionalProperties; @@ -29,4 +36,24 @@ public Map getAdditionalProperties() { public String toString() { return ObjectMappers.stringify(this); } + + public static Builder builder() { + return new Builder(); + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static final class Builder { + @JsonAnySetter + private Map additionalProperties = new HashMap<>(); + + private Builder() {} + + public Builder from(GenerateStreamEvent other) { + return this; + } + + public GenerateStreamEvent build() { + return new GenerateStreamEvent(additionalProperties); + } + } } diff --git a/src/main/java/com/cohere/api/types/TextResponseFormat.java b/src/main/java/com/cohere/api/types/TextResponseFormat.java index edc71d8..2976b38 100644 --- a/src/main/java/com/cohere/api/types/TextResponseFormat.java +++ b/src/main/java/com/cohere/api/types/TextResponseFormat.java @@ -5,8 +5,15 @@ import com.cohere.api.core.ObjectMappers; import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import java.util.HashMap; import java.util.Map; +@JsonInclude(JsonInclude.Include.NON_EMPTY) +@JsonDeserialize(builder = TextResponseFormat.Builder.class) public final class TextResponseFormat { private final Map additionalProperties; @@ -29,4 +36,24 @@ public Map getAdditionalProperties() { public String toString() { return ObjectMappers.stringify(this); } + + public static Builder builder() { + return new Builder(); + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static final class Builder { + @JsonAnySetter + private Map additionalProperties = new HashMap<>(); + + private Builder() {} + + public Builder from(TextResponseFormat other) { + return this; + } + + public TextResponseFormat build() { + return new TextResponseFormat(additionalProperties); + } + } }