Skip to content

Commit a13f8f4

Browse files
committed
#23 - ENH: Add HttpCall asByteArray(), asLines() asInputStream()
1 parent 52a600d commit a13f8f4

File tree

3 files changed

+159
-10
lines changed

3 files changed

+159
-10
lines changed

http-client/client/src/main/java/io/avaje/http/client/DHttpCall.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.avaje.http.client;
22

3+
import java.io.InputStream;
34
import java.net.http.HttpResponse;
45
import java.util.List;
56
import java.util.concurrent.CompletableFuture;
@@ -28,6 +29,21 @@ public HttpCall<HttpResponse<String>> asString() {
2829
return new CallString();
2930
}
3031

32+
@Override
33+
public HttpCall<HttpResponse<byte[]>> asByteArray() {
34+
return new CallBytes();
35+
}
36+
37+
@Override
38+
public HttpCall<HttpResponse<Stream<String>>> asLines() {
39+
return new CallLines();
40+
}
41+
42+
@Override
43+
public HttpCall<HttpResponse<InputStream>> asInputStream() {
44+
return new CallInputStream();
45+
}
46+
3147
@Override
3248
public <E> HttpCall<HttpResponse<E>> withHandler(HttpResponse.BodyHandler<E> bodyHandler) {
3349
return new CallHandler<>(bodyHandler);
@@ -81,6 +97,39 @@ public CompletableFuture<HttpResponse<String>> async() {
8197
}
8298
}
8399

