Skip to content

Commit e5cedbf

Browse files
committed
Update README
1 parent bbc9b6a commit e5cedbf

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,107 @@ assertThat(res.statusCode()).isEqualTo(201);
147147

148148
## Currently, NO support for POSTing multipart-form
149149

150+
## Async processing
151+
152+
### .async().asDiscarding() - HttpResponse<Void>
153+
154+
```java
155+
156+
clientContext.request()
157+
.path("hello/world")
158+
.GET()
159+
.async().asDiscarding()
160+
.whenComplete((hres, throwable) -> {
161+
162+
if (throwable != null) {
163+
...
164+
} else {
165+
int statusCode = hres.statusCode();
166+
...
167+
}
168+
});
169+
170+
```
171+
172+
### .async().asString() - HttpResponse<String>
173+
174+
```java
175+
clientContext.request()
176+
.path("hello/world")
177+
.GET()
178+
.async().asString()
179+
.whenComplete((hres, throwable) -> {
180+
181+
if (throwable != null) {
182+
...
183+
} else {
184+
int statusCode = hres.statusCode();
185+
String body = hres.body();
186+
...
187+
}
188+
});
189+
```
190+
191+
### .async().bean(HelloDto.class)
192+
193+
```java
194+
clientContext.request()
195+
...
196+
.POST().async()
197+
.bean(HelloDto.class)
198+
.whenComplete((helloDto, throwable) -> {
199+
200+
if (throwable != null) {
201+
HttpException httpException = (HttpException) throwable.getCause();
202+
int statusCode = httpException.getStatusCode();
203+
204+
// maybe convert json error response body to a bean (using Jackson/Gson)
205+
MyErrorBean errorResponse = httpException.bean(MyErrorBean.class);
206+
..
207+
208+
} else {
209+
// use helloDto
210+
...
211+
}
212+
});
213+
214+
```
215+
216+
### .async().withHandler(...) - Any response body handler
217+
218+
The example below is a line subscriber processing response content line by line.
219+
220+
```java
221+
CompletableFuture<HttpResponse<Void>> future = clientContext.request()
222+
.path("hello/lineStream")
223+
.GET().async()
224+
.withHandler(HttpResponse.BodyHandlers.fromLineSubscriber(new Flow.Subscriber<>() {
225+
226+
@Override
227+
public void onSubscribe(Flow.Subscription subscription) {
228+
subscription.request(Long.MAX_VALUE);
229+
}
230+
@Override
231+
public void onNext(String item) {
232+
...
233+
}
234+
@Override
235+
public void onError(Throwable throwable) {
236+
...
237+
}
238+
@Override
239+
public void onComplete() {
240+
...
241+
}
242+
}))
243+
.whenComplete((hres, throwable) -> {
244+
int statusCode = hres.statusCode();
245+
...
246+
});
247+
248+
```
249+
250+
150251
## Auth token
151252

152253
Built in support for obtaining and setting an Authorization token.

0 commit comments

Comments
 (0)