getUuid function

Future<PlayerUuid> getUuid(
  1. String username, {
  2. DateTime? timestamp,
})

Returns the UUID for player username.

A timestamp can be passed to retrieve the UUID for the player with username at that point in time. Warning: Since November 2020, the timestamp is ignored, see WEB-3367.

Implementation

Future<PlayerUuid> getUuid(String username, {DateTime? timestamp}) async {
  final time =
      timestamp == null ? '' : '?at=${timestamp.millisecondsSinceEpoch}';

  final response = await request(
      http.get, _mojangApi, 'users/profiles/minecraft/$username$time');
  final map = parseResponseMap(response);
  if (map['error'] != null) {
    if (response.statusCode == 404) {
      throw ArgumentError.value(
          username, 'username', 'No user was found for given username');
    } else if (response.statusCode == 400) {
      throw ArgumentError.value(
          timestamp, 'timestamp', 'The timestamp is invalid.');
    } else if (response.statusCode == 429) {
      throw TooManyRequestsException(map['errorMessage']);
    }
    throw Exception(map['errorMessage']);
  }

  return PlayerUuid(username, map['id']);
}