更改API的结构
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 {};
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue