Skip to content

Commit 38354f8

Browse files
committed
chore: implementation of includeReference
1 parent b4c5cf7 commit 38354f8

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

src/main/java/com/contentstack/cms/stack/Entry.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import retrofit2.Call;
88
import retrofit2.Retrofit;
99

10+
import java.util.ArrayList;
11+
import java.util.Arrays;
1012
import java.util.HashMap;
13+
import java.util.List;
1114
import java.util.Map;
1215
import java.util.Objects;
1316

@@ -85,26 +88,14 @@ public Entry addHeader(@NotNull String key, @NotNull String value) {
8588
*/
8689
@Override
8790
public Entry addParam(@NotNull String key, @NotNull Object value) {
88-
if (key.equals("include[]")) {
89-
if (value instanceof String[]) {
90-
for (String item : (String[]) value) {
91-
this.params.put(key + includeCounter++, item);
92-
}
93-
} else if (value instanceof String) {
94-
this.params.put(key + includeCounter++, value);
95-
}
96-
} else {
97-
this.params.put(key, value);
98-
}
91+
this.params.put(key, value);
9992
return this;
10093
}
10194

10295

10396
@Override
10497
public Entry addParams(@NotNull HashMap<String, Object> params) {
105-
for (Map.Entry<String, Object> entry : params.entrySet()) {
106-
addParam(entry.getKey(), entry.getValue());
107-
}
98+
this.params.putAll(params);
10899
return this;
109100
}
110101

@@ -135,6 +126,19 @@ protected Entry clearParams() {
135126
return this;
136127
}
137128

129+
public Call<ResponseBody> includeReference(@NotNull Object referenceFields) {
130+
List<String> referenceList = new ArrayList<>();
131+
if (referenceFields instanceof String) {
132+
referenceList.add((String) referenceFields);
133+
} else if (referenceFields instanceof String[]) {
134+
referenceList.addAll(Arrays.asList((String[]) referenceFields));
135+
} else {
136+
throw new IllegalArgumentException("Reference fields must be a String or an array of Strings");
137+
}
138+
validateCT();
139+
validateEntry();
140+
return this.service.referCall(this.headers, this.contentTypeUid, referenceList);
141+
}
138142
/**
139143
* <b>Fetches the list of all the entries of a particular content type.</b>
140144
* It also returns the content of each entry in JSON format. You can also

src/main/java/com/contentstack/cms/stack/EntryService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import retrofit2.Call;
66
import retrofit2.http.*;
77

8+
import java.util.List;
89
import java.util.Map;
910

1011
public interface EntryService {
@@ -14,6 +15,12 @@ Call<ResponseBody> fetch(
1415
@HeaderMap Map<String, Object> headers,
1516
@Path("content_type_uid") String contentTypeUid,
1617
@QueryMap(encoded = true) Map<String, Object> queryParameter);
18+
19+
@GET("content_types/{content_type_uid}/entries")
20+
Call<ResponseBody> referCall(
21+
@HeaderMap Map<String, Object> headers,
22+
@Path("content_type_uid") String contentTypeUid,
23+
@Query("include[]") List<String> values);
1724

1825
@Headers("Content-Type: application/json")
1926
@GET("content_types/{content_type_uid}/entries/{entry_uid}")

src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,12 @@ void testSingleEntryQuery() {
100100
Request resp = entryInstance.fetch().request();
101101
Assertions.assertEquals("include_publish_details=true&locale=en-us&include_workflow=false", resp.url().query());
102102
}
103-
104-
@Test
105-
void testaddParam(){
106-
String[] array = {"reference","navigation_menu.page_reference"};
107-
entryInstance.addParam("include[]", array);
108-
Request request = entryInstance.find().request();
109-
Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", request.url().query());
110-
}
111103

112104
@Test
113-
void testaddParams() throws IOException{
114-
HashMap<String, Object> paramslist = new HashMap<>();
115-
paramslist.put("include[]", new String[]{"reference", "navigation_menu.page_reference"});
116-
entryInstance.addParams(paramslist);
117-
Request request = entryInstance.find().request();
118-
Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", request.url().query());
105+
void testIncludeReference() {
106+
String[] array = {"reference","navigation_menu.page_reference"};
107+
Request req = entryInstance.includeReference(array).request();
108+
Assertions.assertEquals("include[]=reference&include[]=navigation_menu.page_reference", req.url().query());
119109
}
120110

121111
@Test

0 commit comments

Comments
 (0)