|
3 | 3 | using System.Net.Http; |
4 | 4 | using System.Security.Cryptography.X509Certificates; |
5 | 5 | using System.Text; |
| 6 | +using System.Text.Json; |
6 | 7 | using System.Threading.Tasks; |
7 | 8 |
|
8 | 9 | using Altinn.Common.AccessTokenClient.Services; |
9 | 10 | using Altinn.Platform.Events.Functions.Clients.Interfaces; |
10 | 11 | using Altinn.Platform.Events.Functions.Configuration; |
11 | 12 | using Altinn.Platform.Events.Functions.Extensions; |
| 13 | +using Altinn.Platform.Events.Functions.Models; |
12 | 14 | using Altinn.Platform.Events.Functions.Services.Interfaces; |
13 | 15 |
|
14 | 16 | using CloudNative.CloudEvents; |
@@ -116,19 +118,48 @@ public async Task ValidateSubscription(int subscriptionId) |
116 | 118 |
|
117 | 119 | if (response.StatusCode == HttpStatusCode.NotFound) |
118 | 120 | { |
119 | | - _logger.LogError("Attempting to validate non existing subscription {subscriptionId}", subscriptionId); |
| 121 | + _logger.LogError("Attempting to validate non existing subscription {SubscriptionId}", subscriptionId); |
120 | 122 | return; |
121 | 123 | } |
122 | | - |
| 124 | + |
123 | 125 | if (!response.IsSuccessStatusCode) |
124 | 126 | { |
125 | 127 | _logger.LogError( |
126 | | - $"// Validate subscription with id {subscriptionId} failed with status code {response.StatusCode}"); |
| 128 | + "Validate subscription with id {SubscriptionId} failed with status code {StatusCode}", subscriptionId, response.StatusCode); |
127 | 129 | throw new HttpRequestException( |
128 | 130 | $"// Validate subscription with id {subscriptionId} failed with status code {response.StatusCode}"); |
129 | 131 | } |
130 | 132 | } |
131 | 133 |
|
| 134 | + /// <inheritdoc/> |
| 135 | + public async Task LogWebhookHttpStatusCode(CloudEventEnvelope cloudEventEnvelope, HttpStatusCode statusCode) |
| 136 | + { |
| 137 | + try |
| 138 | + { |
| 139 | + var endpoint = "storage/events/logs"; |
| 140 | + |
| 141 | + var logEntryData = new LogEntryDto |
| 142 | + { |
| 143 | + CloudEventId = cloudEventEnvelope.CloudEvent.Id, |
| 144 | + CloudEventType = cloudEventEnvelope.CloudEvent.Type, |
| 145 | + CloudEventResource = cloudEventEnvelope.CloudEvent["resource"]?.ToString(), |
| 146 | + Consumer = cloudEventEnvelope.Consumer, |
| 147 | + Endpoint = cloudEventEnvelope.Endpoint, |
| 148 | + SubscriptionId = cloudEventEnvelope.SubscriptionId, |
| 149 | + StatusCode = statusCode, |
| 150 | + }; |
| 151 | + |
| 152 | + StringContent httpContent = new(JsonSerializer.Serialize(logEntryData), Encoding.UTF8, "application/json"); |
| 153 | + var accessToken = await GenerateAccessToken(); |
| 154 | + |
| 155 | + await _client.PostAsync(endpoint, httpContent, accessToken); |
| 156 | + } |
| 157 | + catch (Exception e) |
| 158 | + { |
| 159 | + _logger.LogError(e, "Failed to log trace log webhook status code for cloud event id {CloudEventId}", cloudEventEnvelope.CloudEvent.Id); |
| 160 | + } |
| 161 | + } |
| 162 | + |
132 | 163 | private async Task<(bool Success, HttpStatusCode StatusCode)> PostCloudEventToEndpoint(CloudEvent cloudEvent, string endpoint) |
133 | 164 | { |
134 | 165 | StringContent httpContent = new(cloudEvent.Serialize(), Encoding.UTF8, "application/cloudevents+json"); |
|
0 commit comments