128 lines
3 KiB
Dart
128 lines
3 KiB
Dart
library fever_api;
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
part 'feed.dart';
|
|
part 'item.dart';
|
|
part 'group.dart';
|
|
part 'feeds_group.dart';
|
|
part 'exceptions.dart';
|
|
part 'util.dart';
|
|
|
|
/// Basic implementation of the Fever API
|
|
/// https://github.com/dasmurphy/tinytinyrss-fever-plugin/blob/master/fever-api.md
|
|
|
|
class FeverAPI {
|
|
String apiKey;
|
|
String apiUrl;
|
|
|
|
/// We use a shared HTTP client for all requests
|
|
final _httpClient = http.Client();
|
|
|
|
FeverAPI({ required this.apiKey, required this.apiUrl });
|
|
|
|
static String generateApiKey(String username, String password) {
|
|
var bytes = utf8.encode('$username:$password');
|
|
return md5.convert(bytes).toString();
|
|
}
|
|
|
|
APIRequestBuilder request() {
|
|
return APIRequestBuilder(this, _httpClient);
|
|
}
|
|
}
|
|
|
|
class APIRequestBuilder {
|
|
final FeverAPI api;
|
|
final http.Client _httpClient;
|
|
|
|
List<String> args = [];
|
|
|
|
APIRequestBuilder(this.api, this._httpClient);
|
|
|
|
void _addArg(String arg) {
|
|
if (!args.contains(arg)) args.add(arg);
|
|
}
|
|
|
|
Uri generateUrl() {
|
|
return Uri.parse('${api.apiUrl}?api&${args.join('&')}');
|
|
}
|
|
|
|
/// Include items in response
|
|
APIRequestBuilder withItems() {
|
|
_addArg('items');
|
|
return this;
|
|
}
|
|
|
|
/// Include feeds in response
|
|
APIRequestBuilder withFeeds() {
|
|
_addArg('feeds');
|
|
return this;
|
|
}
|
|
|
|
/// Include groups in response
|
|
APIRequestBuilder withGroups() {
|
|
_addArg('groups');
|
|
return this;
|
|
}
|
|
|
|
/// Executes the API request and returns the JSON data
|
|
Future<Map<String, dynamic>> fetch() async {
|
|
final response = await _httpClient.post(generateUrl(), body: { 'api_key': api.apiKey });
|
|
final data = jsonDecode((response).body);
|
|
if (data['auth'] == 0) throw UnauthenticatedException(data['auth']);
|
|
return data;
|
|
}
|
|
|
|
/// Executes the API request and populates the local cache with the response data
|
|
Future<void> execute() async {
|
|
final data = await fetch();
|
|
|
|
List<Group> groups = [];
|
|
List<Feed> feeds = [];
|
|
List<Item> items = [];
|
|
List<FeedsGroup> feedsGroups = [];
|
|
|
|
if (data.containsKey('groups')) {
|
|
groups = (data['groups'] as List<dynamic>)
|
|
.map((json) => Group.fromJSON(api, json))
|
|
.toList();
|
|
}
|
|
|
|
if (data.containsKey('feeds')) {
|
|
feeds = (data['feeds'] as List<dynamic>)
|
|
.map((json) => Feed.fromJSON(api, json))
|
|
.toList();
|
|
}
|
|
|
|
if (data.containsKey('items')) {
|
|
items = (data['items'] as List<dynamic>)
|
|
.map((json) => Item.fromJSON(api, json))
|
|
.toList();
|
|
}
|
|
|
|
if (data.containsKey('feeds_groups')) {
|
|
feedsGroups = (data['feeds_groups'] as List<dynamic>)
|
|
.map((json) => FeedsGroup.fromJSON(api, json))
|
|
.toList();
|
|
}
|
|
|
|
print(groups);
|
|
print(feeds);
|
|
print(items);
|
|
print(feedsGroups);
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
var api = FeverAPI(apiKey: "", apiUrl: "https://rss.amogus.cloud/api/fever.php");
|
|
|
|
api.request()
|
|
.withFeeds()
|
|
.withGroups()
|
|
.withItems()
|
|
.execute();
|
|
}
|