Skip to content

[http-client] Update the client generation to use HttpClient #133

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 1 commit into from
Jan 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public interface HttpApiProvider<T> {
/**
* Return the provided implementation of the API.
*/
T provide(HttpClientContext client);
T provide(HttpClient client);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.github;

import io.avaje.http.client.HttpApiProvider;
import io.avaje.http.client.HttpClientContext;
import io.avaje.http.client.HttpClient;
import org.example.github.httpclient.Simple$HttpClient;

public class SimpleProvider implements HttpApiProvider<Simple> {
Expand All @@ -12,7 +12,7 @@ public Class<Simple> type() {
}

@Override
public Simple provide(HttpClientContext client) {
public Simple provide(HttpClient client) {
return new Simple$HttpClient(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@

public class Simple$HttpClient implements Simple {

private final HttpClientContext context;
private final HttpClient context;
private final BodyReader<Repo> readRepo;
private final BodyWriter writeRepo;
// private final BodyConverter<List<Repo>, String> toListOfRepo;

public Simple$HttpClient(HttpClientContext context) {
public Simple$HttpClient(HttpClient context) {
this.context = context;
this.readRepo = context.converters().beanReader(Repo.class);
this.writeRepo = context.converters().beanWriter(Repo.class);
// this.toListOfRepo = context.converters().toListOf(Repo.class);
this.readRepo = context.bodyAdapter().beanReader(Repo.class);
this.writeRepo = context.bodyAdapter().beanWriter(Repo.class);
}

@Get("users/{user}/repos")
Expand Down Expand Up @@ -75,7 +73,7 @@ public Class<Simple> type() {
}

@Override
public Simple provide(HttpClientContext client) {
public Simple provide(HttpClient client) {
return new Simple$HttpClient(client);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void write() {
if (!method.isVoid()) {
writer.append("return ");
}
writer.append("clientContext.request()").eol();
writer.append("client.request()").eol();

PathSegments pathSegments = method.pathSegments();
Set<PathSegments.Segment> segments = pathSegments.segments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class ClientWriter extends BaseControllerWriter {

private static final String HTTP_CLIENT_CONTEXT = "io.avaje.http.client.HttpClientContext";
private static final String HTTP_CLIENT = "io.avaje.http.client.HttpClient";
private static final String HTTP_API_PROVIDER = "io.avaje.http.client.HttpApiProvider";

private static final String AT_GENERATED = "@Generated(\"avaje-http-client-generator\")";
Expand All @@ -25,7 +25,7 @@ class ClientWriter extends BaseControllerWriter {

ClientWriter(ControllerReader reader, ProcessingContext ctx, boolean useJsonB) throws IOException {
super(reader, ctx, SUFFIX);
reader.addImportType(HTTP_CLIENT_CONTEXT);
reader.addImportType(HTTP_CLIENT);
reader.addImportType(HTTP_API_PROVIDER);
this.useJsonb = useJsonB;
readMethods();
Expand Down Expand Up @@ -65,7 +65,7 @@ private void writeProvider() {
writer.append(" return %s.class;", shortName).eol();
writer.append(" }").eol();
writer.append(" @Override").eol();
writer.append(" public %s provide(HttpClientContext client) {", shortName).eol();
writer.append(" public %s provide(HttpClient client) {", shortName).eol();
writer.append(" return new %s%s(client);", shortName, SUFFIX).eol();
writer.append(" }").eol();
writer.append(" }").eol();
Expand All @@ -81,10 +81,10 @@ private void writeClassStart() {
writer.append(AT_GENERATED).eol();
writer.append("public class %s%s implements %s {", shortName, SUFFIX, shortName).eol().eol();

writer.append(" private final HttpClientContext clientContext;").eol().eol();
writer.append(" private final HttpClient client;").eol().eol();

writer.append(" public %s%s(HttpClientContext ctx) {", shortName, SUFFIX).eol();
writer.append(" this.clientContext = ctx;").eol();
writer.append(" public %s%s(HttpClient client) {", shortName, SUFFIX).eol();
writer.append(" this.client = client;").eol();
writer.append(" }").eol().eol();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.avaje.http.client.HttpApiProvider;
import io.avaje.http.client.HttpClient;
import io.avaje.http.client.HttpClientContext;
import io.avaje.http.client.JacksonBodyAdapter;

import org.example.httpclient.GitHubUsersHttpClient;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -45,7 +45,7 @@ public Class<GitHubUsers> type() {
}

@Override
public GitHubUsers provide(HttpClientContext client) {
public GitHubUsers provide(HttpClient client) {
return new GitHubUsersHttpClient(client);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package example.github;

//import io.avaje.http.api.Get;

import io.avaje.http.client.HttpApiProvider;
import io.avaje.http.client.HttpClientContext;
import io.avaje.http.client.HttpClient;
import io.avaje.http.client.HttpException;

import java.util.List;
Expand All @@ -19,15 +17,15 @@ public Class<Simple> type() {
}

@Override
public Simple provide(HttpClientContext client) {
public Simple provide(HttpClient client) {
return new SimpleClient(client);
}

private static class SimpleClient implements Simple {

private final HttpClientContext context;
private final HttpClient context;

SimpleClient(HttpClientContext context) {
SimpleClient(HttpClient context) {
this.context = context;
}

Expand Down