Skip to content

Commit

Permalink
chore: cleanup function auditlog-client (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxandr authored Nov 12, 2024
1 parent 62b194b commit 6169f0a
Showing 1 changed file with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -36,8 +37,10 @@ public AuditLogClient(
/// <inheritdoc/>
public async Task SaveAuthenticationEvent(AuthenticationEvent authEvent)
{
string endpointUrl = "auditlog/api/v1/authenticationevent";
var (success, statusCode) = await PostAuthEventToEndpoint(authEvent, null, endpointUrl);
const string ENDPOINT_URL = "auditlog/api/v1/authenticationevent";

using var content = JsonContent.Create(authEvent);
var (success, statusCode) = await PostAuthEventToEndpoint(content, ENDPOINT_URL);

if (!success)
{
Expand All @@ -50,8 +53,10 @@ public async Task SaveAuthenticationEvent(AuthenticationEvent authEvent)
/// <inheritdoc/>
public async Task SaveAuthorizationEvent(AuthorizationEvent authorizationEvent)
{
string endpointUrl = "auditlog/api/v1/authorizationevent";
var (success, statusCode) = await PostAuthEventToEndpoint(null, authorizationEvent, endpointUrl);
const string ENDPOINT_URL = "auditlog/api/v1/authorizationevent";

using var content = JsonContent.Create(authorizationEvent);
var (success, statusCode) = await PostAuthEventToEndpoint(content, ENDPOINT_URL);

if (!success)
{
Expand All @@ -61,19 +66,9 @@ public async Task SaveAuthorizationEvent(AuthorizationEvent authorizationEvent)
}
}

private async Task<(bool Success, HttpStatusCode StatusCode)> PostAuthEventToEndpoint(AuthenticationEvent? authEvent, AuthorizationEvent? authorizationEvent, string endpoint)
private async Task<(bool Success, HttpStatusCode StatusCode)> PostAuthEventToEndpoint(HttpContent content, string endpoint)
{
StringContent? requestBody = null;
if (authEvent != null)
{
requestBody = new StringContent(JsonSerializer.Serialize(authEvent), Encoding.UTF8, "application/json");
}
else if(authorizationEvent != null)
{
requestBody = new StringContent(JsonSerializer.Serialize(authorizationEvent), Encoding.UTF8, "application/json");
}

HttpResponseMessage response = await _client.PostAsync(endpoint, requestBody);
using HttpResponseMessage response = await _client.PostAsync(endpoint, content);
if (!response.IsSuccessStatusCode)
{
return (false, response.StatusCode);
Expand Down

0 comments on commit 6169f0a

Please sign in to comment.