Created
August 20, 2016 21:56
-
-
Save azenla/ef35a1c4786247da312f9580182ff523 to your computer and use it in GitHub Desktop.
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:async"; | |
import "dart:convert"; | |
import "dart:io"; | |
class WhatPulse { | |
final String host; | |
final int port; | |
HttpClient _client; | |
WhatPulse({this.host: "127.0.0.1", this.port: 3490}) { | |
_client = new HttpClient(); | |
} | |
Future<Map<String, dynamic>> _fetch(String path) async { | |
var uri = Uri.parse("http://${host}:${port}${path}"); | |
var request = await _client.getUrl(uri); | |
var response = await request.close(); | |
var bytes = await response.fold(<int>[], (List<int> a, List<int> b) { | |
return a..addAll(b); | |
}); | |
return JSON.decode(UTF8.decode(bytes)); | |
} | |
Future<PulseStats> getAccountStats() async { | |
return new PulseStats.decode(await _fetch("/v1/account-totals")); | |
} | |
void close() { | |
_client.close(force: true); | |
} | |
} | |
class PulseStats { | |
final int clicks; | |
final int download; | |
final int upload; | |
final int uptime; | |
final int keys; | |
final PulseRanks ranks; | |
PulseStats({ | |
this.clicks, | |
this.download, | |
this.upload, | |
this.keys, | |
this.ranks, | |
this.uptime | |
}); | |
factory PulseStats.decode(Map<String, dynamic> m) { | |
return new PulseStats( | |
clicks: int.parse(m["clicks"].toString()), | |
download: int.parse(m["download"].toString()), | |
upload: int.parse(m["upload"].toString()), | |
uptime: int.parse(m["uptime"].toString()), | |
keys: int.parse(m["keys"].toString()), | |
ranks: new PulseRanks.decode(m["ranks"]) | |
); | |
} | |
} | |
class PulseRanks { | |
final int clicks; | |
final int download; | |
final int keys; | |
final int upload; | |
final int uptime; | |
PulseRanks({ | |
this.clicks, | |
this.download, | |
this.keys, | |
this.upload, | |
this.uptime | |
}); | |
factory PulseRanks.decode(Map<String, dynamic> m) { | |
return new PulseRanks( | |
clicks: int.parse(m["rank_clicks"].toString()), | |
download: int.parse(m["rank_download"].toString()), | |
keys: int.parse(m["rank_keys"].toString()), | |
upload: int.parse(m["rank_upload"].toString()), | |
uptime: int.parse(m["rank_uptime"].toString()) | |
); | |
} | |
} | |
main() async { | |
var pulse = new WhatPulse(); | |
var stats = await pulse.getAccountStats(); | |
print("${stats.keys} keys have been hit."); | |
pulse.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment