@@ -26,6 +26,10 @@ class Client
2626 attr_accessor :proxy
2727 attr_accessor :exception_class
2828
29+ attr_accessor :use_accept_header
30+ attr_accessor :use_accept_charset
31+ attr_accessor :use_return_preference
32+
2933 # Call method to initialize FHIR client. This method must be invoked
3034 # with a valid base server URL prior to using the client.
3135 #
@@ -37,10 +41,15 @@ def initialize(base_service_url, default_format: FHIR::Formats::ResourceFormat::
3741 @base_service_url = base_service_url
3842 FHIR . logger . info "Initializing client with #{ @base_service_url } "
3943 @use_format_param = false
40- @exception_class = ClientException
44+ @use_accept_header = true
45+ @use_accept_charset = true
4146 @default_format = default_format
4247 @fhir_version = :stu3
48+ @use_return_preference = false
49+ @return_preference = FHIR ::Formats ::ReturnPreferences ::REPRESENTATION
50+ @exception_class = ClientException
4351 @proxy = proxy
52+
4453 set_no_auth
4554 end
4655
@@ -78,6 +87,20 @@ def use_dstu2
7887 end
7988 end
8089
90+ #
91+ # Instructs the client to specify the minimal Prefer Header where applicable
92+ def use_minimal_preference
93+ @use_return_preference = true
94+ @return_preference = FHIR ::Formats ::ReturnPreferences ::MINIMAL
95+ end
96+
97+ #
98+ # Instructs the client to specify the representation Prefer Header where applicable
99+ def use_representation_preference
100+ @use_return_preference = true
101+ @return_preference = FHIR ::Formats ::ReturnPreferences ::REPRESENTATION
102+ end
103+
81104 def versioned_resource_class ( klass )
82105 if @fhir_version == :stu3
83106 FHIR . const_get ( klass )
@@ -259,7 +282,7 @@ def try_conformance_formats(default_format)
259282 @default_format = nil
260283
261284 formats . each do |frmt |
262- reply = get 'metadata' , fhir_headers ( format : frmt )
285+ reply = get 'metadata' , fhir_headers ( { accept : " #{ frmt } " } )
263286 next unless reply . code == 200
264287 begin
265288 @cached_capability_statement = parse_reply ( FHIR ::CapabilityStatement , frmt , reply )
@@ -283,17 +306,15 @@ def try_conformance_formats(default_format)
283306 end
284307
285308 def resource_url ( options )
286- FHIR ::ResourceAddress . new . resource_url ( options , @use_format_param )
309+ FHIR ::ResourceAddress . resource_url ( options , @use_format_param )
287310 end
288311
289312 def full_resource_url ( options )
290313 @base_service_url + resource_url ( options )
291314 end
292315
293316 def fhir_headers ( options = { } )
294- options . merge! ( additional_headers ) unless additional_headers . nil?
295-
296- FHIR ::ResourceAddress . new . fhir_headers ( options , @use_format_param )
317+ FHIR ::ResourceAddress . fhir_headers ( options , additional_headers , @default_format , @use_accept_header , @use_accept_charset )
297318 end
298319
299320 def parse_reply ( klass , format , response )
@@ -359,8 +380,10 @@ def base_path(path)
359380 # Extract the request payload in the specified format, defaults to XML
360381 def request_payload ( resource , headers )
361382 if headers
362- format_specified = headers [ :format ] || headers [ 'format' ]
363- if format_specified . downcase . include? ( 'xml' )
383+ format_specified = headers [ 'Content-Type' ]
384+ if format_specified . nil?
385+ resource . to_xml
386+ elsif format_specified . downcase . include? ( 'xml' )
364387 resource . to_xml
365388 elsif format_specified . downcase . include? ( 'json' )
366389 resource . to_json
@@ -394,7 +417,7 @@ def request_patch_payload(patchset, format)
394417
395418 def clean_headers ( headers )
396419 headers . delete_if { |k , v | ( k . nil? || v . nil? ) }
397- headers . each_with_object ( { } ) { | ( k , v ) , h | h [ k . to_s ] = v . to_s ; h }
420+ FHIR :: ResourceAddress . convert_symbol_headers ( headers )
398421 end
399422
400423 def scrubbed_response_headers ( result )
@@ -404,10 +427,10 @@ def scrubbed_response_headers(result)
404427 end
405428 end
406429
407- def get ( path , headers )
430+ def get ( path , headers = { } )
408431 url = Addressable ::URI . parse ( build_url ( path ) ) . to_s
409432 FHIR . logger . info "GETTING: #{ url } "
410- headers = clean_headers ( headers )
433+ headers = clean_headers ( headers ) unless headers . empty?
411434 if @use_oauth2_auth
412435 # @client.refresh!
413436 begin
@@ -583,7 +606,7 @@ def patch(path, patchset, headers)
583606 url = URI ( build_url ( path ) ) . to_s
584607 FHIR . logger . info "PATCHING: #{ url } "
585608 headers = clean_headers ( headers )
586- payload = request_patch_payload ( patchset , headers [ 'format ' ] )
609+ payload = request_patch_payload ( patchset , headers [ 'Content-Type ' ] )
587610 if @use_oauth2_auth
588611 # @client.refresh!
589612 begin
0 commit comments