3 import com.google.gson.annotations.SerializedName;
4 import io.bytom.common.ParameterizedTypeImpl;
5 import io.bytom.common.Utils;
6 import io.bytom.exception.BytomException;
7 import io.bytom.http.Client;
8 import org.apache.log4j.Logger;
10 import java.lang.reflect.Type;
11 import java.util.HashMap;
12 import java.util.List;
16 * <h1>AccessToken Class</h1>
18 public class AccessToken {
32 * create time of token
34 @SerializedName(value = "created_at", alternate = {"create"})
35 public String createTime;
37 private static Logger logger = Logger.getLogger(AccessToken.class);
40 * Serializes the AccessToken into a form that is safe to transfer over the wire.
42 * @return the JSON-serialized representation of the AccessToken object
44 public String toJson() {
45 return Utils.serializer.toJson(this);
48 public static class Builder {
62 * @param id the id to set
65 public Builder setId(String id) {
71 * @param type the type to set, possibly null
74 public Builder setType(String type) {
80 * Call create-access-token api
82 * @param client client object that makes requests to the core
83 * @return AccessToken object
84 * @throws BytomException
86 public AccessToken create(Client client) throws BytomException {
87 AccessToken accessToken = client.request("create-access-token", this, AccessToken.class);
89 logger.info("create-access-token:");
90 logger.info(accessToken.toJson());
97 * Call check-access-token api
99 * @param client client object that makes requests to the core
101 * @param secret secret
102 * @throws BytomException
104 public static void check(Client client, String id, String secret) throws BytomException {
105 Map<String, Object> req = new HashMap<String, Object>();
107 req.put("secret", secret);
108 // add a native control
109 if (client.getUrl().equals("http://127.0.0.1:9888") ||
110 client.getUrl().equals("http://127.0.0.1:9888/")) {
111 client.request("check-access-token", req);
112 logger.info("check-access-token successfully.");
114 logger.info("this is a native method.");
119 * Call delete-access-token api
120 * native method, can't rpc
122 * @param client client object that makes requests to the core
124 * @throws BytomException
126 public static void delete(Client client, String id) throws BytomException {
127 Map<String, Object> req = new HashMap<String, Object>();
129 // add a native control
130 if (client.getUrl().equals("http://127.0.0.1:9888") ||
131 client.getUrl().equals("http://127.0.0.1:9888/")) {
132 client.request("delete-access-token", req);
133 logger.info("delete-access-token.");
135 logger.info("this is a native method.");
140 * Call list-access-tokens api.<br>
141 * native method, can't rpc
143 * @param client client object that makes requests to the core
144 * @return list of AccessToken objects
145 * @throws BytomException
147 public static List<AccessToken> list(Client client) throws BytomException {
148 Type listType = new ParameterizedTypeImpl(List.class, new Class[]{AccessToken.class});
149 List<AccessToken> accessTokenList = null;
150 if (client.getUrl().equals("http://127.0.0.1:9888") ||
151 client.getUrl().equals("http://127.0.0.1:9888/")) {
152 accessTokenList = client.request("list-access-tokens", null, listType);
154 logger.info("list-access-tokens:");
155 logger.info("size of accessTokenList:" + accessTokenList.size());
156 logger.info(accessTokenList.get(0).toJson());
158 logger.info("this is a native method.");
161 return accessTokenList;