@@ -11,6 +11,7 @@ class Client
1111 include FHIR ::Sections ::Search
1212 include FHIR ::Sections ::Operations
1313 include FHIR ::Sections ::Transactions
14+ include FHIR ::VersionManagement
1415
1516 attr_accessor :reply
1617 attr_accessor :use_format_param
@@ -37,14 +38,14 @@ class Client
3738 # @param default_format Default Format Mime type
3839 # @return
3940 #
40- def initialize ( base_service_url , default_format : FHIR ::Formats ::ResourceFormat ::RESOURCE_XML , proxy : nil )
41+ def initialize ( base_service_url , default_format : FHIR ::Formats ::ResourceFormat ::RESOURCE_JSON , proxy : nil )
4142 @base_service_url = base_service_url
4243 FHIR . logger . info "Initializing client with #{ @base_service_url } "
4344 @use_format_param = false
4445 @use_accept_header = true
4546 @use_accept_charset = true
4647 @default_format = default_format
47- @fhir_version = :stu3
48+ @fhir_version = :r4
4849 @use_return_preference = false
4950 @return_preference = FHIR ::Formats ::ReturnPreferences ::REPRESENTATION
5051 @exception_class = ClientException
@@ -54,37 +55,26 @@ def initialize(base_service_url, default_format: FHIR::Formats::ResourceFormat::
5455 end
5556
5657 def default_json
57- @default_format = if @fhir_version == :stu3
58- FHIR ::Formats ::ResourceFormat ::RESOURCE_JSON
59- else
60- FHIR ::Formats ::ResourceFormat ::RESOURCE_JSON_DSTU2
61- end
58+ @default_format = versioned_format_class ( :json )
6259 end
6360
6461 def default_xml
65- @default_format = if @fhir_version == :stu3
66- FHIR ::Formats ::ResourceFormat ::RESOURCE_XML
67- else
68- FHIR ::Formats ::ResourceFormat ::RESOURCE_XML_DSTU2
69- end
62+ @default_format = versioned_format_class ( :xml )
7063 end
7164
7265 def use_stu3
7366 @fhir_version = :stu3
74- @default_format = if @default_format . include? ( 'xml' )
75- FHIR ::Formats ::ResourceFormat ::RESOURCE_XML
76- else
77- FHIR ::Formats ::ResourceFormat ::RESOURCE_JSON
78- end
67+ @default_format = versioned_format_class
7968 end
8069
8170 def use_dstu2
8271 @fhir_version = :dstu2
83- @default_format = if @default_format . include? ( 'xml' )
84- FHIR ::Formats ::ResourceFormat ::RESOURCE_XML_DSTU2
85- else
86- FHIR ::Formats ::ResourceFormat ::RESOURCE_JSON_DSTU2
87- end
72+ @default_format = versioned_format_class
73+ end
74+
75+ def use_r4
76+ @fhir_version = :r4
77+ @default_format = versioned_format_class
8878 end
8979
9080 #
@@ -101,23 +91,19 @@ def use_representation_preference
10191 @return_preference = FHIR ::Formats ::ReturnPreferences ::REPRESENTATION
10292 end
10393
104- def versioned_resource_class ( klass )
105- if @fhir_version == :stu3
106- FHIR . const_get ( klass )
107- else
108- FHIR ::DSTU2 . const_get ( klass )
109- end
110- end
111-
11294 def detect_version
11395 cap = capability_statement
11496 if cap . is_a? ( FHIR ::CapabilityStatement )
115- @fhir_version = :stu3
97+ use_r4
98+ elsif cap . is_a? ( FHIR ::STU3 ::CapabilityStatement )
99+ use_stu3
116100 elsif cap . is_a? ( FHIR ::DSTU2 ::Conformance )
117- @fhir_version = :dstu2
101+ use_dstu2
118102 else
119- @fhir_version = :stu3
103+ use_r4
120104 end
105+ # Should update the default_format when changing fhir_version
106+ @default_format = versioned_format_class
121107 FHIR . logger . info ( "Detecting server FHIR version as #{ @fhir_version } via metadata" )
122108 @fhir_version
123109 end
@@ -279,22 +265,31 @@ def try_conformance_formats(default_format)
279265 formats . insert ( 0 , default_format )
280266
281267 @cached_capability_statement = nil
282- @default_format = nil
283268
284269 formats . each do |frmt |
285270 reply = get 'metadata' , fhir_headers ( { accept : "#{ frmt } " } )
286271 next unless reply . code == 200
272+ use_r4
287273 begin
288274 @cached_capability_statement = parse_reply ( FHIR ::CapabilityStatement , frmt , reply )
289275 rescue
290276 @cached_capability_statement = nil
291277 end
292- unless @cached_capability_statement
278+ if @cached_capability_statement . nil? || !@cached_capability_statement . fhirVersion . starts_with? ( '4' )
279+ use_stu3
293280 begin
294- @cached_capability_statement = parse_reply ( FHIR ::DSTU2 :: Conformance , frmt , reply )
281+ @cached_capability_statement = parse_reply ( FHIR ::STU3 :: CapabilityStatement , frmt , reply )
295282 rescue
296283 @cached_capability_statement = nil
297284 end
285+ unless @cached_capability_statement
286+ use_dstu2
287+ begin
288+ @cached_capability_statement = parse_reply ( FHIR ::DSTU2 ::Conformance , frmt , reply )
289+ rescue
290+ @cached_capability_statement = nil
291+ end
292+ end
298293 end
299294 if @cached_capability_statement
300295 @default_format = frmt
@@ -328,12 +323,18 @@ def parse_reply(klass, format, response)
328323 else
329324 FHIR ::DSTU2 ::Json . from_json ( response . body )
330325 end
331- else
326+ elsif ( @fhir_version == :r4 || klass &. ancestors &. include? ( FHIR :: Model ) )
332327 if ( format . include? ( 'xml' ) )
333328 FHIR ::Xml . from_xml ( response . body )
334329 else
335330 FHIR ::Json . from_json ( response . body )
336331 end
332+ else
333+ if ( format . include? ( 'xml' ) )
334+ FHIR ::STU3 ::Xml . from_xml ( response . body )
335+ else
336+ FHIR ::STU3 ::Json . from_json ( response . body )
337+ end
337338 end
338339 res . client = self unless res . nil?
339340 FHIR . logger . warn "Expected #{ klass } but got #{ res . class } " if res . class != klass
@@ -353,11 +354,7 @@ def reissue_request(request)
353354 method ( request [ 'method' ] ) . call ( request [ 'url' ] , request [ 'headers' ] )
354355 elsif [ :post , :put ] . include? ( request [ 'method' ] )
355356 unless request [ 'payload' ] . nil?
356- resource = if @fhir_version == :stu3
357- FHIR . from_contents ( request [ 'payload' ] )
358- else
359- FHIR ::DSTU2 . from_contents ( request [ 'payload' ] )
360- end
357+ resource = versioned_resource_class . from_contents ( request [ 'payload' ] )
361358 end
362359 method ( request [ 'method' ] ) . call ( request [ 'url' ] , resource , request [ 'headers' ] )
363360 end
0 commit comments