|
| 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 | +import java.util.Map; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * An alias acts as a pointer to a particular branch. You can specify the alias ID in your frontend code to pull content |
| 15 | + * from the target branch associated with an alias. |
| 16 | + * |
| 17 | + * @author ***REMOVED*** |
| 18 | + * @version 1.0.0 |
| 19 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#aliases">About Aliases |
| 20 | + * </a> |
| 21 | + * @since 2022-05-19 |
| 22 | + */ |
| 23 | +public class Alias { |
| 24 | + |
| 25 | + protected final Map<String, Object> headers; |
| 26 | + protected Map<String, Object> params; |
| 27 | + protected final AliasService service; |
| 28 | + |
| 29 | + protected Alias(Retrofit instance, @NotNull Map<String, Object> stackHeaders) { |
| 30 | + this.headers = new HashMap<>(); |
| 31 | + this.headers.put("Content-Type", "application/json"); |
| 32 | + this.headers.putAll(stackHeaders); |
| 33 | + params = new HashMap<>(); |
| 34 | + this.service = instance.create(AliasService.class); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Sets header for the request |
| 39 | + * |
| 40 | + * @param key |
| 41 | + * header key for the request |
| 42 | + * @param value |
| 43 | + * header value for the request |
| 44 | + */ |
| 45 | + public void addHeader(@NotNull String key, @NotNull Object value) { |
| 46 | + this.headers.put(key, value); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Sets header for the request |
| 51 | + * |
| 52 | + * @param key |
| 53 | + * query param key for the request |
| 54 | + * @param value |
| 55 | + * query param value for the request |
| 56 | + */ |
| 57 | + public void addParam(@NotNull String key, @NotNull Object value) { |
| 58 | + this.params.put(key, value); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * Set header for the request |
| 64 | + * |
| 65 | + * @param key |
| 66 | + * Removes query param using key of request |
| 67 | + */ |
| 68 | + public void removeParam(@NotNull String key) { |
| 69 | + this.params.remove(key); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + /** |
| 74 | + * To clear all the query params |
| 75 | + */ |
| 76 | + protected void clearParams() { |
| 77 | + this.params.clear(); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * The Get all aliases request returns comprehensive information of all the aliases available in a particular stack |
| 83 | + * in your account. |
| 84 | + * <p> |
| 85 | + * |
| 86 | + * @return Call |
| 87 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#get-all-aliases">Get all |
| 88 | + * aliases</a> |
| 89 | + * @since 1.0.0 |
| 90 | + */ |
| 91 | + public Call<ResponseBody> fetch() { |
| 92 | + return this.service.fetch(this.headers, this.params); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * The Get a single alias request returns information of a specific alias. |
| 97 | + * |
| 98 | + * @param uid |
| 99 | + * The unique ID of the alias of which you want to retrieve the details. The UID of an alias is unique |
| 100 | + * across a stack. Execute the Get all aliases call to retrieve the UID of an alias |
| 101 | + * @return Call |
| 102 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#get-a-single-branch"> |
| 103 | + * Get a single branch</a> |
| 104 | + * @since 1.0.0 |
| 105 | + */ |
| 106 | + public Call<ResponseBody> single(@NotNull String uid) { |
| 107 | + return this.service.single(this.headers, uid); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * <b>Assign or Update an alias</b> |
| 112 | + * <br> |
| 113 | + * The Assign an alias request creates a new alias in a particular stack of your organization. This alias can point |
| 114 | + * to any existing branch (target branch) of your stack. |
| 115 | + * <p> |
| 116 | + * You can use the same request to update the target branch of an alias. In the “Body” section, you need to provide |
| 117 | + * the UID of the new target branch that will be associated with the alias. |
| 118 | + * |
| 119 | + * @return Call |
| 120 | + * @see <a |
| 121 | + * href="https://www.contentstack.com/docs/developers/apis/content-management-api/#assign-or-update-an-alias">Update |
| 122 | + * a branch</a> |
| 123 | + * @see #addHeader(String, Object) to add headers |
| 124 | + * @since 1.0.0 |
| 125 | + */ |
| 126 | + public Call<ResponseBody> update(@NotNull JSONObject body) { |
| 127 | + return this.service.update(this.headers, body); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * The Delete an alias request deletes an existing alias. |
| 132 | + * <p> |
| 133 | + * To confirm deletion of an alias, you need to specify the force=true query parameter. |
| 134 | + * <p> |
| 135 | + * When executing the API call, in the “URL Parameters” section, provide the UID of your alias. |
| 136 | + * |
| 137 | + * @return Call |
| 138 | + * @see <a href="https://www.contentstack.com/docs/developers/apis/content-management-api/#delete-an-alias">Delete a |
| 139 | + * branch</a> |
| 140 | + * @see #addHeader(String, Object) to add headers |
| 141 | + * @see #addParam(String, Object) to add query params |
| 142 | + * @since 1.0.0 |
| 143 | + */ |
| 144 | + public Call<ResponseBody> delete(@NotNull String branchUid) { |
| 145 | + return this.service.delete(this.headers, branchUid, this.params); |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments