Skip to content

Commit

Permalink
Add support for data objects (#14)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
artemlos committed Mar 1, 2019
1 parent 87b01f2 commit d691ea2
Show file tree
Hide file tree
Showing 15 changed files with 522 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -99,4 +99,5 @@ fabric.properties
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
.idea/caches/build_file_checksums.ser
apikeys.json
18 changes: 15 additions & 3 deletions src/main/java/io/cryptolens/internal/HelperMethods.java
Expand Up @@ -5,16 +5,17 @@
import io.cryptolens.legacy.RequestHandler;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

public class HelperMethods {

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

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

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

return null;
}

// from: https://stackoverflow.com/a/1042827/1275924
private static List<Field> getAllFields(List<Field> fields, Class<?> type) {
fields.addAll(Arrays.asList(type.getDeclaredFields()));

if (type.getSuperclass() != null) {
getAllFields(fields, type.getSuperclass());
}

return fields;
}
}
240 changes: 240 additions & 0 deletions src/main/java/io/cryptolens/methods/Data.java
@@ -0,0 +1,240 @@
package io.cryptolens.methods;

import com.google.gson.Gson;
import io.cryptolens.internal.*;
import io.cryptolens.models.*;

import java.util.HashMap;
import java.util.Map;

/**
* <p>The following methods allow you to work with data objects (aka. metadata or custom variables) associated with a
* license key. Data objects can be used to store specific properties that (eg. username, OS version). More importantly
* 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>.
* </p>
*
* <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>
* and to set the <u>keylock to <b>-1</b></u>.</p>
*/
public class Data {
/**
* Adds a new data object to a license key.
* @param token The access token with 'AddDataObject' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @param name The name of the data object. Max 10 characters.
* @param intValue An int value (int32) to store.
* @param stringValue A string value (text) to store. Max 10000 characters.
* @return
*/
public static BasicResult AddDataObject(String token, LicenseKey license, String name, int intValue, String stringValue) {
return AddDataObject(token, new AddDataObjectToKeyModel(license.ProductId, license.Key, name, intValue, stringValue));
}

public static BasicResult AddDataObject(String token, AddDataObjectToKeyModel model) {

Map<String,String> extraParams = new HashMap<>();

extraParams.put("token", token);

return HelperMethods.SendRequestToWebAPI("data/AddDataObjectToKey", model, extraParams, BasicResult.class);
}

/**
* List data objects of a certain license.
* @param token The access token with 'ListDataObjects' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @return
*/
public static ListOfDataObjectsResult ListDataObjects(String token, LicenseKey license) {
return ListDataObjects(token, new ListDataObjectsToKeyModel(license.ProductId, license.Key, ""));
}

/**
* List data objects of a certain license.
* @param token The access token with 'ListDataObjects' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @param contains Shows only Data Objects where the name contains the following string.
* @return
*/
public static ListOfDataObjectsResult ListDataObjects(String token, LicenseKey license, String contains) {
return ListDataObjects(token, new ListDataObjectsToKeyModel(license.ProductId, license.Key, contains));
}

public static ListOfDataObjectsResult ListDataObjects(String token, ListDataObjectsToKeyModel model) {

Map<String,String> extraParams = new HashMap<>();

extraParams.put("token", token);

return HelperMethods.SendRequestToWebAPI("data/listdataobjectstokey", model, extraParams, ListOfDataObjectsResult.class);
}

/**
* This method will assign a new integer value to a Data Object.
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @param id The unique object id for the data object.
* @param intValue The new int value that should be assigned to the data object.
* @return
*/
public static BasicResult SetIntValue(String token, LicenseKey license, long id, int intValue) {
return SetIntValue(token, new SetIntValueToKeyModel(license.ProductId, license.Key, id, intValue));
}

public static BasicResult SetIntValue(String token, SetIntValueToKeyModel model) {

Map<String,String> extraParams = new HashMap<>();

extraParams.put("token", token);

return HelperMethods.SendRequestToWebAPI("data/setintvaluetokey", model, extraParams, BasicResult.class);
}

/**
* This method will increment the integer value in a Data Object by a certain constant (non-negative).
* You can always decrement it. Note, this method does not allow integer overflows, i.e. if you increment
* by a constant that would result in an overflow, an error will be thrown. Note also that you can use the Feature lock
* in the Access Token to specify the upper bound of the increment constant. So, if you only want to allow incrementing
* by 1, please set Feature lock field to 1 also. Please see Remarks for more details (including access token set up).
* @param token The access token with 'IncrementIntValue' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @param id The unique object id for the data object.
* @param intValue The constant int (non-negative) value that should be added to the current
* IntValue of the data object. For example, if this value is set to 5 and the
* old IntValue is 1, then the new IntValue will be the old one plus 5, i.e. 6.
* Note, if you would set this value to -5 instead, the same result would be achieved.
* @return
*/
public static BasicResult IncrementIntValue(String token, LicenseKey license, long id, int intValue) {
return IncrementIntValue(token, new IncrementIntValueToKeyModel(license.ProductId, license.Key, id, intValue, false, 0));
}

/**
* This method will increment the integer value in a Data Object by a certain constant (non-negative).
* You can always decrement it. Note, this method does not allow integer overflows, i.e. if you increment
* by a constant that would result in an overflow, an error will be thrown. Note also that you can use the Feature lock
* in the Access Token to specify the upper bound of the increment constant. So, if you only want to allow incrementing
* by 1, please set Feature lock field to 1 also. Please see Remarks for more details (including access token set up).
* @param token The access token with 'IncrementIntValue' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @param id The unique object id for the data object.
* @param intValue The constant int (non-negative) value that should be added to the current
* IntValue of the data object. For example, if this value is set to 5 and the
* old IntValue is 1, then the new IntValue will be the old one plus 5, i.e. 6.
* Note, if you would set this value to -5 instead, the same result would be achieved.
* @param enableBound If set to true, it will be possible to specify an upper bound. For example,
* if you set the Bound parameter (below) to 10, you will be able to increment
* the int value until you reach ten (inclusive). Once the upper bound is reached,
* an error will be thrown.
* @param bound This is the upper bound that will be enforced on the increment operation.
* It will only be enforced if EnableBound is set to true. Please read the description about enableBound.
* @return
*/
public static BasicResult IncrementIntValue(String token, LicenseKey license, long id, int intValue, boolean enableBound, int bound) {
return IncrementIntValue(token, new IncrementIntValueToKeyModel(license.ProductId, license.Key, id, intValue, enableBound, bound));
}

public static BasicResult IncrementIntValue(String token, IncrementIntValueToKeyModel model) {

Map<String,String> extraParams = new HashMap<>();

extraParams.put("token", token);

return HelperMethods.SendRequestToWebAPI("data/incrementintvaluetokey", model, extraParams, BasicResult.class);
}

/**
* This method will decrement the integer value in a Data Object by a certain constant (non-negative).
* You can always increment it. Note, this method does not allow integer overflows, i.e. if you decrement
* by a constant that would result in an overflow, an error will be thrown. Note also that you can use the
* Feature lock in the Access Token to specify the upper bound of the decrement constant. So, if you only
* want to allow decrementing by 1, please set Feature lock field to 1 also. Please see Remarks for more
* details (including access token setup).
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @param id The unique object id for the data object.
* @param intValue The constant int value that should be subtracted to the current IntValue of the data object.
* For example, if this value is set to 5 and the old IntValue is 11, then the new IntValue
* will be the old one minus 5, i.e. 6. Note, if you would set this value to -5 instead, the
* same result would be achieved.
* @return
*/
public static BasicResult DecrementIntValue(String token, LicenseKey license, long id, int intValue) {
return DecrementIntValue(token, new DecrementIntValueToKeyModel(license.ProductId, license.Key, id, intValue, false, 0));
}

/**
* This method will decrement the integer value in a Data Object by a certain constant (non-negative).
* You can always increment it. Note, this method does not allow integer overflows, i.e. if you decrement
* by a constant that would result in an overflow, an error will be thrown. Note also that you can use the
* Feature lock in the Access Token to specify the upper bound of the decrement constant. So, if you only
* want to allow decrementing by 1, please set Feature lock field to 1 also. Please see Remarks for more
* details (including access token setup).
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @param id The unique object id for the data object.
* @param intValue The constant int value that should be subtracted to the current IntValue of the data object.
* For example, if this value is set to 5 and the old IntValue is 11, then the new IntValue
* will be the old one minus 5, i.e. 6. Note, if you would set this value to -5 instead, the
* same result would be achieved.
* @param enableBound If set to true, it will be possible to specify a lower bound. For example, if you set the Bound
* parameter (below) to 0, you will be able to decrement the int value until you reach zero (inclusive).
* Once the lower bound is reached, an error will be thrown.
* @param bound This is the lower bound that will be enforced on the decrement operation. It will only be enforced if
* EnableBound is set to true. Please read the description above.
* @return
*/
public static BasicResult DecrementIntValue(String token, LicenseKey license, long id, int intValue, boolean enableBound, int bound) {
return DecrementIntValue(token, new DecrementIntValueToKeyModel(license.ProductId, license.Key, id, intValue, enableBound, bound));
}

public static BasicResult DecrementIntValue(String token, DecrementIntValueToKeyModel model) {

Map<String,String> extraParams = new HashMap<>();

extraParams.put("token", token);

return HelperMethods.SendRequestToWebAPI("data/decrementintvaluetokey", model, extraParams, BasicResult.class);
}

/**
* This method will assign a new string value to a Data Object.
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @param id The unique object id for the data object.
* @param stringValue A string value (text) to store. Max 10000 characters.
* @return
*/
public static BasicResult SetStringValue(String token, LicenseKey license, long id, String stringValue) {
return SetStringValue(token, new SetStringValueToKeyModel(license.ProductId, license.Key, id, stringValue));
}

public static BasicResult SetStringValue(String token, SetStringValueToKeyModel model) {

Map<String,String> extraParams = new HashMap<>();

extraParams.put("token", token);

return HelperMethods.SendRequestToWebAPI("data/setstringvaluetokey", model, extraParams, BasicResult.class);
}

/**
* This method will remove an existing Data Object.
* @param token The access token with 'SetIntValue' permission and KeyLock set to '-1'.
* @param license The license key object (it's used to get the product id and key string).
* @param id A string value (text) to store. Max 10000 characters.
* @return
*/
public static BasicResult RemoveDataObject(String token, LicenseKey license, long id) {
return RemoveDataObject(token, new RemoveDataObjectToKeyModel(license.ProductId, license.Key, id));
}

public static BasicResult RemoveDataObject(String token, RemoveDataObjectToKeyModel model) {

Map<String,String> extraParams = new HashMap<>();

extraParams.put("token", token);

return HelperMethods.SendRequestToWebAPI("data/removedataobjecttokey", model, extraParams, BasicResult.class);
}
}
12 changes: 12 additions & 0 deletions src/main/java/io/cryptolens/methods/Helpers.java
@@ -1,5 +1,6 @@
package io.cryptolens.methods;

