import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:voyagehandbook/api/classes.dart';
/*
Voyage Handbook - The open-source WikiVoyage reader
Copyright (C) 2023 Matyáš Caras
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
class WikiApi {
static final _dio = Dio(
BaseOptions(baseUrl: "https://api.wikimedia.org/core/v1/wikivoyage/en/"));
static Future _getRequest(String endpoint) async {
return await _dio.get(
endpoint,
options: Options(
headers: {"User-Agent": "VoyageHandbook/1.0.0"},
validateStatus: (_) => true,
responseType: ResponseType.plain),
);
}
/// Searches for pages using the WikiMedia API
static Future> search(String q, {int limit = 5}) async {
var r = await _getRequest("search/page?q=$q&limit=$limit");
if (r.statusCode! > 399) return Future.error("API error ${r.statusCode}");
var json = jsonDecode(r.data)["pages"];
return List.generate(
json.length, (index) => SearchResponse.fromJson(json[index]));
}
static Future getRawPage(String key) async {
var r = await _getRequest("page/$key/with_html");
if (r.statusCode! > 399) return Future.error("API error ${r.statusCode}");
return RawPage.fromJson(jsonDecode(r.data));
}
}