setAccessToken method
Implementation
Future<void> setAccessToken() async {
try {
/// This method ensures that the token is in place before any request is
/// placed.
/// When called, it first checks if the previous token exists, if so, is it valid?
/// if still valid(by expiry time measure), terminates to indicate that
/// the token is set and ready for usage.
DateTime now = DateTime.now();
if (mAccessExpiresAt != null) {
if (now.isBefore(mAccessExpiresAt!)) {
return;
}
}
// todo: handle exceptions
HttpClient client = HttpClient();
HttpClientRequest req = await client.getUrl(getAuthUrl());
req.headers.add("Accept", "application/json");
req.headers.add("Authorization", "Basic $b64keySecret");
HttpClientResponse res = await req.close();
// u should use `await res.drain()` if u aren't reading the body
await res.transform(utf8.decoder).forEach((bodyString) {
dynamic jsondecodeBody = jsonDecode(bodyString);
mAccessToken = jsondecodeBody["access_token"].toString();
mAccessExpiresAt = now.add(Duration(
seconds: int.parse(jsondecodeBody["expires_in"].toString())));
});
} catch (e) {
logger.e(e);
}
}