|
| 1 | +/* |
| 2 | + * Copyright 2025 Vonage |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.vonage.client; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.annotation.JsonAnyGetter; |
| 19 | +import com.fasterxml.jackson.annotation.JsonAnySetter; |
| 20 | +import com.vonage.client.auth.AuthMethod; |
| 21 | +import com.vonage.client.common.HttpMethod; |
| 22 | +import org.apache.http.HttpResponse; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +/** |
| 27 | + * Client for making custom requests to Vonage APIs unsupported by this SDK. |
| 28 | + * This is useful for testing out beta APIs or making custom requests where the SDK falls short. |
| 29 | + * You can specify the HTTP method, endpoint URL, request body parameters and response object to parse. |
| 30 | + * The implementation is based on {@link DynamicEndpoint}. |
| 31 | + * <p> |
| 32 | + * The supported request and response types (i.e. the {@code <T>} and {@code <R>} generics) |
| 33 | + * should be instances of {@link Jsonable}. See the {@linkplain DynamicEndpoint#parseResponse(HttpResponse)} |
| 34 | + * method for how deserialisation is handled. |
| 35 | + * <p> |
| 36 | + * The valid types for the return type parameter {@code <R>} are generally: |
| 37 | + * <ul> |
| 38 | + * <li>{@link Jsonable} - for parsing the response body into a JSON object</li> |
| 39 | + * <li>{@link Map} - for parsing the response body as JSON into a tree structure</li> |
| 40 | + * <li>{@link Collection} - for parsing the response body as JSON into a list of objects</li> |
| 41 | + * <li>{@link Void} - for ignoring the response body</li> |
| 42 | + * <li>{@code byte[]} - for parsing the response body as binary</li> |
| 43 | + * <li>{@link String} - for returning the response body directly as a string</li> |
| 44 | + * </ul> |
| 45 | + * |
| 46 | + * @since 9.1.0 |
| 47 | + */ |
| 48 | +@SuppressWarnings("unchecked") |
| 49 | +public class CustomClient { |
| 50 | + private final HttpWrapper httpWrapper; |
| 51 | + |
| 52 | + /** |
| 53 | + * Wrapper for converting Map to JSON and vice versa. |
| 54 | + */ |
| 55 | + static final class JsonableMap extends JsonableBaseObject { |
| 56 | + @JsonAnyGetter @JsonAnySetter Map<String, Object> body; |
| 57 | + |
| 58 | + JsonableMap(Map<String, ?> body) { |
| 59 | + this.body = (Map<String, Object>) body; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Constructor for creating a custom client. |
| 65 | + * |
| 66 | + * @param httpWrapper Shared HTTP wrapper object and configuration used for making REST calls. |
| 67 | + */ |
| 68 | + public CustomClient(HttpWrapper httpWrapper) { |
| 69 | + this.httpWrapper = httpWrapper; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Most flexible method for making custom requests. This advanced option should only be used |
| 74 | + * if you are familiar with the underlying {@linkplain DynamicEndpoint} implementation. |
| 75 | + * |
| 76 | + * @param requestMethod The HTTP method to use for the request. |
| 77 | + * @param url Absolute URL to send the request to as a string. |
| 78 | + * @param requestBody The payload to send in the request body. |
| 79 | + * @param responseType Hack for type inference. Do not provide this field (especially, DO NOT pass {@code null}). |
| 80 | + * |
| 81 | + * @return The parsed response object, or {@code null} if absent / not applicable. |
| 82 | + * @throws VonageApiResponseException If the HTTP response code is >= 400. |
| 83 | + * |
| 84 | + * @param <T> The request body type. |
| 85 | + * @param <R> The response body type. |
| 86 | + */ |
| 87 | + public <T, R> R makeRequest(HttpMethod requestMethod, String url, T requestBody, R... responseType) { |
| 88 | + return DynamicEndpoint.<T, R> builder(fixResponseType(responseType)) |
| 89 | + .wrapper(httpWrapper).requestMethod(requestMethod) |
| 90 | + .authMethod(AuthMethod.class) // All available methods are acceptable |
| 91 | + .pathGetter((de, req) -> url) |
| 92 | + .build().execute(requestBody); |
| 93 | + } |
| 94 | + |
| 95 | + private <R> R[] fixResponseType(R... responseType) { |
| 96 | + return responseType == null || Object.class.equals(responseType.getClass().getComponentType()) ? |
| 97 | + (R[]) new Void[0] : responseType; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Convenience method for making DELETE requests. |
| 102 | + * In most cases, you should assign the return value to Void. |
| 103 | + * |
| 104 | + * @param url URL to send the request to as a string. |
| 105 | + * @param responseType Hack for type inference. Do not provide this field (especially, DO NOT pass {@code null}). |
| 106 | + * |
| 107 | + * @return The parsed response object, or {@code null} if absent / not applicable. |
| 108 | + * @throws VonageApiResponseException If the HTTP response code is >= 400. |
| 109 | + * |
| 110 | + * @param <R> The response body type, most likely {@linkplain Void}. |
| 111 | + */ |
| 112 | + public <R> R delete(String url, R... responseType) { |
| 113 | + return makeRequest(HttpMethod.DELETE, url, null, responseType); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Convenience method for making GET requests. |
| 118 | + * |
| 119 | + * @param url URL to send the request to as a string. |
| 120 | + * @param responseType Hack for type inference. Do not provide this field (especially, DO NOT pass {@code null}). |
| 121 | + * |
| 122 | + * @return The parsed response object. |
| 123 | + * @throws VonageApiResponseException If the HTTP response code is >= 400. |
| 124 | + * |
| 125 | + * @param <R> The response body type. |
| 126 | + */ |
| 127 | + public <R> R get(String url, R... responseType) { |
| 128 | + return makeRequest(HttpMethod.GET, url, null, responseType); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Convenience method for making POST requests. |
| 133 | + * |
| 134 | + * @param url URL to send the request to as a string. |
| 135 | + * @param requestBody The payload to send in the request body. |
| 136 | + * @param responseType Hack for type inference. Do not provide this field (especially, DO NOT pass {@code null}). |
| 137 | + * |
| 138 | + * @return The parsed response object, or {@code null} if absent / not applicable. |
| 139 | + * @throws VonageApiResponseException If the HTTP response code is >= 400. |
| 140 | + * |
| 141 | + * @param <R> The response body type. |
| 142 | + */ |
| 143 | + public <R> R post(String url, Jsonable requestBody, R... responseType) { |
| 144 | + return makeRequest(HttpMethod.POST, url, requestBody, responseType); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Convenience method for making JSON-based POST requests. |
| 149 | + * |
| 150 | + * @param url URL to send the request to as a string. |
| 151 | + * @param requestBody The payload to convert to JSON and send in the request body. |
| 152 | + * @param responseType Hack for type inference. Do not provide this field (especially, DO NOT pass {@code null}). |
| 153 | + * |
| 154 | + * @return The parsed response object, or {@code null} if absent / not applicable. |
| 155 | + * @throws VonageApiResponseException If the HTTP response code is >= 400. |
| 156 | + * |
| 157 | + * @param <R> The response body type. |
| 158 | + */ |
| 159 | + public <R> R post(String url, Map<String, ?> requestBody, R... responseType) { |
| 160 | + return post(url, new JsonableMap(requestBody), responseType); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Convenience method for making PUT requests. |
| 165 | + * |
| 166 | + * @param url URL to send the request to as a string. |
| 167 | + * @param requestBody The payload to send in the request body. |
| 168 | + * @param responseType Hack for type inference. Do not provide this field (especially, DO NOT pass {@code null}). |
| 169 | + * |
| 170 | + * @return The parsed response object, or {@code null} if absent / not applicable. |
| 171 | + * @throws VonageApiResponseException If the HTTP response code is >= 400. |
| 172 | + * |
| 173 | + * @param <R> The response body type. |
| 174 | + */ |
| 175 | + public <R> R put(String url, Jsonable requestBody, R... responseType) { |
| 176 | + return makeRequest(HttpMethod.PUT, url, requestBody, responseType); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Convenience method for making JSON-based PUT requests. |
| 181 | + * |
| 182 | + * @param url URL to send the request to as a string. |
| 183 | + * @param requestBody The payload to convert to JSON and send in the request body. |
| 184 | + * @param responseType Hack for type inference. Do not provide this field (especially, DO NOT pass {@code null}). |
| 185 | + * |
| 186 | + * @return The parsed response object, or {@code null} if absent / not applicable. |
| 187 | + * @throws VonageApiResponseException If the HTTP response code is >= 400. |
| 188 | + * |
| 189 | + * @param <R> The response body type. |
| 190 | + */ |
| 191 | + public <R> R put(String url, Map<String, ?> requestBody, R... responseType) { |
| 192 | + return put(url, new JsonableMap(requestBody), responseType); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Convenience method for making PATCH requests. |
| 197 | + * |
| 198 | + * @param url URL to send the request to as a string. |
| 199 | + * @param requestBody The payload to send in the request body. |
| 200 | + * @param responseType Hack for type inference. Do not provide this field (especially, DO NOT pass {@code null}). |
| 201 | + * |
| 202 | + * @return The parsed response object, or {@code null} if absent / not applicable. |
| 203 | + * @throws VonageApiResponseException If the HTTP response code is >= 400. |
| 204 | + * |
| 205 | + * @param <R> The response body type. |
| 206 | + */ |
| 207 | + public <R> R patch(String url, Jsonable requestBody, R... responseType) { |
| 208 | + return makeRequest(HttpMethod.PATCH, url, requestBody, responseType); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Convenience method for making JSON-based PATCH requests. |
| 213 | + * |
| 214 | + * @param url URL to send the request to as a string. |
| 215 | + * @param requestBody The payload to convert to JSON and send in the request body. |
| 216 | + * @param responseType Hack for type inference. Do not provide this field (especially, DO NOT pass {@code null}). |
| 217 | + * |
| 218 | + * @return The parsed response object, or {@code null} if absent / not applicable. |
| 219 | + * @throws VonageApiResponseException If the HTTP response code is >= 400. |
| 220 | + * |
| 221 | + * @param <R> The response body type. |
| 222 | + */ |
| 223 | + public <R> R patch(String url, Map<String, ?> requestBody, R... responseType) { |
| 224 | + return patch(url, new JsonableMap(requestBody), responseType); |
| 225 | + } |
| 226 | +} |
0 commit comments