2024-05-18 10:17:16 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-08-21 07:39:29 +00:00
|
|
|
import 'package:solian/exceptions/request.dart';
|
2024-08-21 07:25:50 +00:00
|
|
|
import 'package:solian/exceptions/unauthorized.dart';
|
2024-07-09 14:39:44 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/services.dart';
|
|
|
|
|
2024-07-23 10:09:41 +00:00
|
|
|
class PostProvider extends GetConnect {
|
2024-05-18 10:17:16 +00:00
|
|
|
@override
|
|
|
|
void onInit() {
|
2024-07-16 11:46:53 +00:00
|
|
|
httpClient.baseUrl = ServiceFinder.buildUrl('interactive', null);
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 10:09:41 +00:00
|
|
|
Future<Response> listRecommendations(int page,
|
2024-08-18 16:14:09 +00:00
|
|
|
{String? realm, String? channel}) async {
|
2024-08-18 16:33:03 +00:00
|
|
|
GetConnect client;
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-05-29 14:42:11 +00:00
|
|
|
final queries = [
|
|
|
|
'take=${10}',
|
|
|
|
'offset=$page',
|
2024-08-18 16:14:09 +00:00
|
|
|
if (realm != null) 'realm=$realm',
|
2024-05-29 14:42:11 +00:00
|
|
|
];
|
2024-08-18 16:33:03 +00:00
|
|
|
if (auth.isAuthorized.value) {
|
|
|
|
client = auth.configureClient('co');
|
|
|
|
} else {
|
|
|
|
client = ServiceFinder.configureClient('co');
|
|
|
|
}
|
|
|
|
final resp = await client.get(
|
2024-07-24 18:00:29 +00:00
|
|
|
channel == null
|
|
|
|
? '/recommendations?${queries.join('&')}'
|
|
|
|
: '/recommendations/$channel?${queries.join('&')}',
|
|
|
|
);
|
2024-05-25 05:19:16 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-05-25 05:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2024-07-09 14:39:44 +00:00
|
|
|
Future<Response> listDraft(int page) async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-08-21 07:39:29 +00:00
|
|
|
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
|
2024-07-09 14:39:44 +00:00
|
|
|
|
|
|
|
final queries = [
|
|
|
|
'take=${10}',
|
|
|
|
'offset=$page',
|
|
|
|
];
|
|
|
|
final client = auth.configureClient('interactive');
|
2024-07-24 17:43:50 +00:00
|
|
|
final resp = await client.get('/posts/drafts?${queries.join('&')}');
|
2024-07-09 14:39:44 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-07-09 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2024-07-25 17:31:45 +00:00
|
|
|
Future<Response> listPost(int page,
|
2024-08-18 16:14:09 +00:00
|
|
|
{String? realm, String? author, tag, category}) async {
|
2024-07-07 03:46:48 +00:00
|
|
|
final queries = [
|
|
|
|
'take=${10}',
|
|
|
|
'offset=$page',
|
2024-07-25 17:31:45 +00:00
|
|
|
if (tag != null) 'tag=$tag',
|
|
|
|
if (category != null) 'category=$category',
|
2024-07-26 08:53:05 +00:00
|
|
|
if (author != null) 'author=$author',
|
2024-08-18 16:14:09 +00:00
|
|
|
if (realm != null) 'realm=$realm',
|
2024-07-07 03:46:48 +00:00
|
|
|
];
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await get('/posts?${queries.join('&')}');
|
2024-07-07 03:46:48 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-07-07 03:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2024-05-25 09:21:27 +00:00
|
|
|
Future<Response> listPostReplies(String alias, int page) async {
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await get('/posts/$alias/replies?take=${10}&offset=$page');
|
2024-05-25 09:21:27 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-05-25 09:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2024-05-25 05:19:16 +00:00
|
|
|
Future<Response> getPost(String alias) async {
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await get('/posts/$alias');
|
2024-05-25 05:19:16 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-05-25 05:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
2024-07-10 02:50:10 +00:00
|
|
|
|
|
|
|
Future<Response> getArticle(String alias) async {
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await get('/articles/$alias');
|
2024-07-10 02:50:10 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-07-10 02:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|