Add cloud variable api

This commit is contained in:
Nutcake 2023-06-03 15:31:15 +02:00
parent 4556d08a07
commit fd5fa92be2
2 changed files with 60 additions and 0 deletions

View file

@ -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<CloudVariable> 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<CloudVariable> readGlobalCloudVariable(ApiClient client, {required String path}) async =>
await readCloudVariable(client, ownerId: "", path: path);
static Future<void> deleteCloudVariable(ApiClient client, {required String ownerId, required String path}) async {
final response = await client.delete("/users/vars/$path");
client.checkResponse(response);
}
static Future<void> 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);
}
}

View file

@ -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"],
);
}
}