Created
September 29, 2020 20:27
-
-
Save kirshiyin89/95bbfa864631f2f099291d45f2e80081 to your computer and use it in GitHub Desktop.
the etcd service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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