chore: pre-commit ci hook, dart format
This commit is contained in:
@@ -12,8 +12,7 @@ import 'log_redaction_manager.dart';
|
||||
import 'plex_http_exception.dart';
|
||||
|
||||
// Platform-specific imports are conditional
|
||||
import 'platform_http_client_stub.dart'
|
||||
if (dart.library.io) 'platform_http_client_io.dart' as platform;
|
||||
import 'platform_http_client_stub.dart' if (dart.library.io) 'platform_http_client_io.dart' as platform;
|
||||
|
||||
/// Response from [PlexHttpClient] requests.
|
||||
class PlexResponse {
|
||||
@@ -25,11 +24,7 @@ class PlexResponse {
|
||||
|
||||
final Map<String, String> headers;
|
||||
|
||||
PlexResponse({
|
||||
required this.statusCode,
|
||||
this.data,
|
||||
required this.headers,
|
||||
});
|
||||
PlexResponse({required this.statusCode, this.data, required this.headers});
|
||||
}
|
||||
|
||||
/// Abort controller for cancelling in-flight HTTP requests.
|
||||
@@ -61,8 +56,8 @@ class PlexHttpClient {
|
||||
Map<String, String> defaultHeaders = const {},
|
||||
this.connectTimeout = const Duration(seconds: 10),
|
||||
this.receiveTimeout = const Duration(seconds: 120),
|
||||
}) : _client = client ?? platform.createPlatformClient(),
|
||||
defaultHeaders = Map.of(defaultHeaders);
|
||||
}) : _client = client ?? platform.createPlatformClient(),
|
||||
defaultHeaders = Map.of(defaultHeaders);
|
||||
|
||||
/// The underlying [http.Client] for direct streaming / multipart requests.
|
||||
http.Client get inner => _client;
|
||||
@@ -82,12 +77,7 @@ class PlexHttpClient {
|
||||
Map<String, String>? headers,
|
||||
Duration? timeout,
|
||||
AbortController? abort,
|
||||
}) =>
|
||||
_send('GET', path,
|
||||
queryParameters: queryParameters,
|
||||
headers: headers,
|
||||
timeout: timeout,
|
||||
abort: abort);
|
||||
}) => _send('GET', path, queryParameters: queryParameters, headers: headers, timeout: timeout, abort: abort);
|
||||
|
||||
Future<PlexResponse> post(
|
||||
String path, {
|
||||
@@ -96,13 +86,15 @@ class PlexHttpClient {
|
||||
Object? body,
|
||||
Duration? timeout,
|
||||
AbortController? abort,
|
||||
}) =>
|
||||
_send('POST', path,
|
||||
queryParameters: queryParameters,
|
||||
headers: headers,
|
||||
body: body,
|
||||
timeout: timeout,
|
||||
abort: abort);
|
||||
}) => _send(
|
||||
'POST',
|
||||
path,
|
||||
queryParameters: queryParameters,
|
||||
headers: headers,
|
||||
body: body,
|
||||
timeout: timeout,
|
||||
abort: abort,
|
||||
);
|
||||
|
||||
Future<PlexResponse> put(
|
||||
String path, {
|
||||
@@ -111,13 +103,15 @@ class PlexHttpClient {
|
||||
Object? body,
|
||||
Duration? timeout,
|
||||
AbortController? abort,
|
||||
}) =>
|
||||
_send('PUT', path,
|
||||
queryParameters: queryParameters,
|
||||
headers: headers,
|
||||
body: body,
|
||||
timeout: timeout,
|
||||
abort: abort);
|
||||
}) => _send(
|
||||
'PUT',
|
||||
path,
|
||||
queryParameters: queryParameters,
|
||||
headers: headers,
|
||||
body: body,
|
||||
timeout: timeout,
|
||||
abort: abort,
|
||||
);
|
||||
|
||||
Future<PlexResponse> delete(
|
||||
String path, {
|
||||
@@ -125,19 +119,10 @@ class PlexHttpClient {
|
||||
Map<String, String>? headers,
|
||||
Duration? timeout,
|
||||
AbortController? abort,
|
||||
}) =>
|
||||
_send('DELETE', path,
|
||||
queryParameters: queryParameters,
|
||||
headers: headers,
|
||||
timeout: timeout,
|
||||
abort: abort);
|
||||
}) => _send('DELETE', path, queryParameters: queryParameters, headers: headers, timeout: timeout, abort: abort);
|
||||
|
||||
/// Fetch raw bytes (e.g. images, BIF files, subtitles).
|
||||
Future<Uint8List> getBytes(
|
||||
String url, {
|
||||
Map<String, String>? headers,
|
||||
Duration? timeout,
|
||||
}) async {
|
||||
Future<Uint8List> getBytes(String url, {Map<String, String>? headers, Duration? timeout}) async {
|
||||
final uri = _isAbsoluteUrl(url) ? Uri.parse(url) : _buildUri(url, null);
|
||||
final request = http.Request('GET', uri);
|
||||
request.headers.addAll({...defaultHeaders, ...?headers});
|
||||
@@ -148,9 +133,10 @@ class PlexHttpClient {
|
||||
.send(request)
|
||||
.namedTimeout(timeout ?? connectTimeout, operation: 'GET ${uri.path} connect');
|
||||
|
||||
final bytes = await streamed.stream
|
||||
.toBytes()
|
||||
.namedTimeout(timeout ?? receiveTimeout, operation: 'GET ${uri.path} receive');
|
||||
final bytes = await streamed.stream.toBytes().namedTimeout(
|
||||
timeout ?? receiveTimeout,
|
||||
operation: 'GET ${uri.path} receive',
|
||||
);
|
||||
|
||||
sw.stop();
|
||||
_logResponse('GET', uri, streamed.statusCode, sw.elapsedMilliseconds);
|
||||
@@ -162,12 +148,7 @@ class PlexHttpClient {
|
||||
}
|
||||
|
||||
/// Stream-download a URL directly into a file.
|
||||
Future<void> downloadFile(
|
||||
String url,
|
||||
String filePath, {
|
||||
Map<String, String>? headers,
|
||||
Duration? timeout,
|
||||
}) async {
|
||||
Future<void> downloadFile(String url, String filePath, {Map<String, String>? headers, Duration? timeout}) async {
|
||||
final uri = _isAbsoluteUrl(url) ? Uri.parse(url) : _buildUri(url, null);
|
||||
final request = http.Request('GET', uri);
|
||||
request.headers.addAll({...defaultHeaders, ...?headers});
|
||||
@@ -190,8 +171,7 @@ class PlexHttpClient {
|
||||
}
|
||||
|
||||
/// Send a streamed request (for image cache etc).
|
||||
Future<http.StreamedResponse> sendStreamed(http.BaseRequest request) =>
|
||||
_client.send(request);
|
||||
Future<http.StreamedResponse> sendStreamed(http.BaseRequest request) => _client.send(request);
|
||||
|
||||
void close() => _client.close();
|
||||
|
||||
@@ -212,10 +192,7 @@ class PlexHttpClient {
|
||||
? _appendQuery(Uri.parse(path), queryParameters)
|
||||
: _buildUri(path, queryParameters);
|
||||
|
||||
final mergedHeaders = <String, String>{
|
||||
...defaultHeaders,
|
||||
...?headers,
|
||||
};
|
||||
final mergedHeaders = <String, String>{...defaultHeaders, ...?headers};
|
||||
|
||||
// Build the request — use AbortableRequest when abort is provided
|
||||
final http.Request request;
|
||||
@@ -235,19 +212,16 @@ class PlexHttpClient {
|
||||
.namedTimeout(timeout ?? connectTimeout, operation: '$method ${uri.path} connect');
|
||||
|
||||
// Phase 2: consume body (receive timeout)
|
||||
final bytes = await streamed.stream
|
||||
.toBytes()
|
||||
.namedTimeout(timeout ?? receiveTimeout, operation: '$method ${uri.path} receive');
|
||||
final bytes = await streamed.stream.toBytes().namedTimeout(
|
||||
timeout ?? receiveTimeout,
|
||||
operation: '$method ${uri.path} receive',
|
||||
);
|
||||
|
||||
sw.stop();
|
||||
_logResponse(method, uri, streamed.statusCode, sw.elapsedMilliseconds);
|
||||
|
||||
final data = await _decodeBody(bytes, streamed.headers);
|
||||
return PlexResponse(
|
||||
statusCode: streamed.statusCode,
|
||||
data: data,
|
||||
headers: streamed.headers,
|
||||
);
|
||||
return PlexResponse(statusCode: streamed.statusCode, data: data, headers: streamed.headers);
|
||||
} catch (e) {
|
||||
sw.stop();
|
||||
throw PlexHttpException.from(e, uri: uri);
|
||||
@@ -293,8 +267,7 @@ class PlexHttpClient {
|
||||
return parts.join('&');
|
||||
}
|
||||
|
||||
static bool _isAbsoluteUrl(String url) =>
|
||||
url.startsWith('http://') || url.startsWith('https://');
|
||||
static bool _isAbsoluteUrl(String url) => url.startsWith('http://') || url.startsWith('https://');
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Body serialization
|
||||
@@ -328,8 +301,7 @@ class PlexHttpClient {
|
||||
|
||||
/// Decode the response body: lenient UTF-8, then JSON parse if applicable.
|
||||
/// Large payloads are decoded in a background isolate.
|
||||
Future<dynamic> _decodeBody(
|
||||
List<int> bytes, Map<String, String> headers) async {
|
||||
Future<dynamic> _decodeBody(List<int> bytes, Map<String, String> headers) async {
|
||||
if (bytes.isEmpty) return null;
|
||||
|
||||
final contentType = headers['content-type'] ?? '';
|
||||
@@ -338,8 +310,7 @@ class PlexHttpClient {
|
||||
// For large JSON payloads, do both UTF-8 decode and JSON parse in a
|
||||
// single isolate roundtrip to avoid two context switches.
|
||||
if (isJson && bytes.length > 50 * 1024) {
|
||||
return await tryIsolateRun(
|
||||
() => jsonDecode(utf8.decode(bytes, allowMalformed: true)));
|
||||
return await tryIsolateRun(() => jsonDecode(utf8.decode(bytes, allowMalformed: true)));
|
||||
}
|
||||
|
||||
final body = bytes.length > 50 * 1024
|
||||
@@ -354,9 +325,7 @@ class PlexHttpClient {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void _logResponse(String method, Uri uri, int statusCode, int ms) {
|
||||
appLogger.d(
|
||||
'$method ${LogRedactionManager.redact(uri.toString())} → $statusCode (${ms}ms)',
|
||||
);
|
||||
appLogger.d('$method ${LogRedactionManager.redact(uri.toString())} → $statusCode (${ms}ms)');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user