-
Notifications
You must be signed in to change notification settings - Fork 1
Add torrent acquisition API client #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheusHen
wants to merge
3
commits into
PapyrusReader:master
Choose a base branch
from
TheusHen:feature/torrent-acquisition
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| 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; | ||
| final bool _ownsHttpClient; | ||
|
|
||
| AcquisitionApiClient({required this.config, http.Client? httpClient}) | ||
| : _httpClient = httpClient ?? http.Client(), | ||
| _ownsHttpClient = httpClient == null; | ||
|
|
||
| void close() { | ||
| if (_ownsHttpClient) { | ||
| _httpClient.close(); | ||
| } | ||
| } | ||
|
|
||
| Future<AcquisitionCapabilities> capabilities(String accessToken) async { | ||
| final response = await _httpClient.get( | ||
| config.endpoint('/acquisition/capabilities'), | ||
| headers: _headers(accessToken), | ||
| ); | ||
| return AcquisitionCapabilities.fromJson(_decodeObject(response)); | ||
| } | ||
|
|
||
| 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<AcquisitionEndpoint> updateEndpoint({ | ||
| required String accessToken, | ||
| required String endpointId, | ||
| String? name, | ||
| Uri? baseUrl, | ||
| String? apiKey, | ||
| String? username, | ||
| String? password, | ||
| bool? enabled, | ||
| }) async { | ||
| final response = await _httpClient.patch( | ||
| config.endpoint('/acquisition/endpoints/$endpointId'), | ||
| headers: _headers(accessToken), | ||
| body: jsonEncode({ | ||
| if (name != null) 'name': name, | ||
| if (baseUrl != null) 'base_url': baseUrl.toString(), | ||
| if (apiKey != null) 'api_key': apiKey, | ||
| if (username != null) 'username': username, | ||
| if (password != null) 'password': password, | ||
| if (enabled != null) 'enabled': enabled, | ||
| }), | ||
| ); | ||
| return AcquisitionEndpoint.fromJson(_decodeObject(response)); | ||
| } | ||
|
|
||
| Future<void> deleteEndpoint({ | ||
| required String accessToken, | ||
| required String endpointId, | ||
| }) async { | ||
| final response = await _httpClient.delete( | ||
| config.endpoint('/acquisition/endpoints/$endpointId'), | ||
| headers: _headers(accessToken), | ||
| ); | ||
| if (response.statusCode >= 200 && response.statusCode < 300) return; | ||
| _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>>(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| enum AcquisitionEndpointKind { | ||
| qbittorrent, | ||
| transmission, | ||
| deluge, | ||
| prowlarr, | ||
| torznab, | ||
| readarr, | ||
| sonarr, | ||
| radarr, | ||
| lidarr, | ||
| whisparr; | ||
|
|
||
| String get apiValue => name; | ||
|
|
||
| String get label => switch (this) { | ||
| AcquisitionEndpointKind.qbittorrent => 'qBittorrent', | ||
| AcquisitionEndpointKind.transmission => 'Transmission', | ||
| AcquisitionEndpointKind.deluge => 'Deluge', | ||
| AcquisitionEndpointKind.prowlarr => 'Prowlarr', | ||
| AcquisitionEndpointKind.torznab => 'Torznab', | ||
| AcquisitionEndpointKind.readarr => 'Readarr', | ||
| AcquisitionEndpointKind.sonarr => 'Sonarr', | ||
| AcquisitionEndpointKind.radarr => 'Radarr', | ||
| AcquisitionEndpointKind.lidarr => 'Lidarr', | ||
| AcquisitionEndpointKind.whisparr => 'Whisparr', | ||
| }; | ||
|
|
||
| bool get isDownloadClient => switch (this) { | ||
| AcquisitionEndpointKind.qbittorrent || | ||
| AcquisitionEndpointKind.transmission || | ||
| AcquisitionEndpointKind.deluge => true, | ||
| _ => false, | ||
| }; | ||
|
|
||
| bool get isIndexer => switch (this) { | ||
| AcquisitionEndpointKind.prowlarr || AcquisitionEndpointKind.torznab => true, | ||
| _ => false, | ||
| }; | ||
|
|
||
| bool get isArr => switch (this) { | ||
| AcquisitionEndpointKind.readarr || | ||
| AcquisitionEndpointKind.sonarr || | ||
| AcquisitionEndpointKind.radarr || | ||
| AcquisitionEndpointKind.lidarr || | ||
| AcquisitionEndpointKind.whisparr => true, | ||
| _ => false, | ||
| }; | ||
| } | ||
|
|
||
| class AcquisitionCapabilities { | ||
| final List<AcquisitionEndpointKind> endpointKinds; | ||
| final List<AcquisitionEndpointKind> indexerKinds; | ||
| final List<AcquisitionEndpointKind> downloadClientKinds; | ||
| final List<AcquisitionEndpointKind> arrKinds; | ||
| final Map<AcquisitionEndpointKind, List<String>> arrCommands; | ||
|
|
||
| const AcquisitionCapabilities({ | ||
| required this.endpointKinds, | ||
| required this.indexerKinds, | ||
| required this.downloadClientKinds, | ||
| required this.arrKinds, | ||
| required this.arrCommands, | ||
| }); | ||
|
|
||
| factory AcquisitionCapabilities.fromJson(Map<String, dynamic> json) { | ||
| return AcquisitionCapabilities( | ||
| endpointKinds: _kinds(json['endpoint_kinds']), | ||
| indexerKinds: _kinds(json['indexer_kinds']), | ||
| downloadClientKinds: _kinds(json['download_client_kinds']), | ||
| arrKinds: _kinds(json['arr_kinds']), | ||
| arrCommands: ((json['arr_commands'] as Map<String, dynamic>?) ?? {}).map( | ||
| (key, value) => MapEntry( | ||
| AcquisitionEndpointKind.values.byName(key), | ||
| (value as List<dynamic>).cast<String>(), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| static List<AcquisitionEndpointKind> _kinds(Object? value) { | ||
| return ((value as List<dynamic>?) ?? []) | ||
| .cast<String>() | ||
| .map(AcquisitionEndpointKind.values.byName) | ||
| .toList(); | ||
| } | ||
| } | ||
|
|
||
| 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?, | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.