66import java .util .List ;
77import java .util .Objects ;
88import java .util .Optional ;
9+ import java .util .concurrent .Future ;
910import java .util .function .Function ;
1011import java .util .stream .Collectors ;
1112
@@ -90,6 +91,33 @@ public static <T, R> Result<R> map(Response<T> response, Function<T, R> map) {
9091 return new Result <>(response , body );
9192 }
9293
94+ /**
95+ * Apply {@code map} function to {@code Response::getBody} and return
96+ * {@link Future} with the transformed body.
97+ *
98+ * <p>
99+ * A {@code null}-body is passed as-is.
100+ *
101+ * <p>
102+ * Usage:
103+ *
104+ * <pre>{@code @Override
105+ * public Future<String> run(FutureCallback<Result<String>> callback) {
106+ * // Deserializes into Person.class but returns Person's firstName or null.
107+ * return sendGetRequest("/person", callback, Result.mapParser(Person.class, Person::getFirstName));
108+ * }
109+ * }</pre>
110+ */
111+ public static <T , R > ResponseParser <R > mapParser (Class <T > cls , Function <T , R > map ) {
112+ return new ResponseParser <R >() {
113+ @ Override
114+ public Result <R > parse (HttpResponse response , String body , ContentType contentType ) {
115+ Response <T > resp = this .serializer .toResponse (response .getCode (), body , cls );
116+ return Result .map (resp , map );
117+ }
118+ };
119+ }
120+
93121 /**
94122 * Convert {@code T[]} response to a {@code List<T>} response.
95123 * This is handy for all request handlers which returns lists,
@@ -109,6 +137,32 @@ public static <T> Result<List<T>> toList(Response<T[]> response) {
109137 return new Result <>(response , Arrays .asList (response .getBody ()));
110138 }
111139
140+ /**
141+ * Convert {@code T[]} response to a {@code List<T>} response.
142+ * This is handy for all request handlers which returns lists,
143+ * as the current client does not support deserializing into a parametrized
144+ * {@code List.class}.
145+ *
146+ * <p>
147+ * Usage:
148+ *
149+ * <pre>{@code @Override
150+ * public Future<List<String>> run(FutureCallback<List<String>> callback) {
151+ * return sendGetRequest("/names", callback, Result.toListParser(String[].class));
152+ * }
153+ * }</pre>
154+ */
155+ public static <T > ResponseParser <List <T >> toListParser (Class <T []> cls ) {
156+ return new ResponseParser <List <T >>() {
157+
158+ @ Override
159+ public Result <List <T >> parse (HttpResponse response , String body , ContentType contentType ) {
160+ Response <T []> resp = this .serializer .toResponse (response .getCode (), body , cls );
161+ return Result .toList (resp );
162+ }
163+ };
164+ }
165+
112166 /**
113167 * Convert {@code Result<Void>} response to a {@code Result<Boolean>}.
114168 * The result contains true if status code is in 100-299 range.
0 commit comments