Skip to content

upgraded to .NET 9.0 and some fixes #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
Expand Down
12 changes: 6 additions & 6 deletions src/OLLoader/IOLMyBooksLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IOLMyBooksLoader
/// </summary>
/// <param name="username">The user to get the reading log of. null to use the username of the logged in account.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public Task<(bool, OLMyBooksData?)> TryGetWantToReadAsync(string? username = null);
public Task<(bool, OLMyBooksData?)> TryGetWantToReadAsync(string? username = null, params KeyValuePair<string, string>[] parameters);

/// <summary>
/// Get data about a user's Want-To-Read reading log.
Expand All @@ -24,14 +24,14 @@ public interface IOLMyBooksLoader
/// <exception cref="System.Net.Http.HttpRequestException"></exception>
/// <exception cref="System.Threading.Tasks.TaskCanceledException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
public Task<OLMyBooksData?> GetWantToReadAsync(string? username = null);
public Task<OLMyBooksData?> GetWantToReadAsync(string? username = null, params KeyValuePair<string, string>[] parameters);

/// <summary>
/// Attempt to get data about a user's Currently-Reading reading log.
/// </summary>
/// <param name="username">The user to get the reading log of. null to use the username of the logged in account.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public Task<(bool, OLMyBooksData?)> TryGetCurrentlyReadingAsync(string? username = null);
public Task<(bool, OLMyBooksData?)> TryGetCurrentlyReadingAsync(string? username = null, params KeyValuePair<string, string>[] parameters);

/// <summary>
/// Get data about a user's Currently-Reading reading log.
Expand All @@ -42,14 +42,14 @@ public interface IOLMyBooksLoader
/// <exception cref="System.Net.Http.HttpRequestException"></exception>
/// <exception cref="System.Threading.Tasks.TaskCanceledException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
public Task<OLMyBooksData?> GetCurrentlyReadingAsync(string? username = null);
public Task<OLMyBooksData?> GetCurrentlyReadingAsync(string? username = null, params KeyValuePair<string, string>[] parameters);

/// <summary>
/// Attempt to get data about a user's Already-Read reading log.
/// </summary>
/// <param name="username">The user to get the reading log of. null to use the username of the logged in account.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public Task<(bool, OLMyBooksData?)> TryGetAlreadyReadAsync(string? username = null);
public Task<(bool, OLMyBooksData?)> TryGetAlreadyReadAsync(string? username = null, params KeyValuePair<string, string>[] parameters);

/// <summary>
/// Get data about a user's Already-Read reading log.
Expand All @@ -60,7 +60,7 @@ public interface IOLMyBooksLoader
/// <exception cref="System.Net.Http.HttpRequestException"></exception>
/// <exception cref="System.Threading.Tasks.TaskCanceledException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
public Task<OLMyBooksData?> GetAlreadyReadAsync(string? username = null);
public Task<OLMyBooksData?> GetAlreadyReadAsync(string? username = null, params KeyValuePair<string, string>[] parameters);

/// <summary>
/// Attempt to get data about a user's specified reading log.
Expand Down
49 changes: 25 additions & 24 deletions src/OLLoader/OLMyBooksLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// Attempt to get data about a user's Want-To-Read reading log.
/// </summary>
/// <param name="username">The user to get the reading log of. null to use the username of the logged in account.</param>
/// <param name="parameters"></param>
/// <returns>The task object representing the asynchronous operation.</returns>
public async Task<(bool, OLMyBooksData?)> TryGetWantToReadAsync(string? username = null)
=> await TryGetWantToReadAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username);
public async Task<(bool, OLMyBooksData?)> TryGetWantToReadAsync(string? username = null, params KeyValuePair<string, string>[] parameters)
=> await TryGetWantToReadAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username , parameters);
/// <summary>
/// Get data about a user's Want-To-Read reading log.
/// </summary>
Expand All @@ -29,16 +30,16 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// <exception cref="System.Net.Http.HttpRequestException"></exception>
/// <exception cref="System.Threading.Tasks.TaskCanceledException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
public async Task<OLMyBooksData?> GetWantToReadAsync(string? username = null)
=> await GetWantToReadAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username);
public async Task<OLMyBooksData?> GetWantToReadAsync(string? username = null, params KeyValuePair<string, string>[] parameters)
=> await GetWantToReadAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username,parameters);

