Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/CatBox.NET/Attributes/ApiValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Diagnostics.CodeAnalysis;

namespace CatBox.NET.Client.Attributes;

[AttributeUsage(AttributeTargets.Field)]
public sealed class ApiValueAttribute : Attribute
{
/// <summary>
/// Specifies the default value for the <see cref='System.ComponentModel.DescriptionAttribute'/>,
/// which is an empty string (""). This <see langword='static'/> field is read-only.
/// </summary>
public static readonly ApiValueAttribute Default = new(string.Empty);

/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.DescriptionAttribute'/> class.
/// </summary>
/// <param name="apiValue">Represents the CatBox API verb or parameter value</param>
public ApiValueAttribute(string apiValue)
{
ApiValue = apiValue;
}

/// <summary>
/// Read/Write property that directly modifies the string stored in the description
/// attribute. The default implementation of the <see cref="ApiValue"/> property
/// simply returns this value.
/// </summary>
public string ApiValue { get; set; }

public override bool Equals([NotNullWhen(true)] object? obj) =>
obj is ApiValueAttribute other && other.ApiValue == ApiValue;

public override int GetHashCode() => ApiValue?.GetHashCode() ?? 0;

public override bool IsDefaultAttribute() => Equals(Default);
}
19 changes: 10 additions & 9 deletions src/CatBox.NET/CatBox.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>11</LangVersion>
<Version>0.3.0</Version>
<Version>0.4.0</Version>
<Authors>Chase Redmon, Kuinox, Adam Sears</Authors>
<Description>CatBox.NET is a .NET Library for uploading files, URLs, and modifying albums on CatBox.moe</Description>
<PackageProjectUrl>https://github.com/ChaseDRedmon/CatBox.NET</PackageProjectUrl>
<RepositoryUrl>https://github.com/ChaseDRedmon/CatBox.NET</RepositoryUrl>
<RepositoryType>Library</RepositoryType>
<PackageTags>Catbox, Catbox.moe, Imgur</PackageTags>
<PackageTags>Catbox, Catbox.moe, Imgur, GfyCat</PackageTags>
<Copyright>Copyright © 2023 Chase Redmon</Copyright>
<PackageLicenseUrl>https://github.com/ChaseDRedmon/CatBox.NET/blob/main/license.txt</PackageLicenseUrl>
<PackageReleaseNotes>Fix required description field on create album endpoint. Description is optional when creating an endpoint. </PackageReleaseNotes>
<PackageReleaseNotes>Fix required description field on create album endpoint. Description is optional when creating an endpoint.</PackageReleaseNotes>
<TargetFrameworks>net7.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="PolySharp" Version="1.13.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="AnyOf" Version="0.3.0"/>
<PackageReference Include="BetterEnums" Version="1.0.2" PrivateAssets="all"/>
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0"/>
<PackageReference Include="PolySharp" Version="1.13.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
16 changes: 12 additions & 4 deletions src/CatBox.NET/CatBoxServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CatBox.NET.Client;
using CatBox.NET.Exceptions;
using Microsoft.Extensions.DependencyInjection;

namespace CatBox.NET;
Expand All @@ -11,18 +12,25 @@ public static class CatBoxServices
/// <param name="services">Service Collection</param>
/// <param name="setupAction">Configure the URL to upload files too</param>
/// <returns>Service Collection</returns>
public static IServiceCollection AddCatBoxServices(this IServiceCollection services, Action<CatBoxConfig> setupAction)
public static IServiceCollection AddCatBoxServices(this IServiceCollection services, Action<CatboxOptions> setupAction)
{
services
.Configure(setupAction)
.AddScoped<ExceptionHandler>()
.AddScoped<ICatBox, Catbox>()
.AddScoped<ILitterboxClient, LitterboxClient>()
.AddScoped<ICatBoxClient, CatBoxClient>()
.AddScoped<ILitterboxClient, LitterboxClient>()
.AddHttpClient<ICatBoxClient, CatBoxClient>();
.AddHttpClientWithMessageHandler<ICatBoxClient, CatBoxClient>(static _ => new ExceptionHandler())
.AddHttpClientWithMessageHandler<ILitterboxClient, LitterboxClient>(static _ => new ExceptionHandler());

services.AddHttpClient<ILitterboxClient, LitterboxClient>();
return services;
}

private static IServiceCollection AddHttpClientWithMessageHandler<TInterface, TImplementation>(this IServiceCollection services, Func<IServiceProvider, DelegatingHandler> configureClient)
where TInterface : class
where TImplementation : class, TInterface
{
services.AddHttpClient<TInterface, TImplementation>().AddHttpMessageHandler(configureClient);
return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Configuration object for storing URLs to the API
/// </summary>
public record CatBoxConfig
public record CatboxOptions
{
/// <summary>
/// URL for the catbox.moe domain
Expand Down
53 changes: 0 additions & 53 deletions src/CatBox.NET/Client/CatBox.cs

This file was deleted.

63 changes: 63 additions & 0 deletions src/CatBox.NET/Client/CatBox/CatBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using CatBox.NET.Requests.CatBox;

namespace CatBox.NET.Client;

/// <inheritdoc/>
public sealed class Catbox : ICatBox
{
private readonly ICatBoxClient _client;

/// <summary>
/// Instantiate a new catbox class
/// </summary>
/// <param name="client">The CatBox Api Client (<see cref="ICatBoxClient"/>)</param>
public Catbox(ICatBoxClient client)
{
_client = client;
}

/// <inheritdoc/>
public async Task<string?> CreateAlbumFromFiles(CreateAlbumRequest requestFromFiles, CancellationToken ct = default)
{
var enumerable = Upload(requestFromFiles, ct);

var createAlbumRequest = new RemoteCreateAlbumRequest
{
Title = requestFromFiles.Title,
Description = requestFromFiles.Description,
UserHash = requestFromFiles.UserHash,
Files = enumerable.ToBlockingEnumerable(cancellationToken: ct)
};

return await _client.CreateAlbum(createAlbumRequest, ct);
}

/// <inheritdoc/>
public async Task<string?> UploadImagesToAlbum(UploadToAlbumRequest request, CancellationToken ct = default)
{
var requestType = request.Request;
var userHash = request.UserHash;
var albumId = request.AlbumId;

var enumerable = Upload(request, ct);

return await _client.ModifyAlbum(new ModifyAlbumImagesRequest
{
Request = requestType,
UserHash = userHash,
AlbumId = albumId,
Files = enumerable.ToBlockingEnumerable()
}, ct);
}

private IAsyncEnumerable<string?> Upload(IAlbumUploadRequest request, CancellationToken ct = default)
{
return request.UploadRequest switch
{
{ IsFirst: true } => _client.UploadFiles(request.UploadRequest, ct),
{ IsSecond: true } => _client.UploadFilesAsStream(request.UploadRequest.Second, ct),
{ IsThird: true } => _client.UploadFilesAsUrl(request.UploadRequest, ct),
_ => throw new InvalidOperationException("Invalid request type")
};
}
}
Loading