Skip to content

Commit 6eadb27

Browse files
committed
- [#2](#2) Restructure Library to support Dependency Injection (DI)
- Enable nullability in the project and make use of nullable reference types.
1 parent c87d008 commit 6eadb27

16 files changed

+146
-65
lines changed

Storage/Bucket.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace Supabase.Storage
66
public class Bucket
77
{
88
[JsonProperty("id")]
9-
public string Id { get; set; }
9+
public string? Id { get; set; }
1010

1111
[JsonProperty("name")]
12-
public string Name { get; set; }
12+
public string? Name { get; set; }
1313

1414
[JsonProperty("owner")]
15-
public string Owner { get; set; }
15+
public string? Owner { get; set; }
1616

1717
[JsonProperty("created_at")]
1818
public DateTime CreatedAt { get; set; }

Storage/Client.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
using System;
1+
using Storage.Interfaces;
2+
using System;
23
using System.Collections.Generic;
34

45
namespace Supabase.Storage
56
{
6-
public class Client : StorageBucketApi
7+
public class Client : StorageBucketApi, IStorageClient<Bucket, FileObject>
78
{
89
public Client(string url, Dictionary<string, string> headers) : base(url, headers)
9-
{}
10+
{ }
1011

1112
/// <summary>
1213
/// Perform a file operation in a bucket
1314
/// </summary>
1415
/// <param name="id">Bucket Id</param>
1516
/// <returns></returns>
16-
public StorageFileApi From(string id) => new StorageFileApi(Url, Headers, id);
17+
public IStorageFileApi<FileObject> From(string id) => new StorageFileApi(Url, Headers, id);
1718
}
1819
}

Storage/CreateSignedUrlResponse.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Supabase.Storage
66
public class CreateSignedUrlResponse
77
{
88
[JsonProperty("signedURL")]
9-
public string SignedUrl { get; set; }
9+
public string? SignedUrl { get; set; }
1010
}
1111

1212
public class CreateSignedUrlsResponse: CreateSignedUrlResponse
1313
{
1414
[JsonProperty("path")]
15-
public string Path { get; set; }
15+
public string? Path { get; set; }
1616
}
1717
}

Storage/Extensions/HttpClientProgress.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Supabase.Storage.Extensions
1313
/// </summary>
1414
internal static class HttpClientProgress
1515
{
16-
public static async Task<MemoryStream> DownloadDataAsync(this HttpClient client, Uri uri, Dictionary<string, string> headers = null, IProgress<float> progress = null, CancellationToken cancellationToken = default(CancellationToken))
16+
public static async Task<MemoryStream> DownloadDataAsync(this HttpClient client, Uri uri, Dictionary<string, string>? headers = null, IProgress<float>? progress = null, CancellationToken cancellationToken = default(CancellationToken))
1717
{
1818
var destination = new MemoryStream();
1919
var message = new HttpRequestMessage(HttpMethod.Get, uri);
@@ -48,7 +48,7 @@ internal static class HttpClientProgress
4848
return destination;
4949
}
5050

51-
static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress<long> progress = null, CancellationToken cancellationToken = default(CancellationToken))
51+
static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress<long>? progress = null, CancellationToken cancellationToken = default(CancellationToken))
5252
{
5353
if (bufferSize < 0)
5454
throw new ArgumentOutOfRangeException(nameof(bufferSize));
@@ -72,19 +72,19 @@ internal static class HttpClientProgress
7272
}
7373
}
7474

75-
public static Task<HttpResponseMessage> UploadFileAsync(this HttpClient client, Uri uri, string filePath, Dictionary<string, string> headers = null, Progress<float> progress = null)
75+
public static Task<HttpResponseMessage> UploadFileAsync(this HttpClient client, Uri uri, string filePath, Dictionary<string, string>? headers = null, Progress<float>? progress = null)
7676
{
7777
var fileStream = new FileStream(filePath, mode: FileMode.Open, FileAccess.Read);
7878
return UploadAsync(client, uri, fileStream, headers, progress);
7979
}
8080

81-
public static Task<HttpResponseMessage> UploadBytesAsync(this HttpClient client, Uri uri, byte[] data, Dictionary<string, string> headers = null, Progress<float> progress = null)
81+
public static Task<HttpResponseMessage> UploadBytesAsync(this HttpClient client, Uri uri, byte[] data, Dictionary<string, string>? headers = null, Progress<float>? progress = null)
8282
{
8383
var stream = new MemoryStream(data);
8484
return UploadAsync(client, uri, stream, headers, progress);
8585
}
8686

