Skip to content

Commit

Permalink
Refactor BunnyCDNStorage, apply BunnyWay#18 and BunnyWay#22 recommend…
Browse files Browse the repository at this point in the history
…ations where possible
  • Loading branch information
OhSoooLucky committed Mar 6, 2023
1 parent 5341d2c commit 12960f1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
10 changes: 5 additions & 5 deletions BunnyCDN.Net.Storage.Tests/BunnyCDN.Net.Storage.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 41 additions & 13 deletions BunnyCDN.Net.Storage/BunnyCDNStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@

namespace BunnyCDN.Net.Storage
{
public class BunnyCDNStorage
public class BunnyCDNStorage : IDisposable
{
/// <summary>
/// The API access key used for authentication
/// </summary>
public string ApiAccessKey { get; private set; }
#if !NETSTANDARD
private string ApiAccessKey { get; init; }
#else
private string ApiAccessKey { get; set; }
#endif

/// <summary>
/// The name of the storage zone we are working on
/// </summary>
public string StorageZoneName { get; private set; }
#if !NETSTANDARD
private string StorageZoneName { get; init; }
#else
private string StorageZoneName { get; set; }
#endif

/// <summary>
/// The HTTP Client used for the API communication
/// </summary>
private HttpClient _http = null;
private bool _disposed = false;

/// <summary>
/// Initializes a new instance of the BunnyCDNStorage class
Expand All @@ -42,7 +51,7 @@ public BunnyCDNStorage(string storageZoneName, string apiAccessKey, string mainR
_http.BaseAddress = new Uri(this.GetBaseAddress(mainReplicationRegion));
}

#region Delete
#region Delete
/// <summary>
/// Delete an object at the given path. If the object is a directory, the contents will also be deleted.
/// </summary>
Expand All @@ -63,7 +72,7 @@ public async Task<bool> DeleteObjectAsync(string path)
}
#endregion

#region List
#region List
/// <summary>
/// Get the list of storage objects on the given path
/// </summary>
Expand All @@ -84,9 +93,9 @@ public async Task<List<StorageObject>> GetStorageObjectsAsync(string path)
throw this.MapResponseToException(response.StatusCode, normalizedPath);
}
}
#endregion
#endregion

#region Upload
#region Upload
/// <summary>
/// Upload an object from a stream (missing path will be created)
/// </summary>
Expand Down Expand Up @@ -171,9 +180,9 @@ public async Task UploadAsync(string localFilePath, string path, bool validateCh
await UploadAsync(fileStream, path, validateChecksum, sha256Checksum, contentTypeOverride);
}
}
#endregion
#endregion

#region Download
#region Download
/// <summary>
/// Download the object to a local file
/// </summary>
Expand Down Expand Up @@ -220,9 +229,27 @@ public async Task<Stream> DownloadObjectAsStreamAsync(string path)
throw this.MapResponseToException((HttpStatusCode)(int)ex.Status, path);
}
}
#endregion
#endregion

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

#region Utils
private void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_http.Dispose();
}
_disposed = true;
}
}

#region Utils
/// <summary>
/// Map the API response to the correct BunnyCDNStorageExecption
/// </summary>
Expand All @@ -245,7 +272,7 @@ private BunnyCDNStorageException MapResponseToException(HttpStatusCode statusCod
/// Normalize a path string
/// </summary>
/// <returns>Recognizable, valid string for use against API calls</returns>
public string NormalizePath(string path, bool? isDirectory = null)
internal string NormalizePath(string path, bool? isDirectory = null)
{
// Trim all prepending & tailing whitespace, fix windows-like paths then remove prepending slashes
path = path.Trim()
Expand All @@ -268,7 +295,7 @@ public string NormalizePath(string path, bool? isDirectory = null)

return path;
}
#endregion


/// <summary>
/// Get the base HTTP URL address of the storage endpoint
Expand All @@ -284,5 +311,6 @@ private string GetBaseAddress(string mainReplicationRegion)

return $"https://{mainReplicationRegion}.storage.bunnycdn.com/";
}
#endregion
}
}

0 comments on commit 12960f1

Please sign in to comment.