Skip to content

Commit

Permalink
FI-1305: Use local client (#143)
Browse files Browse the repository at this point in the history
* prevent logging during unit tests

* prevent coverage report from automatically opening

* update minimum fhir_models versions

* store client on individual resources and references

* add tests for create and search

* set client on operation and transaction responses

* add test for reference#read

* bump version
  • Loading branch information
Jammjammjamm authored Aug 12, 2021
1 parent 94bbf4b commit a69338c
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 47 deletions.
4 changes: 0 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ task :rubocop do
RuboCop::RakeTask.new
end

task :test do
system('open coverage/index.html')
end

task default: [:test]
6 changes: 3 additions & 3 deletions fhir_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Gem::Specification.new do |spec|

spec.add_dependency 'activesupport', '>= 3'
spec.add_dependency 'addressable', '>= 2.3'
spec.add_dependency 'fhir_models', '>= 4.0.2'
spec.add_dependency 'fhir_stu3_models', '>= 3.0.1'
spec.add_dependency 'fhir_dstu2_models', '>= 1.0.10'
spec.add_dependency 'fhir_models', '>= 4.2.0'
spec.add_dependency 'fhir_stu3_models', '>= 3.1.0'
spec.add_dependency 'fhir_dstu2_models', '>= 1.1.0'
spec.add_dependency 'nokogiri', '>= 1.10.4'
spec.add_dependency 'oauth2', '~> 1.1'
spec.add_dependency 'rack', '>= 1.5'
Expand Down
61 changes: 35 additions & 26 deletions lib/fhir_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,35 +315,44 @@ def fhir_headers(options = {})
def parse_reply(klass, format, response)
FHIR.logger.debug "Parsing response with {klass: #{klass}, format: #{format}, code: #{response.code}}."
return nil unless [200, 201].include? response.code
res = nil
begin
res = if(@fhir_version == :dstu2 || klass&.ancestors&.include?(FHIR::DSTU2::Model))
if(format.include?('xml'))
FHIR::DSTU2::Xml.from_xml(response.body)
else
FHIR::DSTU2::Json.from_json(response.body)
end
elsif(@fhir_version == :r4 || klass&.ancestors&.include?(FHIR::Model))
if(format.include?('xml'))
FHIR::Xml.from_xml(response.body)
else
FHIR::Json.from_json(response.body)
end
else
if(format.include?('xml'))
FHIR::STU3::Xml.from_xml(response.body)
else
FHIR::STU3::Json.from_json(response.body)
end
end
res.client = self unless res.nil?
rescue => e
FHIR.logger.error "Failed to parse #{format} as resource #{klass}: #{e.message}"
res = nil
end
res =
begin
if(@fhir_version == :dstu2 || klass&.ancestors&.include?(FHIR::DSTU2::Model))
if(format.include?('xml'))
FHIR::DSTU2::Xml.from_xml(response.body)
else
FHIR::DSTU2::Json.from_json(response.body)
end
elsif(@fhir_version == :r4 || klass&.ancestors&.include?(FHIR::Model))
if(format.include?('xml'))
FHIR::Xml.from_xml(response.body)
else
FHIR::Json.from_json(response.body)
end
else
if(format.include?('xml'))
FHIR::STU3::Xml.from_xml(response.body)
else
FHIR::STU3::Json.from_json(response.body)
end
end
rescue => e
FHIR.logger.error "Failed to parse #{format} as resource #{klass}: #{e.message}"
nil
end
set_client_on_resource(res) unless res.nil?
res
end

def set_client_on_resource(resource)
return if resource.nil?

resource.client = self
resource.each_element do |element, _, _|
element.client = self if element.is_a?(Reference) || element.respond_to?(:resourceType)
end
end

def strip_base(path)
path.gsub(@base_service_url, '')
end
Expand Down
11 changes: 4 additions & 7 deletions lib/fhir_client/ext/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ module FHIR
module ModelExtras

def self.included base
base.send :include, InstanceMethods
base.include InstanceMethods
base.extend ClassMethods
base.attr_writer :client
end

module InstanceMethods
def client
FHIR::Model.client
end

def client=(client)
FHIR::Model.client = client
@client || FHIR::Model.client
end

def vread(version_id)
Expand Down Expand Up @@ -146,4 +143,4 @@ class Model
include FHIR::ModelExtras
end
end
end
end
2 changes: 1 addition & 1 deletion lib/fhir_client/sections/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def base_create(resource, options, format = nil, additional_header = {})
resource.id = FHIR::ResourceAddress.pull_out_id(resource.class.name.demodulize, reply.self_link)
reply.resource = resource # just send back the submitted resource
end
reply.resource.client = self
set_client_on_resource(reply.resource)
reply.resource_class = resource.class
reply
end
Expand Down
13 changes: 9 additions & 4 deletions lib/fhir_client/sections/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ def match(resource, options = {}, format = @default_format)
add_resource_parameter(params, 'resource', resource)
add_parameter(params, 'onlyCertainMatches', 'Boolean', options[:onlyCertainMatches]) unless options[:onlyCertainMatches].nil?
add_parameter(params, 'count', 'Integer', options[:matchCount]) if options[:matchCount].is_a?(Integer)
post resource_url(options), params, fhir_headers({content_type: "#{format || @default_format}",
accept: "#{format || @default_format}"})
post(
resource_url(options),
params,
fhir_headers({content_type: "#{format || @default_format}", accept: "#{format || @default_format}"})
).tap { |reply| set_client_on_resource(reply.resource) }
end

#
Expand All @@ -154,7 +157,8 @@ def validate(resource, options = {}, format = @default_format)
params = versioned_resource_class('Parameters').new
add_resource_parameter(params, 'resource', resource)
add_parameter(params, 'profile', 'Uri', options[:profile_uri]) unless options[:profile_uri].nil?
post resource_url(options), params, fhir_headers(headers)
post(resource_url(options), params, fhir_headers(headers))
.tap { |reply| set_client_on_resource(reply.resource) }
end

def validate_existing(resource, id, options = {}, format = @default_format)
Expand All @@ -165,7 +169,8 @@ def validate_existing(resource, id, options = {}, format = @default_format)
params = versioned_resource_class('Parameters').new
add_resource_parameter(params, 'resource', resource)
add_parameter(params, 'profile', 'Uri', options[:profile_uri]) unless options[:profile_uri].nil?
post resource_url(options), params, fhir_headers(headers)
post(resource_url(options), params, fhir_headers(headers))
.tap { |reply| set_client_on_resource(reply.resource) }
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/fhir_client/sections/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def end_batch(format = @default_format)
rescue
reply.resource = nil
end
set_client_on_resource(reply.resource)
reply.resource_class = reply.resource.class
reply
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fhir_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module FHIR
class Client
VERSION = '4.0.6'.freeze
VERSION = '5.0.0'.freeze
end
end
4 changes: 3 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
require 'webmock/test_unit'

require 'fhir_client'
FHIR.logger.level = Logger::ERROR
FHIR.logger = Logger.new('/dev/null')
FHIR::STU3.logger = Logger.new('/dev/null')
FHIR::DSTU2.logger = Logger.new('/dev/null')

ACCEPT_REGEX_XML = /^(\s*application\/fhir\+xml\s*)(;\s*charset\s*=\s*utf-8\s*)?$/
ACCEPT_REGEX_JSON = /^(\s*application\/fhir\+json\s*)(;\s*charset\s*=\s*utf-8\s*)?$/
Expand Down
24 changes: 24 additions & 0 deletions test/unit/client_interface_sections/create_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,28 @@ def test_condiitonal_create
assert reply.id == 'foo'
end

def test_create_sets_client_on_resource
patient = FHIR::Patient.new({'gender'=>'female', 'active'=>true, 'deceasedBoolean'=>false})
outcome = FHIR::OperationOutcome.new({'issue'=>[{'code'=>'informational', 'severity'=>'information', 'diagnostics'=>'Successfully created "Patient/foo" in 0 ms'}]})

stub_request(:post, /create-test/)
.with(headers: {'Content-Type'=>'application/fhir+json;charset=utf-8'})
.to_return(status: 201,
body: outcome.to_json,
headers: {'Content-Type'=>'application/fhir+json',
'Location'=>'http://create-test/Patient/foo/_history/0',
'ETag'=>'W/"foo"',
'Last-Modified'=>Time.now.strftime("%a, %e %b %Y %T %Z")})
client.default_json
client.use_r4
reply = client.create(patient)
resource = reply.resource

assert_equal(client, resource.client)

FHIR::Model.client = FHIR::Client.new('abc')

assert_equal(client, resource.client)
end

end
42 changes: 42 additions & 0 deletions test/unit/client_interface_sections/search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,46 @@ def test_get_search_all
assert_requested(search_response)
end

def test_search_sets_client
condition = FHIR::Condition.new(
resourceType: 'Condition',
subject: {
reference: 'Patient/123'
}
)
bundle = FHIR::Bundle.new(
entry: [
{
resource: condition
}
]
)

search_response =
stub_request(:get, 'http://search-test/Condition?subject=Patient/123')
.to_return(
headers: { 'Content-Type': 'application/fhir+json' },
body: bundle.to_json
)

reply = @client.search(
FHIR::Condition,
{ search: { parameters: { subject: 'Patient/123' } } }
)

bundle_reply = reply.resource
entry = bundle_reply.entry.first.resource
reference = entry.subject

assert_requested(search_response)
assert_equal(@client, bundle_reply.client)
assert_equal(@client, entry.client)
assert_equal(@client, reference.client)

FHIR::Model.client = FHIR::Client.new('abc')

assert_equal(@client, bundle_reply.client)
assert_equal(@client, entry.client)
assert_equal(@client, reference.client)
end
end
13 changes: 13 additions & 0 deletions test/unit/reference_extras_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,17 @@ def test_logical_reference
assert res.nil?
end

def test_reference_read_client
reference_client = FHIR::Client.new('reference')
model_client = FHIR::Client.new('model')

ref = FHIR::Reference.new({'reference': 'Patient/foo'})
ref.client = reference_client
FHIR::Model.client = model_client

request = stub_request(:get, /reference/)
ref.read

assert_requested(request)
end
end

0 comments on commit a69338c

Please sign in to comment.