2022-11-09 02:26:03 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
2022-11-10 02:02:26 +00:00
|
|
|
import 'package:result_monad/result_monad.dart';
|
|
|
|
|
2022-11-09 02:26:03 +00:00
|
|
|
import 'models/credentials.dart';
|
|
|
|
import 'models/exec_error.dart';
|
|
|
|
import 'models/timeline_entry.dart';
|
|
|
|
import 'serializers/mastodon/timeline_entry_mastodon_extensions.dart';
|
|
|
|
|
|
|
|
class FriendicaClient {
|
|
|
|
final Credentials _credentials;
|
|
|
|
late final String _authHeader;
|
|
|
|
|
|
|
|
String get serverName => _credentials.serverName;
|
|
|
|
|
2022-11-10 02:02:26 +00:00
|
|
|
Credentials get credentials => _credentials;
|
|
|
|
|
2022-11-09 02:26:03 +00:00
|
|
|
FriendicaClient({required Credentials credentials})
|
|
|
|
: _credentials = credentials {
|
|
|
|
final authenticationString =
|
|
|
|
"${_credentials.username}:${_credentials.password}";
|
|
|
|
final encodedAuthString = base64Encode(utf8.encode(authenticationString));
|
|
|
|
_authHeader = "Basic $encodedAuthString";
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<List<TimelineEntry>, ExecError> getUserTimeline(
|
|
|
|
{String userId = '', int page = 1, int count = 10}) async {
|
|
|
|
final baseUrl = 'https://$serverName/api/statuses/user_timeline?';
|
|
|
|
final pagingData = 'count=$count&page=$page';
|
|
|
|
final url = userId.isEmpty
|
|
|
|
? '$baseUrl$pagingData'
|
|
|
|
: '${baseUrl}screen_name=$userId$pagingData';
|
|
|
|
final request = Uri.parse(url);
|
|
|
|
return (await _getApiListRequest(request).andThenSuccessAsync(
|
|
|
|
(postsJson) async => postsJson
|
|
|
|
.map((json) => TimelineEntryMastodonExtensions.fromJson(json))
|
|
|
|
.toList()))
|
|
|
|
.mapError((error) => error as ExecError);
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<List<TimelineEntry>, ExecError> getHomeTimeline(
|
|
|
|
{int page = 1, int count = 10}) async {
|
|
|
|
//
|
|
|
|
//final baseUrl = 'https://$serverName/api/statuses/home_timeline';
|
|
|
|
final baseUrl = 'https://$serverName/api/v1/timelines/public?limit=2';
|
|
|
|
final pagingData = '';
|
|
|
|
// final pagingData = 'count=$count&page=$page';
|
|
|
|
final url = '$baseUrl$pagingData';
|
|
|
|
final request = Uri.parse(url);
|
|
|
|
return (await _getApiListRequest(request).andThenSuccessAsync(
|
|
|
|
(postsJson) async => postsJson
|
|
|
|
.map((json) => TimelineEntryMastodonExtensions.fromJson(json))
|
|
|
|
.toList()))
|
|
|
|
.mapError((error) => error as ExecError);
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<String, ExecError> getMyProfile() async {
|
|
|
|
final request = Uri.parse('https://$serverName/api/friendica/profile/show');
|
|
|
|
return (await _getApiRequest(request))
|
|
|
|
.mapValue((value) => value.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<HttpClientResponse, ExecError> getUrl(Uri url) async {
|
|
|
|
try {
|
|
|
|
final request = await HttpClient().getUrl(url);
|
|
|
|
request.headers.add('authorization', _authHeader);
|
|
|
|
final response = await request.close();
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
return Result.error(ExecError(
|
|
|
|
type: ErrorType.authentication,
|
|
|
|
message: '${response.statusCode}: ${response.reasonPhrase}'));
|
|
|
|
}
|
|
|
|
return Result.ok(response);
|
|
|
|
} catch (e) {
|
|
|
|
return Result.error(
|
|
|
|
ExecError(type: ErrorType.localError, message: e.toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<List<dynamic>, ExecError> _getApiListRequest(Uri url) async {
|
|
|
|
return (await getUrl(url)
|
|
|
|
.andThenSuccessAsync((response) async =>
|
|
|
|
await response.transform(utf8.decoder).join(''))
|
|
|
|
.andThenSuccessAsync(
|
|
|
|
(jsonText) async => jsonDecode(jsonText) as List<dynamic>))
|
|
|
|
.mapError((error) => error as ExecError);
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<dynamic, ExecError> _getApiRequest(Uri url) async {
|
|
|
|
return (await getUrl(url)
|
|
|
|
.andThenSuccessAsync((response) async =>
|
|
|
|
await response.transform(utf8.decoder).join(''))
|
|
|
|
.andThenSuccessAsync((jsonText) async => jsonDecode(jsonText)))
|
|
|
|
.mapError((error) => error as ExecError);
|
|
|
|
}
|
|
|
|
}
|