getToken method
Get the user's token from their pindo account.
Implementation
Future<String> getToken({
required String username,
required String password,
}) async {
final uri = Uri.https(authority, '/users/token');
final auth = 'Basic ${base64Encode(utf8.encode("$username:$password"))}';
_dio.options.headers['authorization'] = auth;
late Response<Map<String, dynamic>> res;
Map data;
try {
res = await _dio.getUri(uri);
data = res.data as Map<String, dynamic>;
} on DioError catch (e, s) {
throw PindoError(
message: (e.response?.data as Map)['message'],
statusCode: (e.response?.data as Map)['status'] ?? res.statusCode,
type: e.type.valueToString,
stackTrace: s,
);
} on TypeError {
throw PindoCastingError();
} on Exception {
// If the exception is none of the two above, just rethrow it
rethrow;
}
if (data.containsKey('token')) {
return data['token'];
}
throw PindoUnexpectedResponseError(expected: 'token', received: data);
}