Skip to content

Commit

Permalink
fix: support gpas url not ending in a trailing slash (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
chgl authored Jan 2, 2023
1 parent 6093841 commit 79d4635
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Note: The domain name could also have been replaced completely by overriding the
To test gPAS, Vfps, and tracing via Jaeger, run

```sh
docker compose -f docker-compose.dev.yml up
docker compose -f docker-compose.dev.yml --profile=gpas up
```

### Build
Expand Down
49 changes: 27 additions & 22 deletions src/FhirPseudonymizer.Tests/GPasFhirClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using FakeItEasy;
using FhirPseudonymizer.Config;
using FhirPseudonymizer.Pseudonymization;
Expand Down Expand Up @@ -33,27 +29,29 @@ public static IEnumerable<object[]> GetOriginalValueFor_Data()

private static readonly Uri testBaseAddress = new("http://gpas");

private const string ResponseContent = @"{
""resourceType"": ""Parameters"",
""parameter"": [
private const string ResponseContent = $$"""
{
"resourceType": "Parameters",
"parameter": [
{
""name"": ""pseudonym"",
""part"": [
"name": "pseudonym",
"part": [
{
""name"": ""pseudonym"",
""valueIdentifier"": {
""system"": ""https://ths-greifswald.de/gpas"",
""value"": ""24""
"name": "pseudonym",
"valueIdentifier": {
"system": "https://ths-greifswald.de/gpas",
"value": "24"
}
}
]
},
{
""name"": ""42"",
""valueString"": ""24""
"name": "42",
"valueString": "24"
}
]
}";
}
""";

private readonly HttpMessageHandler messageHandler;
private readonly IHttpClientFactory clientFactory;
Expand Down Expand Up @@ -81,10 +79,11 @@ public async Task GetOrCreatePseudonymFor_ResolvesToApiVersionOperation(string g

private void VerifyRequest(HttpMethod requestMethod, string requestUri)
{
A.CallTo(messageHandler).Where(_ => _.Method.Name == "SendAsync")
.WhenArgumentsMatch(((HttpRequestMessage r, CancellationToken _) =>
A.CallTo(messageHandler)
.Where(_ => _.Method.Name == "SendAsync")
.WhenArgumentsMatch((HttpRequestMessage r, CancellationToken _) =>
r.Method == requestMethod &&
r.RequestUri == new Uri(testBaseAddress.AbsoluteUri + requestUri)))
r.RequestUri == new Uri(testBaseAddress.AbsoluteUri + requestUri))
.MustHaveHappenedOnceExactly();
}

Expand Down Expand Up @@ -145,11 +144,17 @@ private IPseudonymServiceClient CreateGPasClient(string gPasVersion)
private static HttpMessageHandler CreateHttpMessageHandler()
{
var handler = A.Fake<HttpMessageHandler>();
A.CallTo(handler).Where(_ => _.Method.Name == "SendAsync").WithReturnType<Task<HttpResponseMessage>>()
A.CallTo(handler)
.Where(_ => _.Method.Name == "SendAsync")
.WithReturnType<Task<HttpResponseMessage>>()
.Returns(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(ResponseContent),
// these values have to be set since they are used by the FhirClient:
// https://github.com/FirelyTeam/firely-net-common/blob/5899ce463f6cf166520cbbe6322310940942f81c/src/Hl7.Fhir.Support.Poco/Rest/HttpToEntryExtensions.cs#L28 &
// https://github.com/FirelyTeam/firely-net-sdk/blob/f71543edc34c9edecf0f13af50d35e9e57ca353a/src/Hl7.Fhir.Core/Rest/TypedEntryResponseToBundle.cs#L24
Content = new StringContent(ResponseContent, new MediaTypeHeaderValue("application/json+fhir")),
RequestMessage = new HttpRequestMessage(HttpMethod.Post, testBaseAddress),
});

return handler;
Expand Down
6 changes: 0 additions & 6 deletions src/FhirPseudonymizer/FhirPseudonymizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,4 @@
<Protobuf Include="Protos\**\*.proto" GrpcServices="Client" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Pseudonymization\NewFolder\**" />
<Content Remove="Pseudonymization\NewFolder\**" />
<EmbeddedResource Remove="Pseudonymization\NewFolder\**" />
<None Remove="Pseudonymization\NewFolder\**" />
</ItemGroup>
</Project>
23 changes: 8 additions & 15 deletions src/FhirPseudonymizer/Pseudonymization/GPas/GPasFhirClient.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using FhirPseudonymizer.Config;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Utility;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Prometheus;

namespace FhirPseudonymizer.Pseudonymization.GPas;
Expand Down Expand Up @@ -41,6 +36,8 @@ public GPasFhirClient(ILogger<GPasFhirClient> logger, IHttpClientFactory clientF

Client = clientFactory.CreateClient("gPAS");

FhirClient = new FhirClient(Client.BaseAddress, Client);

PseudonymCache = pseudonymCache;
OriginalValueCache = originalValueCache;

Expand Down Expand Up @@ -76,6 +73,7 @@ public GPasFhirClient(ILogger<GPasFhirClient> logger, IHttpClientFactory clientF
private TimeSpan AbsoluteExpiration { get; }
private Func<string, string, Task<string>> GetOrCreatePseudonymForResolver { get; }
private Func<string, string, Task<string>> GetOriginalValueForResolver { get; }
private FhirClient FhirClient { get; }

public async Task<string> GetOrCreatePseudonymFor(string value, string domain)
{
Expand Down Expand Up @@ -189,7 +187,7 @@ private async Task<string> GetOrCreatePseudonymForV1(string value, string domain

private async Task<string> GetOrCreatePseudonymForV2(string value, string domain)
{
var responseParameters = await RequestGetOrCreatePseudonymForV2(value, domain, "$pseudonymize-allow-create");
var responseParameters = await RequestGetOrCreatePseudonymForV2(value, domain, "pseudonymize-allow-create");

var firstResponseParameter = responseParameters.Parameter.FirstOrDefault();
var pseudonym = firstResponseParameter?.Part.Find(part => part.Name == "pseudonym");
Expand All @@ -203,7 +201,7 @@ private async Task<string> GetOrCreatePseudonymForV2(string value, string domain

private async Task<string> GetOrCreatePseudonymForV2x(string value, string domain)
{
var responseParameters = await RequestGetOrCreatePseudonymForV2(value, domain, "$pseudonymizeAllowCreate");
var responseParameters = await RequestGetOrCreatePseudonymForV2(value, domain, "pseudonymizeAllowCreate");

var firstResponseParameter = responseParameters.Parameter.FirstOrDefault();
var pseudonym = firstResponseParameter?.Part.Find(part => part.Name == "pseudonym");
Expand All @@ -221,14 +219,9 @@ private async Task<Parameters> RequestGetOrCreatePseudonymForV2(string value, st
.Add("target", new FhirString(domain))
.Add("original", new FhirString(value));

var parametersBody = await FhirSerializer.SerializeToStringAsync(parameters);
using var content = new StringContent(parametersBody, Encoding.UTF8, "application/fhir+json");
var response = await Client.PostAsync(operation, content);
logger.LogDebug("Request to {Operation} responded with: {Response}", operation, response);
response.EnsureSuccessStatusCode();
var response = await FhirClient.WholeSystemOperationAsync(operation, parameters);

var responseContent = await response.Content.ReadAsStringAsync();
return await FhirParser.ParseAsync<Parameters>(responseContent);
return response as Parameters;
}

private async Task<Parameters> RequestGetOriginalValueForV2(string pseudonym, string domain, string operation)
Expand Down

0 comments on commit 79d4635

Please sign in to comment.