Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2.1.1 #111

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>cleverclient</artifactId>
<version>2.1.0</version>
<version>2.1.1</version>
<packaging>jar</packaging>

<name>cleverclient</name>
Expand Down
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 @@ -140,7 +140,7 @@ private FunctionsByCategory getFunctions(ReturnType returnType) {
}

private Request convertToOkHttpRequest(RequestData request) {
var requestBody = createRequestBody(request.getBody(), request.getContentType());
var requestBody = createRequestBody(request.getBody(), request.getContentType(), request.getHttpMethod());
var headersArray = request.getHeaders().toArray(new String[0]);
var requestBuilder = new Request.Builder()
.url(request.getUrl())
Expand All @@ -152,10 +152,13 @@ private Request convertToOkHttpRequest(RequestData request) {
}

@SuppressWarnings("unchecked")
private RequestBody createRequestBody(Object bodyObject, ContentType contentType) {
private RequestBody createRequestBody(Object bodyObject, ContentType contentType, String httpMethod) {
RequestBody requestBody = null;
if (contentType == null) {
logger.debug(REQUEST_BODY_FORMAT, "(Empty)");
if (httpMethod.equalsIgnoreCase("POST")) {
requestBody = RequestBody.create("", null);
}
} else if (contentType == ContentType.MULTIPART_FORMDATA) {
logger.debug(REQUEST_BODY_FORMAT, bodyObject);
var bodyBytes = HttpMultipart.toByteArrays((Map<String, Object>) bodyObject);
Expand Down Expand Up @@ -259,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 @@ -273,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
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,28 @@ default void shouldShutdownWithoutExceptions() {
testShutdown();
}

@Test
default void shouldReturnAnObjectSyncWhenIsPostMethodAndBodyIsEmpty() throws IOException, InterruptedException {
setMocksForString(SyncType.SYNC, "{\"id\":100,\"description\":\"Description\",\"active\":true}");

var service = getHttpProcessor().createProxy(ITest.SyncService.class);
var actualDemo = service.cancelDemo(100);
var expectedDemo = new ITest.Demo(100, "Description", true);

assertEquals(expectedDemo, actualDemo);
}

@Test
default void shouldReturnAnObjectAsyncWhenIsPostMethodAndBodyIsEmpty() throws IOException, InterruptedException {
setMocksForString(SyncType.ASYNC, "{\"id\":100,\"description\":\"Description\",\"active\":true}");

var service = getHttpProcessor().createProxy(ITest.AsyncService.class);
var actualDemo = service.cancelDemo(100).join();
var expectedDemo = new ITest.Demo(100, "Description", true);

assertEquals(expectedDemo, actualDemo);
}

private String transformUsers(String jsonInput) {
List<String> flatUsers = new ArrayList<>();
String patternStr = "\"id\":\\s*(\\d+).*?" + // id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ interface AsyncService {
@GET("/{demoId}")
CompletableFuture<Demo> getDemo(@Path("demoId") Integer demoId);

@POST
CompletableFuture<Demo> cancelDemo(@Path("demoId") Integer demoId);

@GET("/{genericDemoId}")
CompletableFuture<Generic<Demo>> getGenericDemo(@Path("genericDemoId") Integer genericDemoId);

Expand Down Expand Up @@ -113,6 +116,9 @@ interface SyncService {
@GET("/{demoId}")
Demo getDemo(@Path("demoId") Integer demoId);

@POST
Demo cancelDemo(@Path("demoId") Integer demoId);

@GET("/{genericDemoId}")
Generic<Demo> getGenericDemo(@Path("genericDemoId") Integer genericDemoId);

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.
Loading