100+
private class CallBytes implements HttpCall<HttpResponse<byte[]>> {
101+
@Override
102+
public HttpResponse<byte[]> execute() {
103+
return request.asByteArray();
104+
}
105+
@Override
106+
public CompletableFuture<HttpResponse<byte[]>> async() {
107+
return request.async().asByteArray();
108+
}
109+
}
110+
111+
private class CallLines implements HttpCall<HttpResponse<Stream<String>>> {
112+
@Override
113+
public HttpResponse<Stream<String>> execute() {
114+
return request.asLines();
115+
}
116+
@Override
117+
public CompletableFuture<HttpResponse<Stream<String>>> async() {
118+
return request.async().asLines();
119+
}
120+
}
121+
122+
private class CallInputStream implements HttpCall<HttpResponse<InputStream>> {
123+
@Override
124+
public HttpResponse<InputStream> execute() {
125+
return request.asInputStream();
126+
}
127+
@Override
128+
public CompletableFuture<HttpResponse<InputStream>> async() {
129+
return request.async().asInputStream();
130+
}
131+
}
132+
84133
private class CallBean<E> implements HttpCall<E> {
85134
private final Class<E> type;
86135
CallBean(Class<E> type) {

http-client/client/src/main/java/io/avaje/http/client/HttpCallResponse.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.avaje.http.client;
22

3+
import java.io.InputStream;
34
import java.net.http.HttpResponse;
45
import java.util.List;
56
import java.util.stream.Stream;
@@ -80,6 +81,27 @@ public interface HttpCallResponse {
8081
*/
8182
HttpCall<HttpResponse<String>> asString();
8283

84+
/**
85+
* Process as response {@literal HttpResponse<byte[]>}.
86+
*
87+
* @return The CompletableFuture of the response
88+
*/
89+
HttpCall<HttpResponse<byte[]>> asByteArray();
90+
91+
/**
92+
* Process as response {@literal HttpResponse<Stream<String>>}.
93+
*
94+
* @return The CompletableFuture of the response
95+
*/
96+
HttpCall<HttpResponse<Stream<String>>> asLines();
97+
98+
/**
99+
* Process as response {@literal HttpResponse<InputStream>}.
100+
*
101+
* @return The CompletableFuture of the response
102+
*/
103+
HttpCall<HttpResponse<InputStream>> asInputStream();
104+
83105
/**
84106
* Call using any given {@code HttpResponse.BodyHandler}.
85107
* <pre>{@code

http-client/client/src/test/java/io/avaje/http/client/HelloControllerTest.java

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,36 @@ void asLines_async() throws ExecutionException, InterruptedException {
7575
assertThat(lines.get(0)).contains("{\"id\":1, \"name\":\"one\"}");
7676
}
7777

78+
@Test
79+
void asLines_callExecute() {
80+
final HttpResponse<Stream<String>> hres =
81+
clientContext.request()
82+
.path("hello").path("stream")
83+
.GET()
84+
.call().asLines().execute();
85+
86+
assertThat(hres.statusCode()).isEqualTo(200);
87+
final List<String> lines = hres.body().collect(Collectors.toList());
88+
89+
assertThat(lines).hasSize(4);
90+
assertThat(lines.get(0)).contains("{\"id\":1, \"name\":\"one\"}");
91+
}
92+
93+
@Test
94+
void asLines_callAsync() throws ExecutionException, InterruptedException {
95+
final HttpResponse<Stream<String>> hres =
96+
clientContext.request()
97+
.path("hello").path("stream")
98+
.GET()
99+
.call().asLines().async().get();
100+
101+
assertThat(hres.statusCode()).isEqualTo(200);
102+
final List<String> lines = hres.body().collect(Collectors.toList());
103+
104+
assertThat(lines).hasSize(4);
105+
assertThat(lines.get(0)).contains("{\"id\":1, \"name\":\"one\"}");
106+
}
107+
78108
@Test
79109
void asInputStream() throws IOException {
80110
final HttpResponse<InputStream> hres =
@@ -84,13 +114,7 @@ void asInputStream() throws IOException {
84114
.asInputStream();
85115

86116
assertThat(hres.statusCode()).isEqualTo(200);
87-
final LineNumberReader reader = new LineNumberReader(new InputStreamReader(hres.body()));
88-
89-
List<String> allLines = new ArrayList<>();
90-
String line;
91-
while ((line = reader.readLine()) != null) {
92-
allLines.add(line);
93-
}
117+
List<String> allLines = readLines(hres);
94118
assertThat(allLines).hasSize(4);
95119
assertThat(allLines.get(0)).contains("{\"id\":1, \"name\":\"one\"}");
96120
}
@@ -104,15 +128,49 @@ void asInputStream_async() throws IOException, ExecutionException, InterruptedEx
104128

105129
final HttpResponse<InputStream> hres = future.get();
106130
assertThat(hres.statusCode()).isEqualTo(200);
107-
final LineNumberReader reader = new LineNumberReader(new InputStreamReader(hres.body()));
131+
List<String> allLines = readLines(hres);
132+
assertThat(allLines).hasSize(4);
133+
assertThat(allLines.get(0)).contains("{\"id\":1, \"name\":\"one\"}");
134+
}
135+
136+
@Test
137+
void asInputStream_callExecute() throws IOException {
138+
final HttpResponse<InputStream> hres =
139+
clientContext.request()
140+
.path("hello").path("stream")
141+
.GET()
142+
.call()
143+
.asInputStream().execute();
144+
145+
assertThat(hres.statusCode()).isEqualTo(200);
146+
List<String> allLines = readLines(hres);
147+
assertThat(allLines).hasSize(4);
148+
assertThat(allLines.get(0)).contains("{\"id\":1, \"name\":\"one\"}");
149+
}
150+
151+
@Test
152+
void asInputStream_callAsync() throws IOException, ExecutionException, InterruptedException {
153+
final HttpResponse<InputStream> hres =
154+
clientContext.request()
155+
.path("hello").path("stream")
156+
.GET()
157+
.call()
158+
.asInputStream().async().get();
159+
160+
assertThat(hres.statusCode()).isEqualTo(200);
161+
List<String> allLines = readLines(hres);
162+
assertThat(allLines).hasSize(4);
163+
assertThat(allLines.get(0)).contains("{\"id\":1, \"name\":\"one\"}");
164+
}
108165

166+
private List<String> readLines(HttpResponse<InputStream> hres) throws IOException {
167+
final LineNumberReader reader = new LineNumberReader(new InputStreamReader(hres.body()));
109168
List<String> allLines = new ArrayList<>();
110169
String line;
111170
while ((line = reader.readLine()) != null) {
112171
allLines.add(line);
113172
}
114-
assertThat(allLines).hasSize(4);
115-
assertThat(allLines.get(0)).contains("{\"id\":1, \"name\":\"one\"}");
173+
return allLines;
116174
}
117175

118176
@Test
@@ -353,6 +411,26 @@ void callStringAsync() throws ExecutionException, InterruptedException {
353411
assertThat(hres.statusCode()).isEqualTo(200);
354412
}
355413

414+
@Test
415+
void callBytes() {
416+
final HttpResponse<byte[]> hres = clientContext.request()
417+
.path("hello").path("message")
418+
.GET().call().asByteArray().execute();
419+
420+
assertThat(new String(hres.body(), StandardCharsets.UTF_8)).contains("hello world");
421+
assertThat(hres.statusCode()).isEqualTo(200);
422+
}
423+
424+
@Test
425+
void callBytesAsync() throws ExecutionException, InterruptedException {
426+
final HttpResponse<byte[]> hres = clientContext.request()
427+
.path("hello").path("message")
428+
.GET().call().asByteArray().async().get();
429+
430+
assertThat(new String(hres.body(), StandardCharsets.UTF_8)).contains("hello world");
431+
assertThat(hres.statusCode()).isEqualTo(200);
432+
}
433+
356434
@Test
357435
void callWithHandler() {
358436
final HttpResponse<String> hres = clientContext.request()

0 commit comments

Comments
 (0)