更改API的结构

main
落雨楓 2 years ago
parent 4ae5685dde
commit e4c942862e

@ -0,0 +1,51 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:isekai_wiki/global.dart';
import 'package:isekai_wiki/utils/api_utils.dart';
import 'package:http/http.dart' as http;
class HttpResponseCodeError extends Error {
int? statusCode;
HttpResponseCodeError(this.statusCode);
@override
String toString() {
return "Http error: $statusCode";
}
}
class BaseApi {
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(Uri uri, {Map<String, dynamic>? search}) async {
var res = await http.get(uri, headers: await _getHeaders());
if (res.statusCode != 200) {
throw HttpResponseCodeError(res.statusCode);
}
return res.body;
}
static Future<Map> getJson(Uri uri) async {
var resText = await get(uri);
var resData = jsonDecode(resText);
if (resData is Map) {
return resData;
} else {
return {};
}
}
}

@ -2,24 +2,12 @@ import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:isekai_wiki/api/base_api.dart';
import 'package:isekai_wiki/global.dart';
import '../../utils/api_utils.dart';
const apiBaseUrl = 'https://www.isekai.cn/api.php';
class HttpResponseCodeError extends Error {
int? statusCode;
HttpResponseCodeError(this.statusCode);
@override
String toString() {
return "Http error: $statusCode";
}
}
class MWError {
String? code;
String? info;
@ -77,7 +65,7 @@ class MWApiClient extends http.BaseClient {
}
class MWApi {
static Uri apiBaseUri = Uri.parse(apiBaseUrl);
static Uri apiBaseUri = Uri.parse(Global.wikiApiUrl);
static HttpClient getHttpClient() {
return HttpClient();

@ -1,20 +1,9 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:isekai_wiki/api/base_api.dart';
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;
String url = Global.restfulApiUrl;
if (url.endsWith("/")) {
url = url.substring(0, url.length - 1);
}
@ -22,37 +11,13 @@ class RestbaseApi {
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;
return BaseApi.get(uri);
}
static Future<Map> getJson(String path) async {
var resText = await get(path);
var resData = jsonDecode(resText);
if (resData is Map) {
return resData;
} else {
return {};
}
static Future<Map> getJson(String path, {Map<String, dynamic>? search}) async {
var uri = getUri(path, search: search);
return BaseApi.getJson(uri);
}
}

@ -5,13 +5,15 @@ typedef VoidFutureCallback = Future<void> Function();
typedef BoolFutureCallback = Future<bool> Function();
class Global {
static const String siteConfigUrl = "https://www.isekai.cn/app/config.json";
static const String wikiApiUrl = "https://www.isekai.cn/api.php";
static String wikiHomeUrl = "https://www.isekai.cn/";
static String restfulApiUrl = "https://www.isekai.cn/api/rest_v1";
static String pageUrl = "https://www.isekai.cn/index.php?title={{title}}";
static String pageUrl = "https://www.isekai.cn/{{title}}";
static const String renderThemeFallback = "vector";

@ -29,7 +29,11 @@ class SiteConfig {
class AppSettingsController extends GetxController {
bool _ignoreSave = false;
var betaPageRender = false.obs;
bool isInit = false;
List<String> moduleStyles = [];
List<String> moduleScripts = [];
String renderTheme = Global.renderThemeFallback;
@override
void onInit() {
@ -37,26 +41,27 @@ class AppSettingsController extends GetxController {
loadFromStorage();
everAll([betaPageRender], (_) {
saveToStorage();
});
if (isInit) {
// APP
loadFromRemote();
}
}
///
Future<void> loadFromStorage() async {
try {
final prefs = await SharedPreferences.getInstance();
var settingsJson = prefs.getString("siteConfigCache");
if (settingsJson == null) return;
var siteConfigJson = prefs.getString("siteConfigCache");
if (siteConfigJson == null) return;
var settingsObject = jsonDecode(settingsJson);
if (settingsObject == null) return;
var siteConfigObject = jsonDecode(siteConfigJson);
if (siteConfigObject == null) return;
var settingsData = SiteConfig.fromJson(settingsObject);
var siteConfigData = SiteConfig.fromJson(siteConfigObject);
_ignoreSave = true;
// betaPageRender.value = settingsData.betaPageRender ?? betaPageRender.value;
_ignoreSave = false;
moduleScripts = siteConfigData.moduleScripts;
moduleStyles = siteConfigData.moduleStyles;
renderTheme = siteConfigData.renderTheme;
} catch (ex) {
if (kDebugMode) {
print(ex);
@ -66,16 +71,22 @@ class AppSettingsController extends GetxController {
}
}
///
Future<void> loadFromRemote() async {}
///
void saveToStorage() {
if (_ignoreSave) return;
final prefs = Global.sharedPreferences!;
var settingsData = SiteConfig();
var siteConfigData = SiteConfig(
moduleScripts: moduleScripts,
moduleStyles: moduleStyles,
renderTheme: renderTheme,
);
var settingsJson = jsonEncode(settingsData.toJson());
var siteConfigJson = jsonEncode(siteConfigData.toJson());
prefs.setString("siteConfigCache", settingsJson);
prefs.setString("siteConfigCache", siteConfigJson);
}
}

Loading…
Cancel
Save