mirror of
https://gitlab.com/mysocialportal/relatica
synced 2024-10-18 13:33:32 +00:00
Add stubbed out FriendicaClient
This commit is contained in:
parent
4c180f7de8
commit
a9db15887d
1 changed files with 95 additions and 0 deletions
95
lib/friendica_client.dart
Normal file
95
lib/friendica_client.dart
Normal file
|
@ -0,0 +1,95 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'models/credentials.dart';
|
||||
import 'models/exec_error.dart';
|
||||
import 'models/timeline_entry.dart';
|
||||
import 'package:result_monad/result_monad.dart';
|
||||
|
||||
import 'serializers/mastodon/timeline_entry_mastodon_extensions.dart';
|
||||
|
||||
class FriendicaClient {
|
||||
final Credentials _credentials;
|
||||
late final String _authHeader;
|
||||
|
||||
String get serverName => _credentials.serverName;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue