You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
Dart
59 lines
1.4 KiB
Dart
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<String, dynamic>? search}) {
|
|
String url = restApiEndpoint;
|
|
if (url.endsWith("/")) {
|
|
url = url.substring(0, url.length - 1);
|
|
}
|
|
|
|
return Uri.parse(url + endpoint).replace(queryParameters: search);
|
|
}
|
|
|
|
static Future<Map<String, String>> _getHeaders() async {
|
|
Map<String, String> headers = {};
|
|
|
|
if (!kIsWeb) {
|
|
headers["X-IsekaiWikiApp-Version"] = Global.packageInfo?.version ?? "unknow";
|
|
headers["User-Agent"] = await ApiUtils.getUserAgent();
|
|
}
|
|
|
|
return headers;
|
|
}
|
|
|
|
static Future<String> get(String path, {Map<String, dynamic>? 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<Map> getJson(String path) async {
|
|
var resText = await get(path);
|
|
var resData = jsonDecode(resText);
|
|
|
|
if (resData is Map) {
|
|
return resData;
|
|
} else {
|
|
return {};
|
|
}
|
|
}
|
|
}
|