Skip to content

Commit 415139a

Browse files
v1.12.3
- Taxonomy query support - Early Access Feature Support
1 parent 5a43f23 commit 415139a

File tree

4 files changed

+91
-119
lines changed

4 files changed

+91
-119
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import retrofit2.Call;
55
import retrofit2.http.GET;
66
import retrofit2.http.HeaderMap;
7-
import retrofit2.http.QueryMap;
7+
import retrofit2.http.Query;
88
import retrofit2.http.Url;
99

1010
import java.util.LinkedHashMap;
@@ -23,5 +23,5 @@ Call<ResponseBody> getRequest(
2323
@GET("v3/taxonomies/entries")
2424
Call<ResponseBody> getTaxonomy(
2525
@HeaderMap Map<String, Object> headers,
26-
@QueryMap Map<String, Object> query);
26+
@Query("query") String query);
2727
}

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

Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
package com.contentstack.sdk;
22

3-
import okhttp3.Request;
43
import okhttp3.ResponseBody;
54
import org.jetbrains.annotations.NotNull;
5+
import org.json.JSONObject;
66
import retrofit2.Call;
7-
import retrofit2.Response;
87

98
import java.io.IOException;
10-
import java.util.*;
11-
import java.util.stream.Collectors;
9+
import java.util.LinkedHashMap;
10+
import java.util.List;
1211

