authenticateClient method

Future<AccessToken> authenticateClient(
  1. {String languageCode = "en-US",
  2. dynamic onSuccess(
    1. AccessToken
    )?,
  3. dynamic onError(
    1. Error
    )?,
  4. bool notify = true}
)

Implementation

Future<AccessToken> authenticateClient(
    {String languageCode = "en-US",
    Function(AccessToken)? onSuccess,
    Function(Error)? onError,
    bool notify = true}) async {
  defaultApiClient = AuthenticatingAPIClient(attemptToRefreshToken,
      basePath: APIConstants.baseUrl);
  updateHeader(defaultApiClient, languageCode: languageCode);
  updateHeader(authAPIClient, languageCode: languageCode);

  AuthenticationApi authenticationApi = AuthenticationApi(authAPIClient);
  var accessTokenRequest = AccessTokenRequest();
  accessTokenRequest.clientId = APIConstants.clientID;
  accessTokenRequest.clientSecret = APIConstants.clientSecret;
  accessTokenRequest.grantType = APIConstants.grantType;
  accessTokenRequest.scope = APIConstants.scope;

  var response = authenticationApi.getAccessToken(accessTokenRequest);
  return response.then((value) async {
    clientAuthenticationError = null;
    clientToken = value;
    if (onSuccess != null) {
      await onSuccess(value);
    }
    if (notify) {
      notifyListeners();
    }
    return value;
  }).catchError((error) async {
    clientAuthenticationError = error;
    clientToken = null;
    if (kDebugMode) {
      print(error);
    }
    if (onError != null) {
      await onError(error);
    }
    if (notify) {
      notifyListeners();
    }
    return AccessToken();
  });
}