/// <summary>
/// Attempt to get data about a user's Currently-Reading reading log.
/// </summary>
/// <param name="username">The user to get the reading log of. null to use the username of the logged in account.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public async Task<(bool, OLMyBooksData?)> TryGetCurrentlyReadingAsync(string? username = null)
=> await TryGetCurrentlyReadingAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username);
public async Task<(bool, OLMyBooksData?)> TryGetCurrentlyReadingAsync(string? username = null, params KeyValuePair<string, string>[] parameters)
=> await TryGetCurrentlyReadingAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username, parameters);
/// <summary>
/// Get data about a user's Currently-Reading reading log.
/// </summary>
Expand All @@ -48,16 +49,16 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// <exception cref="System.Net.Http.HttpRequestException"></exception>
/// <exception cref="System.Threading.Tasks.TaskCanceledException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
public async Task<OLMyBooksData?> GetCurrentlyReadingAsync(string? username = null)
=> await GetCurrentlyReadingAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username);
public async Task<OLMyBooksData?> GetCurrentlyReadingAsync(string? username = null, params KeyValuePair<string, string>[] parameters)
=> await GetCurrentlyReadingAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username, parameters);

/// <summary>
/// Attempt to get data about a user's Already-Read reading log.
/// </summary>
/// <param name="username">The user to get the reading log of. null to use the username of the logged in account.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public async Task<(bool, OLMyBooksData?)> TryGetAlreadyReadAsync(string? username = null)
=> await TryGetAlreadyReadAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username);
public async Task<(bool, OLMyBooksData?)> TryGetAlreadyReadAsync(string? username = null, params KeyValuePair<string, string>[] parameters)
=> await TryGetAlreadyReadAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username, parameters );
/// <summary>
/// Get data about a user's Already-Read reading log.
/// </summary>
Expand All @@ -67,8 +68,8 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// <exception cref="System.Net.Http.HttpRequestException"></exception>
/// <exception cref="System.Threading.Tasks.TaskCanceledException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
public async Task<OLMyBooksData?> GetAlreadyReadAsync(string? username = null)
=> await GetAlreadyReadAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username);
public async Task<OLMyBooksData?> GetAlreadyReadAsync(string? username = null, params KeyValuePair<string, string>[] parameters )
=> await GetAlreadyReadAsync(_client.BackingClient, username == null ? _client.Username?.ToLower()! : username,parameters);

/// <summary>
/// Attempt to get data about a user's specified reading log.
Expand Down Expand Up @@ -131,9 +132,9 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// <param name="client">An HttpClient instance which will be used to make the request.</param>
/// <param name="username">The user to get the reading log of.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public async static Task<(bool, OLMyBooksData?)> TryGetWantToReadAsync(HttpClient client, string username)
public async static Task<(bool, OLMyBooksData?)> TryGetWantToReadAsync(HttpClient client, string username, params KeyValuePair<string, string>[] parameters)
{
try { return (true, await GetWantToReadAsync(client, username)); }
try { return (true, await GetWantToReadAsync(client, username, parameters)); }
catch { return (false, null); }
}

Expand All @@ -147,12 +148,12 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// <exception cref="System.Net.Http.HttpRequestException"></exception>
/// <exception cref="System.Threading.Tasks.TaskCanceledException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
public async static Task<OLMyBooksData?> GetWantToReadAsync(HttpClient client, string username)
public async static Task<OLMyBooksData?> GetWantToReadAsync(HttpClient client, string username, params KeyValuePair<string, string>[] parameters)
{
return await OpenLibraryUtility.LoadAsync<OLMyBooksData?>
(
client,
OpenLibraryUtility.BuildMyBooksUri(username, "want-to-read")
OpenLibraryUtility.BuildMyBooksUri(username, "want-to-read",parameters)
);
}

Expand All @@ -162,9 +163,9 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// <param name="client">An HttpClient instance which will be used to make the request.</param>
/// <param name="username">The user to get the reading log of.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public async static Task<(bool, OLMyBooksData?)> TryGetCurrentlyReadingAsync(HttpClient client, string username)
public async static Task<(bool, OLMyBooksData?)> TryGetCurrentlyReadingAsync(HttpClient client, string username, params KeyValuePair<string, string>[] parameters)
{
try { return (true, await GetCurrentlyReadingAsync(client, username)); }
try { return (true, await GetCurrentlyReadingAsync(client, username,parameters)); }
catch { return (false, null); }
}

Expand All @@ -178,12 +179,12 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// <exception cref="System.Net.Http.HttpRequestException"></exception>
/// <exception cref="System.Threading.Tasks.TaskCanceledException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
public async static Task<OLMyBooksData?> GetCurrentlyReadingAsync(HttpClient client, string username)
public async static Task<OLMyBooksData?> GetCurrentlyReadingAsync(HttpClient client, string username, params KeyValuePair<string, string>[] parameters)
{
return await OpenLibraryUtility.LoadAsync<OLMyBooksData?>
(
client,
OpenLibraryUtility.BuildMyBooksUri(username, "currently-reading")
OpenLibraryUtility.BuildMyBooksUri(username, "currently-reading",parameters)
);
}

Expand All @@ -193,9 +194,9 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// <param name="client">An HttpClient instance which will be used to make the request.</param>
/// <param name="username">The user to get the reading log of.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public async static Task<(bool, OLMyBooksData?)> TryGetAlreadyReadAsync(HttpClient client, string username)
public async static Task<(bool, OLMyBooksData?)> TryGetAlreadyReadAsync(HttpClient client, string username, params KeyValuePair<string, string>[] parameters)
{
try { return (true, await GetAlreadyReadAsync(client, username)); }
try { return (true, await GetAlreadyReadAsync(client, username, parameters)); }
catch { return (false, null); }
}

Expand All @@ -209,12 +210,12 @@ public class OLMyBooksLoader : IOLMyBooksLoader
/// <exception cref="System.Net.Http.HttpRequestException"></exception>
/// <exception cref="System.Threading.Tasks.TaskCanceledException"></exception>
/// <exception cref="System.ArgumentNullException"></exception>
public async static Task<OLMyBooksData?> GetAlreadyReadAsync(HttpClient client, string username)
public async static Task<OLMyBooksData?> GetAlreadyReadAsync(HttpClient client, string username, params KeyValuePair<string, string>[] parameters)
{
return await OpenLibraryUtility.LoadAsync<OLMyBooksData?>
(
client,
OpenLibraryUtility.BuildMyBooksUri(username, "already-read")
OpenLibraryUtility.BuildMyBooksUri(username, "already-read", parameters)
);
}

Expand Down
46 changes: 42 additions & 4 deletions src/OpenLibraryClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Formats.Asn1;
using System;
using System.Formats.Asn1;
using System.Net;
using System.Net.Http.Json;
using System.Text.RegularExpressions;
Expand All @@ -10,13 +11,19 @@

namespace OpenLibraryNET
{



/// <summary>
/// The main interface to OpenLibrary.<br/>
/// Instantiates HttpClient internally, as OpenLibraryClient uses cookies.<br/>
/// To reuse the backing HttpClient, reuse the OpenLibrary instance.
/// </summary>
public class OpenLibraryClient : IOpenLibraryClient
{



///<inheritdoc/>
public bool LoggedIn => _httpHandler.CookieContainer.GetCookies(OpenLibraryUtility.BaseUri).SingleOrDefault(cookie => cookie.Name == "session") != null;
///<inheritdoc/>
Expand Down Expand Up @@ -46,15 +53,46 @@ public class OpenLibraryClient : IOpenLibraryClient
///<inheritdoc/>
public HttpClient BackingClient => _httpClient;

private class OperationKeyHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
ResilienceContext context = ResilienceContextPool.Shared.Get(cancellationToken);
string uri = request.RequestUri.ToString();
ResilienceContextExtensions.SetRequestMetadata(context, new RequestMetadata("OperationKey", uri, "Additional information if needed"));
return await base.SendAsync(request, cancellationToken);
}
}

