Skip to content

Commit 5fe94a8

Browse files
authored
Merge pull request #68 from contentstack/feat/DX-746-Variants-implementation
Feat/dx 746 variants implementation
2 parents 6faaa3f + d547e50 commit 5fe94a8

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

contentstack/build.gradle

+16-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ android.buildFeatures.buildConfig true
1010
mavenPublishing {
1111
publishToMavenCentral(SonatypeHost.DEFAULT)
1212
signAllPublications()
13-
coordinates("com.contentstack.sdk", "android", "3.16.1")
13+
coordinates("com.contentstack.sdk", "android", "4.0.0")
1414

1515
pom {
1616
name = "contentstack-android"
@@ -112,18 +112,27 @@ android {
112112

113113
def localProperties = new Properties()
114114
localProperties.load(new FileInputStream(rootProject.file("local.properties")))
115+
def getPropertyOrEmpty = { key ->
116+
def value = localProperties.getProperty(key)
117+
return value != null ? "$value" : "\"\""
118+
}
119+
def variantsArray = localProperties.getProperty('variantsUID')?.split(",")?.collect { it.trim() }
120+
def variantsAsArrayString = variantsArray ? 'new String[] {' + variantsArray.collect { "\"$it\"" }.join(", ") + '}' : "new String[0]"
115121
buildTypes {
116122
debug {
117123
debuggable true
118124
testCoverageEnabled true
119125
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
120126

121-
buildConfigField "String", "host", localProperties['host']
122-
buildConfigField "String", "APIKey", localProperties['APIKey']
123-
buildConfigField "String", "deliveryToken", localProperties['deliveryToken']
124-
buildConfigField "String", "environment", localProperties['environment']
125-
buildConfigField "String", "contentTypeUID", localProperties['contentType']
126-
buildConfigField "String", "assetUID", localProperties['assetUid']
127+
buildConfigField "String", "host", getPropertyOrEmpty('host')
128+
buildConfigField "String", "APIKey", getPropertyOrEmpty('APIKey')
129+
buildConfigField "String", "deliveryToken", getPropertyOrEmpty('deliveryToken')
130+
buildConfigField "String", "environment", getPropertyOrEmpty('environment')
131+
buildConfigField "String", "contentTypeUID", getPropertyOrEmpty('contentType')
132+
buildConfigField "String", "assetUID", getPropertyOrEmpty('assetUid')
133+
buildConfigField "String", "variantUID", getPropertyOrEmpty('variantUID')
134+
buildConfigField "String", "variantEntryUID", getPropertyOrEmpty('variantEntryUID')
135+
buildConfigField "String[]", "variantsUID", variantsAsArrayString
127136
}
128137
release {
129138
minifyEnabled false

contentstack/src/androidTest/java/com/contentstack/sdk/EntryTestCase.java

+25
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class EntryTestCase {
2727
private static final String CONTENT_TYPE_UID = BuildConfig.contentTypeUID;
2828
private static CountDownLatch latch;
2929
private static Stack stack;
30+
private static String variantUID = BuildConfig.variantUID;
31+
private static String variantEntryUID = BuildConfig.variantEntryUID;
32+
private static String[] variantsUID = BuildConfig.variantsUID;
3033

3134

3235
@BeforeClass
@@ -319,4 +322,26 @@ public void onCompletion(ResponseType responseType, Error error) {
319322
latch.await();
320323
}
321324

325+
@Test
326+
public void VariantsTestSingleUid(){
327+
final Entry entry = stack.contentType("product").entry(variantEntryUID).variants(variantUID);
328+
entry.fetch(new EntryResultCallBack() {
329+
@Override
330+
public void onCompletion(ResponseType responseType, Error error) {
331+
assertEquals(variantUID, entry.getHeaders().get("x-cs-variant-uid"));
332+
System.out.println(entry.toJSON());
333+
}
334+
});
335+
}
336+
@Test
337+
public void VariantsTestArray(){
338+
final Entry entry = stack.contentType("product").entry(variantEntryUID).variants(variantsUID);
339+
entry.fetch(new EntryResultCallBack() {
340+
@Override
341+
public void onCompletion(ResponseType responseType, Error error) {
342+
System.out.println(entry.toJSON());
343+
}
344+
});
345+
}
346+
322347
}

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

+36
Original file line numberDiff line numberDiff line change
@@ -1494,4 +1494,40 @@ public Entry includeMetadata() {
14941494
}
14951495
return this;
14961496
}
1497+
/**
1498+
* method variants
1499+
* memberof Entry
1500+
* description The variant header will be added to client
1501+
* returns {Entry}
1502+
* example
1503+
* import contentstack from '@contentstack/delivery-sdk'
1504+
*
1505+
* Stack stack = contentstack.Stack("apiKey", "deliveryToken",
1506+
* "environment");
1507+
* Entry entry =
1508+
* stack.contentType("user").entry("entry_uid").variant("variant_uid").fetch();
1509+
*/
1510+
public Entry variants(String variants){
1511+
if (variants != null && variants.length() > 0) {
1512+
this.localHeader.put("x-cs-variant-uid", variants.trim());
1513+
}
1514+
return this;
1515+
1516+
}
1517+
public Entry variants(String[] variants){
1518+
if (variants != null && variants.length > 0) {
1519+
List<String> variantList = new ArrayList<>();
1520+
for (String variant : variants) {
1521+
if(variant != null && !variant.trim().isEmpty())
1522+
variantList.add(variant.trim());
1523+
}
1524+
if(!variantList.isEmpty()){
1525+
this.localHeader.put("x-cs-variant-uid", String.join(", ", variantList));
1526+
}
1527+
}
1528+
return this;
1529+
}
1530+
public ArrayMap<String, Object> getHeaders() {
1531+
return localHeader;
1532+
}
14971533
}

0 commit comments

Comments
 (0)