|
| 1 | +package com.contentstack.cms.stack; |
| 2 | + |
| 3 | +import com.contentstack.cms.core.CMSRuntimeException; |
| 4 | +import okhttp3.RequestBody; |
| 5 | +import okhttp3.ResponseBody; |
| 6 | +import org.jetbrains.annotations.NotNull; |
| 7 | +import org.json.simple.JSONObject; |
| 8 | +import retrofit2.Call; |
| 9 | +import retrofit2.Retrofit; |
| 10 | + |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +public class Extensions { |
| 15 | + |
| 16 | + protected final ExtensionsService service; |
| 17 | + protected HashMap<String, Object> headers; |
| 18 | + protected HashMap<String, Object> params; |
| 19 | + |
| 20 | + protected Extensions(Retrofit retrofit, HashMap<String, Object> stackHeaders) { |
| 21 | + this.headers = new HashMap<>(); |
| 22 | + this.params = new HashMap<>(); |
| 23 | + this.headers.putAll(stackHeaders); |
| 24 | + this.service = retrofit.create(ExtensionsService.class); |
| 25 | + } |
| 26 | + |
| 27 | + public Extensions addHeader(@NotNull String key, @NotNull String value) { |
| 28 | + this.headers.put(key, value); |
| 29 | + return this; |
| 30 | + } |
| 31 | + |
| 32 | + public Extensions addParam(@NotNull String key, @NotNull String value) { |
| 33 | + this.params.put(key, value); |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * The Get all custom fields request is used to get the information of all custom fields created in a stack. |
| 39 | + * |
| 40 | + * @param query |
| 41 | + * For custom fields <b>Example:"type":"field"</b> |
| 42 | + * @param isIncludeBranch |
| 43 | + * Set this to 'true' to include the '_branch' top-level key in the response. This key states the unique ID |
| 44 | + * of the branch where the concerned Contentstack module resides. |
| 45 | + * <b>Example:false</b> |
| 46 | + * @return @{@link Call} |
| 47 | + */ |
| 48 | + public Call<ResponseBody> getAll(@NotNull String query, boolean isIncludeBranch) { |
| 49 | + return this.service.getAll(this.headers, query, isIncludeBranch); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * @param customFieldUid |
| 55 | + * Enter the UID of the custom field of which you want to retrieve the details. |
| 56 | + * @param queryParam |
| 57 | + * Set this to 'true' to include the '_branch' top-level key in the response. This key states the unique ID |
| 58 | + * of the branch where the concerned Contentstack module resides. |
| 59 | + * <p> |
| 60 | + * <b>Example:false</b> |
| 61 | + * @return Call |
| 62 | + */ |
| 63 | + public Call<ResponseBody> get(@NotNull String customFieldUid, Map<String, Object> queryParam) { |
| 64 | + if (queryParam == null) { |
| 65 | + queryParam = new HashMap<>(); |
| 66 | + this.headers.put("Content-Type", "multipart/form-data"); |
| 67 | + } |
| 68 | + return this.service.getSingle(this.headers, customFieldUid, queryParam); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * The Upload a custom field request is used to upload a custom field to Contentstack. |
| 73 | + * <p> |
| 74 | + * - extension[upload]: Select the HTML file of the custom field that you want to upload<br> - extension[title]: |
| 75 | + * Enter the title of the custom field that you want to upload<br> - extension[data_type]: Enter the data type for |
| 76 | + * the input field of the custom field<br> - extension[tags]: Enter the tags that you want to assign to the custom |
| 77 | + * field<br> - extension[multiple]: Enter ‘true’ if you want your custom field to store multiple values<br> - |
| 78 | + * extension[type]: Enter type as ‘field’, since this is a custom field extension.<br> |
| 79 | + * |
| 80 | + * @param queryParam |
| 81 | + * Set this to 'true' to include the '_branch' top-level key in the response. This key states the unique ID |
| 82 | + * of the branch where the concerned Contentstack module resides. |
| 83 | + * <p> |
| 84 | + * <b>Example:false</b> |
| 85 | + * @param body |
| 86 | + * In the ‘Body’ section, you need to provide the following ‘Body’ parameters under ‘form-data’ |
| 87 | + * <pre> |
| 88 | + * Use like: Map<String, RequestBody> params = new HashMap<>(); |
| 89 | + * //prepare RequestBody RequestBody someDataBody = ....; |
| 90 | + * //add it Map object params.put("DYNAMIC_PARAM_NAME", someDataBody); |
| 91 | + * </pre> |
| 92 | + * @return Call |
| 93 | + */ |
| 94 | + public Call<ResponseBody> uploadCustomField(Map<String, RequestBody> body, Map<String, Object> queryParam) { |
| 95 | + if (queryParam == null) { |
| 96 | + queryParam = new HashMap<>(); |
| 97 | + } |
| 98 | + this.headers.put("Content-Type", "multipart/form-data"); |
| 99 | + return this.service.uploadCustomField(this.headers, body, queryParam); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * The Upload a custom field request is used to upload a custom field to Contentstack. |
| 104 | + * <p> |
| 105 | + * - extension[upload]: Select the HTML file of the custom field that you want to upload<br> - extension[title]: |
| 106 | + * Enter the title of the custom field that you want to upload<br> - extension[data_type]: Enter the data type for |
| 107 | + * the input field of the custom field<br> - extension[tags]: Enter the tags that you want to assign to the custom |
| 108 | + * field<br> - extension[multiple]: Enter ‘true’ if you want your custom field to store multiple values<br> - |
| 109 | + * extension[type]: Enter type as ‘field’, since this is a custom field extension.<br> |
| 110 | + * |
| 111 | + * @param queryParam |
| 112 | + * Set this to 'true' to include the '_branch' top-level key in the response. This key states the unique ID |
| 113 | + * of the branch where the concerned Contentstack module resides. |
| 114 | + * <p> |
| 115 | + * <b>Example:false</b> |
| 116 | + * @param body |
| 117 | + * In the ‘Body’ section, you need to provide the following ‘Body’ parameters under ‘form-data’ |
| 118 | + * <pre> |
| 119 | + * Use like: Map<String, RequestBody> params = new HashMap<>(); |
| 120 | + * //prepare RequestBody RequestBody someDataBody = ....; |
| 121 | + * //add it Map object params.put("DYNAMIC_PARAM_NAME", someDataBody); |
| 122 | + * </pre> |
| 123 | + * @return Call |
| 124 | + */ |
| 125 | + public Call<ResponseBody> uploadCustomField(Map<String, Object> queryParam, JSONObject body) { |
| 126 | + if (queryParam != null) { |
| 127 | + queryParam = new HashMap<>(); |
| 128 | + if (!this.headers.containsKey("Content-Type")) { |
| 129 | + this.headers.put("Content-Type", "application/json"); |
| 130 | + } |
| 131 | + } |
| 132 | + return this.service.uploadCustomField(this.headers, queryParam, body); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + /** |
| 137 | + * @param customFieldUid |
| 138 | + * The UID of the custom field that you want to update |
| 139 | + * @param queryParam |
| 140 | + * Set this to 'true' to include the '_branch' top-level key in the response. This key states the unique ID |
| 141 | + * of the branch where the concerned Contentstack module resides. |
| 142 | + * <br> |
| 143 | + * <b>Example:false</b> |
| 144 | + * @param body |
| 145 | + * JSON requestBody |
| 146 | + * @return Call |
| 147 | + */ |
| 148 | + public Call<ResponseBody> update(@NotNull String customFieldUid, Map<String, Object> queryParam, JSONObject body) { |
| 149 | + if (body == null) { |
| 150 | + try { |
| 151 | + throw new CMSRuntimeException("body can not be Null"); |
| 152 | + } catch (CMSRuntimeException e) { |
| 153 | + throw new RuntimeException(e.getLocalizedMessage()); |
| 154 | + } |
| 155 | + } |
| 156 | + if (queryParam == null) { |
| 157 | + queryParam = new HashMap<>(); |
| 158 | + } |
| 159 | + return this.service.update(this.headers, customFieldUid, queryParam, body); |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + /** |
| 164 | + * Delete custom field request is used to delete a specific custom field |
| 165 | + * |
| 166 | + * @param customFieldUid |
| 167 | + * UID of the custom field that you want to update |
| 168 | + * @return Call |
| 169 | + */ |
| 170 | + public Call<ResponseBody> delete(@NotNull String customFieldUid) { |
| 171 | + return this.service.delete(this.headers, customFieldUid); |
| 172 | + } |
| 173 | +} |
0 commit comments