Skip to content

Commit

Permalink
fix: Use per client fetch instance (#818)
Browse files Browse the repository at this point in the history
* fix: use per client fetch instance

* test: adapt tests to missing global fetch

* test: remove mocktail usage
  • Loading branch information
Vinzent03 authored Jan 31, 2024
1 parent a789d79 commit 0f3182c
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 187 deletions.
2 changes: 0 additions & 2 deletions packages/storage_client/lib/src/fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import 'package:storage_client/src/types.dart';

import 'file_io.dart' if (dart.library.js) './file_stub.dart';

Fetch storageFetch = Fetch();

class Fetch {
final Client? httpClient;

Expand Down
3 changes: 3 additions & 0 deletions packages/storage_client/lib/src/storage_bucket_api.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:http/http.dart';
import 'package:meta/meta.dart';
import 'package:storage_client/src/fetch.dart';
import 'package:storage_client/src/types.dart';

class StorageBucketApi {
final String url;
final Map<String, String> headers;
@internal
late Fetch storageFetch;

StorageBucketApi(this.url, this.headers, {Client? httpClient}) {
storageFetch = Fetch(httpClient);
Expand Down
8 changes: 7 additions & 1 deletion packages/storage_client/lib/src/storage_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ class SupabaseStorageClient extends StorageBucketApi {
///
/// [id] The bucket id to operate on.
StorageFileApi from(String id) {
return StorageFileApi(url, headers, id, _defaultRetryAttempts);
return StorageFileApi(
url,
headers,
id,
_defaultRetryAttempts,
storageFetch,
);
}

void setAuth(String jwt) {
Expand Down
30 changes: 16 additions & 14 deletions packages/storage_client/lib/src/storage_file_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class StorageFileApi {
final Map<String, String> headers;
final String? bucketId;
final int _retryAttempts;
final Fetch _storageFetch;

const StorageFileApi(
this.url,
this.headers,
this.bucketId,
this._retryAttempts,
this._storageFetch,
);

String _getFinalPath(String path) {
Expand Down Expand Up @@ -51,7 +53,7 @@ class StorageFileApi {
assert(retryAttempts == null || retryAttempts >= 0,
'retryAttempts has to be greater or equal to 0');
final finalPath = _getFinalPath(path);
final response = await storageFetch.postFile(
final response = await _storageFetch.postFile(
'$url/object/$finalPath',
file,
fileOptions,
Expand Down Expand Up @@ -86,7 +88,7 @@ class StorageFileApi {
assert(retryAttempts == null || retryAttempts >= 0,
'retryAttempts has to be greater or equal to 0');
final finalPath = _getFinalPath(path);
final response = await storageFetch.postBinaryFile(
final response = await _storageFetch.postBinaryFile(
'$url/object/$finalPath',
data,
fileOptions,
Expand Down Expand Up @@ -121,7 +123,7 @@ class StorageFileApi {
var url = Uri.parse('${this.url}/object/upload/sign/$finalPath');
url = url.replace(queryParameters: {'token': token});

await storageFetch.putFile(
await _storageFetch.putFile(
url.toString(),
file,
fileOptions,
Expand Down Expand Up @@ -155,7 +157,7 @@ class StorageFileApi {
var url = Uri.parse('${this.url}/object/upload/sign/$path0');
url = url.replace(queryParameters: {'token': token});

await storageFetch.putBinaryFile(
await _storageFetch.putBinaryFile(
url.toString(),
data,
fileOptions,
Expand All @@ -175,7 +177,7 @@ class StorageFileApi {
Future<SignedUploadURLResponse> createSignedUploadUrl(String path) async {
final finalPath = _getFinalPath(path);

final data = await storageFetch.post(
final data = await _storageFetch.post(
'$url/object/upload/sign/$finalPath',
{},
options: FetchOptions(headers: headers),
Expand Down Expand Up @@ -220,7 +222,7 @@ class StorageFileApi {
assert(retryAttempts == null || retryAttempts >= 0,
'retryAttempts has to be greater or equal to 0');
final finalPath = _getFinalPath(path);
final response = await storageFetch.putFile(
final response = await _storageFetch.putFile(
'$url/object/$finalPath',
file,
fileOptions,
Expand Down Expand Up @@ -256,7 +258,7 @@ class StorageFileApi {
assert(retryAttempts == null || retryAttempts >= 0,
'retryAttempts has to be greater or equal to 0');
final finalPath = _getFinalPath(path);
final response = await storageFetch.putBinaryFile(
final response = await _storageFetch.putBinaryFile(
'$url/object/$finalPath',
data,
fileOptions,
Expand All @@ -276,7 +278,7 @@ class StorageFileApi {
/// `folder/image-new.png`.
Future<String> move(String fromPath, String toPath) async {
final options = FetchOptions(headers: headers);
final response = await storageFetch.post(
final response = await _storageFetch.post(
'$url/object/move',
{
'bucketId': bucketId,
Expand All @@ -297,7 +299,7 @@ class StorageFileApi {
/// `folder/image-copy.png`.
Future<String> copy(String fromPath, String toPath) async {
final options = FetchOptions(headers: headers);
final response = await storageFetch.post(
final response = await _storageFetch.post(
'$url/object/copy',
{
'bucketId': bucketId,
Expand Down Expand Up @@ -326,7 +328,7 @@ class StorageFileApi {
}) async {
final finalPath = _getFinalPath(path);
final options = FetchOptions(headers: headers);
final response = await storageFetch.post(
final response = await _storageFetch.post(
'$url/object/sign/$finalPath',
{
'expiresIn': expiresIn,
Expand Down Expand Up @@ -354,7 +356,7 @@ class StorageFileApi {
int expiresIn,
) async {
final options = FetchOptions(headers: headers);
final response = await storageFetch.post(
final response = await _storageFetch.post(
'$url/object/sign/$bucketId',
{
'expiresIn': expiresIn,
Expand Down Expand Up @@ -391,7 +393,7 @@ class StorageFileApi {
fetchUrl = fetchUrl.replace(queryParameters: queryParams);

final response =
await storageFetch.get(fetchUrl.toString(), options: options);
await _storageFetch.get(fetchUrl.toString(), options: options);
return response as Uint8List;
}

Expand Down Expand Up @@ -424,7 +426,7 @@ class StorageFileApi {
/// name. For example: `remove(['folder/image.png'])`.
Future<List<FileObject>> remove(List<String> paths) async {
final options = FetchOptions(headers: headers);
final response = await storageFetch.delete(
final response = await _storageFetch.delete(
'$url/object/$bucketId',
{'prefixes': paths},
options: options,
Expand All @@ -451,7 +453,7 @@ class StorageFileApi {
...searchOptions.toMap(),
};
final options = FetchOptions(headers: headers);
final response = await storageFetch.post(
final response = await _storageFetch.post(
'$url/object/list/$bucketId',
body,
options: options,
Expand Down
2 changes: 1 addition & 1 deletion packages/storage_client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ dependencies:
http_parser: ^4.0.1
mime: ^1.0.2
retry: ^3.1.0
meta: ^1.7.0

dev_dependencies:
mocktail: ^0.3.0
test: ^1.21.4
lints: ^2.1.1
path: ^1.8.2
Loading

0 comments on commit 0f3182c

Please sign in to comment.