Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup function auditlog-client #153

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading