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 20, 2024
1 parent 9dbf062 commit a584089
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 9 deletions.
53 changes: 53 additions & 0 deletions api/Controllers/AnonymizerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 Uri RawDataUri { get; set; }
public required 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(),
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.");
}
}
4 changes: 2 additions & 2 deletions api/Controllers/Models/InspectionDataResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace api.Controllers.Models
{
public class InspectionDataResponse
{
public string Name { get; set; }
public string Id { get; set; }

[JsonConstructor]
#nullable disable
Expand All @@ -13,7 +13,7 @@ public InspectionDataResponse() { }

public InspectionDataResponse(InspectionData inspectionData)
{
Name = inspectionData.Name;
Id = inspectionData.Id;
}
}
}
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
4 changes: 0 additions & 4 deletions api/Models/InspectionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public class InspectionData
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }

[Required]
[MaxLength(200)]
public string Name { get; set; }

[Required]
public string InspectionId { get; set; }

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 cannot be null.");

if (data.AnonymizedUri == null)
throw new ArgumentNullException(nameof(data), "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 a584089

Please sign in to comment.