Skip to content

Commit

Permalink
Merge branch 'master' into headers_update
Browse files Browse the repository at this point in the history
  • Loading branch information
arscan committed Feb 12, 2019
2 parents 28bc54d + 772e090 commit 535a5da
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 18 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ rvm:
- 2.3
- 2.4
- 2.5
- 2.6
before_install:
- gem update --system
- gem install bundler
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ patient = FHIR::DSTU2::Patient.read('example')
patient = client.read(FHIR::DSTU2::Patient, 'example').resource
```

### Configuration

You can specify additional properties for the `client`:

```ruby
client.additional_headers = {Prefer: 'return=representation'}
client.proxy = 'https://your-proxy.com/'
```

### CRUD Examples
```ruby
# read an existing patient with id "example"
Expand Down
14 changes: 13 additions & 1 deletion lib/fhir_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Client
attr_accessor :fhir_version
attr_accessor :cached_capability_statement
attr_accessor :additional_headers
attr_accessor :proxy
attr_accessor :exception_class

attr_accessor :use_accept_header
attr_accessor :use_accept_charset
Expand All @@ -35,7 +37,7 @@ class Client
# @param default_format Default Format Mime type
# @return
#
def initialize(base_service_url, default_format: FHIR::Formats::ResourceFormat::RESOURCE_XML)
def initialize(base_service_url, default_format: FHIR::Formats::ResourceFormat::RESOURCE_XML, proxy: nil)
@base_service_url = base_service_url
FHIR.logger.info "Initializing client with #{@base_service_url}"
@use_format_param = false
Expand All @@ -45,6 +47,9 @@ def initialize(base_service_url, default_format: FHIR::Formats::ResourceFormat::
@fhir_version = :stu3
@use_return_preference = false
@return_preference = FHIR::Formats::ReturnPreferences::REPRESENTATION
@exception_class = ClientException
@proxy = proxy

set_no_auth
end

Expand Down Expand Up @@ -124,6 +129,8 @@ def set_no_auth
@use_basic_auth = false
@security_headers = {}
@client = RestClient
@client.proxy = proxy unless proxy.nil?
@client
end

# Set the client to use HTTP Basic Authentication
Expand All @@ -135,6 +142,8 @@ def set_basic_auth(client, secret)
@use_oauth2_auth = false
@use_basic_auth = true
@client = RestClient
@client.proxy = proxy unless proxy.nil?
@client
end

# Set the client to use Bearer Token Authentication
Expand All @@ -145,6 +154,8 @@ def set_bearer_token(token)
@use_oauth2_auth = false
@use_basic_auth = true
@client = RestClient
@client.proxy = proxy unless proxy.nil?
@client
end

# Set the client to use OpenID Connect OAuth2 Authentication
Expand All @@ -164,6 +175,7 @@ def set_oauth2_auth(client, secret, authorize_path, token_path, site = nil)
raise_errors: true
}
client = OAuth2::Client.new(client, secret, options)
client.connection.proxy(proxy) unless proxy.nil?
@client = client.client_credentials.get_token
end

Expand Down
30 changes: 15 additions & 15 deletions lib/fhir_client/ext/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def resolve(reference)

private
def handle_response(response)
raise ClientException.new "Server returned #{response.code}.", response if response.code.between?(400, 599)
raise client.exception_class.new "Server returned #{response.code}.", response if response.code.between?(400, 599)
response.resource
end
end
Expand All @@ -66,59 +66,59 @@ def client=(client)
end

def read(id, client = self.client)
handle_response client.read(self, id)
handle_response client.exception_class, client.read(self, id)
end

def read_with_summary(id, summary, client = self.client)
handle_response client.read(self, id, client.default_format, summary)
handle_response client.exception_class, client.read(self, id, client.default_format, summary)
end

def vread(id, version_id, client = self.client)
handle_response client.vread(self, id, version_id)
handle_response client.exception_class, client.vread(self, id, version_id)
end