private const string httpClientName = "OpenLibraryNET.OpenLibraryClient";

private readonly CookieContainer? _cookieContainer = new CookieContainer();

private readonly IHttpClientFactory _httpClientFactory;

/// <summary>
/// Create a new instance of the OpenLibraryClient.<br/>
/// Instantiates HttpClient internally, as OpenLibraryClient uses cookies.<br/>
/// To reuse the backing HttpClient, reuse the OpenLibrary instance.
/// </summary>
public OpenLibraryClient()
public OpenLibraryClient(Action<HttpStandardResilienceOptions>? configureOptions = null, Action<ILoggingBuilder>? logBuilder = null)
{
_httpHandler = new HttpClientHandler() { AllowAutoRedirect = true, UseCookies = true };
_httpClient = new HttpClient(_httpHandler);
ServiceCollection services = new ServiceCollection();
_httpHandler = new HttpClientHandler
{
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = _cookieContainer
};
services.AddHttpClient(httpClientName).ConfigurePrimaryHttpMessageHandler(() => _httpHandler).AddHttpMessageHandler<OperationKeyHandler>()
.AddStandardResilienceHandler(configureOptions);
if (logBuilder != null)
{
services.AddLogging(logBuilder);
}
ServiceProvider provider = services.BuildServiceProvider();
_httpClientFactory = provider.GetService<IHttpClientFactory>();
_httpClient = _httpClientFactory.CreateClient(httpClientName);// +"-standard");

_work = new OLWorkLoader(_httpClient);
_author = new OLAuthorLoader(_httpClient);
Expand Down
8 changes: 4 additions & 4 deletions src/OpenLibraryNET.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down Expand Up @@ -41,14 +41,14 @@
</ItemGroup>

<ItemGroup>
<Analyzer Include="$(CodeGen)OpenLibraryNET.Diagnostics\bin\$(Configuration)\netstandard2.0\OpenLibraryNET.Diagnostics.dll" />
<Analyzer Include="$(CodeGen)OpenLibraryNET.Diagnostics\bin\$(Configuration)\$(TargetFramework)\OpenLibraryNET.Diagnostics.dll" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="Build">
<PropertyGroup>
<BuildCommand>"$(CodeGen)OpenLibraryNET.RemoveGeneratorAttributes\bin\$(Configuration)\net6.0\OpenLibraryNET.RemoveGeneratorAttributes.exe"</BuildCommand>
<BuildCommand>"$(CodeGen)OpenLibraryNET.RemoveGeneratorAttributes\bin\$(Configuration)\$(TargetFramework)\OpenLibraryNET.RemoveGeneratorAttributes.exe"</BuildCommand>
<Argument1>"$(TargetPath)"</Argument1>
<Argument2>"$(CodeGen)OpenLibraryNET.GeneratorAttributes\bin\$(Configuration)\netstandard2.0\OpenLibraryNET.GeneratorAttributes.dll"</Argument2>
<Argument2>"$(CodeGen)OpenLibraryNET.GeneratorAttributes\bin\$(Configuration)\$(TargetFramework)\OpenLibraryNET.GeneratorAttributes.dll"</Argument2>
</PropertyGroup>
<Exec Command="$(BuildCommand) $(Argument1) $(Argument2)" ConsoleToMSBuild="true" LogStandardErrorAsError="true" WorkingDirectory="$(ProjectDir)" />

Expand Down
Loading