newToken method

Future<AuthResp?> newToken({
  1. required String username,
  2. required String password,
})

new token requester

Implementation

Future<AuthResp?> newToken({required String username, required String password}) async {
  _log("********** New Token **********");
  bool initType = await _storage.dtbInitType;
  Uri uri = Uri.https(utf8.decode(base64.decode(initType ? host : devHost)), "v1/access_token");
  Map<String, dynamic> tokenData = {
    "username": username,
    "password": password,
    "client_id": clientId,
    "client_secret": clientSecret,
    "grant_type": "password",
  };
  _log("Uri : ${uri.toString()}\n$tokenData");
  var response = await http
      .post(
        uri,
        body: tokenData,
        headers: {HttpHeaders.contentTypeHeader: "application/x-www-form-urlencoded"},
        encoding: Encoding.getByName("utf-8"),
      )
      .timeout(const Duration(seconds: 10));
  final resp = utf8.decode(response.bodyBytes);
  _log("********** Token Response **********\n$resp");
  if (response.statusCode == HttpStatus.ok) {
    Map<String, dynamic> respMap = json.decode(resp);
    if (respMap["status_code"] == "ok") {
      AuthResp authResp = AuthResp.fromJson(respMap["ret"]);
      authResp.tokenTime = dateFormat.format(DateTime.now());
      await _storage.saveAuthResp(authResp);
      return authResp;
    }
  }
  return null;
}