Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/com/contentstack/cms/stack/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected Asset(Retrofit instance, Map<String, Object> header, String uid) {
* object as its value.
* @return instance of Asset
*/
@Override
public Asset addParam(@NotNull String key, @NotNull Object value) {
this.params.put(key, value);
return this;
Expand All @@ -74,6 +75,7 @@ public Asset addParam(@NotNull String key, @NotNull Object value) {
* header.
* @return instance of Asset
*/
@Override
public Asset addHeader(@NotNull String key, @NotNull String value) {
this.headers.put(key, value);
return this;
Expand All @@ -86,6 +88,7 @@ public Asset addHeader(@NotNull String key, @NotNull String value) {
* representing the header value.
* @return instance of Asset
*/
@Override
public Asset addHeaders(@NotNull HashMap<String, String> headers) {
this.headers.putAll(headers);
return this;
Expand All @@ -98,6 +101,7 @@ public Asset addHeaders(@NotNull HashMap<String, String> headers) {
* annotated with @NotNull, indicating that it cannot be null.
* @return instance of Asset
*/
@Override
public Asset addParams(@NotNull HashMap<String, Object> headers) {
this.params.putAll(headers);
return this;
Expand Down Expand Up @@ -198,6 +202,10 @@ public Call<ResponseBody> find() {
return this.service.fetch(this.headers, this.params);
}

public Call<AssetListResponse> findAsPojo() {
return this.service.fetchPojo(this.headers, this.params);
}

/**
* The Get an asset call returns comprehensive information about a specific
* version of an asset of a stack
Expand All @@ -221,6 +229,11 @@ public Call<ResponseBody> fetch() {
return this.service.single(this.headers, this.assetUid, this.params);
}

public Call<AssetResponse> fetchAsPojo() { // New method for POJO conversion
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
return this.service.singlePojo(this.headers, this.assetUid, this.params);
}

/**
* The Get assets of a specific folder retrieves all assets of a specific asset
* folder; however, it doesn't retrieve
Expand All @@ -241,6 +254,11 @@ public Call<ResponseBody> byFolderUid(@NotNull String folderUid) {
return this.service.specificFolder(this.headers, this.params);
}

public Call<AssetListResponse> byFolderUidAsPojo(@NotNull String folderUid) {
this.params.put("folder", folderUid);
return this.service.specificFolderPojo(this.headers, this.params);
}

/**
* The Get assets and folders of a parent folder retrieves details of both
* assets and asset sub-folders within a
Expand All @@ -264,6 +282,13 @@ public Call<ResponseBody> subfolder(
return this.service.subfolder(this.headers, this.params);
}

public Call<AssetListResponse> subfolderAsPojo(
@NotNull String folderUid, Boolean isIncludeFolders) {
this.params.put("folder", folderUid);
this.params.put("include_folders", isIncludeFolders);
return this.service.subfolderPojo(this.headers, this.params);
}

/**
* The <b>Upload asset</b> request uploads an asset file to your stack.
* <p>
Expand Down Expand Up @@ -731,6 +756,10 @@ public Call<ResponseBody> getSingleFolderByName() {
return this.service.singleFolderByName(this.headers, this.params);
}

public Call<AssetListResponse> getSingleFolderByNameAsPojo() {
return this.service.singleFolderByNamePojo(this.headers, this.params);
}

/**
* Get sub-folders of a parent folder request retrieves the details of only the
* sub-folders of a specific asset
Expand All @@ -750,6 +779,10 @@ public Call<ResponseBody> getSubfolder() {
return this.service.getSubfolder(this.headers, this.params);
}

public Call<AssetListResponse> getSubfolderAsPojo() {
return this.service.getSubfolderPojo(this.headers, this.params);
}

protected Asset removeParam(String key) {
this.params.remove(key);
return this;
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/contentstack/cms/stack/AssetListResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.contentstack.cms.stack;

import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;

public class AssetListResponse {

@SerializedName("assets")
private List<Map<String, Object>> rawJson;

private transient List<AssetPojo> assets;

public List<AssetPojo> getAssets() {
if (assets == null && rawJson != null) {
assets = new Gson().fromJson(new Gson().toJsonTree(rawJson), new TypeToken<List<AssetPojo>>(){}.getType());
}
return assets;
}

public List<Map<String, Object>> getRawJson() {
return rawJson;
}

@Override
public String toString() {
return new com.google.gson.GsonBuilder().setPrettyPrinting().create().toJson(this);
}
}
77 changes: 77 additions & 0 deletions src/main/java/com/contentstack/cms/stack/AssetPojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.contentstack.cms.stack;
import java.util.Map;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AssetPojo {

@SerializedName("uid")
private String uid;

@SerializedName("title")
private String title;

@SerializedName("content_type")
private String contentType;

@SerializedName("file_size")
private String fileSize;

@SerializedName("filename")
private String filename;

@SerializedName("url")
private String url;

@SerializedName("description")
private String description;

@SerializedName("_version")
private int version;

@SerializedName("is_dir")
private boolean isDir;

@SerializedName("tags")
private String[] tags;

@SerializedName("name")
private String name;

// Store any unknown/dynamic fields
@Expose(serialize = false, deserialize = false)
private transient Map<String, Object> additionalFields;

public Map<String, Object> getAdditionalFields() {
return additionalFields;
}

public void setAdditionalFields(Map<String, Object> additionalFields) {
this.additionalFields = additionalFields;
}

// Getters
public String getUid() { return uid; }
public String getTitle() {
if (contentType.equals("application/vnd.contenstack.folder")) {
return name;
}
return title; }
public String getContentType() { return contentType; }
public String getFileSize() { return fileSize; }
public String getFilename() { return filename; }
public String getUrl() { return url; }
public String getDescription() { return description; }
public int getVersion() { return version; }
public boolean isDir() { return isDir; }
public String[] getTags() { return tags; }

@Override
public String toString() {
return new com.google.gson.GsonBuilder()
.setPrettyPrinting()
.create()
.toJson(this);
}
}

27 changes: 27 additions & 0 deletions src/main/java/com/contentstack/cms/stack/AssetResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.contentstack.cms.stack;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.util.Map;

public class AssetResponse {
@SerializedName("asset")
private Map<String, Object> rawJson; // Store the full asset JSON

private AssetPojo assetPojo;

public AssetPojo getAssetPojo() {
if (assetPojo == null && rawJson != null) {
assetPojo = new Gson().fromJson(new Gson().toJson(rawJson), AssetPojo.class);
}
return assetPojo;
}
public Map<String, Object> getRawJson() {
return rawJson;
}
@Override
public String toString() {
return new com.google.gson.GsonBuilder().setPrettyPrinting().create().toJson(this);
}
}


39 changes: 38 additions & 1 deletion src/main/java/com/contentstack/cms/stack/AssetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,44 @@ public interface AssetService {
Call<ResponseBody> fetch(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@GET("assets")
Call<AssetListResponse> fetchPojo(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@GET("assets/{asset_uid}")
Call<ResponseBody> single(
@HeaderMap Map<String, Object> headers,
@Path("asset_uid") String uid,
@QueryMap(encoded = true) Map<String, Object> query);

@GET("assets/{asset_uid}")
Call<AssetResponse> singlePojo(
@HeaderMap Map<String, Object> headers,
@Path("asset_uid") String assetUid,
@QueryMap(encoded = true) Map<String, Object> params);

@GET("assets")
Call<ResponseBody> specificFolder(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@GET("assets")
Call<AssetListResponse> specificFolderPojo(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@GET("assets")
Call<ResponseBody> subfolder(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@GET("assets")
Call<AssetListResponse> subfolderPojo(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@POST("assets")
Call<ResponseBody> uploadAsset(
@HeaderMap Map<String, Object> headers,
Expand Down Expand Up @@ -88,7 +109,7 @@ Call<ResponseBody> deleteVersionName(
@HeaderMap Map<String, Object> headers,
@Path("asset_uid") String assetUid,
@Path("version_number") int versionNumber);

// check this if possible to support
@GET("assets/{asset_uid}/references")
Call<ResponseBody> getReferences(
@HeaderMap Map<String, Object> headers,
Expand Down Expand Up @@ -124,16 +145,32 @@ Call<ResponseBody> singleFolder(
@Path("folder_uid") String folderUid,
@QueryMap Map<String, Object> query);

@GET("assets/folders/{folder_uid}")
Call<AssetResponse> singleFolderPojo(
@HeaderMap Map<String, Object> headers,
@Path("folder_uid") String folderUid,
@QueryMap Map<String, Object> query);

@GET("assets")
Call<ResponseBody> singleFolderByName(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@GET("assets")
Call<AssetListResponse> singleFolderByNamePojo(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@GET("assets")
Call<ResponseBody> getSubfolder(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@GET("assets")
Call<AssetListResponse> getSubfolderPojo(
@HeaderMap Map<String, Object> headers,
@QueryMap(encoded = true) Map<String, Object> query);

@POST("assets/folders")
Call<ResponseBody> createFolder(
@HeaderMap Map<String, Object> headers,
Expand Down
Loading