39 lines
1.1 KiB
Dart
39 lines
1.1 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:birzha/core/orm/orm.dart';
|
|
|
|
const kApiPath = 'api/v1';
|
|
|
|
Uri baseUrl({required String path, Map<String, dynamic>? queryParameters, String? query}) => Uri(
|
|
scheme: 'https',
|
|
host: 'tmex.gov.tm',
|
|
path: '/$path',
|
|
queryParameters: queryParameters == null ? null : {...queryParameters},
|
|
query: query,
|
|
);
|
|
|
|
class FutureGetList<T extends Orm> {
|
|
late final Uri api;
|
|
late final T Function(Map<String, dynamic>)? constructor;
|
|
late final List<T> Function(http.Response)? parser;
|
|
|
|
List<T> defaultParser(http.Response response) {
|
|
String output = response.body;
|
|
List list = jsonDecode(output);
|
|
return list.map<T>((entity) => constructor!(entity)).toList();
|
|
}
|
|
|
|
Future<List<T>> fetch([Map<String, String>? headers]) async {
|
|
final response = await http.get(api, headers: {...?headers});
|
|
if (constructor != null) {
|
|
return defaultParser(response);
|
|
} else if (parser != null) {
|
|
return parser!(response);
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
FutureGetList(this.api, {this.constructor, this.parser});
|
|
}
|