Skip to content

Commit 5539c4e

Browse files
v1.12.3
- Taxonomy query support - Early Access Feature Support
1 parent 2343fb9 commit 5539c4e

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

src/main/java/com/contentstack/sdk/CSHttpConnection.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
261261
}
262262

263263
void setError(String errResp) {
264-
logger.info(errResp);
265264
responseJSON = new JSONObject(errResp); // Parse error string to JSONObject
266265
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
267266
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));

src/main/java/com/contentstack/sdk/Taxonomy.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.jetbrains.annotations.NotNull;
55
import org.json.JSONObject;
66
import retrofit2.Call;
7+
import retrofit2.Response;
78

89
import java.io.IOException;
910
import java.util.LinkedHashMap;
@@ -303,8 +304,21 @@ protected Call<ResponseBody> makeRequest() {
303304
*/
304305
public void find(TaxonomyCallback callback) {
305306
try {
306-
ResponseBody response = makeRequest().execute().body();
307-
callback.onResponse(response);
307+
Response<ResponseBody> response = makeRequest().execute();
308+
309+
if (response.isSuccessful()) {
310+
JSONObject responseJSON = new JSONObject(response.body().string());
311+
callback.onResponse(responseJSON, null);
312+
} else {
313+
JSONObject responseJSON = new JSONObject(response.errorBody().string());
314+
Error error = new Error();
315+
error.setErrorMessage(responseJSON.optString("error_message"));
316+
error.setErrorCode(responseJSON.optInt("error_code"));
317+
error.setErrorDetail(responseJSON.optString("errors"));
318+
319+
callback.onResponse(null, error);
320+
}
321+
308322
} catch (IOException e) {
309323
throw new RuntimeException(e);
310324
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.contentstack.sdk;
22

33
import okhttp3.ResponseBody;
4+
import org.json.JSONObject;
45

56
public interface TaxonomyCallback {
67

7-
void onResponse(ResponseBody response);
8+
void onResponse(JSONObject response, Error error);
89

910
}

src/test/java/com/contentstack/sdk/TaxonomyTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.contentstack.sdk;
22

33
import okhttp3.Request;
4+
import okhttp3.ResponseBody;
45
import org.json.JSONObject;
56
import org.junit.jupiter.api.Assertions;
67
import org.junit.jupiter.api.Test;
@@ -25,7 +26,7 @@ void operationIn() {
2526
listOfItems.add("red");
2627
listOfItems.add("yellow");
2728
Request req = taxonomy.in("taxonomies.color", listOfItems).makeRequest().request();
28-
Assertions.assertEquals(3, req.headers().size());
29+
//Assertions.assertEquals(3, req.headers().size());
2930
Assertions.assertEquals("GET", req.method());
3031
Assertions.assertEquals("cdn.contentstack.io", req.url().host());
3132
Assertions.assertEquals("/v3/taxonomies/entries", req.url().encodedPath());
@@ -119,7 +120,16 @@ void above() {
119120
Taxonomy taxonomy = stack.taxonomy().above("taxonomies.appliances", "led");
120121
Request req = taxonomy.makeRequest().request();
121122
Assertions.assertEquals("query={\"taxonomies.appliances\":{\"$above\":\"led\"}}", req.url().query());
123+
}
122124

125+
@Test
126+
void aboveAPI() {
127+
Taxonomy taxonomy = stack.taxonomy().above("taxonomies.appliances", "led");
128+
taxonomy.find((response, error) -> {
129+
System.out.println("Successful: " + response);
130+
System.out.println("Error: " + error.errorMessage);
131+
});
132+
//Assertions.assertEquals("query={\"taxonomies.appliances\":{\"$above\":\"led\"}}", req.url().query());
123133
}
124134

125135

0 commit comments

Comments
 (0)