Skip to content

Commit

Permalink
Merge pull request #184 from OpenBracketsCH/development
Browse files Browse the repository at this point in the history
Add v2 to backend to support geojson and access with multiple options
  • Loading branch information
elektrolytmangel authored Apr 17, 2024
2 parents d88628c + 0d2d034 commit 26a342d
Show file tree
Hide file tree
Showing 18 changed files with 588 additions and 25 deletions.
3 changes: 2 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,5 @@ ASALocalRun/
.localhistory/

# BeatPulse healthcheck temp database
healthchecksdb
healthchecksdb
local.settings.json
11 changes: 0 additions & 11 deletions backend/Cache/BlobStorageCacheRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@

namespace DefikarteBackend.Cache
{
public interface IBlobStorageCacheRepository
{
Task CreateAsync(string jsonData, string blobName);

Task<string> ReadAsync(string blobName);

Task UpdateAsync(string jsonData, string blobName);

Task DeleteAsync(string blobName);
}

public class BlobStorageCacheRepository : IBlobStorageCacheRepository, ICacheRepository<OsmNode>
{
private readonly BlobContainerClient _containerClient;
Expand Down
75 changes: 75 additions & 0 deletions backend/Cache/BlobStorageCacheRepositoryV2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Azure.Storage.Blobs;
using DefikarteBackend.Model;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace DefikarteBackend.Cache
{
public class BlobStorageCacheRepositoryV2 : IBlobStorageCacheRepository, IGeoJsonCacheRepository
{
private readonly BlobContainerClient _containerClient;
private readonly string _blobName;

public BlobStorageCacheRepositoryV2(BlobContainerClient containerClient, string blobName)
{
_containerClient = containerClient;
_blobName = blobName;
}

public async Task CreateAsync(string jsonData, string blobName)
{
BlobClient blobClient = _containerClient.GetBlobClient(blobName);
await blobClient.UploadAsync(BinaryData.FromString(jsonData));
}

public async Task<string> ReadAsync(string blobName)
{
BlobClient blobClient = _containerClient.GetBlobClient(blobName);
var response = await blobClient.DownloadContentAsync();
var content = response.Value.Content;
return Encoding.UTF8.GetString(content);
}

public async Task UpdateAsync(string jsonData, string blobName)
{
BlobClient blobClient = _containerClient.GetBlobClient(blobName);
await blobClient.UploadAsync(BinaryData.FromString(jsonData), overwrite: true);
}

public async Task DeleteAsync(string blobName)
{
BlobClient blobClient = _containerClient.GetBlobClient(blobName);
await blobClient.DeleteIfExistsAsync();
}

public async Task<FeatureCollection> GetAsync()
{
var content = await ReadAsync(_blobName);
return JsonConvert.DeserializeObject<FeatureCollection>(content);
}

public async Task<Feature> GetByIdAsync(string id)
{
return (await GetAsync()).Features.FirstOrDefault(x => x.Id == id);
}

public async Task<bool> TryUpdateCacheAsync(FeatureCollection values)
{
var success = false;
try
{
await UpdateAsync(JsonConvert.SerializeObject(values), _blobName);
success = true;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

return success;
}
}
}
15 changes: 15 additions & 0 deletions backend/Cache/IBlobStorageCacheRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading.Tasks;

namespace DefikarteBackend.Cache
{
public interface IBlobStorageCacheRepository
{
Task CreateAsync(string jsonData, string blobName);

Task<string> ReadAsync(string blobName);

Task UpdateAsync(string jsonData, string blobName);

Task DeleteAsync(string blobName);
}
}
14 changes: 14 additions & 0 deletions backend/Cache/IGeoJsonCacheRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using DefikarteBackend.Model;
using System.Threading.Tasks;

namespace DefikarteBackend.Cache
{
public interface IGeoJsonCacheRepository
{
Task<FeatureCollection> GetAsync();

Task<Feature> GetByIdAsync(string id);

Task<bool> TryUpdateCacheAsync(FeatureCollection values);
}
}
26 changes: 26 additions & 0 deletions backend/Configuration/CustomOpenApiConfigurationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.OpenApi.Models;
using System;

namespace DefikarteBackend.Configuration
{
public class CustomOpenApiConfigurationOptions : DefaultOpenApiConfigurationOptions
{
public override OpenApiInfo Info { get; set; } = new OpenApiInfo()
{
Version = "1.0.0",
Title = "OpenAPI Document for Defikarte.ch-API",
Description = "HTTP APIs used for the Defikarte.ch",
TermsOfService = new Uri("https://defikarte.ch/impressum.html"),
Contact = new OpenApiContact()
{
Name = "Defikarte.ch-API",
Email = "[email protected]",
Url = new Uri("https://github.com/OpenBracketsCH/defikarte.ch-app/issues"),
},
};

public override OpenApiVersionType OpenApiVersion { get; set; } = OpenApiVersionType.V3;
}
}
3 changes: 3 additions & 0 deletions backend/Configuration/ServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class ServiceConfiguration

public string BlobStorageBlobName { get; set; }

public string BlobStorageBlobNameV2 { get; set; }

public string BlobStoragaConnectionString { get; set; }

public static ServiceConfiguration Initialize(IConfigurationRoot configuration)
Expand All @@ -29,6 +31,7 @@ public static ServiceConfiguration Initialize(IConfigurationRoot configuration)
BlobStoragaConnectionString = configuration.GetConnectionStringOrSetting("AzureWebJobsStorage"),
BlobStorageContainerName = configuration.GetConnectionStringOrSetting("BLOB_STORAGE_CONTAINER_NAME"),
BlobStorageBlobName = configuration.GetConnectionStringOrSetting("BLOB_STORAGE_BLOB_NAME"),
BlobStorageBlobNameV2 = configuration.GetConnectionStringOrSetting("BLOB_STORAGE_BLOB_NAME_V2"),
};
}
}
Expand Down
10 changes: 10 additions & 0 deletions backend/DefibrillatorFunction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
Expand All @@ -13,7 +14,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using OsmSharp;
using OsmSharp.IO.API;
Expand All @@ -33,6 +37,8 @@ public DefibrillatorFunction(ServiceConfiguration config, ICacheRepository<OsmNo
}

[FunctionName("Defibrillators_GETALL")]
[OpenApiOperation(operationId: "GetDefibrillators_V1", tags: new[] { "Defibrillator-V1" }, Summary = "Get all defibrillators from switzerland as custom json.")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(List<OsmNode>), Description = "The OK response")]
public async Task<IActionResult> GetAll(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "defibrillator")] HttpRequestMessage req,
ILogger log)
Expand Down Expand Up @@ -67,6 +73,10 @@ public async Task<IActionResult> GetAll(


[FunctionName("Defibrillators_POST")]
[OpenApiOperation(operationId: "CreateDefibrillator_V1", tags: new[] { "Defibrillator-V1" }, Summary = "Create a new defibrillator. [Soon deprecated, use V2]")]
[OpenApiRequestBody("application/json", typeof(DefibrillatorRequest))]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.Created, contentType: "application/json", bodyType: typeof(DefibrillatorResponse), Description = "The OK response")]
[OpenApiSecurity("Defikarte.ch API-Key", SecuritySchemeType.ApiKey, In = OpenApiSecurityLocationType.Header, Name = "x-functions-key")]
public async Task<IActionResult> Create(
[HttpTrigger(AuthorizationLevel.Function, "Post", Route = "defibrillator")] HttpRequest req,
ILogger log)
Expand Down
Loading

0 comments on commit 26a342d

Please sign in to comment.