Skip to content

Commit 6ba37e3

Browse files
committed
Fix JsonableMap
1 parent 7eb6869 commit 6ba37e3

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ the dependency co-ordinates and add `mavenLocal()` to the `repositories` block i
112112
* For a searchable list of code snippets examples, see [**SNIPPETS.md**](https://github.com/Vonage/vonage-java-code-snippets/blob/main/SNIPPETS.md).
113113
* For Video API usage instructions, see [the guide on our developer portal](https://developer.vonage.com/en/video/server-sdks/java).
114114

115+
### Custom Requests
116+
Beginning with v9.1.0, you can now make customisable requests to any Vonage API endpoint using the `CustomClient` class,
117+
obtained from the `VonageClient#getCustomClient()` method. This will take care of auth and serialisation for you.
118+
You can use existing data models from the SDK or create your own by extending `com.vonage.client.JsonableBaseObject`
119+
or implementing the `com.vonage.client.Jsonable` interface. For example, you can check your account balance using
120+
the following code, which will send a `get` request to the specified URL and return a `BalanceResponse` object:
121+
122+
```java
123+
BalanceResponse response = client.getCustomClient().get("https://rest.nexmo.com/account/get-balance");
124+
```
125+
126+
You can also parse the response into a `Map<String, ?>` which represents the JSON response body as a tree like so:
127+
128+
```java
129+
Map<String, ?> response = client.getCustomClient().getJson("https://api-eu.vonage.com/v3/media?order=ascending&page_size=50");
130+
```
131+
132+
The same applies for POST, PUT and PATCH requests when sending data.
133+
115134
## Configuration
116135

117136
## Typical Instantiation

src/main/java/com/vonage/client/CustomClient.java

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,8 @@ public class CustomClient {
4444
/**
4545
* Wrapper for converting Map to JSON and vice versa.
4646
*/
47-
static final class JsonableMap implements Jsonable {
48-
private Map<String, ?> body;
49-
50-
/**
51-
* Gets the body of the request.
52-
*
53-
* @return The request body as a Map.
54-
*/
55-
@JsonAnyGetter
56-
Map<String, ?> getBody() {
57-
return body;
58-
}
59-
60-
/**
61-
* Sets the body of the request.
62-
*
63-
* @param body The request body as a Map.
64-
*/
65-
@JsonAnySetter
66-
public void setBody(Map<String, ?> body) {
67-
this.body = body;
68-
}
47+
static final class JsonableMap extends JsonableBaseObject {
48+
@JsonAnyGetter @JsonAnySetter Map<String, Object> body;
6949
}
7050

7151
/**
@@ -196,11 +176,11 @@ public <R> R patch(String url, Jsonable requestBody, R... responseType) {
196176
* @return The response body converted from JSON into a Map.
197177
* @throws VonageApiResponseException If the HTTP response code is >= 400.
198178
*/
199-
public Map<String, ?> postJson(String url, Map<String, ?> requestBody) {
179+
public Map<String, ?> postJson(String url, Map<String, Object> requestBody) {
200180
JsonableMap map = new JsonableMap();
201-
map.setBody(requestBody);
181+
map.body = requestBody;
202182
map = post(url, map);
203-
return map.getBody();
183+
return map.body;
204184
}
205185

206186
/**
@@ -213,7 +193,7 @@ public <R> R patch(String url, Jsonable requestBody, R... responseType) {
213193
*/
214194
public Map<String, ?> getJson(String url) {
215195
JsonableMap map = get(url);
216-
return map.getBody();
196+
return map.body;
217197
}
218198

219199
/**
@@ -225,11 +205,11 @@ public <R> R patch(String url, Jsonable requestBody, R... responseType) {
225205
* @return The response body converted from JSON into a Map.
226206
* @throws VonageApiResponseException If the HTTP response code is >= 400.
227207
*/
228-
public Map<String, ?> putJson(String url, Map<String, ?> requestBody) {
208+
public Map<String, ?> putJson(String url, Map<String, Object> requestBody) {
229209
JsonableMap map = new JsonableMap();
230-
map.setBody(requestBody);
210+
map.body = requestBody;
231211
map = put(url, map);
232-
return map.getBody();
212+
return map.body;
233213
}
234214

235215
/**
@@ -241,10 +221,10 @@ public <R> R patch(String url, Jsonable requestBody, R... responseType) {
241221
* @return The response body converted from JSON into a Map.
242222
* @throws VonageApiResponseException If the HTTP response code is >= 400.
243223
*/
244-
public Map<String, ?> patchJson(String url, Map<String, ?> requestBody) {
224+
public Map<String, ?> patchJson(String url, Map<String, Object> requestBody) {
245225
JsonableMap map = new JsonableMap();
246-
map.setBody(requestBody);
226+
map.body = requestBody;
247227
map = patch(url, map);
248-
return map.getBody();
228+
return map.body;
249229
}
250230
}

0 commit comments

Comments
 (0)