-
Notifications
You must be signed in to change notification settings - Fork 8
/
fhir-api.html
799 lines (701 loc) · 35.8 KB
/
fhir-api.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
<!doctype html>
<!--
FHIR Drills
Copyright 2016 Australian Digital Health Agency. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="FHIR Tutorials by the Australian Digital Health Agency to help you understand FHIR and its spec! No technical experience required.">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<title>FHIR APIs | FHIR® tutorials</title>
<!-- Add to homescreen for Chrome on Android -->
<!-- <meta name="mobile-web-app-capable" content="yes">
<link rel="icon" sizes="192x192" href="images/android-desktop.png"> -->
<!-- Add to homescreen for Safari on iOS -->
<!-- <meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="Material Design Lite">
<link rel="apple-touch-icon-precomposed" href="images/ios-desktop.png"> -->
<!-- Tile icon for Win8 (144x144 + tile color) -->
<!-- <meta name="msapplication-TileImage" content="images/touch/ms-touch-icon-144x144-precomposed.png">
<meta name="msapplication-TileColor" content="#3372DF"> -->
<link rel="shortcut icon" href="images/favicon.ico">
<!-- SEO: If your mobile URL is different from the desktop URL, add a canonical link to the desktop page https://developers.google.com/webmasters/smartphone-sites/feature-phones -->
<!--
<link rel="canonical" href="http://www.example.com/">
-->
<!-- CodeMirror -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.4/codemirror.min.css"></link>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:regular,bold,italic,thin,light,bolditalic,black,medium&lang=en">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.deep_purple-pink.min.css">
<link rel="stylesheet" href="styles.css">
<style>
#view-source {
position: fixed;
display: block;
right: 0;
bottom: 0;
margin-right: 40px;
margin-bottom: 40px;
z-index: 900;
}
</style>
<link href="css/lightbox.min.css" rel="stylesheet">
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-80290058-1', 'auto');
ga('send', 'pageview');
</script>
</head>
<body class="mdl-demo mdl-color--grey-100 mdl-color-text--grey-700 mdl-base">
<!-- The drawer is always open in large screens. The header is always shown,
even in small screens. -->
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-drawer
mdl-layout--fixed-header">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<div class="mdl-layout-spacer"></div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable
mdl-textfield--floating-label mdl-textfield--align-right">
<label class="mdl-button mdl-js-button mdl-button--icon"
for="fixed-header-drawer-exp">
<i class="material-icons">search</i>
</label>
<div class="mdl-textfield__expandable-holder">
<input class="mdl-textfield__input" type="text" name="sample"
id="fixed-header-drawer-exp">
</div>
</div>
</div>
</header>
<div class="mdl-layout__drawer">
<span class="mdl-layout-title">FHIR<sup>®</sup> tutorials</span>
<nav class="mdl-navigation">
<a href="./index.html" class="mdl-navigation__link">Overview</a>
<a href="./simple-patient.html" id="simple-patient" class="mdl-navigation__link">Simple Patient</a>
<a href="./simple-search.html" id="simple-search" class="mdl-navigation__link">Simple Search</a>
<a href="./patient-with-references.html" id="patient-with-references" class="mdl-navigation__link">Patient with References</a>
<a href="./bundle.html" id="bundle" class="mdl-navigation__link">Bundle</a>
<a href="./operations.html" id="operations" class="mdl-navigation__link">Operations</a>
<a href="./ValueSet-And-CodeSystem.html" id="ValueSet-And-CodeSystem" class="mdl-navigation__link">ValueSet & CodeSystem</a>
<a href="./conceptmap.html" id="conceptmap" class="mdl-navigation__link">ConceptMap</a>
<a href="./fhir-api.html" id="conceptmap" class="mdl-navigation__link is-active-tab">FHIR APIs</a>
<a href="#" class="mdl-navigation__link"></a>
<a href="./exercises.html" class="mdl-navigation__link">FHIR exercises</a>
</nav>
</div>
<main class="mdl-layout__content">
<section class="section--center mdl-grid mdl-grid--no-spacing mdl-shadow--2dp">
<div class="mdl-card mdl-cell mdl-cell--12-col-desktop mdl-cell--6-col-tablet mdl-cell--4-col-phone">
<div class="mdl-card__supporting-text">
<h4>Working with APIs for FHIR</h4>
<p>If you're a developer, or just someone interested in coding with FHIR, then this is the page for you to read! In this tutorial we'll walk you through doing all of the tutorials you've been doing with Postman programatically.</p>
<note>This tutorial requires technical knowledge to do! If you're not technically minded, you can still learn FHIR by doing other tutorials on this website.</note>
<h6>Click the apropirate button below depending on which programming language you are intrested in. This will help this web site display the examples most relevant to your choice. </h6>
<div id="fhir-api-code-buttons">
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" onclick="switchCodeTabs('csharp')">
Show me C# code
</button>
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" onclick="switchCodeTabs('java')">
Show me Java code
</button>
</div>
<ul class="toc">
<h4>Contents</h4>
<!-- pr prefix stands for 'patient references'-->
<a href="#api-setup">Step 1: Setup</a>
<a href="#api-find-patient">Step 2: Create a basic Patient</a>
<a href="#api-upload-patient">Step 3: Upload your basic Patient</a>
<a href="#api-search">Step 4: Search for a resource</a>
<a href="#api-operations">Step 5: Run an operation</a>
</ul>
<h5 id="api-setup">Step 1: Setup</h5>
<p>FHIR provides some official API code libaraies in Java and .Net to help you work with the standard.</p>
<p>They are:</p>
<b>Java</b>
<p><a href="http://hapifhir.io">HAPI FHIR library</a>: To get started with HAPI, add the dependency <a href="http://hapifhir.io/download.html#Maven_Users">via Maven</a> or <a href="http://hapifhir.io/download.html#Gradle_Users">Gradle</a> - or just <a href="https://github.com/jamesagnew/hapi-fhir/releases/download/v2.3/hapi-fhir-2.3-standard-distribution.zip">download</a> the library locally. If you've done the latter, include all non-hapi jars in your classpath, and only <code>hapi-fhir-base-2.3.jar</code>, <code>hapi-fhir-structures-dstu3-2.3.jar</code>, <code>hapi-fhir-validation-resources-dstu3-2.3.jar</code> hapi ones if you're working with STU3.</p>
<p>The Java examples listed in this tutorial require Java 8 SDK installed, however HAPI itself supports Java all the way back to 6.</p>
<b>Microsoft .Net</b>
<p><a href="https://github.com/ewoutkramer/fhir-net-api">FHIR .NET API</a>: The simplest way to get started with the .Net API is to search for it in the Nuget pacakge manager built into Visual Studio. Right click your Solution and select 'Manage Nuget Packages' and then search and install both 'Hl7.Fhir.STU3' and 'Hl7.FluentPath'. You may also need to select the 'Include prerelease' if you wish to have the latest STU3 release. You can find a C# project <a href="https://github.com/angusmillar/SimpleFhir">here</a> on GitHub that contans all the C# examples seen below.</p>
<h5 id="api-find-patient">Step 2: Create a basic Patient</h5>
<p>Upon creating a new project, you'll see a new blank class not much different from this:</p>
<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect">
<div class="mdl-tabs__tab-bar">
<a href="#csharp-panel" class="mdl-tabs__tab is-active csharp">C#</a>
<a href="#java-panel" class="mdl-tabs__tab java">Java</a>
</div>
<div class="mdl-tabs__panel is-active" id="csharp-panel">
<textarea class="csharp-code">
using System;
using System.Collections.Generic;
namespace SimpleFhir
{
class Program
{
static void Main(string[] args)
{
// write your code here
}
}
}</textarea>
</div>
<div class="mdl-tabs__panel" id="java-panel">
<textarea class="java-code">
package com.myhotcompany;
public class Main {
public static void main(String[] args) {
// write your code here
}
}</textarea>
</div>
</div>
<p>Create a basic Patient resource and populate it with some information. Running this code will print the resource as XML for you:</p>
<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect">
<div class="mdl-tabs__tab-bar">
<a href="#csharp-panel" class="mdl-tabs__tab is-active csharp">C#</a>
<a href="#java-panel" class="mdl-tabs__tab java">Java</a>
</div>
<div class="mdl-tabs__panel is-active" id="csharp-panel">
<textarea class="csharp-code">
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace SimpleFhir
{
class Program
{
static void Main(string[] args)
{
var MyPatient = new Hl7.Fhir.Model.Patient();
//Patient's Name
var PatientName = new Hl7.Fhir.Model.HumanName();
PatientName.Use = Hl7.Fhir.Model.HumanName.NameUse.Official;
PatientName.Prefix = new string[] { "Mr" };
PatientName.Given = new string[] { "Sam" };
PatientName.Family = "Fhirman";
MyPatient.Name = new List<Hl7.Fhir.Model.HumanName>();
MyPatient.Name.Add(PatientName);
//Patient Identifier
var PatientIdentifier = new Hl7.Fhir.Model.Identifier();
PatientIdentifier.System = "http://ns.electronichealth.net.au/id/hi/ihi/1.0";
PatientIdentifier.Value = "8003608166690503";
MyPatient.Identifier = new List<Hl7.Fhir.Model.Identifier>();
MyPatient.Identifier.Add(PatientIdentifier);
Console.WriteLine("Press any key to serialise Resource to the console as XML.");
Console.ReadKey();
Console.WriteLine("");
try
{
//attempt to serialize the resource
string xml = Hl7.Fhir.Serialization.FhirSerializer.SerializeResourceToXml(MyPatient);
XDocument xDoc = XDocument.Parse(xml);
Console.Write(xDoc.ToString());
Console.WriteLine("");
Console.WriteLine("");
}
catch (Exception Exec)
{
Console.Write("Error message: " + Exec.Message);
}
Console.Write("Press any key to end.");
Console.ReadKey();
}
}
}</textarea>
</div>
<div class="mdl-tabs__panel" id="java-panel">
<textarea class="java-code">
package com.myhotcompany;
import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.dstu3.model.HumanName;
import org.hl7.fhir.dstu3.model.Patient;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
// context - create this once, as it's an expensive operation
// see http://hapifhir.io/doc_intro.html
FhirContext ctx = FhirContext.forDstu3();
Patient patient = new Patient();
// you can use the Fluent API to chain calls
// see http://hapifhir.io/doc_fhirobjects.html
patient.addName().setUse(HumanName.NameUse.OFFICIAL)
.addPrefix("Mr").addFamily("Fhirman").addGiven("Sam");
patient.addIdentifier()
.setSystem("http://ns.electronichealth.net.au/id/hi/ihi/1.0")
.setValue("8003608166690503");
System.out.println("Press Enter to serialise Resource to the console as XML.");
System.in.read();
// create a new XML parser and serialize our Patient object with it
String encoded = ctx.newXmlParser().setPrettyPrint(true)
.encodeResourceToString(patient);
System.out.println(encoded);
System.out.println("Press Enter to end.");
System.in.read();
}
}</textarea>
</div>
</div>
<p>Once run, you should see something like this:</p>
<textarea class="fhir-resource-xml">
<Patient xmlns="http://hl7.org/fhir">
<identifier>
<system value="http://ns.electronichealth.net.au/id/hi/ihi/1.0"/>
<value value="8003608166690503"/>
</identifier>
<name>
<use value="official"/>
<family value="Fhirman"/>
<given value="Sam"/>
<prefix value="Mr"/>
</name>
</Patient></textarea>
<p>If your IDE supports <a href="https://en.wikipedia.org/wiki/Intelligent_code_completion">code completion</a>, try using it to explore the Patient resource and populate it with some information such as the gender, birthday and phone number. Otherwise, consult the FHIR <a href="http://hl7.org/fhir/STU3/patient.html">Patient</a> page to see what can you populate your resource with (<a href="http://hapifhir.io/apidocs-dstu3/org/hl7/fhir/dstu3/model/Patient.html">Javadoc</a> is also available).</p>
<h5 id="api-upload-patient">Step 3: Upload your basic Patient</h5>
<p>Now that we can create a simple resource, let's upload it to a FHIR server:</p>
<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect">
<div class="mdl-tabs__tab-bar">
<a href="#csharp-panel" class="mdl-tabs__tab is-active csharp">C#</a>
<a href="#java-panel" class="mdl-tabs__tab java">Java</a>
</div>
<div class="mdl-tabs__panel is-active" id="csharp-panel">
<textarea class="csharp-code">
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace SimpleFhir
{
class Program
{
static void Main(string[] args)
{
//The fhir server end point address
string ServiceRootUrl = "https://hapi.fhir.org/baseDstu3";
//Create a patient resource instance
var MyPatient = new Hl7.Fhir.Model.Patient();
//Patient's Name
var PatientName = new Hl7.Fhir.Model.HumanName();
PatientName.Use = Hl7.Fhir.Model.HumanName.NameUse.Official;
PatientName.Prefix = new string[] { "Mr" };
PatientName.Given = new string[] { "Sam" };
PatientName.Family = "Fhirman" ;
MyPatient.Name = new List<Hl7.Fhir.Model.HumanName>();
MyPatient.Name.Add(PatientName);
//Patient Identifier
var PatientIdentifier = new Hl7.Fhir.Model.Identifier();
PatientIdentifier.System = "http://ns.electronichealth.net.au/id/hi/ihi/1.0";
PatientIdentifier.Value = "8003608166690503";
MyPatient.Identifier = new List<Hl7.Fhir.Model.Identifier>();
MyPatient.Identifier.Add(PatientIdentifier);
//Create a client to send to the server at a given endpoint.
var FhirClient = new Hl7.Fhir.Rest.FhirClient(ServiceRootUrl);
// increase timeouts since the server might be powered down
FhirClient.Timeout = (60 * 1000);
Console.WriteLine("Press any key to send to server: " + ServiceRootUrl);
Console.ReadKey();
try
{
//Attempt to send the resource to the server endpoint
Hl7.Fhir.Model.Patient ReturnedPatient = FhirClient.Create<Hl7.Fhir.Model.Patient>(MyPatient);
Console.WriteLine(string.Format("Resource is available at: {0}", ReturnedPatient.Id));
Console.WriteLine();
Console.WriteLine("This is what we sent up: ");
Console.WriteLine();
string xml = Hl7.Fhir.Serialization.FhirSerializer.SerializeResourceToXml(MyPatient);
XDocument xDoc = XDocument.Parse(xml);
Console.WriteLine(xDoc.ToString());
Console.WriteLine();
Console.WriteLine("This is what we received back: ");
Console.WriteLine();
xml = Hl7.Fhir.Serialization.FhirSerializer.SerializeResourceToXml(ReturnedPatient);
xDoc = XDocument.Parse(xml);
Console.WriteLine(xDoc.ToString());
Console.WriteLine();
}
catch (Hl7.Fhir.Rest.FhirOperationException FhirOpExec)
{
//Process any Fhir Errors returned as OperationOutcome resource
Console.WriteLine();
Console.WriteLine("An error message: " + FhirOpExec.Message);
Console.WriteLine();
string xml = Hl7.Fhir.Serialization.FhirSerializer.SerializeResourceToXml(FhirOpExec.Outcome);
XDocument xDoc = XDocument.Parse(xml);
Console.WriteLine(xDoc.ToString());
}
catch (Exception GeneralException)
{
Console.WriteLine();
Console.WriteLine("An error message: " + GeneralException.Message);
Console.WriteLine();
}
Console.WriteLine("Press any key to end.");
Console.ReadKey();
}
}
}</textarea>
</div>
<div class="mdl-tabs__panel" id="java-panel">
<textarea class="java-code">
package com.myhotcompany;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.IGenericClient;
import org.hl7.fhir.dstu3.model.*;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
// context - create this once, as it's an expensive operation
// see http://hapifhir.io/doc_intro.html
FhirContext ctx = FhirContext.forDstu3();
String serverBaseUrl = "http://sqlonfhir-stu3.azurewebsites.net/fhir";
Patient ourPatient = new Patient();
// you can use the Fluent API to chain calls
// see http://hapifhir.io/doc_fhirobjects.html
ourPatient.addName().setUse(HumanName.NameUse.OFFICIAL)
.addPrefix("Mr").addFamily("Fhirman").addGiven("Sam");
ourPatient.addIdentifier()
.setSystem("http://ns.electronichealth.net.au/id/hi/ihi/1.0")
.setValue("8003608166690503");
// increase timeouts since the server might be powered down
// see http://hapifhir.io/doc_rest_client_http_config.html
ctx.getRestfulClientFactory().setConnectTimeout(60 * 1000);
ctx.getRestfulClientFactory().setSocketTimeout(60 * 1000);
// create the RESTful client to work with our FHIR server
// see http://hapifhir.io/doc_rest_client.html
IGenericClient client = ctx.newRestfulGenericClient(serverBaseUrl);
System.out.println("Press Enter to send to server: " + serverBaseUrl);
System.in.read();
try {
// send our resource up - result will be stored in 'outcome'
// see http://hapifhir.io/doc_rest_client.html#Create_-_Type
MethodOutcome outcome = client.create()
.resource(ourPatient)
.prettyPrint()
.encodedXml()
.execute();
IdType id = (IdType) outcome.getId();
System.out.println("Resource is available at: " + id.getValue());
IParser xmlParser = ctx.newXmlParser().setPrettyPrint(true);
Patient receivedPatient = (Patient) outcome.getResource();
System.out.println("This is what we sent up: \n"
+ xmlParser.encodeResourceToString(ourPatient)
+ "\n\nThis is what we received: \n"
+ xmlParser.encodeResourceToString(receivedPatient));
} catch (DataFormatException e) {
System.out.println("An error occurred trying to upload:");
e.printStackTrace();
}
System.out.println("Press Enter to end.");
System.in.read();
}
}</textarea>
</div>
</div>
<p>Comparing the resource that we sent up to the resource we received back, you'll notice that the server generated a basic narrative for us from the data available and also added its <a href="http://hl7.org/fhir/STU3/resource.html#id">logical ID</a></p>
<h5 id="api-search">Step 4: Search for a resource</h5>
<p>Now that you've uploaded a resource, try searching for it. Remember that you'll get <a href="bundle.html">a bundle</a> back with many entries, one resource per entry.</p>
<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect">
<div class="mdl-tabs__tab-bar">
<a href="#csharp-panel" class="mdl-tabs__tab is-active csharp">C#</a>
<a href="#java-panel" class="mdl-tabs__tab java">Java</a>
</div>
<div class="mdl-tabs__panel is-active" id="csharp-panel">
<textarea class="csharp-code">
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace SimpleFhir
{
class Program
{
static void Main(string[] args)
{
//The fhir server end point address
string ServiceRootUrl = "https://hapi.fhir.org/baseDstu3";
//Create a client to send to the server at a given endpoint.
var FhirClient = new Hl7.Fhir.Rest.FhirClient(ServiceRootUrl);
// increase timeouts since the server might be powered down
FhirClient.Timeout = (60 * 1000);
Console.WriteLine("Press any key to send to server: " + ServiceRootUrl);
Console.WriteLine();
Console.ReadKey();
try
{
//Attempt to send the resource to the server endpoint
Hl7.Fhir.Model.Bundle ReturnedSearchBundle = FhirClient.Search<Hl7.Fhir.Model.Patient>(new string[] { "family=Fhirman" });
Console.WriteLine(string.Format("Found: {0} Fhirman patients.", ReturnedSearchBundle.Total.ToString()));
Console.WriteLine("Their logical IDs are:");
foreach (var Entry in ReturnedSearchBundle.Entry)
{
Console.WriteLine("ID: " + Entry.Resource.Id);
}
Console.WriteLine();
}
catch (Hl7.Fhir.Rest.FhirOperationException FhirOpExec)
{
//Process any Fhir Errors returned as OperationOutcome resource
Console.WriteLine();
Console.WriteLine("An error message: " + FhirOpExec.Message);
Console.WriteLine();
string xml = Hl7.Fhir.Serialization.FhirSerializer.SerializeResourceToXml(FhirOpExec.Outcome);
XDocument xDoc = XDocument.Parse(xml);
Console.WriteLine(xDoc.ToString());
}
catch (Exception GeneralException)
{
Console.WriteLine();
Console.WriteLine("An error message: " + GeneralException.Message);
Console.WriteLine();
}
Console.WriteLine("Press any key to end.");
Console.ReadKey();
}
}
}</textarea>
</div>
<div class="mdl-tabs__panel" id="java-panel">
<textarea class="java-code">
package com.myhotcompany;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.client.IGenericClient;
import org.hl7.fhir.dstu3.model.*;
import java.io.IOException;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
// context - create this once, as it's an expensive operation
// see http://hapifhir.io/doc_intro.html
FhirContext ctx = FhirContext.forDstu3();
String serverBaseUrl = "http://sqlonfhir-stu3.azurewebsites.net/fhir";
// increase timeouts since the server might be powered down
// see http://hapifhir.io/doc_rest_client_http_config.html
ctx.getRestfulClientFactory().setConnectTimeout(60 * 1000);
ctx.getRestfulClientFactory().setSocketTimeout(60 * 1000);
// create the RESTful client to work with our FHIR server
// see http://hapifhir.io/doc_rest_client.html
IGenericClient client = ctx.newRestfulGenericClient(serverBaseUrl);
System.out.println("Press Enter to send to server: " + serverBaseUrl);
System.in.read();
try {
// perform a search for Patients with last name 'Fhirman'
// see http://hapifhir.io/doc_rest_client.html#SearchQuery_-_Type
Bundle response = client.search()
.forResource(Patient.class)
.where(Patient.FAMILY.matches().values("Fhirman"))
.returnBundle(Bundle.class)
.execute();
System.out.println("Found " + response.getTotal()
+ " Fhirman patients. Their logical IDs are:");
response.getEntry().forEach((entry) -> {
// within each entry is a resource - print its logical ID
System.out.println(entry.getResource().getIdElement().getIdPart());
});
} catch (Exception e) {
System.out.println("An error occurred trying to search:");
e.printStackTrace();
}
System.out.println("Press Enter to end.");
System.in.read();
}
}</textarea>
</div>
</div>
<h5 id="api-operations">Step 5: Run an operation</h5>
<p>Lastly, let's execute an operation on the server. A simple operation we can do is the <a href="https://hl7-fhir.github.io/patient-operations.html#everything">Patient/$everything</a> - this'll fetch us the Patient resource and all related resources to it.</p>
<p>Firstly, lets upload the sample Patient we've explored before in the <a href="patient-with-references.html">Patient with References</a> tutorial:</p>
<p>
<center>
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent" id="patient-with-references-button">
Upload</button>
<br>
<div id="patient-with-references-progress" class="mdl-progress mdl-js-progress" style="display: none;"></div>
</center>
</p>
<p>Now we can run a query on the operation to get a Bundle with the Patient and associated resources. Here's how it looks in code. Make sure to update MY_PATIENT_ID with <span class="patient-with-references-serverid" id="rf-patient">(press Upload first)</span>.</p>
<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect">
<div class="mdl-tabs__tab-bar">
<a href="#csharp-panel" class="mdl-tabs__tab is-active csharp">C#</a>
<a href="#java-panel" class="mdl-tabs__tab java">Java</a>
</div>
<div class="mdl-tabs__panel is-active" id="csharp-panel">
<textarea class="csharp-code">
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace SimpleFhir
{
class Program
{
static void Main(string[] args)
{
//The fhir server end point address
string ServiceRootUrl = "http://sqlonfhir-stu3.azurewebsites.net/fhir";
//Create a client to send to the server at a given endpoint.
var FhirClient = new Hl7.Fhir.Rest.FhirClient(ServiceRootUrl);
// increase timeouts since the server might be powered down
FhirClient.Timeout = (60 * 1000);
Console.WriteLine("Press any key to send to server: " + ServiceRootUrl);
Console.WriteLine();
Console.ReadKey();
try
{
//Attempt to send the resource to the server endpoint
UriBuilder UriBuilderx = new UriBuilder(ServiceRootUrl);
UriBuilderx.Path = "Patient/MY_PATIENT_ID";
Hl7.Fhir.Model.Resource ReturnedResource = FhirClient.InstanceOperation(UriBuilderx.Uri, "everything");
if (ReturnedResource is Hl7.Fhir.Model.Bundle)
{
Hl7.Fhir.Model.Bundle ReturnedBundle = ReturnedResource as Hl7.Fhir.Model.Bundle;
Console.WriteLine("Received: " + ReturnedBundle.Total + " results, the resources are: ");
foreach (var Entry in ReturnedBundle.Entry)
{
Console.WriteLine(string.Format("{0}/{1}",Entry.Resource.TypeName, Entry.Resource.Id));
}
}
else
{
throw new Exception("Operation call must return a bundle resource");
}
Console.WriteLine();
}
catch (Hl7.Fhir.Rest.FhirOperationException FhirOpExec)
{
//Process any Fhir Errors returned as OperationOutcome resource
Console.WriteLine();
Console.WriteLine("An error message: " + FhirOpExec.Message);
Console.WriteLine();
string xml = Hl7.Fhir.Serialization.FhirSerializer.SerializeResourceToXml(FhirOpExec.Outcome);
XDocument xDoc = XDocument.Parse(xml);
Console.WriteLine(xDoc.ToString());
}
catch (Exception GeneralException)
{
Console.WriteLine();
Console.WriteLine("An error message: " + GeneralException.Message);
Console.WriteLine();
}
Console.WriteLine("Press any key to end.");
Console.ReadKey();
}
}
}</textarea>
</div>
<div class="mdl-tabs__panel" id="java-panel">
<textarea class="java-code">
package com.myhotcompany;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import org.hl7.fhir.dstu3.model.*;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
// context - create this once, as it's an expensive operation
// see http://hapifhir.io/doc_intro.html
FhirContext ctx = FhirContext.forDstu3();
String serverBaseUrl = "http://sqlonfhir-stu3.azurewebsites.net/fhir";
// increase timeouts since the server might be powered down
// see http://hapifhir.io/doc_rest_client_http_config.html
ctx.getRestfulClientFactory().setConnectTimeout(60 * 1000);
ctx.getRestfulClientFactory().setSocketTimeout(60 * 1000);
// create the RESTful client to work with our FHIR server
// see http://hapifhir.io/doc_rest_client.html
IGenericClient client = ctx.newRestfulGenericClient(serverBaseUrl);
client.registerInterceptor(new LoggingInterceptor(true));
System.out.println("Press Enter to send to server: " + serverBaseUrl);
System.in.read();
try {
// Invoke $everything on our Patient
// See http://hapifhir.io/doc_rest_client.html#Extended_Operations
Parameters outParams = client
.operation()
.onInstance(new IdType("Patient", "MY_PATIENT_ID"))
.named("$everything")
.withNoParameters(Parameters.class) // No input parameters
.execute();
// FHIR normally returns a 'Parameters' resource to an operation, but
// in case of a single resource response, it just returns the resource
// itself. This is why it seems that we have to fish a Bundle out of the
// resulting Params result - HAPI needs to update for the FHIR shortcut
Bundle result = (Bundle) outParams.getParameterFirstRep().getResource();
System.out.println("Received " + result.getTotal()
+ " results. The resources are:");
result.getEntry().forEach((entry) -> {
Resource resource = entry.getResource();
System.out.println(resource.getResourceType() + "/"
+ resource.getIdElement().getIdPart());
});
} catch (Exception e) {
System.out.println("An error occurred trying to run the operation:");
e.printStackTrace();
}
System.out.println("Press Enter to end.");
System.in.read();
}
}</textarea>
</div>
</div>
<p>Try running the code and you should see the same IDs for related resources as on this page:</p>
<pre>Patient/<span class="patient-with-references-serverid" id="rf-patient">(press Upload first)</span></pre>
<pre>Encounter/<span class="patient-with-references-serverid" id="rf-encounter">(press Upload first)</span></pre>
<pre>Observation/<span class="patient-with-references-serverid" id="rf-observation1">(press Upload first)</span></pre>
<pre>Observation/<span class="patient-with-references-serverid" id="rf-observation2">(press Upload first)</span></pre>
<pre>DiagnosticReport/<span class="patient-with-references-serverid" id="rf-diagnosticreport">(press Upload first)</span></pre>
<p>That's about it for the basics! Happy coding. Check out the <a href="http://hapifhir.io/docindex.html">HAPI documentation</a> if you're using Java for more as well.</p>
</div>
</div>
</section>
<footer class="mdl-mega-footer">
<div class="mdl-mega-footer--middle-section">
<div class="mdl-mega-footer--drop-down-section">
<input class="mdl-mega-footer--heading-checkbox" type="checkbox" checked>
<h1 class="mdl-mega-footer--heading">The Agency</h1>
<ul class="mdl-mega-footer--link-list">
<li><a href="https://www.digitalhealth.gov.au/">digitalhealth.gov.au</a></li>
<li><a href="https://myhealthrecord.gov.au/internet/mhr/publishing.nsf/content/home" title="Australian national health record">My Health Record</a></li>
</ul>
</div>
<div class="mdl-mega-footer--drop-down-section">
<input class="mdl-mega-footer--heading-checkbox" type="checkbox" checked>
<h1 class="mdl-mega-footer--heading">FHIR</h1>
<ul class="mdl-mega-footer--link-list">
<li><a href="http://wiki.hl7.org/index.php?title=FHIR_Connectathon_15" title="Next FHIR get-together in Madrid in May 2017">Connectathon 15</a></li>
<li><a href="http://hl7.org/fhir/STU3" title="Latest release of FHIR, for use in Madrid Connectathon">FHIR STU3 Release</a></li>
<li><a href="http://build.fhir.org/" title="The latest FHIR as it is in development">FHIR Continuous Integration Build</a></li>
</ul>
</div>
</div>
</footer>
</main>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.4/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.4/mode/xml/xml.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.4/mode/javascript/javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.13.4/mode/clike/clike.min.js"></script>
<script src="https://code.getmdl.io/1.1.3/material.min.js"></script>
<script src="js/lightbox-plus-jquery.min.js"></script>
<!-- lua.vm.js -->
<script src="js/lua.vm.js/dist/lua.vm.js"></script>
<!-- main javascript for this page-->
<script src="js/main.js"></script>
<!-- supporting javascript to actually do the uploading -->
<script src="js/post-resources.js"></script>
</body>
</html>