
com.contentstack.cms.stack.ManagementToken Maven / Gradle / Ivy
Show all versions of cms Show documentation
package com.contentstack.cms.stack;
import com.contentstack.cms.BaseImplementation;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONObject;
import retrofit2.Call;
import java.util.HashMap;
import java.util.Map;
/**
* To authenticate Content Management API (CMA) requests over your stack
* content, you can use Management Tokens
*
*
* @author ***REMOVED***
* @version v0.1.0
* @since 2022-10-22
*/
public class ManagementToken implements BaseImplementation {
protected final TokenService service;
protected HashMap headers;
protected HashMap params;
private String tokenUid;
protected ManagementToken(TokenService service,Map headers) {
this.headers = new HashMap<>();
this.headers.putAll(headers);
this.params = new HashMap<>();
this.service = service;
}
protected ManagementToken(TokenService service,Map headers, String tokenUid) {
this.headers = new HashMap<>();
this.headers.putAll(headers);
this.params = new HashMap<>();
this.service = service;
this.tokenUid = tokenUid;
}
void validate() {
if (this.tokenUid == null || this.tokenUid.isEmpty())
throw new IllegalAccessError("Token uid Can Not Be Null OR Empty");
}
/**
* Sets header for the request
*
* @param key query param key for the request
* @param value query param value for the request
*/
@Override
public ManagementToken addParam(@NotNull String key, @NotNull Object value) {
this.params.put(key, value);
return this;
}
@Override
public ManagementToken addHeader(@NotNull String key, @NotNull String value) {
this.headers.put(key, value);
return this;
}
@Override
public ManagementToken addParams(@NotNull HashMap params) {
this.params.putAll(params);
return this;
}
@Override
public ManagementToken addHeaders(@NotNull HashMap headers) {
this.headers.putAll(headers);
return this;
}
/**
* Set header for the request
*
* @param key Removes query param using key of request
*/
public void removeParam(@NotNull String key) {
this.params.remove(key);
}
/**
* To clear all the query params
*/
protected void clearParams() {
this.params.clear();
}
/**
* The Get all management tokens request returns the details of all the
* management tokens generated in a stack and
* NOT the actual management tokens.
*
* @return Call
* @see Get
* all
* Management Tokens
*
* @see #addHeader(String, String) to add headers
* @see #addParam(String, Object) to add query parameters
* @since 0.1.0
*/
public Call find() {
return this.service.fetchManagementToken(this.headers, this.params);
}
/**
* The Get a single management token request returns the details of a specific
* management token generated in a stack
* and NOT the actual management token.
*
* @return Call
* @see Get
* a
* single management token
*
* @see #addHeader(String, String) to add headers
* @since 0.1.0
*/
public Call fetch() {
validate();
return this.service.getSingleManagementToken(this.headers, this.tokenUid);
}
/**
* The Create management token request is used to create a management token in a
* stack. This token provides you with
* read-write access to the content of your stack.
*
* @param requestBody details of the management token in @{@link JSONObject}
* format
* @return Call
* @see Create
* a
* management token
*
* @see #addHeader(String, String) to add headers
* @since 0.1.0
*/
public Call create(@NotNull JSONObject requestBody) {
return this.service.createManagementToken(this.headers, requestBody);
}
/**
* The Update management token request lets you update the details of a
* management token. You can change the name
* and description of the token; update the stack-level permissions assigned to
* the token; and change the expiry
* date of the token (if set).
*
* In the Request Body, you need to pass the updated details of the management
* token in JSON format.
*
* To specify the updated branch and alias scope for your management token, use
* the following schema in the request
* body:
*
* @param requestBody details of the management token in @{@link JSONObject}
* format
* @return Call
* @see Update
* management token
*
* @see #addHeader(String, String) to add headers
* @since 0.1.0
*/
public Call update(@NotNull JSONObject requestBody) {
validate();
return this.service.updateManagementToken(this.headers, this.tokenUid, requestBody);
}
/**
* The Delete management token request deletes a specific management token
*
* @return Call
* @see Delete
* management token
*
* @see #addHeader(String, String) to add headers
* @since 0.1.0
*/
public Call delete() {
validate();
return this.service.deleteManagementToken(this.headers, this.tokenUid);
}
}