diff --git a/api/AquaApi/AquaApi.csproj b/api/AquaApi/AquaApi.csproj index 7015e9b..a5d7b8c 100644 --- a/api/AquaApi/AquaApi.csproj +++ b/api/AquaApi/AquaApi.csproj @@ -7,6 +7,9 @@ + + + diff --git a/api/AquaApi/MeasurementRow.cs b/api/AquaApi/MeasurementRow.cs new file mode 100644 index 0000000..68f6eee --- /dev/null +++ b/api/AquaApi/MeasurementRow.cs @@ -0,0 +1,12 @@ +namespace AquaApi; + +public class MeasurementRow +{ + public string lat { get; set; } + public string lon { get; set; } + public string variable_name { get; set; } + public string time { get; set; } + public string ocean_temperature { get; set; } + public string depth_meters { get; set; } + public string fetch_timestamp{ get; set; } +} diff --git a/api/AquaApi/Program.cs b/api/AquaApi/Program.cs index 55d7c0a..8403303 100644 --- a/api/AquaApi/Program.cs +++ b/api/AquaApi/Program.cs @@ -1,13 +1,56 @@ +using System.Globalization; +using System.Text.Json; +using AquaApi; +using Azure.Identity; +using Azure.Storage.Blobs; +using CsvHelper; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +// Add services to the container builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +// Register the BlobServiceClient with the connection string from app settings +builder.Services.AddSingleton(x => +{ + var connectionString = builder.Configuration.GetConnectionString("BlobStorage"); + return new BlobServiceClient(connectionString); +}); + var app = builder.Build(); -app.MapGet("/", () => "Hello World!"); -app.Run("http://0.0.0.0:80"); +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + + + +// Endpoint to fetch a CSV from Blob Storage, parse it, and return JSON +app.MapGet("/temperature_predictions/latest", async (BlobServiceClient blobServiceClient) => + { + var containerClient = blobServiceClient.GetBlobContainerClient("datalake"); + var blobClient = containerClient.GetBlobClient("havvarsel/gold/havtemp-pred-latest.csv"); + + // Check if the file exists + if (!await blobClient.ExistsAsync()) + { + return Results.NotFound(new { message = "File not found" }); + } + + // Download the CSV file from Blob Storage + var downloadStream = new MemoryStream(); + await blobClient.DownloadToAsync(downloadStream); + downloadStream.Position = 0; // Reset the stream position + // Parse the CSV file using CsvHelper + using var reader = new StreamReader(downloadStream); + using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); + var records = csv.GetRecords().ToList(); // Dynamically read CSV data + return Results.Ok(records); // Return the JSON data + }) + .WithName("DownloadCsvAndParseToJson"); +app.Run(); \ No newline at end of file