Skip to content

Commit

Permalink
feat: update dotnet version, added xml documentation generation, Evit…
Browse files Browse the repository at this point in the history
…aClient async non-blocking startup by calling static constructor, refactored tests
  • Loading branch information
Khertys committed Dec 19, 2023
1 parent 0f4f491 commit fc92126
Show file tree
Hide file tree
Showing 18 changed files with 216 additions and 165 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ csharp_preserve_single_line_blocks = true

resharper_co_variant_array_conversion_highlighting = none

# Suppress docs warnings
dotnet_diagnostic.CS1591.severity = none
dotnet_diagnostic.CS1574.severity = none
dotnet_diagnostic.CS0419.severity = none
dotnet_diagnostic.CS1580.severity = none
dotnet_diagnostic.CS1711.severity = none

[*.proto]
indent_style = space
indent_size = 2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dev-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Setup dotnet
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.X' # setup dotnet 7
dotnet-version: '8.0.X' # setup dotnet 8

- name: Restore solution
run: dotnet restore ./EvitaDB.sln
Expand Down
44 changes: 22 additions & 22 deletions EvitaDB.Client/Certificate/ClientCertificateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@ public class ClientCertificateManager

private ClientCertificateManager(string clientCertificateFolderPath, string? clientCertificatePath,
string? clientCertificateKeyPath, string? clientCertificateKeyPassword, bool useGeneratedCertificate,
bool trustedServerCertificate, string host, int port)
bool trustedServerCertificate)
{
string certificateDirectory;
if (useGeneratedCertificate)
{
certificateDirectory = GetCertificatesFromServer(host, port, clientCertificateFolderPath).GetAwaiter().GetResult();
}
else
{
certificateDirectory = IdentifyServerDirectory(host, port, clientCertificateFolderPath).GetAwaiter().GetResult();
}

ClientCertificateFolderPath = certificateDirectory;
ClientCertificateFolderPath = clientCertificateFolderPath;
ClientCertificatePath = clientCertificatePath;
ClientCertificateKeyPath = clientCertificateKeyPath;
ClientCertificateKeyPassword = clientCertificateKeyPassword;
UseGeneratedCertificate = useGeneratedCertificate;
TrustedServerCertificate = trustedServerCertificate;
}

private static async ValueTask<string> GetServerDirectoryPath(string host, int port, string clientCertificateFolderPath, bool useGeneratedCertificate)
{
if (useGeneratedCertificate)
{
return await GetCertificatesFromServer(host, port, clientCertificateFolderPath);
}
return await IdentifyServerDirectory(host, port, clientCertificateFolderPath);
}

public class Builder
{
private string ClientCertificateFolderPath { get; set; } = DefaultClientCertificateFolderPath;
Expand All @@ -50,11 +49,12 @@ public class Builder
private string? Host { get; set; }
private int Port { get; set; }

public ClientCertificateManager Build()
public async Task<ClientCertificateManager> Build()
{
return new ClientCertificateManager(ClientCertificateFolderPath, ClientCertificatePath,
string certificatePath = await GetServerDirectoryPath(Host!, Port, ClientCertificateFolderPath, UseGeneratedCertificate);
return new ClientCertificateManager(certificatePath, ClientCertificatePath,
ClientCertificateKeyPath, ClientCertificateKeyPassword, UseGeneratedCertificate,
TrustedServerCertificate, Host!, Port);
TrustedServerCertificate);
}

public Builder SetClientCertificateFolderPath(string? clientCertificateFolderPath)
Expand Down Expand Up @@ -96,7 +96,7 @@ public Builder SetTrustedServerCertificate(bool trustedServerCertificate)
}
}