import io.cryptolens.internal.BasicResult;
import io.cryptolens.models.ActivatedMachine;
import io.cryptolens.models.LicenseKey;
import oshi.SystemInfo;
Expand Down Expand Up @@ -221,6 +222,17 @@ private static String SHA256(String rawData) {
}
}

/**
* Checks if a response from Cryptolens is successful.
* @param result The response from an API call. All responses inherit from BasicResult.
* @return True if the response is successful and false otherwise.
*/
public static boolean IsSuccessful(BasicResult result) {
if(result == null || result.result == 1)
return false;
return true;
}

private static String getRawDeviceID()
{
//thanks to https://stackoverflow.com/a/37705082. may require root.
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/cryptolens/models/AddDataObjectToKeyModel.java
@@ -0,0 +1,24 @@
package io.cryptolens.models;

public class AddDataObjectToKeyModel extends ProductAndKeyModel {
/**
* The name of the data object. Max 10 characters.
*/
public String Name;
/**
* An int value (int32) to store.
*/
public int IntValue;
/**
* A string value (text) to store. Max 10000 characters.
*/
public String StringValue;

public AddDataObjectToKeyModel(int productId, String key, String name, int intValue, String stringValue) {
Name = name;
StringValue = stringValue;
IntValue = intValue;
this.ProductId = productId;
this.Key = key;
}
}
6 changes: 6 additions & 0 deletions src/main/java/io/cryptolens/models/DataObject.java
@@ -1,8 +1,14 @@
package io.cryptolens.models;

import com.google.gson.annotations.SerializedName;

public class DataObject {
@SerializedName(value = "id", alternate = {"Id"})
public int Id;
@SerializedName(value = "name", alternate = {"Name"})
public String Name;
@SerializedName(value = "stringValue", alternate = {"StringValue"})
public String StringValue;
@SerializedName(value = "intValue", alternate = {"IntValue"})
public int IntValue;
}
@@ -0,0 +1,17 @@
package io.cryptolens.models;

public class DecrementIntValueToKeyModel extends ProductAndKeyModel {
public long Id;
public int IntValue;
public boolean EnableBound;
public int Bound;

public DecrementIntValueToKeyModel(int productId, String key, long id, int intValue, boolean enableBound, int bound) {
Id = id;
IntValue = intValue;
this.ProductId = productId;
this.Key = key;
this.EnableBound = enableBound;
this.Bound = bound;
}
}
@@ -0,0 +1,17 @@
package io.cryptolens.models;

public class IncrementIntValueToKeyModel extends ProductAndKeyModel {
public long Id;
public int IntValue;
public boolean EnableBound;
public int Bound;

public IncrementIntValueToKeyModel(int productId, String key, long id, int intValue, boolean enableBound, int bound) {
Id = id;
this.IntValue = intValue;
this.ProductId = productId;
this.Key = key;
this.EnableBound = enableBound;
this.Bound = bound;
}
}
11 changes: 11 additions & 0 deletions src/main/java/io/cryptolens/models/ListDataObjectsToKeyModel.java
@@ -0,0 +1,11 @@
package io.cryptolens.models;

public class ListDataObjectsToKeyModel extends ProductAndKeyModel {
public String Contains = "";

public ListDataObjectsToKeyModel(int productId, String key, String contains) {
Contains = contains;
this.ProductId = productId;
this.Key = key;
}
}

0 comments on commit d691ea2

Please sign in to comment.