|
6 | 6 | import java.util.List; |
7 | 7 | import java.util.Objects; |
8 | 8 | import java.util.Optional; |
| 9 | +import java.util.concurrent.Future; |
9 | 10 | import java.util.function.Function; |
10 | 11 | import java.util.stream.Collectors; |
11 | 12 |
|
@@ -66,6 +67,102 @@ public <C> Result<C> toErrorResult() { |
66 | 67 | WeaviateErrorResponse.builder().error(this.error.getMessages()).build()); |
67 | 68 | } |
68 | 69 |
|
| 70 | + /** |
| 71 | + * Apply {@code map} function to {@code Response::getBody} and return |
| 72 | + * {@link Result} with the transformed body. |
| 73 | + * |
| 74 | + * <p> |
| 75 | + * A {@code null}-body is passed as-is. |
| 76 | + * |
| 77 | + * <p> |
| 78 | + * Usage: |
| 79 | + * |
| 80 | + * <pre>{@code @Override |
| 81 | + * public Result<String> run() { |
| 82 | + * // Deserializes into Person.class but returns Person's firstName or null. |
| 83 | + * return Result.map(sendGetRequest("/person", Person.class), Person::getFirstName); |
| 84 | + * } |
| 85 | + * }</pre> |
| 86 | + */ |
| 87 | + public static <T, R> Result<R> map(Response<T> response, Function<T, R> map) { |
| 88 | + R body = response.getBody() != null |
| 89 | + ? map.apply(response.getBody()) |
| 90 | + : null; |
| 91 | + return new Result<>(response, body); |
| 92 | + } |
| 93 | + |
| 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 | + |
| 121 | + /** |
| 122 | + * Convert {@code T[]} response to a {@code List<T>} response. |
| 123 | + * This is handy for all request handlers which returns lists, |
| 124 | + * as the current client does not support deserializing into a parametrized |
| 125 | + * {@code List.class}. |
| 126 | + * |
| 127 | + * <p> |
| 128 | + * Usage: |
| 129 | + * |
| 130 | + * <pre>{@code @Override |
| 131 | + * public Result<List<String>> run() { |
| 132 | + * return Result.toList(sendGetRequest("/names", String[].class)); |
| 133 | + * } |
| 134 | + * }</pre> |
| 135 | + */ |
| 136 | + public static <T> Result<List<T>> toList(Response<T[]> response) { |
| 137 | + return new Result<>(response, Arrays.asList(response.getBody())); |
| 138 | + } |
| 139 | + |
| 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 | + |
69 | 166 | /** |
70 | 167 | * Convert {@code Result<Void>} response to a {@code Result<Boolean>}. |
71 | 168 | * The result contains true if status code is in 100-299 range. |
|
0 commit comments