diff --git a/CHANGELOG.md b/CHANGELOG.md
index 091dce2d..01f7715d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
# CHANGELOG
+## v2.0.0
+
+### Date: 27-August-2024
+
+-Feature: Variants method introduced
+
## v1.15.0
### Date: 04-Sep-2024
diff --git a/pom.xml b/pom.xml
index 62b53ce2..8fa801f7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
- *
- * Example :
+ * custom_header_value
+ *
+ * Example :
*
- *
- *+ *
+ **/ public void setHeader(String key, String value) { @@ -93,15 +95,15 @@ public void setHeader(String key, String value) { * Remove header key. * * @param key - * custom_header_key
- * Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); - * Entry entry = stack.contentType("form_name").entry("entry_uid"); - * entry.removeHeader("custom_header_key"); - *+ * custom_header_key
+ * Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); + * Entry entry = stack.contentType("form_name").entry("entry_uid"); + * entry.removeHeader("custom_header_key"); + **/ public void removeHeader(String key) { @@ -114,10 +116,10 @@ public void removeHeader(String key) { * Get title string * * @return String @title
+ ** String title = entry.getTitle(); **/ @@ -130,10 +132,10 @@ public String getTitle() { * Get url string * * @return String @url
- *
- * Example :
+ *
+ * Example :
* - *+ ** String url = entry.getURL(); **/ @@ -146,10 +148,10 @@ public String getURL() { * Get tags. * * @return String @tags
- *
- * Example :
+ *
+ * Example :
* - *+ ** String[] tags = entry.getURL(); **/ @@ -166,10 +168,10 @@ protected void setTags(String[] tags) { * Get contentType name. * * @return String @contentTypeName
- *
- * Example :
+ *
+ * Example :
* - *+ ** String contentType = entry.getFileType(); **/ @@ -182,10 +184,10 @@ public String getContentType() { * Get uid. * * @return String @uid
- *
- * Example :
+ *
+ * Example :
* - *+ ** String uid = entry.getUid(); **/ @@ -204,12 +206,12 @@ public String getLocale() { /** * @param locale - * {@link String} + * {@link String} * @return Entry
- *
- * Example :
+ *
+ * Example :
* - *+ ** Entry entry = entry.setLanguage(); **/ @@ -223,10 +225,10 @@ public Entry setLocale(@NotNull String locale) { * Get entry representation in json * * @return resultJson
- *
- * Example :
+ *
+ * Example :
* - *+ ** JSONObject json = entry.toJSON(); **/ @@ -239,13 +241,14 @@ public JSONObject toJSON() { * Get object value for key. * * @param key - * field_uid as key.
- *
- * Example :
- * - *- * Object obj = entry.get("key"); - *+ * field_uid as key.
+ *
+ * Example :
+ * + *+ * Object obj = entry.get("key"); + *+ * * @return Object @resultJson */ public Object get(@NotNull String key) { @@ -256,13 +259,14 @@ public Object get(@NotNull String key) { * Get string value for key. * * @param key - * field_uid as key.
- *
- * Example :
- * - *- * String value = entry.getString("key"); - *+ * field_uid as key.
+ *
+ * Example :
+ * + *+ * String value = entry.getString("key"); + *+ * * @return String @getString */ @@ -278,13 +282,14 @@ public String getString(@NotNull String key) { * Get boolean value for key. * * @param key - * field_uid as key.
- *
- * Example :
- * - *- * Boolean value = entry.getBoolean("key"); - *+ * field_uid as key.
+ *
+ * Example :
+ * + *+ * Boolean value = entry.getBoolean("key"); + *+ * * @return boolean @getBoolean */ @@ -300,13 +305,14 @@ public Boolean getBoolean(@NotNull String key) { * Get {@link JSONArray} value for key * * @param key - * field_uid as key.
- *
- * Example :
- * - *- * JSONArray value = entry.getJSONArray("key"); - *+ * field_uid as key.
+ *
+ * Example :
+ * + *+ * JSONArray value = entry.getJSONArray("key"); + *+ * * @return JSONArray @getJSONArray */ @@ -322,13 +328,14 @@ public JSONArray getJSONArray(@NotNull String key) { * Get {@link JSONObject} value for key * * @param key - * field_uid as key.
- *
- * Example :
- * - *- * JSONObject value = entry.getJSONObject("key"); - *+ * field_uid as key.
+ *
+ * Example :
+ * + *+ * JSONObject value = entry.getJSONObject("key"); + *+ * * @return JSONObject @getJSONObject */ public JSONObject getJSONObject(@NotNull String key) { @@ -343,13 +350,14 @@ public JSONObject getJSONObject(@NotNull String key) { * Get {@link JSONObject} value for key * * @param key - * field_uid as key.
- *
- * Example :
- * - *- * JSONObject value = entry.getJSONObject("key"); - *+ * field_uid as key.
+ *
+ * Example :
+ * + *+ * JSONObject value = entry.getJSONObject("key"); + *+ * * @return Number @getNumber */ @@ -365,13 +373,14 @@ public Number getNumber(@NotNull String key) { * Get integer value for key * * @param key - * field_uid as key.
- *
- * Example :
- * - *- * int value = entry.getInt("key"); - *+ * field_uid as key.
+ *
+ * Example :
+ * + *+ * int value = entry.getInt("key"); + *+ * * @return int @getInt */ @@ -387,12 +396,12 @@ public int getInt(@NotNull String key) { * Get integer value for key * * @param key - * field_uid as key. + * field_uid as key. * @return float @getFloat
- *
- * Example :
+ *
+ * Example :
* - *+ ** float value = entry.getFloat("key"); **/ @@ -409,12 +418,12 @@ public float getFloat(@NotNull String key) { * Get double value for key * * @param key - * field_uid as key. + * field_uid as key. * @return double @getDouble
- *
- * Example :
+ *
+ * Example :
* - *+ ** double value = entry.getDouble("key"); **/ @@ -431,13 +440,14 @@ public double getDouble(@NotNull String key) { * Get long value for key * * @param key - * field_uid as key.
- *
- * Example :
- * - *- * long value = entry.getLong("key"); - *+ * field_uid as key.
+ *
+ * Example :
+ * + *+ * long value = entry.getLong("key"); + *+ * * @return long @getLong */ @@ -453,15 +463,16 @@ public long getLong(@NotNull String key) { * Get short value for key * * @param key - * field_uid as key. + * field_uid as key. * - *
- *
- * Example :
+ *
+ *
+ * Example :
* - *- * short value = entry.getShort("key"); - *+ *+ * short value = entry.getShort("key"); + *+ * * @return short @getShort */ public short getShort(@NotNull String key) { @@ -476,13 +487,14 @@ public short getShort(@NotNull String key) { * Get {@link Calendar} value for key * * @param key - * field_uid as key.
- *
- * Example :
- * - *- * Calendar value = entry.getDate("key"); - *+ * field_uid as key.
+ *
+ * Example :
+ * + *+ * Calendar value = entry.getDate("key"); + *+ * * @return Calendar @getDate */ @@ -500,10 +512,10 @@ public Calendar getDate(@NotNull String key) { * Get {@link Calendar} value of creation time of entry. * * @return Calendar @getCreateAt
- *
- * Example :
+ *
+ * Example :
* - *+ ** Calendar createdAt = entry.getCreateAt("key"); **/ @@ -522,10 +534,10 @@ public Calendar getCreateAt() { * Get uid who created this entry. * * @return String @getCreatedBy
- *
- * Example :
+ *
+ * Example :
* - *+ ** String createdBy_uid = entry.getCreatedBy(); **/ @@ -537,10 +549,10 @@ public String getCreatedBy() { * Get {@link Calendar} value of updating time of entry. * * @return Calendar @getUpdateAt
- *
- * Example :
+ *
+ * Example :
* - *+ ** Calendar updatedAt = entry.getUpdateAt("key"); **/ @@ -559,10 +571,10 @@ public Calendar getUpdateAt() { * Get uid who updated this entry. * * @return String @getString
- *
- * Example :
+ *
+ * Example :
* - *+ ** String updatedBy_uid = entry.getUpdatedBy(); **/ @@ -575,11 +587,11 @@ public String getUpdatedBy() { * * @return Calendar * - *
- *
- * Example :
+ *
+ *
+ * Example :
* - *+ ** Calendar updatedAt = entry.getUpdateAt("key"); **/ @@ -598,10 +610,10 @@ public Calendar getDeleteAt() { * Get uid who deleted this entry. * * @return String
- *
- * Example :
+ *
+ * Example :
* - *+ ** String deletedBy_uid = entry.getDeletedBy(); **/ @@ -613,12 +625,12 @@ public String getDeletedBy() { * Get an asset from the entry * * @param key - * field_uid as key. + * field_uid as key. * @return Asset
- *
- * Example :
+ *
+ * Example :
* - *+ ** * @param key - * This is the String key + * This is the String key * @return ArrayList This returns list of Assets. */ @@ -659,14 +671,15 @@ public List* Asset asset = entry.getAsset("key"); **/ @@ -640,7 +652,7 @@ public Asset getAsset(String key) { *getAssets(String key) { /** * @param key - * field_uid as key.
- *
- * Example :
+ * field_uid as key.
+ *
+ * Example :
* - *+ *+ * * @return {@link Group} */ public Group getGroup(String key) { @@ -682,12 +695,12 @@ public Group getGroup(String key) { * Note :- This will work when group is multiple true. * * @param key - * field_uid as key. + * field_uid as key. * @return list of group from entry* Group innerGroup = entry.getGroup("key"); * return null - *+ *
- *
- * Example :
+ *
+ * Example :
* - *+ ** Group innerGroup = entry.getGroups("key"); **/ @@ -708,15 +721,16 @@ public ListgetGroups(String key) { * Get value for the given reference key. * * @param refKey - * key of a reference field. + * key of a reference field. * @param refContentType - * class uid. - * @return {@link ArrayList} of {@link Entry} instances. Also specified contentType value will be set as class uid - * for all {@link Entry} instance.
- *
- * Example :
+ * class uid. + * @return {@link ArrayList} of {@link Entry} instances. Also specified + * contentType value will be set as class uid + * for all {@link Entry} instance.
+ *
+ * Example :
* - *+ ** {@code * Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); * Query csQuery = stack.contentType("contentType_name").query(); @@ -763,10 +777,11 @@ public ListgetAllEntries(String refKey, String refContentType) { } /** - * Specifies list of field ids that would be 'excluded' from the response. + * Specifies list of field ids that would be 'excluded' from the + * response. * * @param fieldUid - * field uid which get 'excluded' from the response. + * field uid which get 'excluded' from the response. */ public Entry except(@NotNull String[] fieldUid) { @@ -785,7 +800,7 @@ public Entry except(@NotNull String[] fieldUid) { * Add a constraint that requires a particular reference key details. * * @param referenceField - * key that to be constrained. + * key that to be constrained. */ public Entry includeReference(@NotNull String referenceField) { if (!referenceField.isEmpty()) { @@ -802,7 +817,7 @@ public Entry includeReference(@NotNull String referenceField) { * Add a constraint that requires a particular reference key details. * * @param referenceFields - * array key that to be constrained. + * array key that to be constrained. */ public Entry includeReference(@NotNull String[] referenceFields) { if (referenceFields.length > 0) { @@ -818,15 +833,17 @@ public Entry includeReference(@NotNull String[] referenceFields) { } /** - * Specifies an array of 'only' keys in BASE object that would be 'included' in the response. + * Specifies an array of 'only' keys in BASE object that would be + * 'included' in the response. * * @param fieldUid - * Array of the 'only' reference keys to be included in response. + * Array of the 'only' reference keys to be included in + * response. * @return {@link Entry} object, so you can chain this call.
- *
- * Example :
+ *
+ * Example :
* - *+ ** Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); * Entry entry = stack.contentType("form_name").entry("entry_uid");
* entry.only(new String[]{"name", "description"}); @@ -845,12 +862,14 @@ public Entry only(String[] fieldUid) { } /** - * Specifies an array of 'only' keys that would be 'included' in the response. + * Specifies an array of 'only' keys that would be 'included' in + * the response. * * @param fieldUid - * Array of the 'only' reference keys to be included in response. + * Array of the 'only' reference keys to be + * included in response. * @param referenceFieldUid - * Key who has reference to some other class object. + * Key who has reference to some other class object. */ public Entry onlyWithReferenceUid(@NotNull ListfieldUid, @NotNull String referenceFieldUid) { @@ -865,12 +884,14 @@ public Entry onlyWithReferenceUid(@NotNull List fieldUid, @NotNull Strin } /** - * Specifies an array of 'except' keys that would be 'excluded' in the response. + * Specifies an array of 'except' keys that would be 'excluded' + * in the response. * * @param fieldUid - * Array of the 'except' reference keys to be excluded in response. + * Array of the 'except' reference keys to be + * excluded in response. * @param referenceFieldUid - * Key who has reference to some other class object. + * Key who has reference to some other class object. */ public Entry exceptWithReferenceUid(@NotNull List fieldUid, @NotNull String referenceFieldUid) { if (exceptJsonObject == null) { @@ -887,11 +908,12 @@ public Entry exceptWithReferenceUid(@NotNull List fieldUid, @NotNull Str * Fetches the latest version of the entries from Contentstack.com content stack * * @param callback - * {@link EntryResultCallBack} object to notify the application when the request has completed.
- *
- * Example :
+ * {@link EntryResultCallBack} object to notify the application + * when the request has completed.
+ *
+ * Example :
* - *+ ** {@code * Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); * Entry entry = stack.contentType("form_name").entry("entry_uid");
@@ -917,7 +939,6 @@ public void fetch(EntryResultCallBack callback) { fetchFromNetwork(urlString, urlQueries, callback); } - private void fetchFromNetwork(String urlString, JSONObject urlQueries, EntryResultCallBack callBack) { JSONObject mainJson = new JSONObject(); @@ -929,7 +950,6 @@ private void fetchFromNetwork(String urlString, JSONObject urlQueries, EntryResu } - private LinkedHashMapgetUrlParams(JSONObject jsonMain) { JSONObject queryJSON = jsonMain.optJSONObject("query"); LinkedHashMap hashMap = new LinkedHashMap<>(); @@ -987,16 +1007,16 @@ private void throwException(@Nullable String errorMsg, Exception e, EntryResultC * This method adds key and value to an Entry. * * @param key - * The key as string which needs to be added to an Entry + * The key as string which needs to be added to an Entry * @param value - * The value as string which needs to be added to an Entry + * The value as string which needs to be added to an Entry * @return {@link Entry} * - *
- *
- * Example :
+ *
+ *
+ * Example :
* - *+ ** {@code * Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); * final Entry entry = stack.contentType("user").entry("entryUid");
@@ -1016,7 +1036,8 @@ public Entry addParam(@NotNull String key, @NotNull String value) { } /** - * This method also includes the content type UIDs of the referenced entries returned in the response + * This method also includes the content type UIDs of the referenced entries + * returned in the response */ public Entry includeReferenceContentTypeUID() { params.put("include_reference_content_type_uid", "true"); @@ -1034,13 +1055,14 @@ public Entry includeContentType() { } /** - * Retrieve the published content of the fallback locale if an entry is not localized in specified locale + * Retrieve the published content of the fallback locale if an entry is not + * localized in specified locale * * @return {@link Entry} object, so you can chain this call.
- *
- * Example :
+ *
+ * Example :
* - *+ ** Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); * final Entry entry = stack.contentType("user").entry("entryUid"); * entry.includeFallback(); @@ -1052,7 +1074,8 @@ public Entry includeFallback() { } /** - * includeEmbeddedItems instance of Entry Include Embedded Objects (Entries and Assets) along with entry/entries + * includeEmbeddedItems instance of Entry Include Embedded Objects (Entries and + * Assets) along with entry/entries * details.
*/ public Entry includeEmbeddedItems() { @@ -1065,11 +1088,11 @@ public Entry includeEmbeddedItems() { * * @return {@link Entry} object, so you can chain this call. * - *
- *
- * Example :
+ *
+ *
+ * Example :
* - *+ ** Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); * final Entry entry = stack.contentType("user").entry("entryUid"); * entry.includeBranch(); @@ -1085,11 +1108,11 @@ public Entry includeBranch() { * * @return {@link Entry} object, so you can chain this call. * - *
- *
- * Example :
+ *
+ *
+ * Example :
* - *+ ** Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); * final Entry entry = stack.contentType("user").entry("entryUid"); * entry.includeMetadata(); @@ -1099,4 +1122,43 @@ public Entry includeMetadata() { params.put("include_metadata", true); return this; } + + /** + * @method variants + * @memberof Entry + * @description The variant header will be added to client + * @returns {Entry} + * @example + * import contentstack from '@contentstack/delivery-sdk' + * + * Stack stack = contentstack.Stack("apiKey", "deliveryToken", + * "environment"); + * Entry entry = + * stack.contentType("user").entry("entry_uid").variant("variant_uid").fetch(); + */ + public Entry variants(@NotNull String variants) { + if (variants != null && variants.length() > 0) { + this.headers.put("x-cs-variant-uid", variants.trim()); + } + return this; + + } + + public Entry variants(@NotNull String[] variants) { + if (variants != null && variants.length > 0) { + ListvariantList = new ArrayList<>(); + for (String variant : variants) { + if (variant != null && !variant.trim().isEmpty()) + variantList.add(variant.trim()); + } + if (!variantList.isEmpty()) { + this.headers.put("x-cs-variant-uid", String.join(", ", variantList)); + } + } + return this; + } + + public LinkedHashMap getHeaders() { + return headers; + } } diff --git a/src/test/java/com/contentstack/sdk/Credentials.java b/src/test/java/com/contentstack/sdk/Credentials.java index e2a93451..e513b837 100644 --- a/src/test/java/com/contentstack/sdk/Credentials.java +++ b/src/test/java/com/contentstack/sdk/Credentials.java @@ -1,12 +1,13 @@ package com.contentstack.sdk; + import io.github.cdimascio.dotenv.Dotenv; import java.rmi.AccessException; +import java.util.Arrays; public class Credentials { static Dotenv env = getEnv(); - private static String envChecker() { String githubActions = System.getenv("GITHUB_ACTIONS"); if (githubActions != null && githubActions.equals("true")) { @@ -25,14 +26,25 @@ public static Dotenv getEnv() { return Dotenv.load(); } - public final static String HOST = (env.get("HOST") != null) ? env.get("HOST") : "cdn.contentstack.io"; public final static String API_KEY = (env.get("API_KEY") != null) ? env.get("API_KEY") : ""; public final static String DELIVERY_TOKEN = (env.get("DELIVERY_TOKEN") != null) ? env.get("DELIVERY_TOKEN") : ""; public final static String ENVIRONMENT = (env.get("ENVIRONMENT") != null) ? env.get("ENVIRONMENT") : "env1"; public final static String CONTENT_TYPE = (env.get("contentType") != null) ? env.get("contentType") : "product"; public final static String ENTRY_UID = (env.get("assetUid") != null) ? env.get("assetUid") : ""; + public final static String VARIANT_UID = (env.get("variantUid") != null) ? env.get("variantUid") : ""; + public final static String[] VARIANTS_UID; + static { + String variantsUidString = env.get("variantsUid"); + if (variantsUidString != null && !variantsUidString.trim().isEmpty()) { + VARIANTS_UID = Arrays.stream(variantsUidString.split(",")) + .map(String::trim) + .toArray(String[]::new); + } else { + VARIANTS_UID = new String[] {}; + } + } private static volatile Stack stack; diff --git a/src/test/java/com/contentstack/sdk/TestEntry.java b/src/test/java/com/contentstack/sdk/TestEntry.java index 4595b3ad..657be712 100644 --- a/src/test/java/com/contentstack/sdk/TestEntry.java +++ b/src/test/java/com/contentstack/sdk/TestEntry.java @@ -7,6 +7,8 @@ import java.util.GregorianCalendar; import java.util.logging.Logger; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -18,6 +20,8 @@ class TestEntry { private final Stack stack = Credentials.getStack(); private Entry entry; private final String CONTENT_TYPE = Credentials.CONTENT_TYPE; + private final String VARIANT_UID = Credentials.VARIANT_UID; + private static final String[] VARIANT_UIDS = Credentials.VARIANTS_UID; @Test @Order(1) @@ -63,6 +67,41 @@ public void onCompletion(ResponseType responseType, Error error) { logger.info("passed.."); } + @Test + void VariantsTestSingleUid() { + entry = stack.contentType(CONTENT_TYPE).entry(entryUid).variants(VARIANT_UID); + entry.fetch(new EntryResultCallBack() { + @Override + public void onCompletion(ResponseType responseType, Error error) { + assertEquals(VARIANT_UID.trim(), entry.getHeaders().get("x-cs-variant-uid")); + System.out.println(entry.toJSON()); + } + }); + } + + @Test + void VariantsTestArray() { + entry = stack.contentType(CONTENT_TYPE).entry(entryUid).variants(VARIANT_UIDS); + entry.fetch(new EntryResultCallBack() { + @Override + public void onCompletion(ResponseType responseType, Error error) { + System.out.println(entry.toJSON()); + } + }); + } + + @Test + void VariantsTestNullString() { + entry = stack.contentType(CONTENT_TYPE).entry(entryUid).variants((String) null); + entry.fetch(new EntryResultCallBack() { + @Override + public void onCompletion(ResponseType responseType, Error error) { + assertNull(entry.getHeaders().get("x-cs-variant-uid")); + System.out.println(entry.toJSON()); + } + }); + } + @Test @Order(4) void entryCalling() {