|
| 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 | +} |
0 commit comments