|
| 1 | +package com.contentstack.cms.stack; |
| 2 | + |
| 3 | +import okhttp3.ResponseBody; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | +import org.json.simple.JSONObject; |
| 6 | +import retrofit2.Call; |
| 7 | +import retrofit2.Retrofit; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | + |
| 11 | + |
| 12 | +/** |
| 13 | + * <b>Delivery tokens:</b>Delivery tokens provide read-only access to the associated environments, while management |
| 14 | + * tokens provide read-write access to the content of your stack. Use these tokens along with the stack API key to make |
| 15 | + * authorized API requests. |
| 16 | + * <br> |
| 17 | + * <b>Management tokens: </b> To authenticate Content Management API (CMA) requests over your stack content, you can |
| 18 | + * use Management Tokens |
| 19 | + * <br> |
| 20 | + * |
| 21 | + * @author ***REMOVED*** |
| 22 | + * @version 1.0.0 |
| 23 | + * @since 2022-05-19 |
| 24 | + */ |
| 25 | +public class DeliveryToken { |
| 26 | + |
| 27 | + protected final TokenService service; |
| 28 | + protected HashMap<String, Object> headers; |
| 29 | + protected HashMap<String, Object> params; |
| 30 | + |
| 31 | + protected DeliveryToken(TokenService tokenService, HashMap<String, Object> stack) { |
| 32 | + this.headers = new HashMap<>(); |
| 33 | + this.params = new HashMap<>(); |
| 34 | + this.headers.putAll(stack); |
| 35 | + this.service = tokenService; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * Sets header for the request |
| 41 | + * |
| 42 | + * @param key |
| 43 | + * header key for the request |
| 44 | + * @param value |
| 45 | + * header value for the request |
| 46 | + */ |
| 47 | + public void addHeader(@NotNull String key, @NotNull Object value) { |
| 48 | + this.headers.put(key, value); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Sets header for the request |
| 53 | + * |
| 54 | + * @param key |
| 55 | + * query param key for the request |
| 56 | + * @param value |
| 57 | + * query param value for the request |
| 58 | + */ |
| 59 | + public void addParam(@NotNull String key, @NotNull Object value) { |
| 60 | + this.params.put(key, value); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Set header for the request |
| 66 | + * |
| 67 | + * @param key |
| 68 | + * Removes query param using key of request |
| 69 | + */ |
| 70 | + public void removeParam(@NotNull String key) { |
| 71 | + this.params.remove(key); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * To clear all the query params |
| 77 | + */ |
| 78 | + protected void clearParams() { |
| 79 | + this.params.clear(); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + /** |
| 84 | + * The Get all delivery tokens request returns the details of all the delivery tokens created in a stack. |
| 85 | + * |
| 86 | + * @return Call |
| 87 | + * @see <a |
| 88 | + * href="https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-delivery-tokens">Get all |
| 89 | + * Delivery Tokens |
| 90 | + * </a> |
| 91 | + * @see #addHeader(String, Object) to add headers |
| 92 | + * @see #addParam(String, Object) to add query parameters |
| 93 | + * @since 1.0.0 |
| 94 | + */ |
| 95 | + public Call<ResponseBody> fetch() { |
| 96 | + return this.service.getDeliveryToken(this.headers); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * The Get a single delivery token request returns the details of all the delivery tokens created in a stack. |
| 101 | + * |
| 102 | + * @param tokenUid |
| 103 | + * The UID of the token that you want to retrieve a delivery token |
| 104 | + * @return Call |
| 105 | + * @see <a |
| 106 | + * href="https://www.contentstack.com/docs/developers/apis/content-management-api/#get-a-single-delivery-token">Get |
| 107 | + * a Single Delivery Token |
| 108 | + * </a> |
| 109 | + * @see #addHeader(String, Object) to add headers |
| 110 | + * @see #addParam(String, Object) to add query parameters |
| 111 | + * @since 1.0.0 |
| 112 | + */ |
| 113 | + public Call<ResponseBody> single(@NotNull String tokenUid) { |
| 114 | + return this.service.getDeliveryToken(this.headers, tokenUid); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * The Create delivery token request is used to create a delivery token in the stack. |
| 119 | + * <p> |
| 120 | + * In the Request Body, you need to pass the details of the delivery token in JSON format. The details include the |
| 121 | + * name, description, and the environment of the delivery token. |
| 122 | + * |
| 123 | + * @param requestBody |
| 124 | + * The request body to create a delivery token |
| 125 | + * @return Call |
| 126 | + * @see <a |
| 127 | + * href="https://www.contentstack.com/docs/developers/apis/content-management-api/#create-delivery-token">Create |
| 128 | + * Delivery Token |
| 129 | + * </a> |
| 130 | + * @see #addHeader(String, Object) to add headers |
| 131 | + * @see #addParam(String, Object) to add query parameters |
| 132 | + * @since 1.0.0 |
| 133 | + */ |
| 134 | + public Call<ResponseBody> create(@NotNull JSONObject requestBody) { |
| 135 | + return this.service.createDeliveryToken(this.headers, requestBody); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * The Update delivery token request lets you update the details of a delivery token. |
| 140 | + * <p> |
| 141 | + * In the Request Body, you need to pass the updated details of the delivery token in JSON format. The details |
| 142 | + * include the updated name, description, and/or the environment of the delivery token. |
| 143 | + * <p> |
| 144 | + * You need to specify the branch and alias scope for your delivery token through the following schema in the |
| 145 | + * request body: |
| 146 | + * |
| 147 | + * @param tokenUid |
| 148 | + * The UID of the token that you want to update |
| 149 | + * @param requestBody |
| 150 | + * the body should be of @{@link JSONObject} type |
| 151 | + * @return Call |
| 152 | + * @see <a |
| 153 | + * href="https://www.contentstack.com/docs/developers/apis/content-management-api/#update-delivery-token">Update |
| 154 | + * Delivery Token |
| 155 | + * </a> |
| 156 | + * @see #addHeader(String, Object) to add headers |
| 157 | + * @see #addParam(String, Object) to add query parameters |
| 158 | + * @since 1.0.0 |
| 159 | + */ |
| 160 | + public Call<ResponseBody> update(@NotNull String tokenUid, @NotNull JSONObject requestBody) { |
| 161 | + return this.service.updateDeliveryToken(this.headers, tokenUid, requestBody); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * The Delete delivery token request deletes a specific delivery token |
| 166 | + * |
| 167 | + * @param tokenUid |
| 168 | + * The UID of the token that you want to delete |
| 169 | + * @return Call |
| 170 | + * @see <a |
| 171 | + * href="https://www.contentstack.com/docs/developers/apis/content-management-api/#delete-delivery-token">Delete |
| 172 | + * Delivery Token |
| 173 | + * </a> |
| 174 | + * @see #addHeader(String, Object) to add headers |
| 175 | + * @see #addParam(String, Object) to add query parameters |
| 176 | + * @since 1.0.0 |
| 177 | + */ |
| 178 | + public Call<ResponseBody> delete(@NotNull String tokenUid) { |
| 179 | + return this.service.deleteDeliveryToken(this.headers, tokenUid, false); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * The Delete delivery token request deletes a specific delivery token. |
| 184 | + * |
| 185 | + * @param tokenUid |
| 186 | + * The UID of the token that you want to delete |
| 187 | + * @param isForce |
| 188 | + * provide ‘true’ to force delete a delivery token |
| 189 | + * @return Call |
| 190 | + * @see <a |
| 191 | + * href="https://www.contentstack.com/docs/developers/apis/content-management-api/#delete-delivery-token">Delete |
| 192 | + * Delivery Token |
| 193 | + * </a> |
| 194 | + * @see #addHeader(String, Object) to add headers |
| 195 | + * @see #addParam(String, Object) to add query parameters |
| 196 | + * @since 1.0.0 |
| 197 | + */ |
| 198 | + public Call<ResponseBody> delete(@NotNull String tokenUid, @NotNull Boolean isForce) { |
| 199 | + return this.service.deleteDeliveryToken(this.headers, tokenUid, isForce); |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | +} |
0 commit comments