Skip to content

Commit

Permalink
Handle OkHttp read binary result
Browse files Browse the repository at this point in the history
  • Loading branch information
sashirestela committed Jan 24, 2025
1 parent bd859e6 commit 2d4cda5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.sashirestela.cleverclient.CleverClient;
import io.github.sashirestela.cleverclient.annotation.GET;
import io.github.sashirestela.cleverclient.annotation.Path;
import io.github.sashirestela.cleverclient.annotation.Resource;

import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -20,21 +21,22 @@ public FileDownloadExample() {

public void run() throws IOException {
var cleverClient = CleverClient.builder()
.baseUrl("https://via.placeholder.com")
.baseUrl("https://en.wikipedia.org")
.clientAdapter(clientAdapter)
.build();

var imageService = cleverClient.create(ImageService.class);
var binaryData = imageService.getImage("92c952");
var binaryData = imageService.getImage("wikipedia.png");
var file = new FileOutputStream("src/test/resources/download.png");
file.write(binaryData.readAllBytes());
file.close();
}

@Resource("/static/images/icons")
static interface ImageService {

@GET("/150/{id}")
InputStream getImage(@Path("id") String id);
@GET("/{name}")
InputStream getImage(@Path("name") String name);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected Object send(RequestData request) {
try {
var response = okHttpClient.newCall(okHttpRequest).execute();
logger.debug(RESPONSE_CODE_FORMAT, response.code());
if (returnType.isStream()) {
if (returnType.isStream() || returnType.isInputStream()) {
var responseContent = getResponseContent(response.body(), returnType);
var originalResponseData = convertToResponseData(response, responseContent);
throwExceptionIfErrorIsPresent(originalResponseData);
Expand Down Expand Up @@ -96,7 +96,7 @@ public void onFailure(Call call, IOException e) {
@Override
public void onResponse(Call call, Response response) throws IOException {
logger.debug(RESPONSE_CODE_FORMAT, response.code());
if (returnType.isStream()) {
if (returnType.isStream() || returnType.isInputStream()) {
try {
var responseContent = getResponseContent(response.body(), returnType);
var originalResponseData = convertToResponseData(response, responseContent);
Expand Down Expand Up @@ -262,7 +262,7 @@ public FunctionsByCategory(BiFunction<Object, ReturnType, Object> responseConver
private void fillFunctionsByCategory() {
this.functionsByCategoryMap = new EnumMap<>(Category.class);
functionsByCategoryMap.put(Category.SYNC_BINARY, new FunctionsByCategory(
(r, t) -> r));
(r, t) -> ((ResponseData) r).getBody()));
functionsByCategoryMap.put(Category.SYNC_PLAIN_TEXT, new FunctionsByCategory(
(r, t) -> r));
functionsByCategoryMap.put(Category.SYNC_CUSTOM, new FunctionsByCategory(
Expand All @@ -276,7 +276,7 @@ private void fillFunctionsByCategory() {
functionsByCategoryMap.put(Category.SYNC_STREAM_EVENT, new FunctionsByCategory(
(r, t) -> convertToStreamOfEvents((ResponseData) r, t)));
functionsByCategoryMap.put(Category.ASYNC_BINARY, new FunctionsByCategory(
(r, t) -> r));
(r, t) -> ((ResponseData) r).getBody()));
functionsByCategoryMap.put(Category.ASYNC_PLAIN_TEXT, new FunctionsByCategory(
(r, t) -> r));
functionsByCategoryMap.put(Category.ASYNC_CUSTOM, new FunctionsByCategory(
Expand Down
Binary file modified src/test/resources/download.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2d4cda5

Please sign in to comment.