diff --git a/lib/apis/cloud_variable_api.dart b/lib/apis/cloud_variable_api.dart new file mode 100644 index 0000000..d5f6cb7 --- /dev/null +++ b/lib/apis/cloud_variable_api.dart @@ -0,0 +1,27 @@ +import 'dart:convert'; + +import 'package:contacts_plus_plus/clients/api_client.dart'; +import 'package:contacts_plus_plus/models/cloud_variable.dart'; + +class CloudVariableApi { + static Future readCloudVariable(ApiClient client, + {required String ownerId, required String path,}) async { + final response = await client.get("/${ownerId.isEmpty ? "globalvars" : "users/$ownerId/vars"}/$path"); + client.checkResponse(response); + final body = jsonDecode(response.body); + return CloudVariable.fromMap(body); + } + + static Future readGlobalCloudVariable(ApiClient client, {required String path}) async => + await readCloudVariable(client, ownerId: "", path: path); + + static Future deleteCloudVariable(ApiClient client, {required String ownerId, required String path}) async { + final response = await client.delete("/users/vars/$path"); + client.checkResponse(response); + } + + static Future writeCloudVariable(ApiClient client, {required String ownerId, required String path, required String value}) async { + final response = await client.put("/users/$ownerId/vars/$path", body: value); + client.checkResponse(response); + } +} diff --git a/lib/models/cloud_variable.dart b/lib/models/cloud_variable.dart new file mode 100644 index 0000000..39c8901 --- /dev/null +++ b/lib/models/cloud_variable.dart @@ -0,0 +1,33 @@ +import 'package:contacts_plus_plus/auxiliary.dart'; + +class CloudVariable { + final String ownerId; + final String path; + final String value; + final String partitionKey; + final String rowKey; + final DateTime timestamp; + final String eTag; + + const CloudVariable({ + required this.ownerId, + required this.path, + required this.value, + required this.partitionKey, + required this.rowKey, + required this.timestamp, + required this.eTag, + }); + + factory CloudVariable.fromMap(Map map) { + return CloudVariable( + ownerId: map["ownerId"], + path: map["path"], + value: map["value"], + partitionKey: map["partitionKey"], + rowKey: map["rowKey"], + timestamp: DateTime.tryParse(map["timestamp"]) ?? DateTimeX.epoch, + eTag: map["eTag"], + ); + } +}