87-
public static async Task<HttpResponseMessage> UploadAsync(this HttpClient client, Uri uri, Stream stream, Dictionary<string, string> headers = null, Progress<float> progress = null)
87+
public static async Task<HttpResponseMessage> UploadAsync(this HttpClient client, Uri uri, Stream stream, Dictionary<string, string>? headers = null, Progress<float>? progress = null)
8888
{
8989
var content = new ProgressableStreamContent(stream, 4096, progress);
9090

Storage/Extensions/ProgressableStreamContent.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Storage.Interfaces;
2+
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics.Contracts;
45
using System.IO;
@@ -9,15 +10,15 @@
910

1011
namespace Supabase.Storage.Extensions
1112
{
12-
internal class ProgressableStreamContent : HttpContent
13+
internal class ProgressableStreamContent : HttpContent, IProgressableStreamContent
1314
{
1415
private const int defaultBufferSize = 4096;
1516

1617
private Stream content;
1718
private int bufferSize;
1819

19-
public EventHandler<UploadState> StateChanged;
20-
public IProgress<float> Progress { get; private set; }
20+
public EventHandler<UploadState>? StateChanged;
21+
public IProgress<float>? Progress { get; private set; }
2122

2223
public enum UploadState
2324
{
@@ -29,7 +30,7 @@ public enum UploadState
2930

3031
public ProgressableStreamContent(Stream content) : this(content, defaultBufferSize) { }
3132

32-
public ProgressableStreamContent(Stream content, int bufferSize, Progress<float> progress = null)
33+
public ProgressableStreamContent(Stream content, int bufferSize, Progress<float>? progress = null)
3334
{
3435
if (content == null)
3536
{
@@ -71,9 +72,9 @@ protected override Task SerializeToStreamAsync(Stream stream, TransportContext c
7172

7273
uploaded += length;
7374

74-
Progress.Report((uploaded / size) * 100f);
75+
Progress?.Report((uploaded / size) * 100f);
7576

76-
stream.Write(buffer, 0, length);
77+
stream!.Write(buffer, 0, length);
7778

7879
StateChanged?.Invoke(this, UploadState.InProgress);
7980
}

Storage/FileObject.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ namespace Supabase.Storage
77
public class FileObject
88
{
99
[JsonProperty("name")]
10-
public string Name { get; set; }
10+
public string? Name { get; set; }
1111

1212
[JsonProperty("bucket_id")]
13-
public string BucketId { get; set; }
13+
public string? BucketId { get; set; }
1414

1515
[JsonProperty("owner")]
16-
public string Owner { get; set; }
16+
public string? Owner { get; set; }
1717

1818
[JsonProperty("id")]
19-
public string Id { get; set; }
19+
public string? Id { get; set; }
2020

2121
[JsonProperty("updated_at")]
2222
public DateTime UpdatedAt { get; set; }
@@ -31,6 +31,6 @@ public class FileObject
3131
public Dictionary<string, object> MetaData = new Dictionary<string, object>();
3232

3333
[JsonProperty("buckets")]
34-
public Bucket Buckets { get; set; }
34+
public Bucket? Buckets { get; set; }
3535
}
3636
}

Storage/Helpers.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ internal static class Helpers
2525
/// <param name="reqParams"></param>
2626
/// <param name="headers"></param>
2727
/// <returns></returns>
28-
public static async Task<T> MakeRequest<T>(HttpMethod method, string url, object data = null, Dictionary<string, string> headers = null)
28+
public static async Task<T?> MakeRequest<T>(HttpMethod method, string url, object? data = null, Dictionary<string, string>? headers = null) where T : class
2929
{
3030
var response = await MakeRequest(method, url, data, headers);
3131
var content = await response.Content.ReadAsStringAsync();
3232

3333
if (response.IsSuccessStatusCode)
3434
{
3535
return JsonConvert.DeserializeObject<T>(content);
36-
} else
36+
}
37+
else
3738
{
3839
throw new BadRequestException(response, content);
3940
}
@@ -47,7 +48,7 @@ public static async Task<T> MakeRequest<T>(HttpMethod method, string url, object
4748
/// <param name="reqParams"></param>
4849
/// <param name="headers"></param>
4950
/// <returns></returns>
50-
public static async Task<HttpResponseMessage> MakeRequest(HttpMethod method, string url, object data = null, Dictionary<string, string> headers = null)
51+
public static async Task<HttpResponseMessage> MakeRequest(HttpMethod method, string url, object? data = null, Dictionary<string, string>? headers = null)
5152
{
5253
var builder = new UriBuilder(url);
5354
var query = HttpUtility.ParseQueryString(builder.Query);
@@ -89,7 +90,7 @@ public static async Task<HttpResponseMessage> MakeRequest(HttpMethod method, str
8990

9091
public class BadRequestException : Exception
9192
{
92-
public ErrorResponse ErrorResponse { get; private set; }
93+
public ErrorResponse? ErrorResponse { get; private set; }
9394

9495
public HttpResponseMessage HttpResponse { get; private set; }
9596

@@ -103,15 +104,15 @@ public BadRequestException(HttpResponseMessage httpResponse, string content)
103104
public class GenericResponse
104105
{
105106
[JsonProperty("message")]
106-
public string Message { get; }
107+
public string? Message { get; }
107108
}
108109

109110
public class ErrorResponse
110111
{
111112
[JsonProperty("message")]
112-
public string Message { get; }
113+
public string? Message { get; }
113114

114115
[JsonProperty("error")]
115-
public string Error { get; set; }
116+
public string? Error { get; set; }
116117
}
117118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace Storage.Interfaces
4+
{
5+
internal interface IProgressableStreamContent
6+
{
7+
IProgress<float>? Progress { get; }
8+
}
9+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Supabase.Storage;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
5+
namespace Storage.Interfaces
6+
{
7+
public interface IStorageBucketApi<TBucket>
8+
where TBucket : Bucket
9+
{
10+
Task<string> CreateBucket(string id, BucketUpsertOptions? options = null);
11+
Task<GenericResponse?> DeleteBucket(string id);
12+
Task<GenericResponse?> EmptyBucket(string id);
13+
Task<TBucket?> GetBucket(string id);
14+
Task<List<TBucket>?> ListBuckets();
15+
Task<TBucket?> UpdateBucket(string id, BucketUpsertOptions? options = null);
16+
}
17+
}

Storage/Interfaces/IStorageClient.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Supabase.Storage;
2+
3+
namespace Storage.Interfaces
4+
{
5+
public interface IStorageClient<TBucket, TFileObject>
6+
where TBucket : Bucket
7+
where TFileObject : FileObject
8+
{
9+
IStorageFileApi<TFileObject> From(string id);
10+
}
11+
}

Storage/Interfaces/IStorageFileApi.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Supabase.Storage;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
6+
namespace Storage.Interfaces
7+
{
8+
public interface IStorageFileApi<TFileObject>
9+
where TFileObject : FileObject
10+
{
11+
Task<string> CreateSignedUrl(string path, int expiresIn);
12+
Task<List<CreateSignedUrlsResponse>?> CreateSignedUrls(List<string> paths, int expiresIn);
13+
Task<byte[]> Download(string supabasePath, EventHandler<float>? onProgress = null);
14+
Task<string> Download(string supabasePath, string localPath, EventHandler<float>? onProgress = null);
15+
string GetPublicUrl(string path);
16+
Task<List<TFileObject>?> List(string path = "", SearchOptions? options = null);
17+
Task<bool> Move(string fromPath, string toPath);
18+
Task<List<TFileObject>?> Remove(List<string> paths);
19+
Task<string> Update(byte[] data, string supabasePath, FileOptions? options = null, EventHandler<float>? onProgress = null);
20+
Task<string> Update(string localFilePath, string supabasePath, FileOptions? options = null, EventHandler<float>? onProgress = null);
21+
Task<string> Upload(byte[] data, string supabasePath, FileOptions? options = null, EventHandler<float>? onProgress = null, bool inferContentType = true);
22+
Task<string> Upload(string localFilePath, string supabasePath, FileOptions? options = null, EventHandler<float>? onProgress = null, bool inferContentType = true);
23+
}
24+
}

Storage/SortBy.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Supabase.Storage
66
public class SortBy
77
{
88
[JsonProperty("column")]
9-
public string Column { get; set; }
9+
public string? Column { get; set; }
1010

1111
[JsonProperty("order")]
12-
public string Order { get; set; }
12+
public string? Order { get; set; }
1313
}
1414
}

Storage/Storage.csproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<ReleaseVersion>1.1.1</ReleaseVersion>
1919
<PackageVersion>1.1.1</PackageVersion>
2020
</PropertyGroup>
21+
<PropertyGroup>
22+
<Nullable>enable</Nullable>
23+
<LangVersion>8.0</LangVersion>
24+
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
25+
</PropertyGroup>
2126
<PropertyGroup Condition=" '$(Version)' == '' ">
2227
<VersionPrefix Condition=" '$(VersionPrefix)' == '' ">1.1.1</VersionPrefix>
2328
<Version Condition=" '$(Version)' == '' ">$(VersionPrefix)</Version>
@@ -27,6 +32,6 @@
2732
<PackageReference Include="MimeMapping" Version="1.0.1.37" />
2833
</ItemGroup>
2934
<ItemGroup>
30-
<None Remove="MimeMapping" />
35+
<None Remove="MimeMapping" />
3136
</ItemGroup>
3237
</Project>

0 commit comments

Comments
 (0)