Skip to content

Add support for Jsonb generics #56

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

Merged
merged 5 commits into from
Jan 2, 2023
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public final class ExampleRetry implements RetryHandler {

final var code = response.statusCode();

if (retryCount >= MAX_RETRIES || code >= 400) {
if (retryCount >= MAX_RETRIES || code <= 400) {

return false;
}
Expand Down
10 changes: 5 additions & 5 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb</artifactId>
<version>1.0-RC1</version>
<version>1.1-RC2</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject</artifactId>
<version>8.6</version>
<version>8.10</version>
<optional>true</optional>
</dependency>

Expand Down Expand Up @@ -76,14 +76,14 @@
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>4.1.1</version>
<version>5.2.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-api</artifactId>
<version>1.16</version>
<version>1.20</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -125,7 +125,7 @@
<path>
<groupId>io.avaje</groupId>
<artifactId>avaje-inject-generator</artifactId>
<version>8.6</version>
<version>8.10</version>
</path>
</annotationProcessorPaths>
</configuration>
Expand Down
19 changes: 19 additions & 0 deletions client/src/main/java/io/avaje/http/client/BodyAdapter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.avaje.http.client;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
Expand All @@ -23,11 +24,29 @@ public interface BodyAdapter {
*/
<T> BodyReader<T> beanReader(Class<T> type);

/**
* Return a BodyReader to read response content and convert to a bean.
*
* @param type The bean type to convert the content to.
*/
default <T> BodyReader<T> beanReader(ParameterizedType type) {
throw new UnsupportedOperationException("Parameterized types not supported for this adapter");
}


/**
* Return a BodyReader to read response content and convert to a list of beans.
*
* @param type The bean type to convert the content to.
*/
<T> BodyReader<List<T>> listReader(Class<T> type);

/**
* Return a BodyReader to read response content and convert to a list of beans.
*
* @param type The bean type to convert the content to.
*/
default <T> BodyReader<List<T>> listReader(ParameterizedType type) {
throw new UnsupportedOperationException("Parameterized types not supported for this adapter");
}
}
22 changes: 22 additions & 0 deletions client/src/main/java/io/avaje/http/client/DHttpAsync.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.http.client;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -78,4 +79,25 @@ public <E> CompletableFuture<Stream<E>> stream(Class<E> type) {
.performSendAsync(false, HttpResponse.BodyHandlers.ofLines())
.thenApply(httpResponse -> request.asyncStream(type, httpResponse));
}

@Override
public <E> CompletableFuture<E> bean(ParameterizedType type) {
return request
.performSendAsync(true, HttpResponse.BodyHandlers.ofByteArray())
.thenApply(httpResponse -> request.asyncBean(type, httpResponse));
}

@Override
public <E> CompletableFuture<List<E>> list(ParameterizedType type) {
return request
.performSendAsync(true, HttpResponse.BodyHandlers.ofByteArray())
.thenApply(httpResponse -> request.asyncList(type, httpResponse));
}

@Override
public <E> CompletableFuture<Stream<E>> stream(ParameterizedType type) {
return request
.performSendAsync(false, HttpResponse.BodyHandlers.ofLines())
.thenApply(httpResponse -> request.asyncStream(type, httpResponse));
}
}
67 changes: 61 additions & 6 deletions client/src/main/java/io/avaje/http/client/DHttpCall.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.http.client;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -64,6 +65,21 @@ public <E> HttpCall<Stream<E>> stream(Class<E> type) {
return new CallStream<>(type);
}

@Override
public <E> HttpCall<E> bean(ParameterizedType type) {
return new CallBean<>(type);
}

@Override
public <E> HttpCall<List<E>> list(ParameterizedType type) {
return new CallList<>(type);
}

@Override
public <E> HttpCall<Stream<E>> stream(ParameterizedType type) {
return new CallStream<>(type);
}

private class CallVoid implements HttpCall<HttpResponse<Void>> {
@Override
public HttpResponse<Void> execute() {
Expand Down Expand Up @@ -132,46 +148,85 @@ public CompletableFuture<HttpResponse<InputStream>> async() {

private class CallBean<E> implements HttpCall<E> {
private final Class<E> type;
private final ParameterizedType genericType;
private final boolean isGeneric;

CallBean(Class<E> type) {
this.isGeneric = false;
this.type = type;
this.genericType = null;
}

CallBean(ParameterizedType type) {
this.isGeneric = true;
this.type = null;
this.genericType = type;
}

@Override
public E execute() {
return request.bean(type);
return isGeneric ? request.bean(genericType) : request.bean(type);
}

@Override
public CompletableFuture<E> async() {
return request.async().bean(type);
return isGeneric ? request.async().bean(genericType) : request.async().bean(type);
}
}

private class CallList<E> implements HttpCall<List<E>> {
private final Class<E> type;
private final ParameterizedType genericType;
private final boolean isGeneric;

CallList(Class<E> type) {
this.isGeneric = false;
this.type = type;
this.genericType = null;
}

CallList(ParameterizedType type) {
this.isGeneric = true;
this.type = null;
this.genericType = type;
}

@Override
public List<E> execute() {
return request.list(type);
return isGeneric ? request.list(genericType) : request.list(type);
}

@Override
public CompletableFuture<List<E>> async() {
return request.async().list(type);
return isGeneric ? request.async().list(genericType) : request.async().list(type);
}
}

private class CallStream<E> implements HttpCall<Stream<E>> {
private final Class<E> type;
private final ParameterizedType genericType;
private final boolean isGeneric;

CallStream(Class<E> type) {
this.isGeneric = false;
this.type = type;
this.genericType = null;
}

CallStream(ParameterizedType type) {
this.isGeneric = true;
this.type = null;
this.genericType = type;
}

@Override
public Stream<E> execute() {
return request.stream(type);
return isGeneric ? request.stream(genericType) : request.stream(type);
}

@Override
public CompletableFuture<Stream<E>> async() {
return request.async().stream(type);
return isGeneric ? request.async().stream(genericType) : request.async().stream(type);
}
}

Expand Down
14 changes: 14 additions & 0 deletions client/src/main/java/io/avaje/http/client/DHttpClientContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
Expand Down Expand Up @@ -273,6 +274,10 @@ <T> BodyReader<T> beanReader(Class<T> cls) {
return bodyAdapter.beanReader(cls);
}

<T> BodyReader<T> beanReader(ParameterizedType cls) {
return bodyAdapter.beanReader(cls);
}

<T> T readBean(Class<T> cls, BodyContent content) {
return bodyAdapter.beanReader(cls).read(content);
}
Expand All @@ -281,6 +286,15 @@ <T> List<T> readList(Class<T> cls, BodyContent content) {
return bodyAdapter.listReader(cls).read(content);
}

@SuppressWarnings("unchecked")
<T> T readBean(ParameterizedType cls, BodyContent content) {
return (T) bodyAdapter.beanReader(cls).read(content);
}

<T> List<T> readList(ParameterizedType cls, BodyContent content) {
return (List<T>) bodyAdapter.listReader(cls).read(content);
}

void afterResponse(DHttpClientRequest request) {
metricResTotal.add(1);
metricResMicros.add(request.responseTimeMicros());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.net.ssl.SSLSession;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
Expand Down Expand Up @@ -433,7 +434,7 @@ public <T> List<T> list(Class<T> cls) {
readResponseContent();
return context.readList(cls, encodedResponseBody);
}

@Override
public <T> Stream<T> stream(Class<T> cls) {
final HttpResponse<Stream<String>> res = handler(HttpResponse.BodyHandlers.ofLines());
Expand All @@ -445,6 +446,31 @@ public <T> Stream<T> stream(Class<T> cls) {
return res.body().map(bodyReader::readBody);
}


@Override
public <T> T bean(ParameterizedType cls) {
readResponseContent();
return context.readBean(cls, encodedResponseBody);
}

@Override
public <T> List<T> list(ParameterizedType cls) {
readResponseContent();
return context.readList(cls, encodedResponseBody);
}


@Override
public <T> Stream<T> stream(ParameterizedType cls) {
final HttpResponse<Stream<String>> res = handler(HttpResponse.BodyHandlers.ofLines());
this.httpResponse = res;
if (res.statusCode() >= 300) {
throw new HttpException(res, context);
}
final BodyReader<T> bodyReader = context.beanReader(cls);
return res.body().map(bodyReader::readBody);
}

@Override
public <T> HttpResponse<T> handler(HttpResponse.BodyHandler<T> responseHandler) {
final HttpResponse<T> response = sendWith(responseHandler);
Expand Down Expand Up @@ -506,6 +532,28 @@ protected <E> Stream<E> asyncStream(Class<E> type, HttpResponse<Stream<String>>
return response.body().map(bodyReader::readBody);
}

protected <E> E asyncBean(ParameterizedType type, HttpResponse<byte[]> response) {
afterAsyncEncoded(response);
return context.readBean(type, encodedResponseBody);
}

protected <E> List<E> asyncList(ParameterizedType type, HttpResponse<byte[]> response) {
afterAsyncEncoded(response);
return context.readList(type, encodedResponseBody);
}

protected <E> Stream<E> asyncStream(
ParameterizedType type, HttpResponse<Stream<String>> response) {
responseTimeNanos = System.nanoTime() - startAsyncNanos;
httpResponse = response;
context.afterResponse(this);
if (response.statusCode() >= 300) {
throw new HttpException(response, context);
}
final BodyReader<E> bodyReader = context.beanReader(type);
return response.body().map(bodyReader::readBody);
}

private void afterAsyncEncoded(HttpResponse<byte[]> response) {
responseTimeNanos = System.nanoTime() - startAsyncNanos;
httpResponse = response;
Expand Down
26 changes: 26 additions & 0 deletions client/src/main/java/io/avaje/http/client/HttpAsyncResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.http.client;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -334,4 +335,29 @@ default <E> CompletableFuture<HttpResponse<E>> withHandler(HttpResponse.BodyHand
* @return The CompletableFuture of the response
*/
<E> CompletableFuture<Stream<E>> stream(Class<E> type);

/**
* Process expecting a bean response body (typically from json content).
*
* @param type The parameterized type to convert the content to
* @return The CompletableFuture of the response
*/
<E> CompletableFuture<E> bean(ParameterizedType type);

/**
* Process expecting a list of beans response body (typically from json content).
*
* @param type The parameterized type to convert the content to
* @return The CompletableFuture of the response
*/
<E> CompletableFuture<List<E>> list(ParameterizedType type);

/**
* Process response as a stream of beans (x-json-stream).
*
* @param type The parameterized type to convert the content to
* @return The CompletableFuture of the response
*/
<E> CompletableFuture<Stream<E>> stream(ParameterizedType type);

}
Loading