2022-11-26 18:16:02 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:openapi/api.dart';
|
|
|
|
|
|
|
|
/// Extension methods to retrieve ETag together with the API call
|
|
|
|
extension WithETag on AssetApi {
|
|
|
|
/// Get all AssetEntity belong to the user
|
|
|
|
///
|
|
|
|
/// Parameters:
|
|
|
|
///
|
|
|
|
/// * [String] eTag:
|
|
|
|
/// ETag of data already cached on the client
|
2023-05-21 03:41:34 +02:00
|
|
|
Future<(List<AssetResponseDto>? assets, String? eTag)> getAllAssetsWithETag({
|
2022-11-26 18:16:02 +02:00
|
|
|
String? eTag,
|
2023-05-25 05:52:43 +02:00
|
|
|
String? userId,
|
2023-06-14 16:35:32 +02:00
|
|
|
bool? isFavorite,
|
|
|
|
bool? isArchived,
|
|
|
|
bool? withoutThumbs,
|
2022-11-26 18:16:02 +02:00
|
|
|
}) async {
|
|
|
|
final response = await getAllAssetsWithHttpInfo(
|
|
|
|
ifNoneMatch: eTag,
|
2023-05-25 05:52:43 +02:00
|
|
|
userId: userId,
|
2023-06-14 16:35:32 +02:00
|
|
|
isFavorite: isFavorite,
|
|
|
|
isArchived: isArchived,
|
|
|
|
withoutThumbs: withoutThumbs,
|
2022-11-26 18:16:02 +02:00
|
|
|
);
|
|
|
|
if (response.statusCode >= HttpStatus.badRequest) {
|
|
|
|
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
|
|
|
|
}
|
|
|
|
// When a remote server returns no body with a status of 204, we shall not decode it.
|
|
|
|
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
|
|
|
|
// FormatException when trying to decode an empty string.
|
|
|
|
if (response.body.isNotEmpty &&
|
|
|
|
response.statusCode != HttpStatus.noContent) {
|
|
|
|
final responseBody = await _decodeBodyBytes(response);
|
|
|
|
final etag = response.headers[HttpHeaders.etagHeader];
|
|
|
|
final data = (await apiClient.deserializeAsync(
|
2023-01-26 16:40:19 +02:00
|
|
|
responseBody,
|
|
|
|
'List<AssetResponseDto>',
|
|
|
|
) as List)
|
2022-11-26 18:16:02 +02:00
|
|
|
.cast<AssetResponseDto>()
|
|
|
|
.toList();
|
2023-05-21 03:41:34 +02:00
|
|
|
return (data, etag);
|
2022-11-26 18:16:02 +02:00
|
|
|
}
|
2023-05-21 03:41:34 +02:00
|
|
|
return (null, null);
|
2022-11-26 18:16:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the decoded body as UTF-8 if the given headers indicate an 'application/json'
|
|
|
|
/// content type. Otherwise, returns the decoded body as decoded by dart:http package.
|
|
|
|
Future<String> _decodeBodyBytes(Response response) async {
|
|
|
|
final contentType = response.headers['content-type'];
|
|
|
|
return contentType != null &&
|
|
|
|
contentType.toLowerCase().startsWith('application/json')
|
|
|
|
? response.bodyBytes.isEmpty
|
|
|
|
? ''
|
|
|
|
: utf8.decode(response.bodyBytes)
|
|
|
|
: response.body;
|
|
|
|
}
|