@@ -127,6 +127,33 @@ class BaseCloudStorage {
127127 !root[" result" ][" empty" ]
128128 );
129129 }
130+
131+ // Increment
132+ cloud_storage_utils::ResultWrapper<int > inc (String key, int value = 1 ) {
133+ return atomic<int >(key, " inc" , value);
134+ }
135+
136+ // Decrement
137+ cloud_storage_utils::ResultWrapper<int > dec (String key, int value = 1 ) {
138+ return atomic<int >(key, " dec" , value);
139+ }
140+
141+ // Replace an item if it is a new minimum
142+ template <class Ty >
143+ cloud_storage_utils::ResultWrapper<Ty> put_min (String key, Ty value) {
144+ return atomic<Ty>(key, " min" , value);
145+ }
146+
147+ // Replace an item if it is a new maximum
148+ template <class Ty >
149+ cloud_storage_utils::ResultWrapper<Ty> put_max (String key, Ty value) {
150+ return atomic<Ty>(key, " max" , value);
151+ }
152+
153+ // Replace an item if it is a new maximum
154+ cloud_storage_utils::ResultWrapper<String> datetime (String key) {
155+ return atomic<String>(key, " date" , " " );
156+ }
130157
131158private:
132159 String _username, _password;
@@ -197,6 +224,24 @@ class BaseCloudStorage {
197224 return jsonString;
198225 }
199226
227+ // Utility method for constructing generic *Atomic* request json string
228+ template <class Ty >
229+ String buildAtomicRequestJson (String key, String action, Ty value) {
230+ // Compose request json object
231+ StaticJsonBuffer<200 > jsonBuffer;
232+ JsonObject& root = jsonBuffer.createObject ();
233+ root[" username" ] = _username;
234+ root[" password" ] = _password;
235+ root[" key" ] = key;
236+ root[" action" ] = action;
237+ root[" value" ] = value;
238+
239+ // Return string form of the object
240+ String jsonString;
241+ root.printTo (jsonString);
242+ return jsonString;
243+ }
244+
200245 // Method for accessing nested json objects with '.' seperated keys.
201246 // for examples "name.first.english" for accessing name:{.., first:{ english: "MyName", .... }}
202247 template <class Type >
@@ -211,7 +256,34 @@ class BaseCloudStorage {
211256
212257 // traverse the parentKey object
213258 return getValueByKey<Type>(root[parentKey], remainder);
214- }
259+ }
260+
261+ // Generic Atomic Request
262+ template <class Ty >
263+ cloud_storage_utils::ResultWrapper<Ty> atomic (String key, String action, Ty value) {
264+ // Build request json
265+ String jsonString = buildAtomicRequestJson<Ty>(key, action, value);
266+
267+ // Construct http request
268+ RequestType request (
269+ _baseServerUrl + " /data/object/atomic" ,
270+ http::Method::GET,
271+ jsonString
272+ );
273+ request.addHeader (" Content-Type" , " application/json; charset=utf-8" );
274+
275+ // Execute request
276+ http::Response response = request.execute ();
277+ if (response.statusCode != 200 ) return false ;
278+
279+ // Parse response body and extract the wanted value
280+ StaticJsonBuffer<300 > jsonBuffer;
281+ JsonObject& root = jsonBuffer.parseObject (response.body );
282+ return cloud_storage_utils::ResultWrapper<Ty>(
283+ !root[" error" ],
284+ getValueByKey<Ty>(root[" result" ], key)
285+ );
286+ }
215287};
216288
217289typedef BaseCloudStorage<http::GenericEspRequest> CloudStorage;
0 commit comments