-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Console log event bus * cleanup --------- Co-authored-by: Hammerbeck <[email protected]>
- Loading branch information
Showing
19 changed files
with
238 additions
and
5 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
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
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,7 @@ | ||
namespace Altinn.Correspondence.Core.Options; | ||
|
||
public class AltinnOptions | ||
{ | ||
public string OpenIdWellKnown { get; set; } = string.Empty; | ||
public string PlatformGatewayUrl { get; set; } = string.Empty; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Altinn.Correspondence.Core/Services/Enums/AltinnEventType.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,24 @@ | ||
namespace Altinn.Correspondence.Core.Services.Enums; | ||
|
||
public enum AltinnEventType | ||
{ | ||
AttachmentInitialized, | ||
AttachmentUploadProcessing, | ||
AttachmentPublished, | ||
AttachmentUploadFailed, | ||
AttachmentPurged, | ||
AttachmentDownloaded, | ||
|
||
CorrespondenceInitialized, | ||
CorrespondencePublished, | ||
CorrespondenceArchived, | ||
CorrespondencePurged, | ||
|
||
CorrespondenceReceiverRead, | ||
CorrespondenceReceiverConfirmed, | ||
CorrespondenceReceiverReplied, | ||
CorrespondenceReceiverNeverConfirmed, | ||
CorrespondenceReceiverReserved, | ||
|
||
|
||
} |
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,8 @@ | ||
using Altinn.Correspondence.Core.Services.Enums; | ||
|
||
namespace Altinn.Correspondence.Core.Services; | ||
|
||
public interface IEventBus | ||
{ | ||
Task Publish(AltinnEventType type, string resourceId, string itemId, string eventSource, string? organizationId = null, CancellationToken cancellationToken = default); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Altinn.Correspondence.Integrations/Altinn.Correspondence.Integrations.csproj
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Altinn.Correspondence.Core\Altinn.Correspondence.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
63 changes: 63 additions & 0 deletions
63
src/Altinn.Correspondence.Integrations/Altinn/Events/AltinnEventBus.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,63 @@ | ||
using System.Net.Http.Json; | ||
using System.Text.Json; | ||
|
||
using Altinn.Correspondence.Core.Options; | ||
using Altinn.Correspondence.Core.Repositories; | ||
using Altinn.Correspondence.Core.Services; | ||
using Altinn.Correspondence.Core.Services.Enums; | ||
using Altinn.Correspondence.Integrations.Altinn.Events.Helpers; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Altinn.Correspondence.Integrations.Altinn.Events; | ||
public class AltinnEventBus : IEventBus | ||
{ | ||
private readonly AltinnOptions _altinnOptions; | ||
private readonly HttpClient _httpClient; | ||
private readonly ILogger<AltinnEventBus> _logger; | ||
|
||
public AltinnEventBus(HttpClient httpClient, IOptions<AltinnOptions> altinnOptions, ILogger<AltinnEventBus> logger) | ||
{ | ||
_httpClient = httpClient; | ||
_altinnOptions = altinnOptions.Value; | ||
_logger = logger; | ||
} | ||
|
||
public async Task Publish(AltinnEventType type, string resourceId, string itemId, string eventSource, string? organizationId = null, CancellationToken cancellationToken = default) | ||
{ | ||
string? partyId = null; | ||
// TODO: Get party id | ||
|
||
var cloudEvent = CreateCloudEvent(type, resourceId, itemId, partyId, organizationId, eventSource); | ||
var serializerOptions = new JsonSerializerOptions | ||
{ | ||
PropertyNamingPolicy = new LowerCaseNamingPolicy() | ||
}; | ||
var response = await _httpClient.PostAsync("events/api/v1/events", JsonContent.Create(cloudEvent, options: serializerOptions, mediaType: new System.Net.Http.Headers.MediaTypeHeaderValue("application/cloudevents+json")), cancellationToken); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
_logger.LogError("Unexpected null or invalid json response when posting cloud event {type} of {resourceId} with {eventSource} id {itemId}.", type, resourceId, eventSource, itemId); | ||
_logger.LogError("Statuscode was: {}, error was: {error}", response.StatusCode, await response.Content.ReadAsStringAsync(cancellationToken)); | ||
} | ||
} | ||
|
||
private CloudEvent CreateCloudEvent(AltinnEventType type, string resourceId, string itemId, string? partyId, string? alternativeSubject, string eventSource) | ||
{ | ||
CloudEvent cloudEvent = new CloudEvent() | ||
{ | ||
Id = Guid.NewGuid(), | ||
SpecVersion = "1.0", | ||
Time = DateTime.UtcNow, | ||
Resource = "urn:altinn:resource:" + resourceId, | ||
ResourceInstance = itemId, | ||
Type = "no.altinn.correspondence." + type.ToString().ToLowerInvariant(), | ||
Source = _altinnOptions.PlatformGatewayUrl + "correspondence/api/v1/" + eventSource, | ||
Subject = !string.IsNullOrWhiteSpace(partyId) ? "/party/" + partyId : null, | ||
AlternativeSubject = !string.IsNullOrWhiteSpace(alternativeSubject) ? "/organisation/" + alternativeSubject : null, | ||
}; | ||
|
||
return cloudEvent; | ||
} | ||
} | ||
|
15 changes: 15 additions & 0 deletions
15
src/Altinn.Correspondence.Integrations/Altinn/Events/CloudEvent.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,15 @@ | ||
namespace Altinn.Correspondence.Integrations.Altinn.Events; | ||
|
||
public class CloudEvent | ||
{ | ||
public string SpecVersion { get; set; } = "1.0"; | ||
public Guid Id { get; set; } | ||
public string Type { get; set; } = null!; | ||
public DateTimeOffset Time { get; set; } | ||
public string Resource { get; set; } = null!; | ||
public string ResourceInstance { get; set; } = null!; | ||
public string? Subject { get; set; } | ||
public string? AlternativeSubject { get; set; } | ||
public string Source { get; set; } = null!; | ||
public Dictionary<string, object>? Data { get; set; } | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Altinn.Correspondence.Integrations/Altinn/Events/ConsoleLogEventBus.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,21 @@ | ||
using Altinn.Correspondence.Core.Services; | ||
using Altinn.Correspondence.Core.Services.Enums; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Altinn.Correspondence.Integrations.Altinn.Events; | ||
public class ConsoleLogEventBus : IEventBus | ||
{ | ||
private readonly ILogger<ConsoleLogEventBus> _logger; | ||
|
||
public ConsoleLogEventBus(ILogger<ConsoleLogEventBus> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task Publish(AltinnEventType type, string resourceId, string itemId, string eventSource, string? organizationId = null, CancellationToken cancellationToken = default) | ||
{ | ||
_logger.LogInformation("{CloudEventType} event raised on instance {eventSource} {itemId} for party with organization number {organizationId}", type.ToString(), eventSource, itemId, organizationId); | ||
return Task.CompletedTask; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Altinn.Correspondence.Integrations/Altinn/Events/Helpers/LowerCaseNamingPolicy.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,11 @@ | ||
| ||
using System.Text.Json; | ||
|
||
namespace Altinn.Correspondence.Integrations.Altinn.Events.Helpers; | ||
internal class LowerCaseNamingPolicy : JsonNamingPolicy | ||
{ | ||
public override string ConvertName(string name) | ||
{ | ||
return name.ToLower(); | ||
} | ||
} |
Oops, something went wrong.