Skip to content

Commit 535a5da

Browse files
authored
Merge branch 'master' into headers_update
2 parents 28bc54d + 772e090 commit 535a5da

File tree

6 files changed

+94
-18
lines changed

6 files changed

+94
-18
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ rvm:
33
- 2.3
44
- 2.4
55
- 2.5
6+
- 2.6
67
before_install:
78
- gem update --system
89
- gem install bundler

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ patient = FHIR::DSTU2::Patient.read('example')
8484
patient = client.read(FHIR::DSTU2::Patient, 'example').resource
8585
```
8686

87+
### Configuration
88+
89+
You can specify additional properties for the `client`:
90+
91+
```ruby
92+
client.additional_headers = {Prefer: 'return=representation'}
93+
client.proxy = 'https://your-proxy.com/'
94+
```
95+
8796
### CRUD Examples
8897
```ruby
8998
# read an existing patient with id "example"

lib/fhir_client/client.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Client
2323
attr_accessor :fhir_version
2424
attr_accessor :cached_capability_statement
2525
attr_accessor :additional_headers
26+
attr_accessor :proxy
27+
attr_accessor :exception_class
2628

2729
attr_accessor :use_accept_header
2830
attr_accessor :use_accept_charset
@@ -35,7 +37,7 @@ class Client
3537
# @param default_format Default Format Mime type
3638
# @return
3739
#
38-
def initialize(base_service_url, default_format: FHIR::Formats::ResourceFormat::RESOURCE_XML)
40+
def initialize(base_service_url, default_format: FHIR::Formats::ResourceFormat::RESOURCE_XML, proxy: nil)
3941
@base_service_url = base_service_url
4042
FHIR.logger.info "Initializing client with #{@base_service_url}"
4143
@use_format_param = false
@@ -45,6 +47,9 @@ def initialize(base_service_url, default_format: FHIR::Formats::ResourceFormat::
4547
@fhir_version = :stu3
4648
@use_return_preference = false
4749
@return_preference = FHIR::Formats::ReturnPreferences::REPRESENTATION
50+
@exception_class = ClientException
51+
@proxy = proxy
52+
4853
set_no_auth
4954
end
5055

@@ -124,6 +129,8 @@ def set_no_auth
124129
@use_basic_auth = false
125130
@security_headers = {}
126131
@client = RestClient
132+
@client.proxy = proxy unless proxy.nil?
133+
@client
127134
end
128135

129136
# Set the client to use HTTP Basic Authentication
@@ -135,6 +142,8 @@ def set_basic_auth(client, secret)
135142
@use_oauth2_auth = false
136143
@use_basic_auth = true
137144
@client = RestClient
145+
@client.proxy = proxy unless proxy.nil?
146+
@client
138147
end
139148

140149
# Set the client to use Bearer Token Authentication
@@ -145,6 +154,8 @@ def set_bearer_token(token)
145154
@use_oauth2_auth = false
146155
@use_basic_auth = true
147156
@client = RestClient
157+
@client.proxy = proxy unless proxy.nil?
158+
@client
148159
end
149160

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

lib/fhir_client/ext/model.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def resolve(reference)
5050

5151
private
5252
def handle_response(response)
53-
raise ClientException.new "Server returned #{response.code}.", response if response.code.between?(400, 599)
53+
raise client.exception_class.new "Server returned #{response.code}.", response if response.code.between?(400, 599)
5454
response.resource
5555
end
5656
end
@@ -66,59 +66,59 @@ def client=(client)
6666
end
6767

6868
def read(id, client = self.client)
69-
handle_response client.read(self, id)
69+
handle_response client.exception_class, client.read(self, id)
7070
end
7171

7272
def read_with_summary(id, summary, client = self.client)
73-
handle_response client.read(self, id, client.default_format, summary)
73+
handle_response client.exception_class, client.read(self, id, client.default_format, summary)
7474
end
7575

7676
def vread(id, version_id, client = self.client)
77-
handle_response client.vread(self, id, version_id)
77+
handle_response client.exception_class, client.vread(self, id, version_id)
7878
end
7979

8080
def resource_history(client = self.client)
81-
handle_response client.resource_history(self)
81+
handle_response client.exception_class, client.resource_history(self)
8282
end
8383

8484
def resource_history_as_of(last_update, client = self.client)
85-
handle_response client.resource_history_as_of(self, last_update)
85+
handle_response client.exception_class, client.resource_history_as_of(self, last_update)
8686
end
8787

8888
def resource_instance_history(id, client = self.client)
89-
handle_response client.resource_instance_history(self, id)
89+
handle_response client.exception_class, client.resource_instance_history(self, id)
9090
end
9191

9292
def resource_instance_history_as_of(id, last_update, client = self.client)
93-
handle_response client.resource_instance_history_as_of(self, id, last_update)
93+
handle_response client.exception_class, client.resource_instance_history_as_of(self, id, last_update)
9494
end
9595

9696
def search(params = {}, client = self.client)
97-
handle_response client.search(self, search: { parameters: params })
97+
handle_response client.exception_class, client.search(self, search: { parameters: params })
9898
end
9999

100100
def create(model, client = self.client)
101101
model = new(model) unless model.is_a?(self)
102-
handle_response client.create(model)
102+
handle_response client.exception_class, client.create(model)
103103
end
104104

105105
def conditional_create(model, params, client = self.client)
106106
model = new(model) unless model.is_a?(self)
107-
handle_response client.conditional_create(model, params)
107+
handle_response client.exception_class, client.conditional_create(model, params)
108108
end
109109

110110
def partial_update(id, patchset, options = {})
111-
handle_response client.partial_update(self, id, patchset, options)
111+
handle_response client.exception_class, client.partial_update(self, id, patchset, options)
112112
end
113113

114114
def all(client = self.client)
115-
handle_response client.read_feed(self)
115+
handle_response client.exception_class, client.read_feed(self)
116116
end
117117

118118
private
119119

120-
def handle_response(response)
121-
raise ClientException.new "Server returned #{response.code}.", response if response.code.between?(400, 599)
120+
def handle_response(exception_class, response)
121+
raise exception_class.new "Server returned #{response.code}.", response if response.code.between?(400, 599)
122122
response.resource
123123
end
124124
end

lib/fhir_client/ext/reference.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def read
5959
if relative? || reference == client.full_resource_url(resource: resource_class, id: reference_id)
6060
read_client = client
6161
else
62-
read_client = FHIR::Client.new base_uri, default_format: client.default_format
62+
read_client = FHIR::Client.new base_uri, default_format: client.default_format, proxy: client.proxy
6363
end
6464
resource_class.read(reference_id, read_client)
6565
end
@@ -69,7 +69,7 @@ def vread
6969
if relative? || reference == client.full_resource_url(resource: resource_class, id: reference_id)
7070
read_client = client
7171
else
72-
read_client = FHIR::Client.new base_uri, default_format: client.default_format
72+
read_client = FHIR::Client.new base_uri, default_format: client.default_format, proxy: client.proxy
7373
end
7474
resource_class.vread(reference_id, version_id, read_client)
7575
end

test/unit/model_test.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require_relative '../test_helper'
2+
3+
class TestUnitCustomException < ClientException ; end
4+
5+
class ModelTest < Test::Unit::TestCase
6+
7+
def test_default_instance_exception
8+
stub_request(:post, /create/).to_return(status: 403, body: "")
9+
client = FHIR::Client.new('create')
10+
client.default_json
11+
FHIR::Model.client = client
12+
13+
assert_raise ClientException do
14+
FHIR::Patient.new({'id':'foo'}).create
15+
end
16+
end
17+
18+
def test_custom_instance_exception
19+
stub_request(:post, /create/).to_return(status: 403, body: "")
20+
client = FHIR::Client.new('create')
21+
client.default_json
22+
client.exception_class = TestUnitCustomException
23+
FHIR::Model.client = client
24+
25+
assert_raise TestUnitCustomException do
26+
FHIR::Patient.new({'id':'foo'}).create
27+
end
28+
end
29+
30+
def test_default_class_exception
31+
stub_request(:post, /create/).to_return(status: 403, body: "")
32+
client = FHIR::Client.new('create')
33+
client.default_json
34+
FHIR::Model.client = client
35+
36+
assert_raise ClientException do
37+
FHIR::Patient.create({'id':'foo'})
38+
end
39+
end
40+
41+
def test_custom_class_exception
42+
stub_request(:post, /create/).to_return(status: 403, body: "")
43+
client = FHIR::Client.new('create')
44+
client.default_json
45+
client.exception_class = TestUnitCustomException
46+
FHIR::Model.client = client
47+
48+
assert_raise TestUnitCustomException do
49+
FHIR::Patient.create({'id':'foo'})
50+
end
51+
end
52+
53+
end
54+

0 commit comments

Comments
 (0)