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

[http-client] Expose more client attributes / Enhance @Path Annotation #299

Merged
merged 12 commits into from
Oct 5, 2023
23 changes: 6 additions & 17 deletions http-api/src/main/java/io/avaje/http/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,14 @@
* }
*
* }</pre>
*
* <h3>Client.Import</h3>
*
* <p>When the client interface already exists in another module we use <code>Client.Import</code>
* to generate the client.
*
* <p>Specify the <code>@Client.Import</code> on the package or class to refer to the client
* interface we want to generate.
*
* <pre>{@code
* @Client.Import(types = OtherApi.class)
* package org.example;
*
* }</pre>
*/
@Target(value = TYPE)
@Retention(value = RUNTIME)
@Target(TYPE)
@Retention(RUNTIME)
public @interface Client {

/** Specify the path mapping request to the controller. */
String value() default "";

/**
* Flag to set whether to generate a Client Implementation. Set false if the interface exists merely to be extended by
* other client interfaces
Expand All @@ -57,7 +46,7 @@
*
* }</pre>
*/
@Target(value = {TYPE, PACKAGE})
@Target({TYPE, PACKAGE})
@Retention(RUNTIME)
@interface Import {

Expand Down
4 changes: 2 additions & 2 deletions http-api/src/main/java/io/avaje/http/api/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* </p>
*
*/
@Target(value={PARAMETER, FIELD})
@Retention(value=RUNTIME)
@Target({PARAMETER, FIELD})
@Retention(RUNTIME)
public @interface Cookie {

/**
Expand Down
4 changes: 2 additions & 2 deletions http-api/src/main/java/io/avaje/http/api/Default.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*
* }</pre>
*/
@Target(value = {PARAMETER, FIELD})
@Retention(value = RUNTIME)
@Target({PARAMETER, FIELD})
@Retention(RUNTIME)
public @interface Default {

/** The default values. */
Expand Down
4 changes: 2 additions & 2 deletions http-api/src/main/java/io/avaje/http/api/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
*
* }</pre>
*/
@Target(value={PARAMETER,METHOD})
@Retention(value=RUNTIME)
@Target({PARAMETER,METHOD})
@Retention(RUNTIME)
public @interface Form {

}
4 changes: 2 additions & 2 deletions http-api/src/main/java/io/avaje/http/api/FormParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
/**
* Marks a method parameter to be a form parameter.
*/
@Target(value={PARAMETER,FIELD})
@Retention(value=RUNTIME)
@Target({PARAMETER,FIELD})
@Retention(RUNTIME)
public @interface FormParam {

/**
Expand Down
4 changes: 2 additions & 2 deletions http-api/src/main/java/io/avaje/http/api/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*
* }</pre>
*/
@Target(value={PARAMETER,FIELD})
@Retention(value=RUNTIME)
@Target({PARAMETER,FIELD})
@Retention(RUNTIME)
public @interface Header {

/**
Expand Down
4 changes: 2 additions & 2 deletions http-api/src/main/java/io/avaje/http/api/MatrixParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.lang.annotation.Target;

/** Marks a method parameter to be a matrix parameter. */
@Target(value = {PARAMETER})
@Retention(value = RUNTIME)
@Target({PARAMETER})
@Retention(RUNTIME)
public @interface MatrixParam {

/**
Expand Down
33 changes: 17 additions & 16 deletions http-api/src/main/java/io/avaje/http/api/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.MODULE;
import static java.lang.annotation.ElementType.PACKAGE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Specify the path mapping request to the controller.
* Specify the path mapping request to a controller. When placed on a package-info or a module-info
* file, all routes in the package/module will have value added as a prefix
*
* <pre>{@code
*
* @Controller
* @Path("/customers")
* class CustomerController {
* ...
* }
*
* @Controller
* @Path("/customers")
rbygrave marked this conversation as resolved.
Show resolved Hide resolved
* class CustomerController {
* ...
* }
* }</pre>
* <pre>{@code
* @Path("/customers") // all routes in this module will have a customers prefix
* module example.module {
* ...
* }
*
* <h4>JAX-RS note</h4>
* <p>
* Note that unlike JAX-RS we only use <code>@Path</code> on the controller type and don't
* use it on the methods. This is because the <code>@Get, @Post etc</code> annotations
* include a path as well.
* </p>
* }</pre>
*/
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Target({TYPE, PACKAGE, MODULE})
@Retention(RUNTIME)
public @interface Path {
String value();
}
4 changes: 2 additions & 2 deletions http-api/src/main/java/io/avaje/http/api/QueryParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
/**
* Marks a method parameter to be a query parameter.
*/
@Target(value={PARAMETER,FIELD})
@Retention(value=RUNTIME)
@Target({PARAMETER,FIELD})
@Retention(RUNTIME)
public @interface QueryParam {

/**
Expand Down
3 changes: 3 additions & 0 deletions http-api/src/main/java/io/avaje/http/api/RequestTimeout.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
@Target(METHOD)
@Retention(RUNTIME)
public @interface RequestTimeout {

/** How long the timeout should be */
long value();

/** Unit of time of the timeout value */
ChronoUnit chronoUnit() default ChronoUnit.MILLIS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class DHttpClientRequest implements HttpClientRequest, HttpClientResponse {

private static final String CONTENT_TYPE = "Content-Type";
private static final String CONTENT_ENCODING = "Content-Encoding";
private static final String VERB_GET = "GET";
private static final String VERB_POST = "POST";
private static final String VERB_PUT = "PUT";
private static final String VERB_DELETE = "DELETE";
private static final String VERB_HEAD = "HEAD";
private static final String VERB_PATCH = "PATCH";
Expand Down Expand Up @@ -58,6 +61,7 @@ class DHttpClientRequest implements HttpClientRequest, HttpClientResponse {
private Map<String, Object> customAttributes;
protected Function<HttpException, RuntimeException> errorMapper;
protected boolean isRetry;
private String method;

DHttpClientRequest(DHttpClientContext context, Duration requestTimeout) {
this.context = context;
Expand All @@ -66,6 +70,20 @@ class DHttpClientRequest implements HttpClientRequest, HttpClientResponse {
this.errorMapper = context.errorMapper();
}

public String method() {
rbygrave marked this conversation as resolved.
Show resolved Hide resolved
return method;
}

@Override
public String url() {
return url.build();
}

@Override
public Optional<BodyContent> bodyContent() {
return Optional.ofNullable(encodedRequestBody);
}

@Override
public HttpClientRequest skipAuthToken() {
this.skipAuthToken = true;
Expand Down Expand Up @@ -164,10 +182,18 @@ public HttpClientRequest header(Map<String, ?> headers) {
@Override
public List<String> header(String name) {
if (headers == null) {
return Collections.emptyList();
return List.of();
rbygrave marked this conversation as resolved.
Show resolved Hide resolved
}
final List<String> values = headers.get(name);
return values == null ? Collections.emptyList() : values;
return values == null ? List.of() : values;
}

@Override
public Map<String, List<String>> headers() {
if (headers == null) {
return Map.of();
}
return headers;
}

@Override
Expand Down Expand Up @@ -421,6 +447,7 @@ public HttpClientResponse HEAD() {
return this;
}

@Override
public HttpClientResponse GET() {
httpRequest = newGet(url.build());
return this;
Expand Down Expand Up @@ -731,14 +758,17 @@ private HttpRequest.Builder newHead(String url) {
}

private HttpRequest.Builder newGet(String url) {
this.method = VERB_GET;
return newReq(url).GET();
}

private HttpRequest.Builder newPost(String url, HttpRequest.BodyPublisher body) {
this.method = VERB_POST;
return newReq(url).POST(body);
}

private HttpRequest.Builder newPut(String url, HttpRequest.BodyPublisher body) {
this.method = VERB_PUT;
return newReq(url).PUT(body);
}

Expand All @@ -759,6 +789,7 @@ protected HttpRequest.Builder newRequest(String method, String url, HttpRequest.
if (body == null) {
throw new IllegalArgumentException("body is null but required for " + method + " to " + url);
}
this.method = method;
return HttpRequest.newBuilder()
.uri(URI.create(url))
.timeout(requestTimeout)
Expand All @@ -777,7 +808,7 @@ boolean isSkipAuthToken() {
return skipAuthToken;
}

private class ListenerEvent implements RequestListener.Event {
private class ListenerEvent implements RequestListener.Event {
rbygrave marked this conversation as resolved.
Show resolved Hide resolved

@Override
public long responseTimeMicros() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -145,6 +146,13 @@ public interface HttpClientRequest {
*/
List<String> header(String name);

/**
* Return the header values that have been set for this request.
*
* @return The headers values or an empty map if no headers have been specified yet.
*/
Map<String, List<String>> headers();

/**
* Set if body content should be gzip encoded.
*
Expand All @@ -171,6 +179,13 @@ public interface HttpClientRequest {
*/
HttpClientRequest url(String url);

/**
* The URL for this request including the query parameters.
*
* @return The url for this request
*/
String url();

/**
* Add a path segment to the URL.
*
Expand Down Expand Up @@ -389,6 +404,13 @@ default HttpClientRequest queryParam(String name, Collection<String> values) {
*/
HttpClientRequest body(HttpRequest.BodyPublisher body);

/**
* Get the body content for this request if available. Will return an empty optional for streaming calls
*
* @return The request body
*/
Optional<BodyContent> bodyContent();

/**
* Set the mapper used to transform {@link HttpException} into a different kind of exception.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

/**
* Common controller writer.
Expand Down
Loading