Skip to content

Commit 53e0305

Browse files
Merge pull request #17 from contentstack/dev
🎉 stacks and assets
2 parents cd9784d + 027fe77 commit 53e0305

File tree

9 files changed

+495
-66
lines changed

9 files changed

+495
-66
lines changed

pom.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,16 @@
256256
<source>8</source>
257257
<target>8</target>
258258
<compilerArgument>-Xlint:all</compilerArgument>
259-
<showWarnings>true</showWarnings>
260259
<showDeprecation>true</showDeprecation>
260+
<showWarnings>true</showWarnings>
261+
<compilerArgs>
262+
<!-- We turn off:
263+
- `serial` because we don't use Java serialization
264+
(and all it does is stupidly complain about the serialVersionUID)
265+
- `processing` because it complains about every annotation
266+
-->
267+
<arg>-Xlint:all,-serial,-processing</arg>
268+
</compilerArgs>
261269
</configuration>
262270
</plugin>
263271
<plugin>
@@ -287,7 +295,22 @@
287295
<groupId>org.jacoco</groupId>
288296
<artifactId>jacoco-maven-plugin</artifactId>
289297
<version>${jococo-plugin.version}</version>
298+
<executions>
299+
<execution>
300+
<goals>
301+
<goal>prepare-agent</goal>
302+
</goals>
303+
</execution>
304+
<execution>
305+
<id>generate-code-coverage-report</id>
306+
<phase>test</phase>
307+
<goals>
308+
<goal>report</goal>
309+
</goals>
310+
</execution>
311+
</executions>
290312
</plugin>
313+
291314
</plugins>
292315

293316
<pluginManagement>

