Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions app/lib/acquisition/acquisition_api_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:papyrus/acquisition/acquisition_models.dart';
import 'package:papyrus/auth/auth_api_client.dart';
import 'package:papyrus/auth/papyrus_api_config.dart';

class AcquisitionApiClient {
final PapyrusApiConfig config;
final http.Client _httpClient;

AcquisitionApiClient({required this.config, http.Client? httpClient})
: _httpClient = httpClient ?? http.Client();

Future<List<AcquisitionEndpoint>> listEndpoints(String accessToken) async {
final response = await _httpClient.get(
config.endpoint('/acquisition/endpoints'),
headers: _headers(accessToken),
);
return _decodeList(response).map(AcquisitionEndpoint.fromJson).toList();
}

Future<AcquisitionEndpoint> createEndpoint({
required String accessToken,
required String name,
required AcquisitionEndpointKind kind,
required Uri baseUrl,
String? apiKey,
String? username,
String? password,
}) async {
final response = await _httpClient.post(
config.endpoint('/acquisition/endpoints'),
headers: _headers(accessToken),
body: jsonEncode({
'name': name,
'kind': kind.apiValue,
'base_url': baseUrl.toString(),
if (apiKey != null) 'api_key': apiKey,
if (username != null) 'username': username,
if (password != null) 'password': password,
}),
);
return AcquisitionEndpoint.fromJson(_decodeObject(response));
}

Future<List<TorrentRelease>> search({
required String accessToken,
required String query,
List<String>? endpointIds,
}) async {
final response = await _httpClient.post(
config.endpoint('/acquisition/search'),
headers: _headers(accessToken),
body: jsonEncode({
'query': query,
if (endpointIds != null) 'endpoint_ids': endpointIds,
}),
);
return _decodeList(response).map(TorrentRelease.fromJson).toList();
}

Future<void> submitRelease({
required String accessToken,
required String endpointId,
required TorrentRelease release,
String? category,
String? savePath,
}) async {
final response = await _httpClient.post(
config.endpoint('/acquisition/submissions'),
headers: _headers(accessToken),
body: jsonEncode({
'endpoint_id': endpointId,
'title': release.title,
'download_url': release.downloadUrl,
if (category != null) 'category': category,
if (savePath != null) 'save_path': savePath,
}),
);
_decodeObject(response);
}

Future<void> runArrCommand({
required String accessToken,
required String endpointId,
required String command,
required List<int> ids,
}) async {
final response = await _httpClient.post(
config.endpoint('/acquisition/arr/$endpointId/commands'),
headers: _headers(accessToken),
body: jsonEncode({'command': command, 'ids': ids}),
);
_decodeObject(response);
}

Map<String, String> _headers(String accessToken) => {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer $accessToken',
};

Map<String, dynamic> _decodeObject(http.Response response) {
final decoded = response.body.isEmpty
? <String, dynamic>{}
: jsonDecode(response.body) as Map<String, dynamic>;
if (response.statusCode >= 200 && response.statusCode < 300) return decoded;
final error = decoded['error'];
throw AuthApiException(
statusCode: response.statusCode,
message: error is Map<String, dynamic>
? error['message'] as String? ?? 'Acquisition request failed'
: 'Acquisition request failed',
);
}

List<Map<String, dynamic>> _decodeList(http.Response response) {
if (response.statusCode < 200 || response.statusCode >= 300) {
_decodeObject(response);
}
return (jsonDecode(response.body) as List<dynamic>)
.cast<Map<String, dynamic>>();
}
}
69 changes: 69 additions & 0 deletions app/lib/acquisition/acquisition_models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
enum AcquisitionEndpointKind {
qbittorrent,
transmission,
deluge,
prowlarr,
torznab,
newznab,
readarr,
sonarr,
radarr,
lidarr,
whisparr;

String get apiValue => name;
}

class AcquisitionEndpoint {
final String id;
final String name;
final AcquisitionEndpointKind kind;
final Uri baseUrl;
final bool enabled;

const AcquisitionEndpoint({
required this.id,
required this.name,
required this.kind,
required this.baseUrl,
required this.enabled,
});

factory AcquisitionEndpoint.fromJson(Map<String, dynamic> json) =>
AcquisitionEndpoint(
id: json['endpoint_id'] as String,
name: json['name'] as String,
kind: AcquisitionEndpointKind.values.byName(json['kind'] as String),
baseUrl: Uri.parse(json['base_url'] as String),
enabled: json['enabled'] as bool,
);
}

