Skip to content

Commit 7c6e0cb

Browse files
committed
Merge remote-tracking branch 'upstream/master' into jakarta
2 parents 85b0130 + e9e93df commit 7c6e0cb

File tree

16 files changed

+779
-63
lines changed

16 files changed

+779
-63
lines changed

http-client/README.md

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
[![Maven Central](https://img.shields.io/maven-central/v/io.avaje/avaje-http-client.svg?label=Maven%20Central)](https://mvnrepository.com/artifact/io.avaje/avaje-http-client)
33
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/avaje/avaje-http-client/blob/master/LICENSE)
44

5-
# avaje-http-client
6-
7-
Documentation at [avaje.io/http-client](https://avaje.io/http-client/)
5+
# [Avaje-HTTP-Client](https://avaje.io/http-client/)
86

97
A lightweight wrapper to the [JDK 11+ Java Http Client](http://openjdk.java.net/groups/net/httpclient/intro.html)
108

@@ -28,26 +26,28 @@ A lightweight wrapper to the [JDK 11+ Java Http Client](http://openjdk.java.net/
2826
</dependency>
2927
```
3028

31-
### Create HttpClientContext
29+
### Create HttpClient
3230

33-
Create a HttpClientContext with a baseUrl, Jackson or Gson based JSON
31+
Create a HttpClient with a baseUrl, Jackson or Gson based JSON
3432
body adapter, logger.
3533

3634
```java
37-
public HttpClientContext client() {
38-
return HttpClientContext.builder()
39-
.baseUrl(baseUrl)
40-
.bodyAdapter(new JsonbBodyAdapter())
41-
//.bodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
42-
//.bodyAdapter(new GsonBodyAdapter(new Gson()))
43-
.build();
44-
}
45-
35+
HttpClient client = HttpClient.builder()
36+
.baseUrl(baseUrl)
37+
.bodyAdapter(new JsonbBodyAdapter())
38+
//.bodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
39+
//.bodyAdapter(new GsonBodyAdapter(new Gson()))
40+
.build();
41+
42+
HttpResponse<String> hres = client.request()
43+
.path("hello")
44+
.GET()
45+
.asString();
4646
```
4747

4848
## Requests
4949

50-
From HttpClientContext:
50+
From HttpClient:
5151
- Create a request
5252
- Build the url via path(), matrixParam(), queryParam()
5353
- Optionally set headers(), cookies() etc
@@ -78,18 +78,43 @@ From HttpClientContext:
7878

7979
#### Example GET as String
8080
```java
81-
HttpResponse<String> hres = clientContext.request()
81+
HttpClient client = HttpClient.builder()
82+
.baseUrl(baseUrl)
83+
.build();
84+
85+
HttpResponse<String> hres = client.request()
8286
.path("hello")
8387
.GET()
8488
.asString();
8589
```
8690

8791
#### Example GET as JSON marshalling into a java class/dto
8892
```java
89-
CustomerDto customer = clientContext.request()
93+
HttpResponse<CustomerDto> customer = client.request()
94+
.path("customers").path(42)
95+
.GET()
96+
.as(CustomerDto.class);
97+
98+
// just get the bean without HttpResponse
99+
CustomerDto customer = client.request()
90100
.path("customers").path(42)
91101
.GET()
92102
.bean(CustomerDto.class);
103+
104+
// get a List
105+
HttpResponse<List<CustomerDto>> customers = client.request()
106+
.path("customers")
107+
.queryParam("active", "true")
108+
.GET()
109+
.asList(CustomerDto.class);
110+
111+
112+
// get a Stream - `application/x-json-stream`
113+
HttpResponse<List<CustomerDto>> customers = client.request()
114+
.path("customers/stream")
115+
.GET()
116+
.asStream(CustomerDto.class);
117+
93118
```
94119

95120
#### Example Async GET as String
@@ -98,7 +123,7 @@ CustomerDto customer = clientContext.request()
98123
- In the example below hres is of type HttpResponse&lt;String&gt;
99124

100125
```java
101-
clientContext.request()
126+
client.request()
102127
.path("hello")
103128
.GET()
104129
.async().asString() // CompletableFuture<HttpResponse<String>>
@@ -124,6 +149,9 @@ Overview of response types for sync calls.
124149
<tr><td><b>sync processing</b></td><td>&nbsp;</td></tr>
125150
<tr><td>asVoid</td><td>HttpResponse&lt;Void&gt;</td></tr>
126151
<tr><td>asString</td><td>HttpResponse&lt;String&gt;</td></tr>
152+
<tr><td>as&lt;E&gt</td><td>HttpResponse&lt;E&gt;</td></tr>
153+
<tr><td>asList&lt;E&gt</td><td>HttpResponse&lt;List&lt;E&gt;&gt;</td></tr>
154+
<tr><td>asStream&lt;E&gt</td><td>HttpResponse&lt;Stream&lt;E&gt;&gt;</td></tr>
127155
<tr><td>bean&lt;E&gt</td><td>E</td></tr>
128156
<tr><td>list&lt;E&gt</td><td>List&lt;E&gt;</td></tr>
129157
<tr><td>stream&lt;E&gt</td><td>Stream&lt;E&gt;</td></tr>
@@ -132,6 +160,9 @@ Overview of response types for sync calls.
132160
<tr><td><b>async processing</b></td><td>&nbsp;</td></tr>
133161
<tr><td>asVoid</td><td>CompletableFuture&lt;HttpResponse&lt;Void&gt;&gt;</td></tr>
134162
<tr><td>asString</td><td>CompletableFuture&lt;HttpResponse&lt;String&gt;&gt;</td></tr>
163+
<tr><td>as&lt;E&gt</td><td>CompletableFuture&lt;HttpResponse&lt;E&gt;&gt;</td></tr>
164+
<tr><td>asList&lt;E&gt</td><td>CompletableFuture&lt;HttpResponse&lt;List&lt;E&gt;&gt;&gt;</td></tr>
165+
<tr><td>asStream&lt;E&gt</td><td>CompletableFuture&lt;HttpResponse&lt;Stream&lt;E&gt;&gt;&gt;</td></tr>
135166
<tr><td>bean&lt;E&gt</td><td>CompletableFuture&lt;E&gt;</td></tr>
136167
<tr><td>list&lt;E&gt</td><td>CompletableFuture&lt;List&lt;E&gt;&gt;</td></tr>
137168
<tr><td>stream&lt;E&gt</td><td>CompletableFuture&lt;Stream&lt;E&gt;&gt;</td></tr>
@@ -173,7 +204,7 @@ When sending body content we can use:
173204

174205
#### GET as String
175206
```java
176-
HttpResponse<String> hres = clientContext.request()
207+
HttpResponse<String> hres = client.request()
177208
.path("hello")
178209
.GET()
179210
.asString();
@@ -185,7 +216,7 @@ HttpResponse<String> hres = clientContext.request()
185216
- In the example below hres is of type HttpResponse&lt;String&gt;
186217

187218
```java
188-
clientContext.request()
219+
client.request()
189220
.path("hello")
190221
.GET()
191222
.async().asString()
@@ -205,23 +236,38 @@ clientContext.request()
205236

206237
#### GET as json to single bean
207238
```java
208-
Customer customer = clientContext.request()
239+
HttpResponse<Customer> customer = client.request()
240+
.path("customers").path(42)
241+
.GET()
242+
.as(Customer.class);
243+
244+
Customer customer = client.request()
209245
.path("customers").path(42)
210246
.GET()
211247
.bean(Customer.class);
212248
```
213249

214250
#### GET as json to a list of beans
215251
```java
216-
List<Customer> list = clientContext.request()
252+
HttpResponse<List<Customer>> list = client.request()
253+
.path("customers")
254+
.GET()
255+
.asList(Customer.class);
256+
257+
List<Customer> list = client.request()
217258
.path("customers")
218259
.GET()
219260
.list(Customer.class);
220261
```
221262

222263
#### GET as `application/x-json-stream` as a stream of beans
223264
```java
224-
Stream<Customer> stream = clientContext.request()
265+
HttpResponse<Stream<Customer>> stream = client.request()
266+
.path("customers/all")
267+
.GET()
268+
.asStream(Customer.class);
269+
270+
Stream<Customer> stream = client.request()
225271
.path("customers/all")
226272
.GET()
227273
.stream(Customer.class);
@@ -232,7 +278,7 @@ Stream<Customer> stream = clientContext.request()
232278
```java
233279
Hello bean = new Hello(42, "rob", "other");
234280

235-
HttpResponse<Void> res = clientContext.request()
281+
HttpResponse<Void> res = client.request()
236282
.path("hello")
237283
.body(bean)
238284
.POST()
@@ -248,7 +294,7 @@ Multiple calls to `path()` append with a `/`. This is make it easier to build a
248294
programmatically and also build paths that include `matrixParam()`
249295

250296
```java
251-
HttpResponse<String> res = clientContext.request()
297+
HttpResponse<String> res = client.request()
252298
.path("customers")
253299
.path("42")
254300
.path("contacts")
@@ -257,15 +303,15 @@ HttpResponse<String> res = clientContext.request()
257303

258304
// is the same as ...
259305

260-
HttpResponse<String> res = clientContext.request()
306+
HttpResponse<String> res = client.request()
261307
.path("customers/42/contacts")
262308
.GET()
263309
.asString();
264310
```
265311

266312
#### MatrixParam
267313
```java
268-
HttpResponse<String> httpRes = clientContext.request()
314+
HttpResponse<String> httpRes = client.request()
269315
.path("books")
270316
.matrixParam("author", "rob")
271317
.matrixParam("country", "nz")
@@ -277,7 +323,7 @@ HttpResponse<String> httpRes = clientContext.request()
277323

278324
#### QueryParam
279325
```java
280-
List<Product> beans = clientContext.request()
326+
List<Product> beans = client.request()
281327
.path("products")
282328
.queryParam("sortBy", "name")
283329
.queryParam("maxCount", "100")
@@ -287,7 +333,7 @@ List<Product> beans = clientContext.request()
287333

288334
#### FormParam
289335
```java
290-
HttpResponse<Void> res = clientContext.request()
336+
HttpResponse<Void> res = client.request()
291337
.path("register/user")
292338
.formParam("name", "Bazz")
293339
.formParam("email", "[email protected]")
@@ -355,7 +401,7 @@ The `bean()`, `list()` and `stream()` responses throw a `HttpException` if the s
355401

356402
```java
357403

358-
clientContext.request()
404+
client.request()
359405
.path("hello/world")
360406
.GET()
361407
.async().asDiscarding()
@@ -374,7 +420,7 @@ clientContext.request()
374420
### .async().asString() - HttpResponse&lt;String&gt;
375421

376422
```java
377-
clientContext.request()
423+
client.request()
378424
.path("hello/world")
379425
.GET()
380426
.async().asString()
@@ -393,7 +439,7 @@ clientContext.request()
393439
### .async().bean(HelloDto.class)
394440

395441
```java
396-
clientContext.request()
442+
client.request()
397443
...
398444
.POST().async()
399445
.bean(HelloDto.class)
@@ -420,7 +466,7 @@ clientContext.request()
420466
The example below is a line subscriber processing response content line by line.
421467

422468
```java
423-
CompletableFuture<HttpResponse<Void>> future = clientContext.request()
469+
CompletableFuture<HttpResponse<Void>> future = client.request()
424470
.path("hello/lineStream")
425471
.GET().async()
426472
.handler(HttpResponse.BodyHandlers.fromLineSubscriber(new Flow.Subscriber<>() {
@@ -460,7 +506,7 @@ choose `async()` to execute the request asynchronously.
460506

461507
```java
462508
HttpCall<List<Customer>> call =
463-
clientContext.request()
509+
client.request()
464510
.path("customers")
465511
.GET()
466512
.call().list(Customer.class);
@@ -484,8 +530,8 @@ header ("Basic Auth").
484530
##### Example
485531

486532
```java
487-
HttpClientContext clientContext =
488-
HttpClientContext.builder()
533+
HttpClient client =
534+
HttpClient.builder()
489535
.baseUrl(baseUrl)
490536
...
491537
.requestIntercept(new BasicAuthIntercept("myUsername", "myPassword")) <!-- HERE
@@ -496,7 +542,7 @@ HttpClientContext clientContext =
496542
## AuthTokenProvider - Authorization Bearer token
497543

498544
For Authorization using `Bearer` tokens that are obtained and expire, implement `AuthTokenProvider`
499-
and register that when building the HttpClientContext.
545+
and register that when building the HttpClient.
500546

501547
### 1. Implement AuthTokenProvider
502548

@@ -520,10 +566,10 @@ and register that when building the HttpClientContext.
520566
}
521567
```
522568

523-
### 2. Register with HttpClientContext
569+
### 2. Register with HttpClient
524570

525571
```java
526-
HttpClientContext ctx = HttpClientContext.builder()
572+
HttpClient client = HttpClient.builder()
527573
.baseUrl("https://foo")
528574
...
529575
.authTokenProvider(new MyAuthTokenProvider()) <!-- HERE
@@ -532,7 +578,7 @@ and register that when building the HttpClientContext.
532578

533579
### 3. Token obtained and set automatically
534580

535-
All requests using the HttpClientContext will automatically get
581+
All requests using the HttpClient will automatically get
536582
an `Authorization` header with `Bearer` token added. The token will be
537583
obtained for initial request and then renewed when the token has expired.
538584

0 commit comments

Comments
 (0)