parseAuthStr function
- dynamic auth
Returns parsed header Map based on auth
, optionally path to file if auth
was a path to a file.
auth
user-provided auth String or Map.
Implementation
Future<Tuple2<Map<String, String>, String?>> parseAuthStr(dynamic auth) async {
String? authPath;
Map<String, String> headers;
if (auth is String) {
if (auth.startsWith('{')) {
final inputJson = jsonDecode(auth) as JsonMap;
headers = inputJson.map((k, v) => MapEntry(k, v.toString()));
} else if (File(auth).existsSync()) {
authPath = auth;
final inputJson = jsonDecode(File(auth).readAsStringSync()) as JsonMap;
headers = inputJson.map((k, v) => MapEntry(k, v.toString()));
} else {
throw YTMusicUserError('Invalid auth JSON string or file path provided.');
}
} else if (auth is JsonMap) {
headers = auth.map((k, v) => MapEntry(k, v.toString()));
} else {
throw YTMusicUserError('Invalid auth type provided.');
}
return Tuple2(headers, authPath);
}