Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
the etcd service
import 'dart:convert';
Future<List<ConfigData>> fetchServerInfo(String prefix) async {
var key = utf8.encode(prefix);
var keyEnd = utf8.encode("${prefix}p");
String _body =
"{\"key\": \"${base64.encode(key)}\", \"range_end\":\"${base64.encode(keyEnd)}\"}";
final response =
await http.post("http://localhost:2379/v3/kv/range", body: _body);
if (response.statusCode == 200) {
var responseJson = json.decode(response.body);
if (responseJson["count"] != null) {
return (responseJson["kvs"] as List)
.map((p) => ConfigData.fromJson(prefix, p))
.toList();
} else {
return new Future.value(List<ConfigData>());
}
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load server data');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment