get method

Implementation

Future<HttpDetails> get() async {
  if (this.username != null &&
      this.password != null &&
      this.baseUrl != null) {
    return this;
  }

  final User? user = await UserQuery(database: database).getOne();
  this.username = user?.username;
  this.password = user?.password;
  this.baseUrl = user?.baseUrl;
  this.token = user?.token;
  this.tokenType = user?.tokenType;

//!TODO: Improve token refresh implementation not to use the SDK core
//!TODO!: Improve token refresh implementation not to use the SDK core
  if (this.token != null &&
      (this.tokenType == 'bearer' || this.tokenType == 'ApiToken')) {
    final OAuthToken token = OAuthToken(
      token: this.token as String,
      expiresIn: user?.tokenExpiry as int,
      refreshToken: user?.refreshToken as String,
      url: user?.baseUrl as String,
      tokenExpiresAt: DateTime.parse(user?.tokenExpiresAt as String),
    );
    if (token.isTokenExpired() && user != null) {
      dynamic tokenObject = await token.renew();
      if (tokenObject != null && tokenObject['access_token'] != null) {
        dynamic userObject = user.toJson();
        AuthToken authToken = AuthToken.fromJson(tokenObject);
        userObject['token'] = authToken.accessToken;
        userObject['tokenType'] = authToken.tokenType;
        userObject['tokenExpiry'] = token.expiresIn;
        userObject['refreshToken'] = token.refreshToken;
        userObject['isLoggedIn'] = true;
        userObject['dirty'] = true;
        userObject['authType'] = "token";
        userObject['tokenExpiresAt'] = DateTime.now()
            .add(Duration(seconds: token.expiresIn))
            .toIso8601String();
        User userData = User.fromApi(userObject);
        await UserQuery(database: database).setData(userData).save();
        this.token = authToken.accessToken;
      }
    }
  }
  return Future.value(this);
}