Skip to content

Commit 6f56c19

Browse files
authored
feat(storage): add queryParams support to download method (#1291)
* feat(storage): add queryParams support to download method - Add optional queryParams parameter to download method - Merge queryParams with transform query parameters - Add test case for download with query params - Enhance CustomHttpClient to track received requests * test: clear receivedRequests on teardown * ci: run analyzer only on stable dart
1 parent c230538 commit 6f56c19

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

.github/workflows/dart-package-test.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,8 @@ jobs:
7373
run: dart format lib test -l 80 --set-exit-if-changed
7474

7575
- name: analyzer
76-
run: |
77-
if [ "${{ matrix.sdk }}" == "stable" ]; then
78-
dart analyze --fatal-warnings .
79-
else
80-
dart analyze
81-
fi
76+
if: ${{ matrix.sdk == 'stable'}}
77+
run: dart analyze --fatal-warnings .
8278

8379
- name: Start Docker services
8480
if: ${{ inputs.needs-docker }}

packages/storage_client/lib/src/storage_file_api.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,22 @@ class StorageFileApi {
395395
/// name. For example `download('folder/image.png')`.
396396
///
397397
/// [transform] download a transformed variant of the image with the provided options
398-
Future<Uint8List> download(String path, {TransformOptions? transform}) async {
398+
///
399+
/// [queryParams] additional query parameters to be added to the URL
400+
Future<Uint8List> download(String path,
401+
{TransformOptions? transform, Map<String, String>? queryParams}) async {
399402
final wantsTransformations = transform != null;
400403
final finalPath = _getFinalPath(path);
401404
final renderPath =
402405
wantsTransformations ? 'render/image/authenticated' : 'object';
403-
final queryParams = transform?.toQueryParams;
406+
407+
Map<String, String> query = transform?.toQueryParams ?? {};
408+
query.addAll(queryParams ?? {});
409+
404410
final options = FetchOptions(headers: headers, noResolveJson: true);
405411

406412
var fetchUrl = Uri.parse('$url/$renderPath/$finalPath');
407-
fetchUrl = fetchUrl.replace(queryParameters: queryParams);
413+
fetchUrl = fetchUrl.replace(queryParameters: query);
408414

409415
final response =
410416
await _storageFetch.get(fetchUrl.toString(), options: options);

packages/storage_client/test/basic_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void main() {
3838
tearDown(() {
3939
final file = File('a.txt');
4040
if (file.existsSync()) file.deleteSync();
41+
customHttpClient.receivedRequests.clear();
4142
});
4243

4344
group('Client with custom http client', () {
@@ -153,6 +154,24 @@ void main() {
153154
expect(String.fromCharCodes(response), 'Updated content');
154155
});
155156

157+
test('should download public file with query params', () async {
158+
final file = File('a.txt');
159+
file.writeAsStringSync('Updated content');
160+
161+
customHttpClient.response = file.readAsBytesSync();
162+
163+
final response = await client
164+
.from('public_bucket')
165+
.download('b.txt', queryParams: {'version': '1'});
166+
expect(response, isA<Uint8List>());
167+
expect(String.fromCharCodes(response), 'Updated content');
168+
169+
expect(customHttpClient.receivedRequests.length, 1);
170+
171+
final request = customHttpClient.receivedRequests.first;
172+
expect(request.url.queryParameters, {'version': '1'});
173+
});
174+
156175
test('should get public URL of a path', () {
157176
final response = client.from('files').getPublicUrl('b.txt');
158177
expect(response, '$objectUrl/public/files/b.txt');

packages/storage_client/test/custom_http_client.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ class RetryHttpClient extends BaseClient {
3434
}
3535

3636
class CustomHttpClient extends BaseClient {
37+
int statusCode = 201;
3738
dynamic response;
39+
List<BaseRequest> receivedRequests = <BaseRequest>[];
3840

3941
@override
4042
Future<StreamedResponse> send(BaseRequest request) async {
43+
receivedRequests.add(request);
4144
final dynamic body;
4245
if (response is Uint8List) {
4346
body = response;
@@ -47,7 +50,7 @@ class CustomHttpClient extends BaseClient {
4750

4851
return StreamedResponse(
4952
Stream.value(body),
50-
201,
53+
statusCode,
5154
request: request,
5255
);
5356
}

0 commit comments

Comments
 (0)