-
Notifications
You must be signed in to change notification settings - Fork 96
Description
The PR #353, that changed from Apache HTTP Client to OkHttp, still left a dependency to org.apache.httpcomponents:httpclient. It seems like this dependency isn't used very much, only in two deprecated methods in ApiException:
java-genai/src/main/java/com/google/genai/errors/ApiException.java
Lines 55 to 78 in 26c3c35
| /** | |
| * Throws an ApiException from the response if the response is not a OK status. | |
| * | |
| * @param response The response from the API call. | |
| * @deprecated Use {@link #throwFromResponse(Response)} instead. | |
| */ | |
| @ExcludeFromGeneratedCoverageReport | |
| @Deprecated | |
| public static void throwFromResponse(CloseableHttpResponse response) { | |
| StatusLine statusLine = response.getStatusLine(); | |
| int code = statusLine.getStatusCode(); | |
| if (code == HttpStatus.SC_OK) { | |
| return; | |
| } | |
| String status = statusLine.getReasonPhrase(); | |
| String message = getErrorMessageFromResponse(response); | |
| if (code >= 400 && code < 500) { // Client errors. | |
| throw new ClientException(code, status, message); | |
| } else if (code >= 500 && code < 600) { // Server errors. | |
| throw new ServerException(code, status, message); | |
| } else { | |
| throw new ApiException(code, status, message); | |
| } | |
| } |
java-genai/src/main/java/com/google/genai/errors/ApiException.java
Lines 102 to 127 in 26c3c35
| /** | |
| * Returns the error message from the response, if no error or error message is not found, then | |
| * returns an empty string. | |
| */ | |
| @ExcludeFromGeneratedCoverageReport | |
| @Deprecated | |
| static String getErrorMessageFromResponse(CloseableHttpResponse response) { | |
| HttpEntity entity = response.getEntity(); | |
| try { | |
| String responseBody = EntityUtils.toString(entity); | |
| if (responseBody == null || responseBody.isEmpty()) { | |
| return ""; | |
| } | |
| ObjectMapper mapper = new ObjectMapper(); | |
| JsonNode errorNode = mapper.readTree(responseBody).get("error"); | |
| if (errorNode != null && errorNode.isObject()) { | |
| JsonNode messageNode = errorNode.get("message"); | |
| if (messageNode != null && messageNode.isTextual()) { | |
| return messageNode.asText(); | |
| } | |
| } | |
| return ""; | |
| } catch (IOException ignored) { | |
| return ""; | |
| } | |
| } |
Would it be possible to remove these methods and get rid of the dependency on Apache HTTP Client? This would also remove the transitive dependency on old versions of commons-codec and commons-logging, the latter of which is particularly annoying as it has a vulnerable version of Log4j as optional dependency.