Skip to content

Commit

Permalink
Add common method deserializeDataInResponseAsList
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jan 5, 2024
1 parent 6625886 commit dd94b70
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
11 changes: 1 addition & 10 deletions src/main/java/io/github/stefanbratanov/chatjpt/FilesClient.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.github.stefanbratanov.chatjpt;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand Down Expand Up @@ -62,12 +58,7 @@ public List<File> listFiles() {
HttpRequest httpRequest =
newHttpRequestBuilder().uri(baseUrl.resolve(Endpoint.FILES.getPath())).GET().build();
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest);
try {
JsonNode models = objectMapper.readTree(httpResponse.body());
return objectMapper.readValue(models.get("data").toString(), new TypeReference<>() {});
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return deserializeDataInResponseAsList(httpResponse.body(), File.class);
}

/**
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/io/github/stefanbratanov/chatjpt/ModelsClient.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.github.stefanbratanov.chatjpt;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand Down Expand Up @@ -37,12 +33,7 @@ public List<Model> listModels() {
HttpRequest httpRequest =
newHttpRequestBuilder().uri(baseUrl.resolve(Endpoint.MODELS.getPath())).GET().build();
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest);
try {
JsonNode models = objectMapper.readTree(httpResponse.body());
return objectMapper.readValue(models.get("data").toString(), new TypeReference<>() {});
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return deserializeDataInResponseAsList(httpResponse.body(), Model.class);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/github/stefanbratanov/chatjpt/OpenAIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ <T> T deserializeResponse(byte[] response, Class<T> responseClass) {
}
}

<T> List<T> deserializeDataInResponseAsList(byte[] response, Class<T> elementType) {
try {
JsonNode responseNode = objectMapper.readTree(response);
return objectMapper.readValue(
responseNode.get("data").traverse(),
objectMapper.getTypeFactory().constructCollectionType(List.class, elementType));
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}

JsonNode deserializeResponseAsTree(byte[] response) {
try {
return objectMapper.readTree(response);
Expand Down

0 comments on commit dd94b70

Please sign in to comment.