Last active
March 15, 2023 06:01
-
-
Save Korkmatik/37dbfb514bf72f3171e4a2e4c3075839 to your computer and use it in GitHub Desktop.
Example FHIR CRUD operations on the patient resource with C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Hl7.Fhir.Model; | |
using Hl7.Fhir.Rest; | |
using Hl7.Fhir.Serialization; | |
namespace fhir_test | |
{ | |
class Program | |
{ | |
// TODO: Change if you want to use another server | |
static string FhirServer = "http://hapi.fhir.org/baseR4/"; | |
static void Main(string[] args) | |
{ | |
FhirClient client = CreateClient(); | |
Patient patient = CreatePatientLocal(); | |
Patient createdPatient = CreatePatientOnServer(client, patient); | |
UpdatePatient(client, createdPatient); | |
Bundle patients = SearchPatient(client); | |
DeletePatient(client, patients); | |
GetPatientByID(client, patient); | |
} | |
private static FhirClient CreateClient() | |
{ | |
// Creating FHIR client | |
FhirClient client = new FhirClient(Program.FhirServer); | |
// Setting to JSON, so we receive JSON data from the server | |
client.Settings.PreferredFormat = ResourceFormat.Json; | |
Console.WriteLine("Successfully created client"); | |
return client; | |
} | |
private static Patient CreatePatientLocal() | |
{ | |
Console.WriteLine("\n***\nPart 1: Create a new Patient\n***"); | |
Patient patient = new Patient(); | |
// Creating a name for the patient | |
HumanName name = new HumanName().WithGiven("Bruce").AndFamily("Wayne"); | |
name.Use = HumanName.NameUse.Official; | |
patient.Name.Add(name); | |
// Creating address for patient | |
Address address = new Address() | |
{ | |
City = "Gotham", | |
Country = "DC-Comic-Land" | |
}; | |
patient.Address.Add(address); | |
Console.WriteLine("Patient created localy:"); | |
Program.PrettyPrint(patient); | |
return patient; | |
} | |
private static void UpdatePatient(FhirClient client, Patient patient) | |
{ | |
Console.WriteLine("\n***\nPart 2: Update/Modify a patient\n***"); | |
// Creating a nickname and adding it to the patient | |
HumanName name = new HumanName().WithGiven("Batman"); | |
name.Use = HumanName.NameUse.Nickname; | |
patient.Name.Add(name); | |
// Updating the patient on the server | |
var updateResult = client.Update<Patient>(patient); | |
Console.WriteLine("Successfully updated patient:"); | |
Program.PrettyPrint(updateResult); | |
} | |
private static Patient CreatePatientOnServer(FhirClient client, Patient patient) | |
{ | |
var create = client.Create<Patient>(patient); | |
Console.WriteLine("Successfully created patient on remote server:"); | |
Program.PrettyPrint(create); | |
return create; | |
} | |
private static Bundle SearchPatient(FhirClient client) | |
{ | |
Console.WriteLine("\n***\nPart 3: Search for a patient\n***"); | |
// Setting search parameters | |
Bundle results = client.Search<Patient>(new string[] | |
{ | |
"name:exact=Batman", | |
"address=Gotham" | |
}); | |
// Printing and counting all resources that matches the search parameters | |
int totalResources = 0; | |
Console.WriteLine("Resources:"); | |
foreach (Patient resource in results.GetResources()) | |
{ | |
Program.PrettyPrint(resource); | |
totalResources++; | |
} | |
Console.WriteLine("Total Number of Resources: " + totalResources); | |
return results; | |
} | |
private static void DeletePatient(FhirClient client, Bundle patients) | |
{ | |
Console.WriteLine("\n***\nPart 4: Delete a patient or patients\n***"); | |
// Iterating over all patients and deleting them | |
foreach (Patient p in patients.GetResources()) | |
{ | |
client.Delete(p); | |
Console.WriteLine("Successfully deleted patient: " + p.Id); | |
} | |
} | |
private static void GetPatientByID(FhirClient client, Patient patient) | |
{ | |
Console.WriteLine("\n***\nPart 5: Get a patient by ID\n***"); | |
try | |
{ | |
// TODO: Change ID if you want to get another patient | |
Patient existingPatient = client.Read<Patient>("Patient/1895826"); | |
Program.PrettyPrint(patient); | |
} | |
catch (FhirOperationException exception) | |
{ | |
// If something went wrong, the exception "FhirOperationException" will be thrown | |
// The property "Outcome" will have greater detail about the error | |
Console.WriteLine("Could not get patient!"); | |
Console.WriteLine(exception.Outcome); | |
} | |
} | |
/// <summary> | |
/// This function just prints the patient in JSON format on the console | |
/// </summary> | |
/// <param name="patient">Patient that should be printed</param> | |
private static void PrettyPrint(Patient patient) | |
{ | |
var serializer = new FhirJsonSerializer(new SerializerSettings() | |
{ | |
Pretty = true | |
}); | |
Console.WriteLine(serializer.SerializeToString(patient)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
P.S.: Nuget package
Hl7.Fhir.R4
needs to be installed