diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java index 0b6568b..7c3ba09 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java @@ -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; @@ -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); } diff --git a/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java b/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java index 1642085..e04d09f 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java +++ b/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java @@ -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); @@ -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); @@ -262,7 +262,7 @@ public FunctionsByCategory(BiFunction 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( @@ -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( diff --git a/src/test/resources/download.png b/src/test/resources/download.png index 73bcf8c..4dd2046 100644 Binary files a/src/test/resources/download.png and b/src/test/resources/download.png differ