import 'dart:convert'; import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; import 'package:isekai_wiki/global.dart'; import 'package:isekai_wiki/utils/api_utils.dart'; import '../mw/mw_api.dart'; const restBaseUrl = "https://www.isekai.cn/api/rest_v1"; class RestbaseApi { static String restApiEndpoint = restBaseUrl; static Uri getUri(String endpoint, {Map? search}) { String url = restApiEndpoint; if (url.endsWith("/")) { url = url.substring(0, url.length - 1); } return Uri.parse(url + endpoint).replace(queryParameters: search); } static Future> _getHeaders() async { Map headers = {}; if (!kIsWeb) { headers["X-IsekaiWikiApp-Version"] = Global.packageInfo?.version ?? "unknow"; headers["User-Agent"] = await ApiUtils.getUserAgent(); } return headers; } static Future get(String path, {Map? search}) async { var uri = getUri(path, search: search); var res = await http.get(uri, headers: await _getHeaders()); if (res.statusCode != 200) { throw HttpResponseCodeError(res.statusCode); } return res.body; } static Future getJson(String path) async { var resText = await get(path); var resData = jsonDecode(resText); if (resData is Map) { return resData; } else { return {}; } } }