OSDN Git Service

adjust structure
[bytom/bytom-java-sdk.git] / java-sdk / src / main / java / io / bytom / api / AccessToken.java
1 package io.bytom.api;
2
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;
9
10 import java.lang.reflect.Type;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 /**
16  * <h1>AccessToken Class</h1>
17  */
18 public class AccessToken {
19     /**
20      * Token id
21      */
22     public String id;
23     /**
24      * Token token
25      */
26     public String token;
27     /**
28      * Token type
29      */
30     public String type;
31     /**
32      * create time of token
33      */
34     @SerializedName(value = "created_at", alternate = {"create"})
35     public String createTime;
36
37     private static Logger logger = Logger.getLogger(AccessToken.class);
38
39     /**
40      * Serializes the AccessToken into a form that is safe to transfer over the wire.
41      *
42      * @return the JSON-serialized representation of the AccessToken object
43      */
44     public String toJson() {
45         return Utils.serializer.toJson(this);
46     }
47
48     public static class Builder {
49         /**
50          * id of Token
51          */
52         public String id;
53         /**
54          * type of Token
55          */
56         public String type;
57
58         public Builder() {
59         }
60
61         /**
62          * @param id the id to set
63          * @return Builder
64          */
65         public Builder setId(String id) {
66             this.id = id;
67             return this;
68         }
69
70         /**
71          * @param type the type to set, possibly null
72          * @return Builder
73          */
74         public Builder setType(String type) {
75             this.type = type;
76             return this;
77         }
78
79         /**
80          * Call create-access-token api
81          *
82          * @param client client object that makes requests to the core
83          * @return AccessToken object
84          * @throws BytomException
85          */
86         public AccessToken create(Client client) throws BytomException {
87             AccessToken accessToken = client.request("create-access-token", this, AccessToken.class);
88
89             logger.info("create-access-token:");
90             logger.info(accessToken.toJson());
91
92             return accessToken;
93         }
94     }
95
96     /**
97      * Call check-access-token api
98      *
99      * @param client client object that makes requests to the core
100      * @param id     id
101      * @param secret secret
102      * @throws BytomException
103      */
104     public static void check(Client client, String id, String secret) throws BytomException {
105         Map<String, Object> req = new HashMap<String, Object>();
106         req.put("id", id);
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.");
113         } else {
114             logger.info("this is a native method.");
115         }
116     }
117
118     /**
119      * Call delete-access-token api
120      * native method, can't rpc
121      *
122      * @param client client object that makes requests to the core
123      * @param id     id
124      * @throws BytomException
125      */
126     public static void delete(Client client, String id) throws BytomException {
127         Map<String, Object> req = new HashMap<String, Object>();
128         req.put("id", id);
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.");
134         } else {
135             logger.info("this is a native method.");
136         }
137     }
138
139     /**
140      * Call list-access-tokens api.<br>
141      * native method, can't rpc
142      *
143      * @param client client object that makes requests to the core
144      * @return list of AccessToken objects
145      * @throws BytomException
146      */
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);
153
154             logger.info("list-access-tokens:");
155             logger.info("size of accessTokenList:" + accessTokenList.size());
156             logger.info(accessTokenList.get(0).toJson());
157         } else {
158             logger.info("this is a native method.");
159         }
160
161         return accessTokenList;
162     }
163
164 }