def resource_history(client = self.client)
handle_response client.resource_history(self)
handle_response client.exception_class, client.resource_history(self)
end

def resource_history_as_of(last_update, client = self.client)
handle_response client.resource_history_as_of(self, last_update)
handle_response client.exception_class, client.resource_history_as_of(self, last_update)
end

def resource_instance_history(id, client = self.client)
handle_response client.resource_instance_history(self, id)
handle_response client.exception_class, client.resource_instance_history(self, id)
end

def resource_instance_history_as_of(id, last_update, client = self.client)
handle_response client.resource_instance_history_as_of(self, id, last_update)
handle_response client.exception_class, client.resource_instance_history_as_of(self, id, last_update)
end

def search(params = {}, client = self.client)
handle_response client.search(self, search: { parameters: params })
handle_response client.exception_class, client.search(self, search: { parameters: params })
end

def create(model, client = self.client)
model = new(model) unless model.is_a?(self)
handle_response client.create(model)
handle_response client.exception_class, client.create(model)
end

def conditional_create(model, params, client = self.client)
model = new(model) unless model.is_a?(self)
handle_response client.conditional_create(model, params)
handle_response client.exception_class, client.conditional_create(model, params)
end

def partial_update(id, patchset, options = {})
handle_response client.partial_update(self, id, patchset, options)
handle_response client.exception_class, client.partial_update(self, id, patchset, options)
end

def all(client = self.client)
handle_response client.read_feed(self)
handle_response client.exception_class, client.read_feed(self)
end

private

def handle_response(response)
raise ClientException.new "Server returned #{response.code}.", response if response.code.between?(400, 599)
def handle_response(exception_class, response)
raise exception_class.new "Server returned #{response.code}.", response if response.code.between?(400, 599)
response.resource
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/fhir_client/ext/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def read
if relative? || reference == client.full_resource_url(resource: resource_class, id: reference_id)
read_client = client
else
read_client = FHIR::Client.new base_uri, default_format: client.default_format
read_client = FHIR::Client.new base_uri, default_format: client.default_format, proxy: client.proxy
end
resource_class.read(reference_id, read_client)
end
Expand All @@ -69,7 +69,7 @@ def vread
if relative? || reference == client.full_resource_url(resource: resource_class, id: reference_id)
read_client = client
else
read_client = FHIR::Client.new base_uri, default_format: client.default_format
read_client = FHIR::Client.new base_uri, default_format: client.default_format, proxy: client.proxy
end
resource_class.vread(reference_id, version_id, read_client)
end
Expand Down
54 changes: 54 additions & 0 deletions test/unit/model_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require_relative '../test_helper'

class TestUnitCustomException < ClientException ; end

class ModelTest < Test::Unit::TestCase

def test_default_instance_exception
stub_request(:post, /create/).to_return(status: 403, body: "")
client = FHIR::Client.new('create')
client.default_json
FHIR::Model.client = client

assert_raise ClientException do
FHIR::Patient.new({'id':'foo'}).create
end
end

def test_custom_instance_exception
stub_request(:post, /create/).to_return(status: 403, body: "")
client = FHIR::Client.new('create')
client.default_json
client.exception_class = TestUnitCustomException
FHIR::Model.client = client

assert_raise TestUnitCustomException do
FHIR::Patient.new({'id':'foo'}).create
end
end

def test_default_class_exception
stub_request(:post, /create/).to_return(status: 403, body: "")
client = FHIR::Client.new('create')
client.default_json
FHIR::Model.client = client

assert_raise ClientException do
FHIR::Patient.create({'id':'foo'})
end
end

def test_custom_class_exception
stub_request(:post, /create/).to_return(status: 403, body: "")
client = FHIR::Client.new('create')
client.default_json
client.exception_class = TestUnitCustomException
FHIR::Model.client = client

assert_raise TestUnitCustomException do
FHIR::Patient.create({'id':'foo'})
end
end

end

0 comments on commit 535a5da

Please sign in to comment.