Skip to content

Instantly share code, notes, and snippets.

@kirshiyin89
Created September 29, 2020 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirshiyin89/95bbfa864631f2f099291d45f2e80081 to your computer and use it in GitHub Desktop.
Save kirshiyin89/95bbfa864631f2f099291d45f2e80081 to your computer and use it in GitHub Desktop.
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