Skip to content

Commit d4ce779

Browse files
committed
Initial client add
1 parent b5d77a2 commit d4ce779

File tree

14 files changed

+307
-14
lines changed

14 files changed

+307
-14
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444

4545
<modules>
4646
<module>controller</module>
47+
<module>client</module>
4748
<module>generator-core</module>
4849
<module>generator-javalin</module>
4950
<module>generator-helidon</module>
5051
<module>tests</module>
52+
<module>generator-client</module>
5153
</modules>
5254

5355
</project>

tests/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<modules>
1313
<module>test-helidon</module>
1414
<module>test-javalin</module>
15+
<module>test-client</module>
1516
</modules>
1617

1718
</project>

tests/test-client/pom.xml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515

1616
<dependencies>
1717

18+
<dependency>
19+
<groupId>io.dinject</groupId>
20+
<artifactId>dinject-controller</artifactId>
21+
<version>1.21</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>io.avaje.rs</groupId>
26+
<artifactId>client</artifactId>
27+
<version>0.1-SNAPSHOT</version>
28+
</dependency>
29+
1830
<dependency>
1931
<groupId>com.squareup.retrofit2</groupId>
2032
<artifactId>retrofit</artifactId>
@@ -33,11 +45,7 @@
3345
<version>2.9.0</version>
3446
</dependency>
3547

36-
<dependency>
37-
<groupId>io.dinject</groupId>
38-
<artifactId>dinject-controller</artifactId>
39-
<version>1.21</version>
40-
</dependency>
48+
4149

4250
<dependency>
4351
<groupId>org.junit.jupiter</groupId>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
package org.example;
22

3+
import retrofit2.Call;
4+
import retrofit2.http.GET;
5+
import retrofit2.http.Path;
6+
7+
import java.util.List;
8+
39
public interface GitHubService {
10+
@GET("users/{user}/repos")
11+
Call<List<Repo>> listRepos(@Path("user") String user);
12+
13+
@GET("users/{user}/repos")
14+
Call<String> list2(@Path("user") String user);
15+
416
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package org.example;
22

33
public class Repo {
4+
public long id;
5+
public String name;
46
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,60 @@
11
package org.example;
22

3+
import java.io.IOException;
4+
import java.lang.reflect.Type;
5+
import java.net.URI;
6+
import java.net.http.HttpClient;
7+
import java.net.http.HttpConnectTimeoutException;
8+
import java.net.http.HttpRequest;
9+
import java.net.http.HttpResponse;
10+
import java.time.Duration;
11+
import java.util.function.Consumer;
12+
13+
import static java.net.http.HttpResponse.BodyHandlers.ofString;
14+
315
public class RsClient {
16+
17+
private final HttpClient httpClient;
18+
19+
private long requestTimeout = 15;
20+
21+
private final String baseUrl;
22+
23+
public RsClient(HttpClient httpClient, String baseUrl) {
24+
this.httpClient = httpClient;
25+
this.baseUrl = baseUrl;
26+
}
27+
28+
29+
public void get(String uri, Type type) throws IOException, InterruptedException {
30+
31+
final HttpRequest request = HttpRequest.newBuilder()
32+
.timeout(Duration.ofSeconds(requestTimeout))
33+
.uri(URI.create(baseUrl + "/" + uri))
34+
.GET()
35+
.build();
36+
37+
final HttpResponse<String> res = httpClient.send(request, ofString());
38+
}
39+
40+
public void async(String uri, Consumer<String> target, Consumer<HttpResponse<?>> errRes) {
41+
42+
final HttpRequest request = HttpRequest.newBuilder()
43+
.timeout(Duration.ofSeconds(requestTimeout))
44+
.uri(URI.create(baseUrl + "/" + uri))
45+
.GET()
46+
.build();
47+
48+
httpClient.sendAsync(request, ofString())
49+
.thenAccept(res -> {
50+
final int code = res.statusCode();
51+
if (code < 300) {
52+
target.accept(res.body());
53+
} else {
54+
errRes.accept(res);
55+
//throw new IOException("sf");
56+
//log.info("Failed to send metrics - response code:{} body:{}", code, res.body());
57+
}
58+
});
59+
}
460
}
Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
11
package org.example;
22

3-
public class Simple$Client {
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.http.HttpClient;
6+
import java.net.http.HttpResponse;
7+
import java.time.Duration;
8+
import java.util.List;
9+
import java.util.function.Consumer;
10+
import java.util.stream.Stream;
11+
12+
13+
public class Simple$Client implements Simple {
14+
15+
private final RsClient client;
16+
17+
public Simple$Client() {
18+
HttpClient httpClient = HttpClient.newBuilder()
19+
.version(HttpClient.Version.HTTP_1_1)
20+
.connectTimeout(Duration.ofSeconds(15))
21+
.build();
22+
23+
this.client = new RsClient(httpClient, "https://api.github.com/");
24+
}
25+
26+
@Override
27+
public List<Repo> listRepos(String user, String other) throws IOException, InterruptedException {
28+
29+
String uri = String.format("users/%s/repos", user);
30+
if (other != null) {
31+
uri += "?other="+other;
32+
}
33+
34+
//client.get(uri);
35+
return null;
36+
}
37+
38+
@Override
39+
public Repo getById(String id) {
40+
return null;
41+
}
42+
43+
@Override
44+
public HttpResponse<Repo> getById2(String id) {
45+
return null;
46+
}
47+
48+
@Override
49+
public void readUsing(String id, Consumer<Stream<String>> consumer) {
50+
51+
}
52+
53+
@Override
54+
public void downloadMe(String id, Consumer<InputStream> file) {
55+
56+
}
457
}
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
11
package org.example;
22

3-
public class Simple {
3+
4+
import io.dinject.controller.Get;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.net.http.HttpResponse;
9+
import java.nio.file.Path;
10+
import java.util.List;
11+
import java.util.function.Consumer;
12+
import java.util.stream.Stream;
13+
14+
public interface Simple {
15+
16+
17+
@Get("users/{user}/repos")
18+
List<Repo> listRepos(String user, String other) throws IOException, InterruptedException;
19+
20+
@Get("users/{id}")
21+
Repo getById(String id);
22+
//Call<List<Repo>> listRepos(@Path("user") String user);
23+
24+
@Get("users/{id}")
25+
HttpResponse<Repo> getById2(String id);
26+
27+
@Get("users/{id}")
28+
void readUsing(String id, Consumer<Stream<String>> consumer);
29+
30+
@Get("dwn/{id}")
31+
void downloadMe(String id, Consumer<InputStream> file);
32+
433
}
Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
package org.example;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import org.junit.jupiter.api.Test;
4+
import retrofit2.Call;
5+
import retrofit2.Response;
6+
import retrofit2.Retrofit;
7+
import retrofit2.converter.gson.GsonConverterFactory;
8+
import retrofit2.converter.scalars.ScalarsConverterFactory;
49

5-
class GitHubServiceTest {
10+
import java.io.IOException;
11+
import java.util.List;
612

13+
public class GitHubServiceTest {
14+
15+
@Test
16+
public void test() throws IOException {
17+
18+
Retrofit retrofit = new Retrofit.Builder()
19+
.baseUrl("https://api.github.com/")
20+
.addConverterFactory(ScalarsConverterFactory.create())
21+
//.addConverterFactory(GsonConverterFactory.create())
22+
.build();
23+
24+
GitHubService service = retrofit.create(GitHubService.class);
25+
final Call<List<Repo>> call = service.listRepos("octocat");
26+
final Response<List<Repo>> res = call.execute();
27+
final List<Repo> body = res.body();
28+
29+
System.out.println("done "+body);
30+
31+
final Call<String> call2 = service.list2("octocat");
32+
final Response<String> res2 = call2.execute();
33+
final String body2 = res2.body();
34+
35+
System.out.println("done "+body2);
36+
37+
}
738
}

tests/test-helidon/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
<parent>
1212
<groupId>org.avaje</groupId>
13-
<artifactId>java8-oss</artifactId>
14-
<version>2.1</version>
13+
<artifactId>java11-oss</artifactId>
14+
<version>2.1.2</version>
1515
</parent>
1616

1717
<properties>

tests/test-javalin/pom.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
<parent>
1212
<groupId>org.avaje</groupId>
13-
<artifactId>java8-oss</artifactId>
14-
<version>2.1</version>
13+
<artifactId>java11-oss</artifactId>
14+
<version>2.1.2</version>
1515
</parent>
1616

1717
<properties>
@@ -105,6 +105,13 @@
105105
<scope>test</scope>
106106
</dependency>
107107

108+
<dependency>
109+
<groupId>org.avaje.beta.http</groupId>
110+
<artifactId>http-client</artifactId>
111+
<version>0.1-SNAPSHOT</version>
112+
<scope>test</scope>
113+
</dependency>
114+
108115
</dependencies>
109116

110117
<build>

tests/test-javalin/src/main/java/org/example/myapp/web/HelloController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ HelloDto post(HelloDto dto) {
8686
* @param foo The hello doo id
8787
* @param dto The hello body as json
8888
*/
89-
@Roles({ADMIN})
89+
// @Roles({ADMIN})
9090
@Post("/savebean/:foo")
9191
void saveBean(String foo, HelloDto dto, Context context) {
9292
// save hello data ...

0 commit comments

Comments
 (0)