class TorrentRelease {
final String title;
final String downloadUrl;
final String protocol;
final String indexer;
final int? seeders;
final int? sizeBytes;

const TorrentRelease({
required this.title,
required this.downloadUrl,
required this.protocol,
required this.indexer,
this.seeders,
this.sizeBytes,
});

bool get isMagnet => downloadUrl.startsWith('magnet:');

factory TorrentRelease.fromJson(Map<String, dynamic> json) => TorrentRelease(
title: json['title'] as String,
downloadUrl: json['download_url'] as String,
protocol: json['protocol'] as String,
indexer: json['indexer'] as String,
seeders: json['seeders'] as int?,
sizeBytes: json['size_bytes'] as int?,
);
}
100 changes: 80 additions & 20 deletions app/lib/config/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:papyrus/pages/shelf_contents_page.dart';
import 'package:papyrus/pages/shelves_page.dart';
import 'package:papyrus/pages/statistics_page.dart';
import 'package:papyrus/pages/annotations_page.dart';
import 'package:papyrus/pages/acquisition_page.dart';
import 'package:papyrus/pages/notes_page.dart';
import 'package:papyrus/pages/welcome_page.dart';
import 'package:papyrus/widgets/shell/adaptive_app_shell.dart';
Expand All @@ -46,24 +47,34 @@ class AppRouter {
GoRoute(
name: 'LOGIN',
path: 'login',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const LoginPage()),
pageBuilder: (context, state) =>
NoTransitionPage(key: state.pageKey, child: const LoginPage()),
),
GoRoute(
name: 'REGISTER',
path: 'register',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const RegisterPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const RegisterPage(),
),
),
GoRoute(
name: 'FORGOT_PASSWORD',
path: 'forgot-password',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const ForgotPasswordPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const ForgotPasswordPage(),
),
),
GoRoute(
name: 'RESET_PASSWORD',
path: 'reset-password',
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: ForgotPasswordPage(resetToken: state.uri.queryParameters['token'], isResetLink: true),
child: ForgotPasswordPage(
resetToken: state.uri.queryParameters['token'],
isResetLink: true,
),
),
),
GoRoute(
Expand All @@ -87,26 +98,40 @@ class AppRouter {
GoRoute(
name: 'DASHBOARD',
path: '/dashboard',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const DashboardPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const DashboardPage(),
),
),
// Library and sub-routes
GoRoute(
name: 'LIBRARY',
path: '/library',
redirect: (context, state) {
return state.uri.toString() == '/library' ? '/library/books' : null;
return state.uri.toString() == '/library'
? '/library/books'
: null;
},
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const LibraryPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const LibraryPage(),
),
routes: [
GoRoute(
name: 'BOOKS',
path: 'books',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const LibraryPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const LibraryPage(),
),
),
GoRoute(
name: 'SHELVES',
path: 'shelves',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const ShelvesPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const ShelvesPage(),
),
routes: [
GoRoute(
name: 'SHELF_CONTENTS',
Expand All @@ -124,22 +149,34 @@ class AppRouter {
GoRoute(
name: 'BOOKMARKS',
path: 'bookmarks',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const BookmarksPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const BookmarksPage(),
),
),
GoRoute(
name: 'ANNOTATIONS',
path: 'annotations',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const AnnotationsPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const AnnotationsPage(),
),
),
GoRoute(
name: 'NOTES',
path: 'notes',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const NotesPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const NotesPage(),
),
),
GoRoute(
name: 'SEARCH_OPTIONS',
path: 'search/options',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const SearchOptionsPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const SearchOptionsPage(),
),
),
GoRoute(
name: 'BOOK_DETAILS',
Expand Down Expand Up @@ -169,24 +206,42 @@ class AppRouter {
GoRoute(
name: 'GOALS',
path: '/goals',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const GoalsPage()),
pageBuilder: (context, state) =>
NoTransitionPage(key: state.pageKey, child: const GoalsPage()),
),
// Statistics
GoRoute(
name: 'STATISTICS',
path: '/statistics',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const StatisticsPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const StatisticsPage(),
),
),
GoRoute(
name: 'ACQUISITION',
path: '/acquisition',
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const AcquisitionPage(),
),
),
// Profile
GoRoute(
name: 'PROFILE',
path: '/profile',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const ProfilePage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const ProfilePage(),
),
routes: [
GoRoute(
name: 'EDIT_PROFILE',
path: 'edit',
pageBuilder: (context, state) => NoTransitionPage(key: state.pageKey, child: const EditProfilePage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const EditProfilePage(),
),
),
],
),
Expand All @@ -195,8 +250,10 @@ class AppRouter {
GoRoute(
name: 'DEVELOPER_OPTIONS',
path: '/developer-options',
pageBuilder: (context, state) =>
NoTransitionPage(key: state.pageKey, child: const DeveloperOptionsPage()),
pageBuilder: (context, state) => NoTransitionPage(
key: state.pageKey,
child: const DeveloperOptionsPage(),
),
),
],
),
Expand Down Expand Up @@ -227,7 +284,10 @@ class AppRouter {
return '/';
}

if (location == '/' || location == '/login' || location == '/register' || location == '/reset-password') {
if (location == '/' ||
location == '/login' ||
location == '/register' ||
location == '/reset-password') {
return '/library/books';
}

Expand Down
Loading