1312
/**
14-
* The type Taxonomy.
15-
*
16-
* @author Shailesh Mishra <br> <b>Taxonomy : </b> <p> Taxonomy, currently in the Early Access Phase simplifies the process of organizing content in your system, making it effortless to find and retrieve information.
13+
* @author Shailesh Mishra
14+
* <br>
15+
* <b>Taxonomy : </b>
16+
* Taxonomy, currently in the Early Access Phase simplifies
17+
* the process of organizing content in your system, making
18+
* it effortless to find and retrieve information.
19+
* @implSpec To implement the taxonomy use below code
20+
* <pre>
21+
* {@code
22+
* Stack stack = Contentstack.stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
23+
* Taxonomy taxonomy = stack.taxonomy()
24+
* }
25+
* </pre>
26+
* @see <a href="https://www.contentstack.com/docs/developers/apis/content-delivery-api#taxonomy"
27+
* @since v1.13.0
1728
*/
1829
public class Taxonomy {
1930

2031

2132
protected LinkedHashMap<String, Object> headers;
2233
protected APIService service;
23-
protected HashMap<String, Object> query = new HashMap<>();
34+
protected JSONObject query = new JSONObject();
2435
protected Config config;
2536

2637
/**
2738
* Instantiates a new Taxonomy.
2839
*
29-
* @param service the service
30-
* @param config the config
31-
* @param headers the headers
40+
* @param service the service of type {@link APIService}
41+
* @param config the config of type {@link Config}
42+
* @param headers the headers of the {@link LinkedHashMap}
3243
*/
33-
public Taxonomy(APIService service, Config config, LinkedHashMap<String, Object> headers) {
44+
protected Taxonomy(APIService service, Config config, LinkedHashMap<String, Object> headers) {
3445
this.service = service;
3546
this.headers = headers;
3647
this.config = config;
@@ -55,11 +66,10 @@ public Taxonomy(APIService service, Config config, LinkedHashMap<String, Object>
5566
* @param listOfItems the list of taxonomy fields
5667
* @return an instance of the Taxonomy with the specified conditions added to the query
5768
*/
58-
public Taxonomy in(String taxonomy, String[] listOfItems) {
59-
String formattedValues = Arrays.stream(listOfItems).map(value -> "\"" + value.trim() + "\"").collect(Collectors.joining(" , "));
60-
61-
String stringify = "{ \"$in\" : [" + formattedValues + "] }}";
62-
this.query.put(taxonomy, stringify);
69+
public Taxonomy in(String taxonomy, List<String> listOfItems) {
70+
JSONObject innerObj = new JSONObject();
71+
innerObj.put("$in", listOfItems);
72+
this.query.put(taxonomy, innerObj);
6373
return this;
6474
}
6575

@@ -79,29 +89,22 @@ public Taxonomy in(String taxonomy, String[] listOfItems) {
7989
* ]}
8090
*
8191
* </pre>
82-
* Example: If you want to retrieve entries with either the color or size taxonomy applied and linked to the terms yellow and small, respectively.
92+
* <b>Example:</b> If you want to retrieve entries with either the color or size taxonomy applied and linked to the terms yellow and small, respectively.
8393
* <br>
8494
* <pre>
8595
*
86-
* {$or: [
96+
* { $or: [
8797
* { "taxonomies.color" : "yellow" },
8898
* { "taxonomies.size" : "small" }
8999
* ]}
90100
*
91-
*
92101
* </pre>
93102
*
94-
* @param listOfItems
95-
* @return
103+
* @param listOfItems the list of items
104+
* @return instance {@link Taxonomy}
96105
*/
97-
public Taxonomy or(@NotNull List<HashMap<String, String>> listOfItems) {
98-
for (int i = 0; i < listOfItems.size(); i++) {
99-
HashMap<String, String> param = listOfItems.get(i);
100-
if (i > 0) {
101-
this.query.put("$or", listOfItems.toArray());
102-
}
103-
this.query.put("$or", param);
104-
}
106+
public Taxonomy or(@NotNull List<JSONObject> listOfItems) {
107+
this.query.put("$or", listOfItems);
105108
return this;
106109
}
107110

@@ -135,14 +138,8 @@ public Taxonomy or(@NotNull List<HashMap<String, String>> listOfItems) {
135138
* @param listOfItems the list of items to that you want to include in the query string
136139
* @return instance of the Taxonomy
137140
*/
138-
public Taxonomy and(@NotNull List<HashMap<String, String>> listOfItems) {
139-
for (int i = 0; i < listOfItems.size(); i++) {
140-
HashMap<String, String> param = listOfItems.get(i);
141-
if (i > 0) {
142-
this.query.put("$and", listOfItems.toArray());
143-
}
144-
this.query.put("$and", param);
145-
}
141+
public Taxonomy and(@NotNull List<JSONObject> listOfItems) {
142+
this.query.put("$and", listOfItems.toString());
146143
return this;
147144
}
148145

@@ -166,9 +163,9 @@ public Taxonomy and(@NotNull List<HashMap<String, String>> listOfItems) {
166163
* @return instance of Taxonomy
167164
*/
168165
public Taxonomy exists(@NotNull String taxonomy, @NotNull Boolean value) {
169-
HashMap<String, Boolean> param = new HashMap<>();
170-
param.put("$exists", value);
171-
this.query.put(taxonomy, param);
166+
JSONObject json = new JSONObject();
167+
json.put("$exists", value);
168+
this.query.put(taxonomy, json);
172169
return this;
173170
}
174171

@@ -191,7 +188,7 @@ public Taxonomy exists(@NotNull String taxonomy, @NotNull Boolean value) {
191188
* @return instance of Taxonomy
192189
*/
193190
public Taxonomy equalAndBelow(@NotNull String taxonomy, @NotNull String termsUid) {
194-
HashMap<String, String> param = new HashMap<>();
191+
JSONObject param = new JSONObject();
195192
param.put("$eq_below", termsUid);
196193
this.query.put(taxonomy, param);
197194
return this;
@@ -206,7 +203,7 @@ public Taxonomy equalAndBelow(@NotNull String taxonomy, @NotNull String termsUid
206203
* @return instance of Taxonomy
207204
*/
208205
public Taxonomy equalAndBelowWithLevel(@NotNull String taxonomy, @NotNull String termsUid, @NotNull int level) {
209-
Map<String, Object> innerMap = new HashMap<>();
206+
JSONObject innerMap = new JSONObject();
210207
innerMap.put("$eq_below", termsUid + ", level: " + level);
211208
this.query.put(taxonomy, innerMap);
212209
return this;
@@ -232,7 +229,7 @@ public Taxonomy equalAndBelowWithLevel(@NotNull String taxonomy, @NotNull String
232229
* @return instance of Taxonomy
233230
*/
234231
public Taxonomy below(@NotNull String taxonomy, @NotNull String termsUid) {
235-
HashMap<String, String> param = new HashMap<>();
232+
JSONObject param = new JSONObject();
236233
param.put("$below", termsUid);
237234
this.query.put(taxonomy, param);
238235
return this;
@@ -257,7 +254,7 @@ public Taxonomy below(@NotNull String taxonomy, @NotNull String termsUid) {
257254
* @return instance of Taxonomy
258255
*/
259256
public Taxonomy equalAbove(@NotNull String taxonomy, @NotNull String termUid) {
260-
Map<String, Object> innerMap = new HashMap<>();
257+
JSONObject innerMap = new JSONObject();
261258
innerMap.put("$eq_above", termUid);
262259
this.query.put(taxonomy, innerMap);
263260
return this;
@@ -282,7 +279,7 @@ public Taxonomy equalAbove(@NotNull String taxonomy, @NotNull String termUid) {
282279
* @return instance of {@link Taxonomy}
283280
*/
284281
public Taxonomy above(@NotNull String taxonomy, @NotNull String termUid) {
285-
Map<String, Object> innerMap = new HashMap<>();
282+
JSONObject innerMap = new JSONObject();
286283
innerMap.put("$above", termUid);
287284
this.query.put(taxonomy, innerMap);
288285
return this;
@@ -295,9 +292,7 @@ public Taxonomy above(@NotNull String taxonomy, @NotNull String termUid) {
295292
* @return instance of Call<ResponseBody>
296293
*/
297294
protected Call<ResponseBody> makeRequest() {
298-
HashMap<String, Object> map = new HashMap<>();
299-
map.put("query", query);
300-
return this.service.getTaxonomy(this.headers, map);
295+
return this.service.getTaxonomy(this.headers, this.query.toString());
301296
}
302297

303298

@@ -308,13 +303,8 @@ protected Call<ResponseBody> makeRequest() {
308303
*/
309304
public void find(TaxonomyCallback callback) {
310305
try {
311-
Response<ResponseBody> response = makeRequest().execute();
312-
if (response.isSuccessful()) {
313-
callback.onResponse(response.body());
314-
} else {
315-
Request request = makeRequest().request();
316-
callback.onFailure(request, response.errorBody());
317-
}
306+
ResponseBody response = makeRequest().execute().body();
307+
callback.onResponse(response);
318308
} catch (IOException e) {
319309
throw new RuntimeException(e);
320310
}
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
package com.contentstack.sdk;
22

3-
import okhttp3.Request;
43
import okhttp3.ResponseBody;
54

65
public interface TaxonomyCallback {
76

8-
/**
9-
* Called when the HTTP response was successfully returned by the remote server. The callback may
10-
* proceed to read the response body. The response is still live until
11-
* its response body is {@linkplain ResponseBody closed}. The recipient of the callback may
12-
* consume the response body on another thread.
13-
*
14-
* <p>Note that transport-layer success (receiving a HTTP response code, headers and body) does
15-
* not necessarily indicate application-layer success: {@code response} may still indicate an
16-
* unhappy HTTP response code like 404 or 500.
17-
*/
187
void onResponse(ResponseBody response);
198

20-
/**
21-
* Called when the request could not be executed due to cancellation, a connectivity problem or
22-
* timeout. Because networks can fail during an exchange, it is possible that the remote server
23-
* accepted the request before the failure.
24-
*/
25-
void onFailure(Request request, ResponseBody errorMessage);
26-
27-
289
}

0 commit comments

Comments
 (0)