Skip to content

[http client] Use more generic Type rather than ParameterizedType #194

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 3 commits into from
Apr 3, 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
14 changes: 7 additions & 7 deletions http-client/src/main/java/io/avaje/http/client/BodyAdapter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.avaje.http.client;

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

/**
Expand All @@ -22,9 +22,9 @@ public interface BodyAdapter {
*
* @param type The type of the bean this writer is for
*/
default <T> BodyWriter<T> beanWriter(ParameterizedType type) {
default <T> BodyWriter<T> beanWriter(Type type) {

throw new UnsupportedOperationException("Parameterized types not supported for this adapter");
throw new UnsupportedOperationException("java.lang.reflect.Type is not supported for this adapter");
}

/**
Expand All @@ -39,8 +39,8 @@ default <T> BodyWriter<T> beanWriter(ParameterizedType type) {
*
* @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");
default <T> BodyReader<T> beanReader(Type type) {
throw new UnsupportedOperationException("java.lang.reflect.Type is not supported for this adapter");
}

/**
Expand All @@ -55,7 +55,7 @@ default <T> BodyReader<T> beanReader(ParameterizedType type) {
*
* @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");
default <T> BodyReader<List<T>> listReader(Type type) {
throw new UnsupportedOperationException("java.lang.reflect.Type is not supported for this adapter");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand Down Expand Up @@ -269,15 +269,15 @@ <T> BodyContent write(T bean, Class<?> type, String contentType) {
return bodyAdapter.beanWriter(type).write(bean, contentType);
}

<T> BodyContent write(T bean, ParameterizedType type, String contentType) {
<T> BodyContent write(T bean, Type type, String contentType) {
return bodyAdapter.beanWriter(type).write(bean, contentType);
}

<T> BodyReader<T> beanReader(Class<T> type) {
return bodyAdapter.beanReader(type);
}

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

Expand All @@ -290,12 +290,12 @@ <T> List<T> readList(Class<T> type, BodyContent content) {
}

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

@SuppressWarnings("unchecked")
<T> List<T> readList(ParameterizedType type, BodyContent content) {
<T> List<T> readList(Type type, BodyContent content) {
return (List<T>) bodyAdapter.listReader(type).read(content);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.net.ssl.SSLSession;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
Expand Down Expand Up @@ -282,7 +282,7 @@ public HttpClientRequest body(Object bean, Class<?> type) {
}

@Override
public HttpClientRequest body(Object bean, ParameterizedType type) {
public HttpClientRequest body(Object bean, Type type) {
encodedRequestBody = context.write(bean, type, null);
return this;
}
Expand Down Expand Up @@ -465,7 +465,7 @@ public <T> HttpResponse<T> as(Class<T> type) {
}

@Override
public <T> HttpResponse<T> as(ParameterizedType type) {
public <T> HttpResponse<T> as(Type type) {
return new HttpWrapperResponse<>(bean(type), httpResponse);
}

Expand All @@ -476,7 +476,7 @@ public <T> T bean(Class<T> type) {
}

@Override
public <T> T bean(ParameterizedType type) {
public <T> T bean(Type type) {
readResponseContent();
return context.readBean(type, encodedResponseBody);
}
Expand All @@ -487,7 +487,7 @@ public <T> HttpResponse<List<T>> asList(Class<T> type) {
}

@Override
public <T> HttpResponse<List<T>> asList(ParameterizedType type) {
public <T> HttpResponse<List<T>> asList(Type type) {
return new HttpWrapperResponse<>(list(type), httpResponse);
}

Expand All @@ -498,7 +498,7 @@ public <T> List<T> list(Class<T> type) {
}

@Override
public <T> List<T> list(ParameterizedType type) {
public <T> List<T> list(Type type) {
readResponseContent();
return context.readList(type, encodedResponseBody);
}
Expand All @@ -509,7 +509,7 @@ public <T> HttpResponse<Stream<T>> asStream(Class<T> type) {
}

@Override
public <T> HttpResponse<Stream<T>> asStream(ParameterizedType type) {
public <T> HttpResponse<Stream<T>> asStream(Type type) {
return new HttpWrapperResponse<>(stream(type), httpResponse);
}

Expand All @@ -519,7 +519,7 @@ public <T> Stream<T> stream(Class<T> type) {
}

@Override
public <T> Stream<T> stream(ParameterizedType type) {
public <T> Stream<T> stream(Type type) {
return stream(context.beanReader(type));
}

Expand Down Expand Up @@ -577,7 +577,7 @@ protected <E> HttpResponse<E> asyncBean(Class<E> type, HttpResponse<byte[]> resp
return new HttpWrapperResponse<>(context.readBean(type, encodedResponseBody), httpResponse);
}

protected <E> E asyncBean(ParameterizedType type, HttpResponse<byte[]> response) {
protected <E> E asyncBean(Type type, HttpResponse<byte[]> response) {
afterAsyncEncoded(response);
return context.readBean(type, encodedResponseBody);
}
Expand All @@ -587,7 +587,7 @@ protected <E> HttpResponse<List<E>> asyncList(Class<E> type, HttpResponse<byte[]
return new HttpWrapperResponse<>(context.readList(type, encodedResponseBody), httpResponse);
}

protected <E> HttpResponse<List<E>> asyncList(ParameterizedType type, HttpResponse<byte[]> response) {
protected <E> HttpResponse<List<E>> asyncList(Type type, HttpResponse<byte[]> response) {
afterAsyncEncoded(response);
return new HttpWrapperResponse<>(context.readList(type, encodedResponseBody), httpResponse);
}
Expand All @@ -603,7 +603,7 @@ protected <E> HttpResponse<Stream<E>> asyncStream(Class<E> type, HttpResponse<St
return new HttpWrapperResponse<>(response.body().map(bodyReader::readBody), httpResponse);
}

protected <E> HttpResponse<Stream<E>> asyncStream(ParameterizedType type, HttpResponse<Stream<String>> response) {
protected <E> HttpResponse<Stream<E>> asyncStream(Type type, HttpResponse<Stream<String>> response) {
responseTimeNanos = System.nanoTime() - startAsyncNanos;
httpResponse = response;
context.afterResponse(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.avaje.http.client;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
Expand Down Expand Up @@ -324,7 +324,7 @@ default HttpClientRequest queryParam(String name, Collection<String> values) {
* @param type The parameterized type used by the body content adapter to write the body content
* @return The request being built
*/
HttpClientRequest body(Object bean, ParameterizedType type);
HttpClientRequest body(Object bean, Type type);

/**
* Set the body as a bean with the given content type and additionally specifying
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.avaje.http.client;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.http.HttpResponse;
import java.nio.file.Path;
import java.util.List;
Expand Down Expand Up @@ -83,7 +83,7 @@ public interface HttpClientResponse {
* @return The response containing the converted body.
* @throws HttpException when the response has error status codes
*/
<T> HttpResponse<T> as(ParameterizedType type);
<T> HttpResponse<T> as(Type type);

/**
* Return the response with the body containing a list of the given type.
Expand All @@ -109,7 +109,7 @@ public interface HttpClientResponse {
* @return The response containing the converted body.
* @throws HttpException when the response has error status codes
*/
<T> HttpResponse<List<T>> asList(ParameterizedType type);
<T> HttpResponse<List<T>> asList(Type type);

/**
* Return the response with the body containing a stream of beans of the given type.
Expand Down Expand Up @@ -151,7 +151,7 @@ public interface HttpClientResponse {
* @return The response containing the converted body.
* @throws HttpException when the response has error status codes
*/
<T> HttpResponse<Stream<T>> asStream(ParameterizedType type);
<T> HttpResponse<Stream<T>> asStream(Type type);

/**
* Return the response as a single bean.
Expand Down Expand Up @@ -210,7 +210,7 @@ public interface HttpClientResponse {
* @return The bean the response is converted into.
* @throws HttpException when the response has error status codes
*/
<T> T bean(ParameterizedType type);
<T> T bean(Type type);

/**
* Return the response as a list of beans.
Expand All @@ -222,7 +222,7 @@ public interface HttpClientResponse {
* @return The list of beans the response is converted into.
* @throws HttpException when the response has error status codes
*/
<T> List<T> list(ParameterizedType type);
<T> List<T> list(Type type);

/**
* Return the response as a stream of beans.
Expand All @@ -242,7 +242,7 @@ public interface HttpClientResponse {
* @return The stream of beans from the response
* @throws HttpException when the response has error status codes
*/
<T> Stream<T> stream(ParameterizedType type);
<T> Stream<T> stream(Type type);

/**
* Return the response with check for 200 range status code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ public HttpException(int statusCode, Throwable cause) {
}

HttpException(HttpResponse<?> httpResponse, DHttpClientContext context) {
super();
super("Http Call failed with status: " + httpResponse.statusCode());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll change that to "Http call ..." using lower case "c" for "call" as we have HttpCall as a type

this.httpResponse = httpResponse;
this.statusCode = httpResponse.statusCode();
this.context = context;
this.responseAsBytes = false;
}

HttpException(DHttpClientContext context, HttpResponse<byte[]> httpResponse) {
super();
super("Http Call failed with status: " + httpResponse.statusCode());
this.httpResponse = httpResponse;
this.statusCode = httpResponse.statusCode();
this.context = context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.avaje.http.client;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -49,7 +48,7 @@ public <T> BodyWriter<T> beanWriter(Class<?> cls) {

@SuppressWarnings("unchecked")
@Override
public <T> BodyWriter<T> beanWriter(ParameterizedType type) {
public <T> BodyWriter<T> beanWriter(Type type) {
return (BodyWriter<T>) beanWriterCache.computeIfAbsent(type, aClass -> new JWriter<>(jsonb.type(type)));
}

Expand All @@ -61,13 +60,13 @@ public <T> BodyReader<T> beanReader(Class<T> cls) {

@SuppressWarnings("unchecked")
@Override
public <T> BodyReader<T> beanReader(ParameterizedType type) {
public <T> BodyReader<T> beanReader(Type type) {
return (BodyReader<T>) beanReaderCache.computeIfAbsent(type, aClass -> new JReader<>(jsonb.type(type)));
}

@SuppressWarnings("unchecked")
@Override
public <T> BodyReader<List<T>> listReader(ParameterizedType type) {
public <T> BodyReader<List<T>> listReader(Type type) {
return (BodyReader<List<T>>) listReaderCache.computeIfAbsent(type, aClass -> new JReader<>(jsonb.type(type).list()));
}

Expand Down