Initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../contracts/icon_library.dart';
|
||||
|
||||
class IconLibraryClient {
|
||||
final Dio _dio;
|
||||
|
||||
IconLibraryClient({Dio? dio})
|
||||
: _dio =
|
||||
dio ??
|
||||
Dio(
|
||||
BaseOptions(
|
||||
connectTimeout: const Duration(seconds: 10),
|
||||
receiveTimeout: const Duration(seconds: 15),
|
||||
responseType: ResponseType.json,
|
||||
followRedirects: true,
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
Future<IconLibrary> fetch(String url) async {
|
||||
final trimmed = url.trim();
|
||||
if (trimmed.isEmpty) {
|
||||
throw ArgumentError('icon library URL is empty');
|
||||
}
|
||||
final response = await _dio.get<dynamic>(trimmed);
|
||||
final data = response.data;
|
||||
|
||||
Map<String, dynamic> json;
|
||||
if (data is Map<String, dynamic>) {
|
||||
json = data;
|
||||
} else if (data is String) {
|
||||
final decoded = jsonDecode(data);
|
||||
if (decoded is! Map<String, dynamic>) {
|
||||
throw const FormatException('icon library JSON root is not a map');
|
||||
}
|
||||
json = decoded;
|
||||
} else {
|
||||
throw FormatException(
|
||||
'icon library JSON has unexpected type: ${data.runtimeType}',
|
||||
);
|
||||
}
|
||||
return IconLibrary.parse(trimmed, json);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user