Skip to content

Commit

Permalink
Fixing error throwing capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
holycardboard committed Sep 24, 2023
1 parent d77766b commit 482cde8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/MangaDexSharp.Cli/Program.cs
Expand Up @@ -12,7 +12,7 @@
.WriteTo.Console()
.CreateLogger());
});
});
}, throwOnError: true);

var chapter = await api.Chapter.Get("158f54ed-9983-406d-b882-208858288874");
Console.WriteLine("Chapter: " + chapter.Data.Attributes.Title);
Expand Down
34 changes: 28 additions & 6 deletions src/MangaDexSharp/CredentialsService.cs
Expand Up @@ -20,6 +20,11 @@ public interface ICredentialsService
/// </summary>
/// <returns>The user's authentication token</returns>
Task<string?> GetToken();

/// <summary>
/// Whether or not to throw an exception if the API returns an error
/// </summary>
bool ThrowOnError { get; }
}

/// <summary>
Expand All @@ -44,6 +49,11 @@ public class ConfigurationCredentialsService : ICredentialsService
/// </summary>
public static string UserAgentPath { get; set; } = "Mangadex:UserAgent";

/// <summary>
/// Where to fetch the ThrowOnError flag from in the config file
/// </summary>
public static string ErrorThrownPath { get; set; } = "Mangadex:ThrowOnError";

/// <summary>
/// The authentication token from the configuration file
/// </summary>
Expand All @@ -59,11 +69,16 @@ public class ConfigurationCredentialsService : ICredentialsService
/// </summary>
public string UserAgent => _config[UserAgentPath] ?? API_USER_AGENT;

/// <summary>
/// Represents a provider that fetches the <see cref="ICredentialsService"/> from the configuration
/// </summary>
/// <param name="config">The <see cref="IConfiguration"/> object to fetch the variables from</param>
public ConfigurationCredentialsService(IConfiguration config)
/// <summary>
/// Whether or not to throw an exception if the API returns an error
/// </summary>
public bool ThrowOnError => _config[ErrorThrownPath] == "true";

/// <summary>
/// Represents a provider that fetches the <see cref="ICredentialsService"/> from the configuration
/// </summary>
/// <param name="config">The <see cref="IConfiguration"/> object to fetch the variables from</param>
public ConfigurationCredentialsService(IConfiguration config)
{
_config = config;
}
Expand Down Expand Up @@ -98,17 +113,24 @@ public class HardCodedCredentialsService : ICredentialsService
/// </summary>
public string UserAgent { get; set; }

/// <summary>
/// Whether or not to throw an exception if the API returns an error
/// </summary>
public bool ThrowOnError { get; set; }

/// <summary>
/// Represents a provider that stores the credentials in-memory
/// </summary>
/// <param name="token">The user's authentication token</param>
/// <param name="apiUrl">The API url</param>
/// <param name="userAgent">The User-Agent header to send with requests</param>
public HardCodedCredentialsService(string? token = null, string? apiUrl = null, string? userAgent = null)
/// <param name="throwOnError">Whether or not to throw an exception if the API returns an error</param>
public HardCodedCredentialsService(string? token = null, string? apiUrl = null, string? userAgent = null, bool throwOnError = false)
{
UserAgent = userAgent ?? API_USER_AGENT;
Token = token;
ApiUrl = apiUrl ?? API_ROOT;
ThrowOnError = throwOnError;
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/MangaDexSharp/Extensions.cs
Expand Up @@ -150,11 +150,12 @@ public static IServiceCollection AddMangaDex(this IServiceCollection services)
/// <param name="token">The user's authentication token</param>
/// <param name="apiUrl">The API url for the MangaDex environment (see <see cref="API_ROOT"/> or <see cref="API_DEV_ROOT"/>)</param>
/// <param name="userAgent">The User-Agent header to send with requests (see <see cref="API_USER_AGENT"/>)</param>
/// <param name="throwOnError">Whether or not to throw an exception if the API returns an error</param>
/// <returns>The service collection for chaining</returns>
public static IServiceCollection AddMangaDex(this IServiceCollection services, string token, string? apiUrl = null, string? userAgent = null)
public static IServiceCollection AddMangaDex(this IServiceCollection services, string token, string? apiUrl = null, string? userAgent = null, bool throwOnError = false)
{
return services
.AddMangaDex(new HardCodedCredentialsService(token, apiUrl, userAgent));
.AddMangaDex(new HardCodedCredentialsService(token, apiUrl, userAgent, throwOnError));
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion src/MangaDexSharp/Helpers/MdApiService.cs
Expand Up @@ -83,11 +83,18 @@ public string WrapUrl(string url)
/// <returns>The instance of the <see cref="IHttpBuilder"/></returns>
public override IHttpBuilder Create(string url, IJsonService json, string method = "GET")
{
return base
var res = base
.Create(WrapUrl(url), json, method)
.With(t =>
{
t.Headers.Add("User-Agent", _creds.UserAgent);
});

if (_creds.ThrowOnError)
res.FailWithThrow();
else
res.FailGracefully();

return res;
}
}
5 changes: 3 additions & 2 deletions src/MangaDexSharp/MangaDex.cs
Expand Up @@ -236,11 +236,12 @@ public class MangaDex : IMangaDex
/// <param name="apiUrl">The optional api URL</param>
/// <param name="config">The optional configuration action</param>
/// <param name="userAgent">The User-Agent header to send with requests (see <see cref="API_USER_AGENT"/>)</param>
/// <param name="throwOnError">Whether or not to throw an exception if the API returns an error</param>
/// <returns>The instance of the MangaDex API</returns>
public static IMangaDex Create(string? token = null, string? apiUrl = null, Action<IServiceCollection>? config = null, string? userAgent = null)
public static IMangaDex Create(string? token = null, string? apiUrl = null, Action<IServiceCollection>? config = null, string? userAgent = null, bool throwOnError = false)
{
var create = new ServiceCollection()
.AddMangaDex(token ?? string.Empty, apiUrl, userAgent)
.AddMangaDex(token ?? string.Empty, apiUrl, userAgent, throwOnError)
.AddCardboardHttp()
.AddJson();

Expand Down
4 changes: 2 additions & 2 deletions src/MangaDexSharp/MangaDexSharp.csproj
Expand Up @@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/calico-crusade/mangadex-sharp</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>Manga;Mangadex;Anime;Cardboard;mangadex-sharp;</PackageTags>
<Version>1.0.17</Version>
<Version>1.0.18</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -35,7 +35,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CardboardBox.Http" Version="2.0.0" />
<PackageReference Include="CardboardBox.Http" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/MangaDexSharp/Models/Base/MangaDexRoot.cs
Expand Up @@ -35,6 +35,12 @@ public class MangaDexRoot<T> : MangaDexRoot where T : new()
/// </summary>
[JsonPropertyName("data")]
public T Data { get; set; } = new();

/// <summary>
/// This is a shorthand for <see cref="MangaDexRoot.Result"/> == "error"
/// </summary>
[JsonIgnore]
public bool ErrorOccurred => Result.ToLower().Trim() == "error";
}

/// <summary>
Expand Down

0 comments on commit 482cde8

Please sign in to comment.