-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Api): Added a new IWorkflowInstanceApiClient interface and defaul…
…t implementation
- Loading branch information
Showing
16 changed files
with
227 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/api/Synapse.Api.Client.Core/Services/IWorkflowInstanceApiClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright © 2024-Present The Synapse Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"), | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Neuroglia.Data.Infrastructure; | ||
|
||
namespace Synapse.Api.Client.Services; | ||
|
||
/// <summary> | ||
/// Defines the fundamentals of a service used to manage <see cref="WorkflowInstance"/>s using the Synapse API | ||
/// </summary> | ||
public interface IWorkflowInstanceApiClient | ||
: INamespacedResourceApiClient<WorkflowInstance> | ||
{ | ||
|
||
/// <summary> | ||
/// Reads the logs of the specified <see cref="WorkflowInstance"/> | ||
/// </summary> | ||
/// <param name="name">The name of the <see cref="WorkflowInstance"/> to read the logs of</param> | ||
/// <param name="namespace">The namespace the <see cref="WorkflowInstance"/> to read the logs of belongs to</param> | ||
/// <param name="cancellationToken">A <see cref="CancellationToken"/></param> | ||
/// <returns>The logs of the specified <see cref="WorkflowInstance"/></returns> | ||
Task<string> ReadLogsAsync(string name, string @namespace, CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Watches the logs of the specified <see cref="WorkflowInstance"/> | ||
/// </summary> | ||
/// <param name="name">The name of the <see cref="WorkflowInstance"/> to watch the logs of</param> | ||
/// <param name="namespace">The namespace the <see cref="WorkflowInstance"/> to watch the logs of belongs to</param> | ||
/// <param name="cancellationToken">A <see cref="CancellationToken"/></param> | ||
/// <returns>A new <see cref="IAsyncEnumerable{T}"/> used to watch the logs of the specified <see cref="WorkflowInstance"/></returns> | ||
Task<IAsyncEnumerable<ITextDocumentWatchEvent>> WatchLogsAsync(string name, string @namespace, CancellationToken cancellationToken = default); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/api/Synapse.Api.Client.Http/Services/WorkflowInstanceHttpApiClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright © 2024-Present The Synapse Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"), | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Neuroglia.Data.Infrastructure; | ||
|
||
namespace Synapse.Api.Client.Services; | ||
|
||
/// <summary> | ||
/// Represents the default HTTP implementation of the <see cref="IWorkflowInstanceApiClient"/> interface | ||
/// </summary> | ||
/// <inheritdoc/> | ||
public class WorkflowInstanceHttpApiClient(IServiceProvider serviceProvider, ILogger<ResourceHttpApiClient<WorkflowInstance>> logger, IOptions<SynapseHttpApiClientOptions> options, IJsonSerializer jsonSerializer, HttpClient httpClient) | ||
: ResourceHttpApiClient<WorkflowInstance>(serviceProvider, logger, options, jsonSerializer, httpClient), IWorkflowInstanceApiClient | ||
{ | ||
|
||
/// <inheritdoc/> | ||
public virtual async Task<string> ReadLogsAsync(string name, string @namespace, CancellationToken cancellationToken = default) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(name); | ||
ArgumentException.ThrowIfNullOrWhiteSpace(@namespace); | ||
var resource = new WorkflowInstance(); | ||
var uri = $"/api/{resource.Definition.Version}/{resource.Definition.Plural}/{@namespace}/{name}/logs"; | ||
using var request = await this.ProcessRequestAsync(new HttpRequestMessage(HttpMethod.Get, uri), cancellationToken).ConfigureAwait(false); | ||
using var response = await this.ProcessResponseAsync(await this.HttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false); | ||
return await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual async Task<IAsyncEnumerable<ITextDocumentWatchEvent>> WatchLogsAsync(string name, string @namespace, CancellationToken cancellationToken = default) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(name); | ||
ArgumentException.ThrowIfNullOrWhiteSpace(@namespace); | ||
var resource = new WorkflowInstance(); | ||
var uri = $"/api/{resource.Definition.Version}/{resource.Definition.Plural}/{@namespace}/{name}/logs/watch"; | ||
using var request = await this.ProcessRequestAsync(new HttpRequestMessage(HttpMethod.Get, uri), cancellationToken).ConfigureAwait(false); | ||
var response = await this.ProcessResponseAsync(await this.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false); | ||
var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); | ||
return this.JsonSerializer.DeserializeAsyncEnumerable<TextDocumentWatchEvent>(responseStream, cancellationToken)!; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tests/Synapse.UnitTests/Services/MockTextDocumentRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright © 2024-Present The Synapse Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"), | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Neuroglia.Data.Infrastructure; | ||
using Neuroglia.Data.Infrastructure.Services; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Synapse.UnitTests.Services; | ||
|
||
internal class MockTextDocumentRepository<TKey> | ||
: ITextDocumentRepository<TKey> | ||
where TKey : IEquatable<TKey> | ||
{ | ||
public Task AppendAsync(TKey key, string text, CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
|
||
public Task AppendAsync(object key, string text, CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
|
||
public Task DeleteAsync(TKey key, CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
|
||
public Task DeleteAsync(object key, CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
|
||
public async IAsyncEnumerable<ITextDocument<TKey>> GetAllAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
await Task.CompletedTask; | ||
yield break; | ||
} | ||
|
||
public Task<ITextDocument<TKey>?> GetAsync(TKey key, CancellationToken cancellationToken = default) => Task.FromResult((ITextDocument<TKey>?)null); | ||
|
||
public Task<ITextDocument?> GetAsync(object key, CancellationToken cancellationToken = default) => Task.FromResult((ITextDocument?)null); | ||
|
||
public Task<System.Collections.Generic.ICollection<ITextDocument<TKey>>> ListAsync(int? max = null, int? skip = null, CancellationToken cancellationToken = default) => Task.FromResult((System.Collections.Generic.ICollection<ITextDocument<TKey>>)[]); | ||
|
||
public async IAsyncEnumerable<string> ReadAsync(TKey key, [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
await Task.CompletedTask; | ||
yield break; | ||
} | ||
|
||
public async IAsyncEnumerable<string> ReadAsync(object key, [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
await Task.CompletedTask; | ||
yield break; | ||
} | ||
|
||
public Task<string> ReadToEndAsync(TKey key, CancellationToken cancellationToken = default) => Task.FromResult(string.Empty); | ||
|
||
public Task<string> ReadToEndAsync(object key, CancellationToken cancellationToken = default) => Task.FromResult(string.Empty); | ||
|
||
public Task ReplaceAsync(TKey key, string text, CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
|
||
public Task ReplaceAsync(object key, string text, CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
|
||
public Task<ITextDocumentWatch> WatchAsync(TKey key, CancellationToken cancellationToken = default) => Task.FromResult((ITextDocumentWatch)null!); | ||
|
||
public Task<ITextDocumentWatch> WatchAsync(object key, CancellationToken cancellationToken = default) => Task.FromResult((ITextDocumentWatch)null!); | ||
|
||
async IAsyncEnumerable<ITextDocument> ITextDocumentRepository.GetAllAsync([EnumeratorCancellation] CancellationToken cancellationToken) | ||
{ | ||
await Task.CompletedTask; | ||
yield break; | ||
} | ||
|
||
Task<System.Collections.Generic.ICollection<ITextDocument>> ITextDocumentRepository.ListAsync(int? max, int? skip, CancellationToken cancellationToken) => Task.FromResult((System.Collections.Generic.ICollection<ITextDocument>)[]); | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
tests/Synapse.UnitTests/Services/MockWorkflowInstanceApiClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright © 2024-Present The Synapse Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"), | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Neuroglia.Data.Infrastructure; | ||
using Neuroglia.Data.Infrastructure.ResourceOriented.Services; | ||
using Neuroglia.Data.Infrastructure.Services; | ||
using Synapse.Api.Client.Services; | ||
|
||
namespace Synapse.UnitTests.Services; | ||
|
||
internal class MockWorkflowInstanceApiClient(IResourceRepository resources, ITextDocumentRepository<string> logs) | ||
: MockNamespacedResourceApiClient<WorkflowInstance>(resources), IWorkflowInstanceApiClient | ||
{ | ||
|
||
public Task<string> ReadLogsAsync(string name, string @namespace, CancellationToken cancellationToken = default) => logs.ReadToEndAsync($"{name}.{@namespace}", cancellationToken); | ||
|
||
public async Task<IAsyncEnumerable<ITextDocumentWatchEvent>> WatchLogsAsync(string name, string @namespace, CancellationToken cancellationToken = default) => (await logs.WatchAsync($"{name}.{@namespace}", cancellationToken)).ToAsyncEnumerable(); | ||
|
||
} |