Skip to content

Commit d691ea2

Browse files
authored
Add support for data objects (#14)
* Start on data object methods * Add more methods * Finish all API methods * Add additional methods in data objects that take in a license key * Fix tests and missing parameters * Update javadoc * Update ProductAndKeyModel.java
1 parent 87b01f2 commit d691ea2

15 files changed

+522
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,5 @@ fabric.properties
9999
.idea/httpRequests
100100

101101
# Android studio 3.1+ serialized cache file
102-
.idea/caches/build_file_checksums.ser
102+
.idea/caches/build_file_checksums.ser
103+
apikeys.json

src/main/java/io/cryptolens/internal/HelperMethods.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
import io.cryptolens.legacy.RequestHandler;
66

77
import java.lang.reflect.Field;
8-
import java.util.HashMap;
9-
import java.util.Map;
8+
import java.util.*;
109

1110
public class HelperMethods {
1211

1312
public static <T extends BasicResult> T SendRequestToWebAPI(String method, Object model, Map<String,String> extraParams, Class<T> clazz) {
1413

1514
Map<String,String> params = new HashMap<>();
15+
List<Field> allFields = new ArrayList<>();
16+
getAllFields(allFields, model.getClass());
1617

17-
for(Field field : model.getClass().getDeclaredFields()) {
18+
for(Field field : allFields) {
1819
field.setAccessible(true);
1920
try {
2021
Object value = field.get(model);
@@ -47,4 +48,15 @@ public static <T extends BasicResult> T SendRequestToWebAPI(String method, Objec
4748

4849
return null;
4950
}
51+
52+
// from: https://stackoverflow.com/a/1042827/1275924
53+
private static List<Field> getAllFields(List<Field> fields, Class<?> type) {
54+
fields.addAll(Arrays.asList(type.getDeclaredFields()));
55+
56+
if (type.getSuperclass() != null) {
57+
getAllFields(fields, type.getSuperclass());
58+
}
59+
60+
return fields;
61+
}
5062
}
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
package io.cryptolens.methods;
2+
3+
import com.google.gson.Gson;
4+
import io.cryptolens.internal.*;
5+
import io.cryptolens.models.*;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
/**
11+
* <p>The following methods allow you to work with data objects (aka. metadata or custom variables) associated with a
12+
* license key. Data objects can be used to store specific properties that (eg. username, OS version). More importantly
13+
* though, they are used if you plan to implement a <a href="https://help.cryptolens.io/licensing-models/usage-based" target="_blank">usage-based licensing model</a>.
14+
* </p>
15+
*
16+
* <p><b>Access token remarks:</b> When you create an access token for any of the methods below, we recommend to <u>specify the product</u>
17+
* and to set the <u>keylock to <b>-1</b></u>.</p>
18+
*/
19+
public class Data {
20+
/**
21+
* Adds a new data object to a license key.
22+
* @param token The access token with 'AddDataObject' permission and KeyLock set to '-1'.
23+
* @param license The license key object (it's used to get the product id and key string).
24+
* @param name The name of the data object. Max 10 characters.
25+
* @param intValue An int value (int32) to store.
26+
* @param stringValue A string value (text) to store. Max 10000 characters.
27+
* @return
28+
*/
29+
public static BasicResult AddDataObject(String token, LicenseKey license, String name, int intValue, String stringValue) {
30+
return AddDataObject(token, new AddDataObjectToKeyModel(license.ProductId, license.Key, name, intValue, stringValue));
31+
}
32+
33+
public static BasicResult AddDataObject(String token, AddDataObjectToKeyModel model) {
34+
35+
Map<String,String> extraParams = new HashMap<>();
36+
37+
extraParams.put("token", token);
38+
39+
return HelperMethods.SendRequestToWebAPI("data/AddDataObjectToKey", model, extraParams, BasicResult.class);
40+
}
41+
42+
/**
43+
* List data objects of a certain license.
44+
* @param token The access token with 'ListDataObjects' permission and KeyLock set to '-1'.
45+
* @param license The license key object (it's used to get the product id and key string).
46+
* @return
47+
*/
48+
public static ListOfDataObjectsResult ListDataObjects(String token, LicenseKey license) {
49+
return ListDataObjects(token, new ListDataObjectsToKeyModel(license.ProductId, license.Key, ""));
50+
}
51+
52+
/**
53+
* List data objects of a certain license.
54+
* @param token The access token with 'ListDataObjects' permission and KeyLock set to '-1'.
55+
* @param license The license key object (it's used to get the product id and key string).
56+
* @param contains Shows only Data Objects where the name contains the following string.
57+
* @return
58+
*/
59+
public static ListOfDataObjectsResult ListDataObjects(String token, LicenseKey license, String contains) {
60+
return ListDataObjects(token, new ListDataObjectsToKeyModel(license.ProductId, license.Key, contains));
61+
}
62+
63+
public static ListOfDataObjectsResult ListDataObjects(String token, ListDataObjectsToKeyModel model) {
64+
65+
Map<String,String> extraParams = new HashMap<>();
66+
67+
extraParams.put("token", token);
68+
69+
return HelperMethods.SendRequestToWebAPI("data/listdataobjectstokey", model, extraParams, ListOfDataObjectsResult.class);
70+
}
71+
72+
/**
73+
* This method will assign a new integer value to a Data Object.
74+
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
75+
* @param license The license key object (it's used to get the product id and key string).
76+
* @param id The unique object id for the data object.
77+
* @param intValue The new int value that should be assigned to the data object.
78+
* @return
79+
*/
80+
public static BasicResult SetIntValue(String token, LicenseKey license, long id, int intValue) {
81+
return SetIntValue(token, new SetIntValueToKeyModel(license.ProductId, license.Key, id, intValue));
82+
}
83+
84+
public static BasicResult SetIntValue(String token, SetIntValueToKeyModel model) {
85+
86+
Map<String,String> extraParams = new HashMap<>();
87+
88+
extraParams.put("token", token);
89+
90+
return HelperMethods.SendRequestToWebAPI("data/setintvaluetokey", model, extraParams, BasicResult.class);
91+
}
92+
93+
/**
94+
* This method will increment the integer value in a Data Object by a certain constant (non-negative).
95+
* You can always decrement it. Note, this method does not allow integer overflows, i.e. if you increment
96+
* by a constant that would result in an overflow, an error will be thrown. Note also that you can use the Feature lock
97+
* in the Access Token to specify the upper bound of the increment constant. So, if you only want to allow incrementing
98+
* by 1, please set Feature lock field to 1 also. Please see Remarks for more details (including access token set up).
99+
* @param token The access token with 'IncrementIntValue' permission and KeyLock set to '-1'.
100+
* @param license The license key object (it's used to get the product id and key string).
101+
* @param id The unique object id for the data object.
102+
* @param intValue The constant int (non-negative) value that should be added to the current
103+
* IntValue of the data object. For example, if this value is set to 5 and the
104+
* old IntValue is 1, then the new IntValue will be the old one plus 5, i.e. 6.
105+
* Note, if you would set this value to -5 instead, the same result would be achieved.
106+
* @return
107+
*/
108+
public static BasicResult IncrementIntValue(String token, LicenseKey license, long id, int intValue) {
109+
return IncrementIntValue(token, new IncrementIntValueToKeyModel(license.ProductId, license.Key, id, intValue, false, 0));
110+
}
111+
112+
/**
113+
* This method will increment the integer value in a Data Object by a certain constant (non-negative).
114+
* You can always decrement it. Note, this method does not allow integer overflows, i.e. if you increment
115+
* by a constant that would result in an overflow, an error will be thrown. Note also that you can use the Feature lock
116+
* in the Access Token to specify the upper bound of the increment constant. So, if you only want to allow incrementing
117+
* by 1, please set Feature lock field to 1 also. Please see Remarks for more details (including access token set up).
118+
* @param token The access token with 'IncrementIntValue' permission and KeyLock set to '-1'.
119+
* @param license The license key object (it's used to get the product id and key string).
120+
* @param id The unique object id for the data object.
121+
* @param intValue The constant int (non-negative) value that should be added to the current
122+
* IntValue of the data object. For example, if this value is set to 5 and the
123+
* old IntValue is 1, then the new IntValue will be the old one plus 5, i.e. 6.
124+
* Note, if you would set this value to -5 instead, the same result would be achieved.
125+
* @param enableBound If set to true, it will be possible to specify an upper bound. For example,
126+
* if you set the Bound parameter (below) to 10, you will be able to increment
127+
* the int value until you reach ten (inclusive). Once the upper bound is reached,
128+
* an error will be thrown.
129+
* @param bound This is the upper bound that will be enforced on the increment operation.
130+
* It will only be enforced if EnableBound is set to true. Please read the description about enableBound.
131+
* @return
132+
*/
133+
public static BasicResult IncrementIntValue(String token, LicenseKey license, long id, int intValue, boolean enableBound, int bound) {
134+
return IncrementIntValue(token, new IncrementIntValueToKeyModel(license.ProductId, license.Key, id, intValue, enableBound, bound));
135+
}
136+
137+
public static BasicResult IncrementIntValue(String token, IncrementIntValueToKeyModel model) {
138+
139+
Map<String,String> extraParams = new HashMap<>();
140+
141+
extraParams.put("token", token);
142+
143+
return HelperMethods.SendRequestToWebAPI("data/incrementintvaluetokey", model, extraParams, BasicResult.class);
144+
}
145+
146+
/**
147+
* This method will decrement the integer value in a Data Object by a certain constant (non-negative).
148+
* You can always increment it. Note, this method does not allow integer overflows, i.e. if you decrement
149+
* by a constant that would result in an overflow, an error will be thrown. Note also that you can use the
150+
* Feature lock in the Access Token to specify the upper bound of the decrement constant. So, if you only
151+
* want to allow decrementing by 1, please set Feature lock field to 1 also. Please see Remarks for more
152+
* details (including access token setup).
153+
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
154+
* @param license The license key object (it's used to get the product id and key string).
155+
* @param id The unique object id for the data object.
156+
* @param intValue The constant int value that should be subtracted to the current IntValue of the data object.
157+
* For example, if this value is set to 5 and the old IntValue is 11, then the new IntValue
158+
* will be the old one minus 5, i.e. 6. Note, if you would set this value to -5 instead, the
159+
* same result would be achieved.
160+
* @return
161+
*/
162+
public static BasicResult DecrementIntValue(String token, LicenseKey license, long id, int intValue) {
163+
return DecrementIntValue(token, new DecrementIntValueToKeyModel(license.ProductId, license.Key, id, intValue, false, 0));
164+
}
165+
166+
/**
167+
* This method will decrement the integer value in a Data Object by a certain constant (non-negative).
168+
* You can always increment it. Note, this method does not allow integer overflows, i.e. if you decrement
169+
* by a constant that would result in an overflow, an error will be thrown. Note also that you can use the
170+
* Feature lock in the Access Token to specify the upper bound of the decrement constant. So, if you only
171+
* want to allow decrementing by 1, please set Feature lock field to 1 also. Please see Remarks for more
172+
* details (including access token setup).
173+
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
174+
* @param license The license key object (it's used to get the product id and key string).
175+
* @param id The unique object id for the data object.
176+
* @param intValue The constant int value that should be subtracted to the current IntValue of the data object.
177+
* For example, if this value is set to 5 and the old IntValue is 11, then the new IntValue
178+
* will be the old one minus 5, i.e. 6. Note, if you would set this value to -5 instead, the
179+
* same result would be achieved.
180+
* @param enableBound If set to true, it will be possible to specify a lower bound. For example, if you set the Bound
181+
* parameter (below) to 0, you will be able to decrement the int value until you reach zero (inclusive).
182+
* Once the lower bound is reached, an error will be thrown.
183+
* @param bound This is the lower bound that will be enforced on the decrement operation. It will only be enforced if
184+
* EnableBound is set to true. Please read the description above.
185+
* @return
186+
*/
187+
public static BasicResult DecrementIntValue(String token, LicenseKey license, long id, int intValue, boolean enableBound, int bound) {
188+
return DecrementIntValue(token, new DecrementIntValueToKeyModel(license.ProductId, license.Key, id, intValue, enableBound, bound));
189+
}
190+
191+
public static BasicResult DecrementIntValue(String token, DecrementIntValueToKeyModel model) {
192+
193+
Map<String,String> extraParams = new HashMap<>();
194+
195+
extraParams.put("token", token);
196+
197+
return HelperMethods.SendRequestToWebAPI("data/decrementintvaluetokey", model, extraParams, BasicResult.class);
198+
}
199+
200+
/**
201+
* This method will assign a new string value to a Data Object.
202+
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
203+
* @param license The license key object (it's used to get the product id and key string).
204+
* @param id The unique object id for the data object.
205+
* @param stringValue A string value (text) to store. Max 10000 characters.
206+
* @return
207+
*/
208+
public static BasicResult SetStringValue(String token, LicenseKey license, long id, String stringValue) {
209+
return SetStringValue(token, new SetStringValueToKeyModel(license.ProductId, license.Key, id, stringValue));
210+
}
211+
212+
public static BasicResult SetStringValue(String token, SetStringValueToKeyModel model) {
213+
214+
Map<String,String> extraParams = new HashMap<>();
215+
216+
extraParams.put("token", token);
217+
218+
return HelperMethods.SendRequestToWebAPI("data/setstringvaluetokey", model, extraParams, BasicResult.class);
219+
}
220+
221+
/**
222+
* This method will remove an existing Data Object.
223+
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
224+
* @param license The license key object (it's used to get the product id and key string).
225+
* @param id A string value (text) to store. Max 10000 characters.
226+
* @return
227+
*/
228+
public static BasicResult RemoveDataObject(String token, LicenseKey license, long id) {
229+
return RemoveDataObject(token, new RemoveDataObjectToKeyModel(license.ProductId, license.Key, id));
230+
}
231+
232+
public static BasicResult RemoveDataObject(String token, RemoveDataObjectToKeyModel model) {
233+
234+
Map<String,String> extraParams = new HashMap<>();
235+
236+
extraParams.put("token", token);
237+
238+
return HelperMethods.SendRequestToWebAPI("data/removedataobjecttokey", model, extraParams, BasicResult.class);
239+
}
240+
}

src/main/java/io/cryptolens/methods/Helpers.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.cryptolens.methods;
22

3+
import io.cryptolens.internal.BasicResult;
34
import io.cryptolens.models.ActivatedMachine;
45
import io.cryptolens.models.LicenseKey;
56
import oshi.SystemInfo;
@@ -221,6 +222,17 @@ private static String SHA256(String rawData) {
221222
}
222223
}
223224

225+
/**
226+
* Checks if a response from Cryptolens is successful.
227+
* @param result The response from an API call. All responses inherit from BasicResult.
228+
* @return True if the response is successful and false otherwise.
229+
*/
230+
public static boolean IsSuccessful(BasicResult result) {
231+
if(result == null || result.result == 1)
232+
return false;
233+
return true;
234+
}
235+
224236
private static String getRawDeviceID()
225237
{
226238
//thanks to https://stackoverflow.com/a/37705082. may require root.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.cryptolens.models;
2+
3+
public class AddDataObjectToKeyModel extends ProductAndKeyModel {
4+
/**
5+
* The name of the data object. Max 10 characters.
6+
*/
7+
public String Name;
8+
/**
9+
* An int value (int32) to store.
10+
*/
11+
public int IntValue;
12+
/**
13+
* A string value (text) to store. Max 10000 characters.
14+
*/
15+
public String StringValue;
16+
17+
public AddDataObjectToKeyModel(int productId, String key, String name, int intValue, String stringValue) {
18+
Name = name;
19+
StringValue = stringValue;
20+
IntValue = intValue;
21+
this.ProductId = productId;
22+
this.Key = key;
23+
}
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package io.cryptolens.models;
22

3+
import com.google.gson.annotations.SerializedName;
4+
35
public class DataObject {
6+
@SerializedName(value = "id", alternate = {"Id"})
47
public int Id;
8+
@SerializedName(value = "name", alternate = {"Name"})
59
public String Name;
10+
@SerializedName(value = "stringValue", alternate = {"StringValue"})
611
public String StringValue;
12+
@SerializedName(value = "intValue", alternate = {"IntValue"})
713
public int IntValue;
814
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.cryptolens.models;
2+
3+
public class DecrementIntValueToKeyModel extends ProductAndKeyModel {
4+
public long Id;
5+
public int IntValue;
6+
public boolean EnableBound;
7+
public int Bound;
8+
9+
public DecrementIntValueToKeyModel(int productId, String key, long id, int intValue, boolean enableBound, int bound) {
10+
Id = id;
11+
IntValue = intValue;
12+
this.ProductId = productId;
13+
this.Key = key;
14+
this.EnableBound = enableBound;
15+
this.Bound = bound;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.cryptolens.models;
2+
3+
public class IncrementIntValueToKeyModel extends ProductAndKeyModel {
4+
public long Id;
5+
public int IntValue;
6+
public boolean EnableBound;
7+
public int Bound;
8+
9+
public IncrementIntValueToKeyModel(int productId, String key, long id, int intValue, boolean enableBound, int bound) {
10+
Id = id;
11+
this.IntValue = intValue;
12+
this.ProductId = productId;
13+
this.Key = key;
14+
this.EnableBound = enableBound;
15+
this.Bound = bound;
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.cryptolens.models;
2+
3+
public class ListDataObjectsToKeyModel extends ProductAndKeyModel {
4+
public String Contains = "";
5+
6+
public ListDataObjectsToKeyModel(int productId, String key, String contains) {
7+
Contains = contains;
8+
this.ProductId = productId;
9+
this.Key = key;
10+
}
11+
}

0 commit comments

Comments
 (0)