src/main/java/com/contentstack/cms/Contentstack.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public Organization organization() {
282282
* <pre>
283283
* Contentstack contentstack = new Contentstack.Builder().build();
284284
* Organization org = contentstack.organization();
285-
* </pre>
285+
* </pre>
286286
*/
287287
public Organization organization(@NotNull String organizationUid) {
288288
if (this.authtoken == null)
@@ -309,7 +309,7 @@ public Organization organization(@NotNull String organizationUid) {
309309
public Stack stack() {
310310
if (this.authtoken == null)
311311
throw new IllegalStateException(ILLEGAL_USER);
312-
return new Stack(this.instance, this.authtoken);
312+
return new Stack(this.instance);
313313
}
314314

315315

@@ -321,10 +321,8 @@ public Stack stack() {
321321
* <b> Example </b>
322322
*
323323
* <pre>
324-
* ```
325324
* Contentstack client = new Contentstack.Builder().build();
326325
* Stack org = client.stack();
327-
* ```
328326
* </pre>
329327
*
330328
* @param header

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ public class Asset {
3030
private final Retrofit instance;
3131
private String assetUid;
3232

33-
protected Asset(Retrofit instance) {
33+
protected Asset(Retrofit instance, Map<String, Object> header) {
3434
this.headers = new HashMap<>();
35-
this.headers.put("Content-Type", "application/json");
35+
this.headers.putAll(header);
3636
this.params = new HashMap<>();
3737
this.instance = instance;
3838
this.service = instance.create(AssetService.class);
3939
}
4040

41-
protected Asset(Retrofit instance, String uid) {
41+
protected Asset(Retrofit instance, Map<String, Object> header, String uid) {
4242
this.instance = instance;
4343
this.assetUid = uid;
4444
this.headers = new HashMap<>();
45-
this.headers.put("Content-Type", "application/json");
45+
this.headers.putAll(header);
4646
this.params = new HashMap<>();
4747
this.service = instance.create(AssetService.class);
4848
}
@@ -99,6 +99,7 @@ protected void clearParams() {
9999

100100
/**
101101
* Gets folder instance
102+
*
102103
* @return Folder
103104
*/
104105
public Folder folder() {
@@ -107,6 +108,7 @@ public Folder folder() {
107108

108109
/**
109110
* Gets folder instance
111+
*
110112
* @param folderUid
111113
* The UID of the folder that you want to either update or move
112114
* @return Folder
@@ -258,17 +260,18 @@ public Call<ResponseBody> subfolder(
258260
* @see #addParam(String, Object) to add query params
259261
* @since 1.0.0<br>
260262
*/
261-
public Call<ResponseBody> uploadAsset(@NotNull String filePath, @NotNull RequestBody requestBody) {
263+
public Call<ResponseBody> uploadAsset(@NotNull String filePath, String description) {
264+
RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), description);
262265
MultipartBody.Part assetPath = uploadFile(filePath);
263-
return this.service.uploadAsset(this.headers, assetPath, requestBody, this.params);
266+
return this.service.uploadAsset(this.headers, assetPath, body, this.params);
264267
}
265268

266269
private MultipartBody.Part uploadFile(@NotNull String filePath) {
267270
if (!filePath.isEmpty()) {
268271
File file = new File(filePath);
269272
if (file.exists()) {
270-
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
271-
return MultipartBody.Part.createFormData("asset[upload]", file.getName(), requestFile);
273+
RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
274+
return MultipartBody.Part.createFormData("asset[upload]", file.getName(), body);
272275
}
273276
}
274277
return null;
@@ -291,9 +294,43 @@ private MultipartBody.Part uploadFile(@NotNull String filePath) {
291294
* @see #addParam(String, Object) to add query params
292295
* @since 1.0.0<br>
293296
*/
294-
public Call<ResponseBody> replace() {
297+
public Call<ResponseBody> replace(@NotNull String filePath, @NotNull String description) {
295298
this.validate();
296-
return this.service.replace(this.headers, this.assetUid, this.params);
299+
MultipartBody.Part assetPath = uploadFile(filePath);
300+
// if (this.headers.containsKey(Util.CONTENT_TYPE)) {
301+
// this.headers.remove(Util.CONTENT_TYPE);
302+
// this.headers.put(Util.CONTENT_TYPE, "multipart/form-data");
303+
// }
304+
305+
RequestBody body = RequestBody.create(MediaType.parse(String.valueOf(MultipartBody.FORM)), description);
306+
return this.service.replace(this.headers, this.assetUid, assetPath, body, this.params);
307+
}
308+
309+
/**
310+
* It helps to replace data with custom file title, file description and path
311+
*
312+
* @param filePath
313+
* The filePath of the payload
314+
* @param fileTitle
315+
* Title of the payload
316+
* @param fileDescription
317+
* Description of the payload
318+
* @return RequestBody
319+
*/
320+
private RequestBody createPayload(@NotNull String filePath, String fileTitle, String fileDescription) {
321+
if (!filePath.isEmpty()) {
322+
File file = new File(filePath);
323+
if (file.exists()) {
324+
fileTitle = (fileTitle != null) ? fileTitle : file.getName();
325+
return new MultipartBody.Builder().setType(MultipartBody.FORM)
326+
.addFormDataPart("asset[upload]", file.getName(),
327+
RequestBody.create(MediaType.parse("application/octet-stream"), file))
328+
.addFormDataPart("asset[title]", fileTitle)
329+
.addFormDataPart("asset[description]", fileDescription).build();
330+
}
331+
return null;
332+
}
333+
return null;
297334
}
298335

299336
/**

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ Call<ResponseBody> uploadAsset(
4040
@Part("asset[description]") RequestBody description,
4141
@QueryMap(encoded = true) Map<String, Object> query);
4242

43+
@Multipart
4344
@PUT("assets/{asset_uid}")
4445
Call<ResponseBody> replace(
4546
@HeaderMap Map<String, Object> headers,
4647
@Path("asset_uid") String uid,
48+
@Part MultipartBody.Part file,
49+
@Part("asset[description]") RequestBody description,
4750
@QueryMap(encoded = true) Map<String, Object> query);
4851

4952
@PUT("assets/{asset_uid}")
@@ -65,8 +68,9 @@ Call<ResponseBody> delete(
6568
@Path("asset_uid") String uid);
6669

6770
@GET("assets/rt")
68-
Call<ResponseBody> rteInfo(@HeaderMap Map<String, Object> headers,
69-
@QueryMap(encoded = true) Map<String, Object> query);
71+
Call<ResponseBody> rteInfo(
72+
@HeaderMap Map<String, Object> headers,
73+
@QueryMap(encoded = true) Map<String, Object> query);
7074

7175
@POST("assets/{asset_uid}/versions/{version_number}/name")
7276
Call<ResponseBody> setVersionName(

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

Lines changed: 132 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.contentstack.cms.stack;
22

3-
import com.contentstack.cms.core.Util;
43
import okhttp3.ResponseBody;
54
import org.jetbrains.annotations.NotNull;
65
import org.json.simple.JSONObject;
@@ -31,12 +30,9 @@ public class Stack {
3130
*
3231
* @param client
3332
* the Retrofit instance
34-
* @param authtoken
35-
* The authtoken
3633
*/
37-
public Stack(@NotNull Retrofit client, String authtoken) {
34+
public Stack(@NotNull Retrofit client) {
3835
this.headers = new HashMap<>();
39-
this.headers.put("authtoken", authtoken);
4036
this.client = client;
4137
this.params = new HashMap<>();
4238
this.service = client.create(StackService.class);
@@ -151,11 +147,72 @@ public ContentType contentType(@NotNull String contentTypeUid) {
151147
* repository for future use.
152148
* <p>
153149
* These files can be attached and used in multiple entries.
150+
* <ul>
151+
* <li>
152+
* folder(optional)
153+
* Enter either the UID of a specific folder to get the assets of that folder, or enter ‘cs_root’ to get all assets and their folder details from the root folder.
154+
* <p>
155+
* Example:bltd899999999
156+
* </li>
157+
* <li>
158+
* include_folders(optional)
159+
* Set this parameter to ‘true’ to include the details of the created folders along with the details of the assets.
160+
* <p>
161+
* Example:true
162+
* </li>
163+
* <li>
164+
* environment(optional)
165+
* Enter the name of the environment to retrieve the assets published on them. You can enter multiple environments.
166+
* <p>
167+
* Example:production
168+
* </li>
169+
* <li>
170+
* version(optional)
171+
* Specify the version number of the asset that you want to retrieve. If the version is not specified, the details of the latest version will be retrieved.
172+
* <p>
173+
* Example:1
174+
* </li>
175+
* <li>
176+
* include_publish_details(optional)
177+
* Enter 'true' to include the publish details of the entry.
178+
* <p>
179+
* Example:true
180+
* </li>
181+
* <li>
182+
* include_count(optional)
183+
* Set this parameter to 'true' to include the total number of assets available in your stack in the response body.
184+
* <p>
185+
* Example:false
186+
* </li>
187+
* <li>
188+
* relative_urls(optional)
189+
* Set this to 'true' to display the relative URL of the asset.
190+
* <p>
191+
* Example:false
192+
* </li>
193+
* <li>
194+
* asc_field_uid(optional)
195+
* Enter the unique ID of the field for sorting the assets in ascending order by that field.
196+
* <p>
197+
* Example:created_at
198+
* </li>
199+
* <li>
200+
* desc_field_uid(optional)
201+
* Enter the unique ID of the field for sorting the assets in descending order by that field.
202+
* <p>
203+
* Example:file_size
204+
* </li>
205+
* <li>
206+
* include_branch(optional)
207+
* Set this to 'true' to include the <b>_branch</b> top-level key in the response. This key states the unique ID of the branch where the concerned Contentstack module resides.
208+
* <p>
209+
* Example:false
210+
* </ul>
154211
*
155212
* @return Asset
156213
*/
157214
public Asset asset() {
158-
return new Asset(this.client);
215+
return new Asset(this.client, this.headers);
159216
}
160217

161218
/**
@@ -164,11 +221,72 @@ public Asset asset() {
164221
* repository for future use.
165222
* <p>
166223
* These files can be attached and used in multiple entries.
224+
* <ul>
225+
* <li>
226+
* folder(optional)
227+
* Enter either the UID of a specific folder to get the assets of that folder, or enter ‘cs_root’ to get all assets and their folder details from the root folder.
228+
* <p>
229+
* Example:bltd899999999
230+
* </li>
231+
* <li>
232+
* include_folders(optional)
233+
* Set this parameter to ‘true’ to include the details of the created folders along with the details of the assets.
234+
* <p>
235+
* Example:true
236+
* </li>
237+
* <li>
238+
* environment(optional)
239+
* Enter the name of the environment to retrieve the assets published on them. You can enter multiple environments.
240+
* <p>
241+
* Example:production
242+
* </li>
243+
* <li>
244+
* version(optional)
245+
* Specify the version number of the asset that you want to retrieve. If the version is not specified, the details of the latest version will be retrieved.
246+
* <p>
247+
* Example:1
248+
* </li>
249+
* <li>
250+
* include_publish_details(optional)
251+
* Enter 'true' to include the publish details of the entry.
252+
* <p>
253+
* Example:true
254+
* </li>
255+
* <li>
256+
* include_count(optional)
257+
* Set this parameter to 'true' to include the total number of assets available in your stack in the response body.
258+
* <p>
259+
* Example:false
260+
* </li>
261+
* <li>
262+
* relative_urls(optional)
263+
* Set this to 'true' to display the relative URL of the asset.
264+
* <p>
265+
* Example:false
266+
* </li>
267+
* <li>
268+
* asc_field_uid(optional)
269+
* Enter the unique ID of the field for sorting the assets in ascending order by that field.
270+
* <p>
271+
* Example:created_at
272+
* </li>
273+
* <li>
274+
* desc_field_uid(optional)
275+
* Enter the unique ID of the field for sorting the assets in descending order by that field.
276+
* <p>
277+
* Example:file_size
278+
* </li>
279+
* <li>
280+
* include_branch(optional)
281+
* Set this to 'true' to include the <b>_branch</b> top-level key in the response. This key states the unique ID of the branch where the concerned Contentstack module resides.
282+
* <p>
283+
* Example:false
284+
* </ul>
167285
*
168286
* @return Asset
169287
*/
170288
public Asset asset(String assetUid) {
171-
return new Asset(this.client, assetUid);
289+
return new Asset(this.client, this.headers, assetUid);
172290
}
173291

174292

@@ -697,17 +815,19 @@ public Call<ResponseBody> transferOwnership(@NotNull JSONObject body) {
697815
* <a href=
698816
* "https://www.contentstack.com/docs/developers/apis/content-management-api/#transfer-stack-ownership-to-other-users">
699817
* (Read more) </a>
818+
* <br>
819+
* <b>uid</b> and <b>api_key</b> is required to get the acceptOwnership information
820+
* <ul>
821+
* <li> To Accept stack owned by other user, user {@link #addParam(String, Object)} to add query parameters </li>
822+
* <li> To Add Header use {@link #addHeader(String, Object)} </li>
823+
* </ul>
700824
*
701825
* @param ownershipToken
702826
* the ownership token received via email by another user.
703-
* @param uid
704-
* the user uid.
705827
* @return the stack
706828
*/
707829
public Call<ResponseBody> acceptOwnership(
708-
@NotNull String ownershipToken, @NotNull String uid) {
709-
this.params.put("api_key", this.headers.get(Util.API_KEY));
710-
this.params.put("uid", uid);
830+
@NotNull String ownershipToken) {
711831
return service.acceptOwnership(this.headers, ownershipToken, this.params);
712832
}
713833

0 commit comments

Comments
 (0)