Skip to content

Commit

Permalink
v1.12.3
Browse files Browse the repository at this point in the history
- Taxonomy query support
- Early Access Feature Support
  • Loading branch information
ishaileshmishra committed Dec 14, 2023
1 parent f521de8 commit 2343fb9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 119 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/contentstack/sdk/APIService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.QueryMap;
import retrofit2.http.Query;
import retrofit2.http.Url;

import java.util.LinkedHashMap;
Expand All @@ -23,5 +23,5 @@ Call<ResponseBody> getRequest(
@GET("v3/taxonomies/entries")
Call<ResponseBody> getTaxonomy(
@HeaderMap Map<String, Object> headers,
@QueryMap Map<String, Object> query);
@Query("query") String query);
}
102 changes: 46 additions & 56 deletions src/main/java/com/contentstack/sdk/Taxonomy.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
package com.contentstack.sdk;

import okhttp3.Request;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.LinkedHashMap;
import java.util.List;

/**
* The type Taxonomy.
*
* @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.
* @author Shailesh Mishra
* <br>
* <b>Taxonomy : </b>
* Taxonomy, currently in the Early Access Phase simplifies
* the process of organizing content in your system, making
* it effortless to find and retrieve information.
* @implSpec To implement the taxonomy use below code
* <pre>
* {@code
* Stack stack = Contentstack.stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
* Taxonomy taxonomy = stack.taxonomy()
* }
* </pre>
* @see <a href="https://www.contentstack.com/docs/developers/apis/content-delivery-api#taxonomy"
* @since v1.13.0
*/
public class Taxonomy {


protected LinkedHashMap<String, Object> headers;
protected APIService service;
protected HashMap<String, Object> query = new HashMap<>();
protected JSONObject query = new JSONObject();
protected Config config;

/**
* Instantiates a new Taxonomy.
*
* @param service the service
* @param config the config
* @param headers the headers
* @param service the service of type {@link APIService}
* @param config the config of type {@link Config}
* @param headers the headers of the {@link LinkedHashMap}
*/
public Taxonomy(APIService service, Config config, LinkedHashMap<String, Object> headers) {
protected Taxonomy(APIService service, Config config, LinkedHashMap<String, Object> headers) {
this.service = service;
this.headers = headers;
this.config = config;
Expand All @@ -55,11 +66,10 @@ public Taxonomy(APIService service, Config config, LinkedHashMap<String, Object>
* @param listOfItems the list of taxonomy fields
* @return an instance of the Taxonomy with the specified conditions added to the query
*/
public Taxonomy in(String taxonomy, String[] listOfItems) {
String formattedValues = Arrays.stream(listOfItems).map(value -> "\"" + value.trim() + "\"").collect(Collectors.joining(" , "));

String stringify = "{ \"$in\" : [" + formattedValues + "] }}";
this.query.put(taxonomy, stringify);
public Taxonomy in(String taxonomy, List<String> listOfItems) {
JSONObject innerObj = new JSONObject();
innerObj.put("$in", listOfItems);
this.query.put(taxonomy, innerObj);
return this;
}

Expand All @@ -79,29 +89,22 @@ public Taxonomy in(String taxonomy, String[] listOfItems) {
* ]}
*
* </pre>
* Example: If you want to retrieve entries with either the color or size taxonomy applied and linked to the terms yellow and small, respectively.
* <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.
* <br>
* <pre>
*
* {$or: [
* { $or: [
* { "taxonomies.color" : "yellow" },
* { "taxonomies.size" : "small" }
* ]}
*
*
* </pre>
*
* @param listOfItems
* @return
* @param listOfItems the list of items
* @return instance {@link Taxonomy}
*/
public Taxonomy or(@NotNull List<HashMap<String, String>> listOfItems) {
for (int i = 0; i < listOfItems.size(); i++) {
HashMap<String, String> param = listOfItems.get(i);
if (i > 0) {
this.query.put("$or", listOfItems.toArray());
}
this.query.put("$or", param);
}
public Taxonomy or(@NotNull List<JSONObject> listOfItems) {
this.query.put("$or", listOfItems);
return this;
}

Expand Down Expand Up @@ -135,14 +138,8 @@ public Taxonomy or(@NotNull List<HashMap<String, String>> listOfItems) {
* @param listOfItems the list of items to that you want to include in the query string
* @return instance of the Taxonomy
*/
public Taxonomy and(@NotNull List<HashMap<String, String>> listOfItems) {
for (int i = 0; i < listOfItems.size(); i++) {
HashMap<String, String> param = listOfItems.get(i);
if (i > 0) {
this.query.put("$and", listOfItems.toArray());
}
this.query.put("$and", param);
}
public Taxonomy and(@NotNull List<JSONObject> listOfItems) {
this.query.put("$and", listOfItems.toString());
return this;
}

Expand All @@ -166,9 +163,9 @@ public Taxonomy and(@NotNull List<HashMap<String, String>> listOfItems) {
* @return instance of Taxonomy
*/
public Taxonomy exists(@NotNull String taxonomy, @NotNull Boolean value) {
HashMap<String, Boolean> param = new HashMap<>();
param.put("$exists", value);
this.query.put(taxonomy, param);
JSONObject json = new JSONObject();
json.put("$exists", value);
this.query.put(taxonomy, json);
return this;
}

Expand All @@ -191,7 +188,7 @@ public Taxonomy exists(@NotNull String taxonomy, @NotNull Boolean value) {
* @return instance of Taxonomy
*/
public Taxonomy equalAndBelow(@NotNull String taxonomy, @NotNull String termsUid) {
HashMap<String, String> param = new HashMap<>();
JSONObject param = new JSONObject();
param.put("$eq_below", termsUid);
this.query.put(taxonomy, param);
return this;
Expand All @@ -206,7 +203,7 @@ public Taxonomy equalAndBelow(@NotNull String taxonomy, @NotNull String termsUid
* @return instance of Taxonomy
*/
public Taxonomy equalAndBelowWithLevel(@NotNull String taxonomy, @NotNull String termsUid, @NotNull int level) {
Map<String, Object> innerMap = new HashMap<>();
JSONObject innerMap = new JSONObject();
innerMap.put("$eq_below", termsUid + ", level: " + level);
this.query.put(taxonomy, innerMap);
return this;
Expand All @@ -232,7 +229,7 @@ public Taxonomy equalAndBelowWithLevel(@NotNull String taxonomy, @NotNull String
* @return instance of Taxonomy
*/
public Taxonomy below(@NotNull String taxonomy, @NotNull String termsUid) {
HashMap<String, String> param = new HashMap<>();
JSONObject param = new JSONObject();
param.put("$below", termsUid);
this.query.put(taxonomy, param);
return this;
Expand All @@ -257,7 +254,7 @@ public Taxonomy below(@NotNull String taxonomy, @NotNull String termsUid) {
* @return instance of Taxonomy
*/
public Taxonomy equalAbove(@NotNull String taxonomy, @NotNull String termUid) {
Map<String, Object> innerMap = new HashMap<>();
JSONObject innerMap = new JSONObject();
innerMap.put("$eq_above", termUid);
this.query.put(taxonomy, innerMap);
return this;
Expand All @@ -282,7 +279,7 @@ public Taxonomy equalAbove(@NotNull String taxonomy, @NotNull String termUid) {
* @return instance of {@link Taxonomy}
*/
public Taxonomy above(@NotNull String taxonomy, @NotNull String termUid) {
Map<String, Object> innerMap = new HashMap<>();
JSONObject innerMap = new JSONObject();
innerMap.put("$above", termUid);
this.query.put(taxonomy, innerMap);
return this;
Expand All @@ -295,9 +292,7 @@ public Taxonomy above(@NotNull String taxonomy, @NotNull String termUid) {
* @return instance of Call<ResponseBody>
*/
protected Call<ResponseBody> makeRequest() {
HashMap<String, Object> map = new HashMap<>();
map.put("query", query);
return this.service.getTaxonomy(this.headers, map);
return this.service.getTaxonomy(this.headers, this.query.toString());
}


Expand All @@ -308,13 +303,8 @@ protected Call<ResponseBody> makeRequest() {
*/
public void find(TaxonomyCallback callback) {
try {
Response<ResponseBody> response = makeRequest().execute();
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
Request request = makeRequest().request();
callback.onFailure(request, response.errorBody());
}
ResponseBody response = makeRequest().execute().body();
callback.onResponse(response);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/contentstack/sdk/TaxonomyCallback.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
package com.contentstack.sdk;

import okhttp3.Request;
import okhttp3.ResponseBody;

public interface TaxonomyCallback {

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

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


}
Loading

0 comments on commit 2343fb9

Please sign in to comment.