|
| 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.kt |
| 17 | + |
| 18 | +import com.vonage.client.* |
| 19 | +import com.vonage.client.common.HttpMethod |
| 20 | + |
| 21 | +/** |
| 22 | + * Custom client for making HTTP requests to APIs that are unsupported by this SDK. |
| 23 | + * This will automatically handle authentication and (de)serialisation for you. |
| 24 | + * |
| 25 | + * Requests should be JSON-based; either a Map representation of the structure or an implementation of [Jsonable]. |
| 26 | + * Responses (i.e. the `R` parameter) may be one of the following: |
| 27 | + * - `Map<String, *>` representation of the JSON response. |
| 28 | + * - `Collection<*>` representation of the JSON response. |
| 29 | + * - `String` representation of the response body. |
| 30 | + * - A custom object that implements the [Jsonable] interface to parse the JSON response into. |
| 31 | + * - [ByteArray] for binary response bodies. |
| 32 | + * - [Void] for empty response bodies. |
| 33 | + * |
| 34 | + * Please note that you must ALWAYS explicitly provide the type parameters when calling methods on this client and |
| 35 | + * also provide the type in the response assignment, otherwise the compiler won't be able to infer the correct type. |
| 36 | + * |
| 37 | + * @param internalJavaSDKCustomClient The underlying Java SDK implementation which this client delegates to. |
| 38 | + * |
| 39 | + * @since 2.1.0 |
| 40 | + */ |
| 41 | +class Custom internal constructor(val internalJavaSDKCustomClient: CustomClient) { |
| 42 | + |
| 43 | + /** |
| 44 | + * Advanced method for making requests to APIs that are unsupported by this SDK. This is the most flexible option. |
| 45 | + * |
| 46 | + * @param requestMethod The HTTP method to use for the request as an enum. |
| 47 | + * @param url Absolute URL to send the request to as a string. |
| 48 | + * @param body The request body, typically in JSON format. See [DynamicEndpoint.makeRequest] for acceptable types. |
| 49 | + * |
| 50 | + * @return The response body, which can be a Map, Collection, String, or custom object implementing [Jsonable]. |
| 51 | + * @throws VonageApiResponseException If the HTTP response code is 400 or greater. |
| 52 | + */ |
| 53 | + inline fun <reified T, reified R> makeRequest(requestMethod: HttpMethod, url: String, body: T): R = |
| 54 | + internalJavaSDKCustomClient.makeRequest<T, R>(requestMethod, url, body) |
| 55 | + |
| 56 | + /** |
| 57 | + * Sends a `DELETE` request to the specified URL. |
| 58 | + * |
| 59 | + * @param url Absolute URL to send the request to as a string. |
| 60 | + * |
| 61 | + * @return The response body if present, typically as JSON. See the class documentation for acceptable types. |
| 62 | + * @throws VonageApiResponseException If the HTTP response code is 400 or greater. |
| 63 | + */ |
| 64 | + inline fun <reified R> delete(url: String): R = |
| 65 | + internalJavaSDKCustomClient.delete<R>(url) |
| 66 | + |
| 67 | + /** |
| 68 | + * Sends a `GET` request to the specified URL. |
| 69 | + * |
| 70 | + * @param url Absolute URL to send the request to as a string. |
| 71 | + * |
| 72 | + * @return The response body if present, typically as JSON. See the class documentation for acceptable types. |
| 73 | + * @throws VonageApiResponseException If the HTTP response code is 400 or greater. |
| 74 | + */ |
| 75 | + inline fun <reified R> get(url: String): R = |
| 76 | + internalJavaSDKCustomClient.get<R>(url) |
| 77 | + |
| 78 | + /** |
| 79 | + * Sends a `POST` request to the specified URL with a JSON body. |
| 80 | + * |
| 81 | + * @param url Absolute URL to send the request to as a string. |
| 82 | + * @param body The request body as a [Jsonable] object. |
| 83 | + * |
| 84 | + * @return The response body if present, typically as JSON. See the class documentation for acceptable types. |
| 85 | + * @throws VonageApiResponseException If the HTTP response code is 400 or greater. |
| 86 | + */ |
| 87 | + inline fun <reified R> post(url: String, body: Jsonable): R = |
| 88 | + internalJavaSDKCustomClient.post<R>(url, body) |
| 89 | + |
| 90 | + /** |
| 91 | + * Sends a `POST` request to the specified URL with a JSON body. |
| 92 | + * |
| 93 | + * @param url Absolute URL to send the request to as a string. |
| 94 | + * @param body The request body in JSON format as a Map tree structure. |
| 95 | + * |
| 96 | + * @return The response body if present, typically as JSON. See the class documentation for acceptable types. |
| 97 | + * @throws VonageApiResponseException If the HTTP response code is 400 or greater. |
| 98 | + */ |
| 99 | + inline fun <reified R> post(url: String, body: Map<String, *>): R = |
| 100 | + internalJavaSDKCustomClient.post<R>(url, body) |
| 101 | + |
| 102 | + /** |
| 103 | + * Sends a `PUT` request to the specified URL with a JSON body. |
| 104 | + * |
| 105 | + * @param url Absolute URL to send the request to as a string. |
| 106 | + * @param body The request body as a [Jsonable] object. |
| 107 | + * |
| 108 | + * @return The response body if present, typically as JSON. See the class documentation for acceptable types. |
| 109 | + * @throws VonageApiResponseException If the HTTP response code is 400 or greater. |
| 110 | + */ |
| 111 | + inline fun <reified R> put(url: String, body: Jsonable): R = |
| 112 | + internalJavaSDKCustomClient.put<R>(url, body) |
| 113 | + |
| 114 | + /** |
| 115 | + * Sends a `PUT` request to the specified URL with a JSON body. |
| 116 | + * |
| 117 | + * @param url Absolute URL to send the request to as a string. |
| 118 | + * @param body The request body in JSON format as a Map tree structure. |
| 119 | + * |
| 120 | + * @return The response body if present, typically as JSON. See the class documentation for acceptable types. |
| 121 | + * @throws VonageApiResponseException If the HTTP response code is 400 or greater. |
| 122 | + */ |
| 123 | + inline fun <reified R> put(url: String, body: Map<String, *>): R = |
| 124 | + internalJavaSDKCustomClient.put<R>(url, body) |
| 125 | + |
| 126 | + /** |
| 127 | + * Sends a `PATCH` request to the specified URL with a JSON body. |
| 128 | + * |
| 129 | + * @param url Absolute URL to send the request to as a string. |
| 130 | + * @param body The request body as a [Jsonable] object. |
| 131 | + * |
| 132 | + * @return The response body if present, typically as JSON. See the class documentation for acceptable types. |
| 133 | + * @throws VonageApiResponseException If the HTTP response code is 400 or greater. |
| 134 | + */ |
| 135 | + inline fun <reified R> patch(url: String, body: Jsonable): R = |
| 136 | + internalJavaSDKCustomClient.patch<R>(url, body) |
| 137 | + |
| 138 | + /** |
| 139 | + * Sends a `PATCH` request to the specified URL with a JSON body. |
| 140 | + * |
| 141 | + * @param url Absolute URL to send the request to as a string. |
| 142 | + * @param body The request body in JSON format as a Map tree structure. |
| 143 | + * |
| 144 | + * @return The response body if present, typically as JSON. See the class documentation for acceptable types. |
| 145 | + * @throws VonageApiResponseException If the HTTP response code is 400 or greater. |
| 146 | + */ |
| 147 | + inline fun <reified R> patch(url: String, body: Map<String, *>): R = |
| 148 | + internalJavaSDKCustomClient.patch<R>(url, body) |
| 149 | +} |
0 commit comments