|
1 | 1 | package ru.vyarus.dropwizard.guice.test.rest; |
2 | 2 |
|
3 | 3 | import jakarta.annotation.Nullable; |
| 4 | +import jakarta.ws.rs.HttpMethod; |
4 | 5 | import jakarta.ws.rs.client.Client; |
5 | 6 | import jakarta.ws.rs.client.Entity; |
6 | 7 | import jakarta.ws.rs.client.Invocation; |
|
64 | 65 | * @author Vyacheslav Rusakov |
65 | 66 | * @since 20.02.2025 |
66 | 67 | */ |
| 68 | +@SuppressWarnings("PMD.TooManyMethods") |
67 | 69 | public class RestClient { |
68 | 70 |
|
69 | 71 | private static final List<Integer> DEFAULT_OK = Arrays.asList(HttpStatus.OK_200, HttpStatus.NO_CONTENT_204); |
@@ -327,6 +329,54 @@ public <T> T put(final String path, final Entity<?> body, final @Nullable Class< |
327 | 329 | } |
328 | 330 | } |
329 | 331 |
|
| 332 | + /** |
| 333 | + * Simple PATCH call shortcut. Provided path should include only the target rest path. |
| 334 | + * Body object assumed to be a json entity (would be serialized as json). For file sending use method with generic |
| 335 | + * entity {@link #put(String, jakarta.ws.rs.client.Entity, Class)}. |
| 336 | + * <p> |
| 337 | + * To provide additional headers and query params see {@link #defaultHeader(String, String)} |
| 338 | + * ({@link #defaultAccept(String...)}) and {@link #defaultQueryParam(String, String)}. |
| 339 | + * For void responses (result class null) checks response status correctness (see {@link #defaultOk(Integer...)}). |
| 340 | + * <p> |
| 341 | + * For response headers validation, use raw {@code Response res = request("/path/").put(Entity.json(body))}. |
| 342 | + * <p> |
| 343 | + * IMPORTANT: default jersey client would not support PATCH calls on jdk > 16 because it uses |
| 344 | + * {@link org.glassfish.jersey.client.HttpUrlConnectorProvider} which supports only HTTP 1.1. Use apache |
| 345 | + * client to workaround this problem on jdk > 16 (see useApacheClient extension option) |
| 346 | + * |
| 347 | + * @param path target path, relative to rest root |
| 348 | + * @param body put body object (serialized as json) |
| 349 | + * @param result result type (when null, accepts any 200 or 204 responses) |
| 350 | + * @param <T> result type |
| 351 | + * @return mapped result object or null (if class not declared) |
| 352 | + */ |
| 353 | + public <T> T patch(final String path, final @Nullable Object body, final @Nullable Class<T> result) { |
| 354 | + return patch(path, Entity.json(body), result); |
| 355 | + } |
| 356 | + |
| 357 | + /** |
| 358 | + * Same as {@link #patch(String, Object, Class)}, but accepts generic entity. Useful for sending multipart |
| 359 | + * requests. |
| 360 | + * <p> |
| 361 | + * IMPORTANT: default jersey client would not support PATCH calls on jdk > 16 because it uses |
| 362 | + * {@link org.glassfish.jersey.client.HttpUrlConnectorProvider} which supports only HTTP 1.1. Use apache |
| 363 | + * client to workaround this problem on jdk > 16 (see useApacheClient extension option) |
| 364 | + * |
| 365 | + * @param path target path, relative to rest root |
| 366 | + * @param body post body object (serialized as json) |
| 367 | + * @param result result type (when null, accepts any 200 or 204 responses) |
| 368 | + * @param <T> result type |
| 369 | + * @return mapped result object or null (if class not declared) |
| 370 | + */ |
| 371 | + public <T> T patch(final String path, final Entity<?> body, final @Nullable Class<T> result) { |
| 372 | + if (result != null) { |
| 373 | + return request(path).method(HttpMethod.PATCH, body, result); |
| 374 | + } else { |
| 375 | + checkVoidResponse(() -> request(path).method(HttpMethod.PATCH, body)); |
| 376 | + return null; |
| 377 | + } |
| 378 | + } |
| 379 | + |
330 | 380 | /** |
331 | 381 | * Simple DELETE call shortcut. Provided path should include only the target rest path. |
332 | 382 | * <p> |
|
0 commit comments