Skip to content

Commit

Permalink
Add triggering anonymizer from endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
olaals committed Nov 19, 2024
1 parent 9dbf062 commit 131b133
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
55 changes: 55 additions & 0 deletions api/Controllers/AnonymizerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using api.Controllers.Models;
using api.Services;
using api.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace api.Controllers;

public class TriggerAnonymizerRequest
{
public required string InspectionId { get; set; }
public required string Name { get; set; }
public Uri? RawDataUri { get; set; }
public Uri? AnonymizedUri { get; set; }
}


[ApiController]
[Route("[controller]")]
public class AnonymizerController(AnonymizerService anonymizerService, IdaDbContext dbContext) : ControllerBase
{
private readonly AnonymizerService anonymizerService = anonymizerService;
private readonly IdaDbContext dbContext = dbContext;

/// <summary>
/// Triggers the anonymizer workflow. NB: Anonymizer workflow should normally be triggered by MQTT message
/// </summary>
[HttpPost]
[Route("trigger-anonymizer")]
[Authorize(Roles = Role.Any)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> TriggerAnonymizer([FromBody] TriggerAnonymizerRequest request)
{
var inspectionData = new InspectionData
{
Id = Guid.NewGuid().ToString(),
Name = request.Name,
InspectionId = request.InspectionId,
RawDataUri = request.RawDataUri,
AnonymizedUri = request.AnonymizedUri,
DateCreated = DateTime.UtcNow,
AnonymizerWorkflowStatus = WorkflowStatus.NotStarted,
AnalysisToBeRun = [],
Analysis = []
};

dbContext.InspectionData.Add(inspectionData);
await dbContext.SaveChangesAsync();

await anonymizerService.TriggerAnonymizerFunc(inspectionData);

return Ok("Anonymizer workflow triggered successfully.");
}
}
3 changes: 3 additions & 0 deletions api/Controllers/WorkflowsControlller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using api.Services;
using api.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
namespace api.Controllers;

public class WorkflowStartedNotification
Expand All @@ -26,6 +27,7 @@ public class WorkflowsController(IInspectionDataService inspectionDataService) :
/// Updates status of inspection data to started
/// </summary>
[HttpPut]
[AllowAnonymous] // TODO: Implement role for notifying and machine-to-machine oauth
[Route("notify-workflow-started")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
Expand All @@ -43,6 +45,7 @@ public async Task<ActionResult<InspectionDataResponse>> WorkflowStarted([FromBod
/// Updates status of inspection data to exit with success or failure
/// </summary>
[HttpPut]
[AllowAnonymous] // TODO: Implement role for notifying and machine-to-machine oauth
[Route("notify-workflow-exited")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
Expand Down
1 change: 1 addition & 0 deletions api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

builder.Services.AddScoped<IBlobService, BlobService>();
builder.Services.AddScoped<IInspectionDataService, InspectionDataService>();
builder.Services.AddScoped<AnonymizerService>();

builder.Services
.AddControllers()
Expand Down
20 changes: 17 additions & 3 deletions api/Services/AnonymizerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using api.Models;
using System.Text.Json;

namespace api.Services;

public class TriggerAnonymizerRequest(string inspectionId, Uri rawDataUri, Uri anonymizedUri)
{
public string InspectionId { get; } = inspectionId;
public Uri RawDataUri { get; } = rawDataUri;
public Uri AnonymizedUri { get; } = anonymizedUri;
}

public class AnonymizerService
{
private static readonly HttpClient client = new();

public async Task TriggerAnonymizerFunc(InspectionData data)
{
var json = JsonSerializer.Serialize(data);
if (data.RawDataUri == null)
throw new ArgumentNullException(nameof(data.RawDataUri), "RawDataUri cannot be null.");

if (data.AnonymizedUri == null)
throw new ArgumentNullException(nameof(data.AnonymizedUri), "AnonymizedUri cannot be null.");

var postRequestData = new TriggerAnonymizerRequest(data.InspectionId, data.RawDataUri, data.AnonymizedUri);
var json = JsonSerializer.Serialize(postRequestData);
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PostAsync("https://<your-function-app-name>.azurewebsites.net/api/AnonymizerFunc", content);
var response = await client.PostAsync("http://trigger-anonymizer-source:8080/trigger-anonymizer", content);

if (response.IsSuccessStatusCode)
{
Expand Down

0 comments on commit 131b133

Please sign in to comment.