public static Bundle RefreshBundle(this FhirClient client, Bundle bundle) { if (bundle == null) { throw Error.ArgumentNull(nameof(bundle)); } if (bundle.Type != Bundle.BundleType.Searchset) { throw Error.Argument("Refresh is only applicable to bundles of type 'searchset'"); } // Clone old bundle, without the entries (so, just the header) Bundle result = (Bundle)bundle.DeepCopy(); result.Id = "urn:uuid:" + Guid.NewGuid().ToString("n"); result.Meta = new Meta(); result.Meta.LastUpdated = DateTimeOffset.Now; foreach (var entry in result.Entry) { if (entry.Resource != null) { entry.Resource = client.Read <Resource>(entry.FullUrl); } } return(result); }
public ActionResult UpdateResource(Models.UpdateResourceViewModel model) { try { var patientID = model.patientID; //Create a client to send to the server at a given endpoint. var FhirClient = new Hl7.Fhir.Rest.FhirClient(OpenFHIRUrl); var ResourceUpdate = ("/Patient/" + patientID); //Reading the data at the given location("URL") model.patientRead = FhirClient.Read <Patient>(ResourceUpdate); //using Json serializer to get the data in Json format FhirJsonSerializer fhirJsonSerializer = new FhirJsonSerializer(); //Serializing the existing patient string oldDetails = fhirJsonSerializer.SerializeToString(model.patientRead); model.patientDetailsOld = JValue.Parse(oldDetails).ToString(); //Updating the changes to the existing Resource model.patientFullnameUpdate = new HumanName(); model.patientFullnameUpdate.Use = HumanName.NameUse.Official; model.patientFullnameUpdate.Prefix = new string[] { model.patientPrefixUpdate }; model.patientFullnameUpdate.Given = new string[] { model.patientFirstnameUpdate }; model.patientFullnameUpdate.Family = model.patientFamilyNameUpdate; model.patientRead.Name = new List <HumanName>(); model.patientRead.Name.Add(model.patientFullnameUpdate); model.patientRead.Gender = model.patientGenderUpdate == "Male" ? AdministrativeGender.Male : AdministrativeGender.Female; model.patientRead.BirthDate = model.patientDateOfBirthUpdate; //Sending the updated changes to the endpoint model.patientupdate = FhirClient.Update <Patient>(model.patientRead); // Serializing the updated patient details string Updatedetails = fhirJsonSerializer.SerializeToString(model.patientupdate); model.patientDetailsUpdated = JValue.Parse(Updatedetails).ToString(); } catch (FhirOperationException FhirOpExec) { var response = FhirOpExec.Outcome; var errorDetails = fhirJsonSerializer.SerializeToString(response); model.ResourceRawJsonData = JValue.Parse(errorDetails).ToString(); } catch (WebException ex) { var response = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); var error = JsonConvert.DeserializeObject(response); model.ResourceRawJsonData = error.ToString(); } return(View(model)); }
public static Bundle RefreshBundle(this FhirClient client, Bundle bundle) { if (bundle == null) { throw Error.ArgumentNull("bundle"); } // Clone old bundle, without the entries (so, just the header) var oldEntries = bundle.Entry; Bundle result; try { bundle.Entry = new List <Bundle.BundleEntryComponent>(); var xml = FhirSerializer.SerializeResourceToXml(bundle, summary: false); result = (Bundle)FhirParser.ParseResourceFromXml(xml); } catch { throw; } finally { bundle.Entry = oldEntries; } result.Id = "urn:uuid:" + Guid.NewGuid().ToString("n"); result.Meta = new Meta(); result.Meta.LastUpdated = DateTimeOffset.Now; result.Entry = new List <Bundle.BundleEntryComponent>(); foreach (var entry in bundle.Entry) { if (entry.Resource != null) { if (!entry.IsDeleted()) { Resource newEntry = client.Read <Resource>(entry.GetResourceLocation()); result.Entry.Add(new Bundle.BundleEntryComponent() { Resource = newEntry, Base = bundle.Base, ElementId = entry.ElementId }); } } else { throw Error.NotSupported("Cannot refresh an entry of type {0}", messageArgs: entry.GetType().Name); } } return(result); }
public static Bundle RefreshBundle(this FhirClient client, Bundle bundle) { if (bundle == null) { throw Error.ArgumentNull("bundle"); } // Clone old bundle, without the entries (so, just the header) var oldEntries = bundle.Entries; Bundle result; try { bundle.Entries = new List <BundleEntry>(); var xml = FhirSerializer.SerializeBundleToXml(bundle); result = FhirParser.ParseBundleFromXml(xml); } catch { throw; } finally { bundle.Entries = oldEntries; } result.Id = new Uri("urn:uuid:" + Guid.NewGuid().ToString()); result.LastUpdated = DateTimeOffset.Now; result.Entries = new List <BundleEntry>(); foreach (var entry in bundle.Entries) { if (entry is ResourceEntry) { var newEntry = client.Read(entry.Id); if (entry.Links.Related != null) { newEntry.Links.Related = entry.Links.Related; } result.Entries.Add(newEntry); } else if (entry is DeletedEntry) { result.Entries.Add(entry); } else { throw Error.NotSupported("Cannot refresh an entry of type {0}", messageArgs: entry.GetType().Name); } } return(result); }
public void ReadPatient() { Patient p = new Patient(); p.Id = "pat1"; // if you support this format for the IDs (client allocated ID) p.Name = new System.Collections.Generic.List <HumanName>(); p.Name.Add(new HumanName().WithGiven("Grahame").AndFamily("Grieve")); p.BirthDate = new DateTime(1970, 3, 1).ToFhirDate(); // yes there are extensions to convert to FHIR format p.Active = true; p.ManagingOrganization = new ResourceReference("Organization/2", "Other Org"); Hl7.Fhir.Rest.FhirClient clientFhir = new Hl7.Fhir.Rest.FhirClient(_baseAddress, false); var result = clientFhir.Update <Patient>(p); Assert.IsNotNull(result.Id, "Newly created patient should have an ID"); Assert.IsNotNull(result.Meta, "Newly created patient should have an Meta created"); Assert.IsNotNull(result.Meta.LastUpdated, "Newly created patient should have the creation date populated"); Assert.IsTrue(result.Active.Value, "The patient was created as an active patient"); // read the record to check that it can be loaded result = clientFhir.Read <Patient>("Patient/pat1"); Assert.AreEqual(p.Id, result.Id, "Newly created patient should have an ID"); Assert.IsNotNull(result.Meta, "Newly created patient should have an Meta created"); Assert.IsNotNull(result.Meta.LastUpdated, "Newly created patient should have the creation date populated"); Assert.IsTrue(result.Active.Value, "The patient was created as an active patient"); try { var p4 = clientFhir.Read <Patient>("Patient/missing-client-id"); Assert.Fail("Should have received an exception running this"); } catch (Hl7.Fhir.Rest.FhirOperationException ex) { // This was the expected outcome System.Diagnostics.Trace.WriteLine(ex.Message); } }
public Hl7.Fhir.Model.Patient GetPatientByBusId(string patientId) { var srvConfig = _integrationServicesConfiguration.GetConfigurationService(IntegrationServicesConfiguration.ConfigurationServicesName.BUS); var baseUrl = srvConfig.BaseURL; var getPatientUrl = srvConfig.GetEndPoint(IntegrationService.ConfigurationEndPointName.PATIENT_GET).URL + $"/{patientId}"; var client = new Hl7.Fhir.Rest.FhirClient(baseUrl); client.OnBeforeRequest += OnBeforeRequestFhirServer; client.OnAfterResponse += OnAfterResponseFhirServer; var ret = client.Read <Hl7.Fhir.Model.Patient>(getPatientUrl); return(ret); }
public void Test_ConditionalRead() { Hl7.Fhir.Rest.FhirClient clientFhir = new Hl7.Fhir.Rest.FhirClient(StaticTestData.FhirEndpoint(), false); clientFhir.Timeout = 1000 * 720; // give the call a while to execute (particularly while debugging). string PatientOneId = string.Empty; string PatientOneVersion = string.Empty; DateTimeOffset?PatientOneModified = null; // Prepare 3 test patients Patient PatientOne = new Patient(); string TestPat1 = Guid.NewGuid().ToString(); PatientOne.Id = TestPat1; PatientOne.Name.Add(HumanName.ForFamily("FhirMan").WithGiven("Sam")); PatientOne.BirthDateElement = new Date("1970-01"); PatientOne.Identifier.Add(new Identifier(StaticTestData.TestIdentiferSystem, "6")); Patient ResultOne = null; try { ResultOne = clientFhir.Update(PatientOne); PatientOneId = ResultOne.Id; PatientOneVersion = ResultOne.VersionId; PatientOneModified = ResultOne.Meta.LastUpdated; } catch (Exception Exec) { Assert.True(false, "Exception thrown on resource Get: " + Exec.Message); } Patient PatientTwo = null; try { //Resource has not changed so should return a 304 Not Modified PatientTwo = clientFhir.Read <Patient>($"{StaticTestData.FhirEndpoint()}/Patient/{PatientOneId}", PatientOneVersion, PatientOneModified); Assert.Fail("Conditional Read is expected to throw an exception due to bug in FHIR .NET API"); } catch (FhirOperationException ExecOp) { //Catch the error and check it is a 304 http status. Assert.True(true, "FhirOperationException should be thrown on resource Read: " + ExecOp.Message); Assert.IsTrue(ExecOp.Status.IsRedirection()); } PatientTwo = null; PatientOneModified = PatientOneModified.Value.AddMinutes(-1); try { //If-Modified-Since has been pushed back 1 min so we should get a resource back this time PatientTwo = clientFhir.Read <Patient>($"{StaticTestData.FhirEndpoint()}/Patient/{PatientOneId}", PatientOneVersion, PatientOneModified); Assert.NotNull(PatientTwo, "Not Resource returned when 1 min subtracted from last-modified on Conditional Read."); //reset the PatientOneModified PatientOneModified = PatientTwo.Meta.LastUpdated; } catch (FhirOperationException ExecOp) { Assert.True(false, "FhirOperationException thrown on resource Read: " + ExecOp.Message); Assert.IsTrue(ExecOp.Status.IsRedirection()); } PatientTwo = null; PatientOneVersion = "xxxxx"; try { //If-None-Match version has been set to not match so we should get a resource back this time PatientTwo = clientFhir.Read <Patient>($"{StaticTestData.FhirEndpoint()}/Patient/{PatientOneId}", PatientOneVersion, PatientOneModified); Assert.NotNull(PatientTwo, "Not Resource returned when Resource Version did not match active resource Version on Conditional Read."); } catch (FhirOperationException ExecOp) { Assert.True(false, "FhirOperationException thrown on resource Read: " + ExecOp.Message); } //Clean up by deleting all Test Patients var sp = new SearchParams().Where("identifier=" + StaticTestData.TestIdentiferSystem + "|"); try { clientFhir.Delete("Patient", sp); } catch (Exception Exec) { Assert.True(false, "Exception thrown on conditional delete of resource G: " + Exec.Message); } }