getCurrentUserProfile method

Future<CarpUser> getCurrentUserProfile()

Asynchronously gets the CARP profile of the current user.

Implementation

Future<CarpUser> getCurrentUserProfile() async {
  if (!currentUser!.isAuthenticated) {
    throw CarpServiceException(message: 'No user is authenticated.');
  }

  http.Response response = await httpr
      .get(Uri.encodeFull(currentUserEndpointUri), headers: headers);
  int httpStatusCode = response.statusCode;
  Map<String, dynamic> responseJson =
      json.decode(response.body) as Map<String, dynamic>;

  if (httpStatusCode == HttpStatus.ok) {
    return _currentUser!
      ..id = responseJson['id'] as int
      // CAWS uses email as username
      ..username = responseJson['email'].toString()
      ..email = responseJson['email'].toString()
      ..accountId = responseJson['accountId'].toString()
      ..isActivated = responseJson['isActivated'] as bool?
      ..firstName = responseJson['firstName'].toString()
      ..lastName = responseJson['lastName'].toString();
  }

  // All other cases are treated as an error.
  throw CarpServiceException(
    httpStatus: HTTPStatus(httpStatusCode, response.reasonPhrase),
    message: responseJson["error_description"].toString(),
  );
}