@@ -14,7 +14,7 @@ A light weight wrapper to the JDK 11+ Java Http Client
1414<dependency >
1515 <groupId >io.avaje</groupId >
1616 <artifactId >avaje-http-client</artifactId >
17- <version >1.0 </version >
17+ <version >1.2 </version >
1818</dependency >
1919```
2020
@@ -27,7 +27,7 @@ Create a HttpClientContext with a baseUrl, Jackson or Gson based JSON
2727 public HttpClientContext client() {
2828 return HttpClientContext . newBuilder()
2929 .withBaseUrl(baseUrl)
30- .withResponseListener (new RequestLogger ())
30+ .withRequestListener (new RequestLogger ())
3131 .withBodyAdapter(new JacksonBodyAdapter (new ObjectMapper ()))
3232// .withBodyAdapter(new GsonBodyAdapter(new Gson()))
3333 .build();
@@ -132,3 +132,46 @@ assertThat(res.statusCode()).isEqualTo(201);
132132```
133133
134134## Currently NO support for POSTing multipart-form
135+
136+ ## Auth token
137+
138+ Built in support for obtaining and setting an Authorization token.
139+
140+ ### 1. Implement AuthTokenProvider
141+
142+ ``` java
143+
144+ class MyAuthTokenProvider implements AuthTokenProvider {
145+
146+ @Override
147+ public AuthToken obtainToken (HttpClientRequest tokenRequest ) {
148+ AuthTokenResponse res = tokenRequest
149+ .url(" https://foo/v2/token" )
150+ .header(" content-type" , " application/json" )
151+ .body(authRequestAsJson())
152+ .post()
153+ .bean(AuthTokenResponse . class);
154+
155+ Instant validUntil = Instant . now(). plusSeconds(res. expires_in). minusSeconds(60 );
156+
157+ return AuthToken . of(res. access_token, validUntil);
158+ }
159+ }
160+ ```
161+
162+ ### 2. Register with HttpClientContext
163+
164+ ``` java
165+ HttpClientContext ctx = HttpClientContext . newBuilder()
166+ .withBaseUrl(" https://foo" )
167+ .withBodyAdapter(new JacksonBodyAdapter (objectMapper))
168+ .withRequestListener(new RequestLogger ())
169+ .withAuthTokenProvider(new MyAuthTokenProvider ()) < ! -- HERE
170+ .build();
171+ ```
172+
173+ ### 3. Token obtained and set automatically
174+
175+ Now all requests using the HttpClientContext will automatically get
176+ an ` Authorization ` header with ` Bearer ` token added. The token will be
177+ obtained when necessary.
0 commit comments