private async Task<string> GetCertificatesFromServer(string host, int systemApiPort,
private static async Task<string> GetCertificatesFromServer(string host, int systemApiPort,
string certificateClientFolderPath)
{
string apiEndpoint = $"http://{host}:{systemApiPort}/system/";
Expand All @@ -121,9 +121,9 @@ private async Task<string> GetCertificatesFromServer(string host, int systemApiP

try
{
DownloadFile(apiEndpoint, serverSpecificDirectory, CertificateUtils.GeneratedCertificateFileName);
DownloadFile(apiEndpoint, serverSpecificDirectory, CertificateUtils.GeneratedClientCertificateFileName);
DownloadFile(apiEndpoint, serverSpecificDirectory, CertificateUtils.GeneratedClientCertificateKeyFileName);
await DownloadFile(apiEndpoint, serverSpecificDirectory, CertificateUtils.GeneratedCertificateFileName);
await DownloadFile(apiEndpoint, serverSpecificDirectory, CertificateUtils.GeneratedClientCertificateFileName);
await DownloadFile(apiEndpoint, serverSpecificDirectory, CertificateUtils.GeneratedClientCertificateKeyFileName);

return serverSpecificDirectory;
}
Expand All @@ -134,14 +134,14 @@ private async Task<string> GetCertificatesFromServer(string host, int systemApiP
}
}

private void DownloadFile(string apiEndpoint, string baseDir, string fileName)
private static async Task DownloadFile(string apiEndpoint, string baseDir, string fileName)
{
using var client = new HttpClient();
var response = client.GetAsync(apiEndpoint + fileName).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
using Stream contentStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult(),
await using Stream contentStream = await response.Content.ReadAsStreamAsync(),
stream = new FileStream(Path.Combine(baseDir, fileName), FileMode.Create);
contentStream.CopyTo(stream);
await contentStream.CopyToAsync(stream);
}

private static async Task<string> IdentifyServerDirectory(string host, int port, string certificateClientFolderPath)
Expand Down
47 changes: 25 additions & 22 deletions EvitaDB.Client/EvitaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,25 @@ public partial class EvitaClient : IClientContext, IDisposable
private static readonly ISchemaMutationConverter<ITopLevelCatalogSchemaMutation, GrpcTopLevelCatalogSchemaMutation>
CatalogSchemaMutationConverter = new DelegatingTopLevelCatalogSchemaMutationConverter();

private readonly ChannelPool _channelPool;
private readonly ChannelInvoker _cdcChannel;
private readonly ChannelPool? _channelPool;
private readonly ChannelInvoker? _cdcChannel;

private static int _active = 1;

private readonly ConcurrentDictionary<Guid, EvitaClientSession> _activeSessions = new();
private readonly ConcurrentDictionary<string, EvitaEntitySchemaCache> _entitySchemaCache = new();

private readonly Action _terminationCallback;
private readonly Action? _terminationCallback;

public EvitaClientConfiguration Configuration { get; }

private static readonly Regex ErrorMessagePattern = MyRegex();

public EvitaClient(EvitaClientConfiguration configuration)
private EvitaClient(EvitaClientConfiguration configuration, ClientCertificateManager certificateManager)
{
Configuration = configuration;
ClientCertificateManager certificateManager = new ClientCertificateManager.Builder()
.SetClientCertificateFolderPath(configuration.CertificateFolderPath)
.SetClientCertificatePath(configuration.CertificateFileName)
.SetClientCertificateKeyPath(configuration.CertificateKeyFileName)
.SetClientCertificateKeyPassword(configuration.CertificateKeyPassword)
.SetUseGeneratedCertificate(configuration.UseGeneratedCertificate, configuration.Host,
configuration.SystemApiPort)
.SetTrustedServerCertificate(configuration.UsingTrustedRootCaCertificate)
.Build();

ChannelBuilder channelBuilder = new ChannelBuilder(
configuration.Host,
configuration.Port,
Configuration.Host,
Configuration.Port,
certificateManager.BuildHttpClientHandler(),
new ClientInterceptor(this)
);
Expand All @@ -95,6 +84,20 @@ void TerminationCallback()
_terminationCallback = TerminationCallback;
}

public static async Task<EvitaClient> Create(EvitaClientConfiguration configuration)
{
ClientCertificateManager certificateManager = await new ClientCertificateManager.Builder()
.SetClientCertificateFolderPath(configuration.CertificateFolderPath)
.SetClientCertificatePath(configuration.CertificateFileName)
.SetClientCertificateKeyPath(configuration.CertificateKeyFileName)
.SetClientCertificateKeyPassword(configuration.CertificateKeyPassword)
.SetUseGeneratedCertificate(configuration.UseGeneratedCertificate, configuration.Host,
configuration.SystemApiPort)
.SetTrustedServerCertificate(configuration.UsingTrustedRootCaCertificate)
.Build();
return new EvitaClient(configuration, certificateManager);
}

/// <summary>
/// This method is used for registering a callback that is invoked any system event, like catalog creation or its
/// top level mutation occurs.
Expand Down Expand Up @@ -422,8 +425,8 @@ public void Close()
{
_activeSessions.Values.ToList().ForEach(session => session.Close());
_activeSessions.Clear();
_channelPool.Shutdown();
_terminationCallback.Invoke();
_channelPool?.Shutdown();
_terminationCallback?.Invoke();
}
}

Expand All @@ -438,7 +441,7 @@ public void Dispose()
private T ExecuteWithBlockingEvitaService<T>(Func<EvitaService.EvitaServiceClient, T> logic)
{
return ExecuteWithEvitaService(
new PooledChannelSupplier(_channelPool),
new PooledChannelSupplier(_channelPool!),
channel => new EvitaService.EvitaServiceClient(channel.Channel),
logic
);
Expand All @@ -447,7 +450,7 @@ private T ExecuteWithBlockingEvitaService<T>(Func<EvitaService.EvitaServiceClien
private T ExecuteWithStreamingEvitaService<T>(Func<EvitaService.EvitaServiceClient, T> logic)
{
return ExecuteWithEvitaService(
new SharedChannelSupplier(_cdcChannel),
new SharedChannelSupplier(_cdcChannel!),
channel => new EvitaService.EvitaServiceClient(channel.Channel),
logic
);
Expand Down Expand Up @@ -556,7 +559,7 @@ public EvitaClientSession CreateSession(SessionTraits traits)
EvitaClientSession session = new EvitaClientSession(
this,
_entitySchemaCache.GetOrAdd(traits.CatalogName, new EvitaEntitySchemaCache(traits.CatalogName)),
_channelPool,
_channelPool!,
traits.CatalogName,
Enum.Parse<CatalogState>(grpcResponse.CatalogState.ToString()),
Guid.Parse(grpcResponse.SessionId),
Expand Down
4 changes: 3 additions & 1 deletion EvitaDB.Client/EvitaDB.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -17,6 +17,8 @@
<PackageReleaseNotes>Initial prerelease version.</PackageReleaseNotes>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion EvitaDB.Client/Queries/Filter/And.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

/// <summary>
/// The `and` container represents a <a href="https://en.wikipedia.org/wiki/Logical_conjunction">logical conjunction</a>.

/// The following query:
/// <code>
/// query(
Expand Down
2 changes: 1 addition & 1 deletion EvitaDB.Client/Queries/Filter/AttributeSpecialValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/// <summary>
/// Represents constant or "special" value attribute can have (or has it implicitly, e.g. missing value is represented by
/// `<see cref="null"/>` that is not comparable by another ways.
/// `null` value that is not comparable by another ways.
///
/// </summary>
/// <seealso cref="AttributeIs"/>
Expand Down
3 changes: 2 additions & 1 deletion EvitaDB.QueryValidator/EvitaDB.QueryValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<TargetName>Validator</TargetName>
<AssemblyName>Validator</AssemblyName>
<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit fc92126

Please sign in to comment.