forked from fhir-crucible/fhir_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_interface.rb
533 lines (493 loc) · 17.7 KB
/
client_interface.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
module FHIR
class Client
include FHIR::Sections::History
include FHIR::Sections::Crud
include FHIR::Sections::Validate
include FHIR::Sections::Tags
include FHIR::Sections::Feed
include FHIR::Sections::Search
include FHIR::Sections::Operations
include FHIR::Sections::Transactions
attr_accessor :reply
attr_accessor :use_format_param
attr_accessor :use_basic_auth
attr_accessor :use_oauth2_auth
attr_accessor :security_headers
attr_accessor :client
attr_accessor :default_format
attr_accessor :cached_conformance
# Call method to initialize FHIR client. This method must be invoked
# with a valid base server URL prior to using the client.
#
# @param baseServiceUrl Base service URL for FHIR Service.
# @return
#
def initialize(baseServiceUrl)
FHIR.logger.info "Initializing client with #{@baseServiceUrl}"
@baseServiceUrl = baseServiceUrl
@use_format_param = false
@default_format = FHIR::Formats::ResourceFormat::RESOURCE_XML
set_no_auth
end
def default_json
@default_format = FHIR::Formats::ResourceFormat::RESOURCE_JSON
end
def default_xml
@default_format = FHIR::Formats::ResourceFormat::RESOURCE_XML
end
# Set the client to use no authentication mechanisms
def set_no_auth
FHIR.logger.info "Configuring the client to use no authentication."
@use_oauth2_auth = false
@use_basic_auth = false
@security_headers = {}
@client = RestClient
end
# Set the client to use HTTP Basic Authentication
def set_basic_auth(client,secret)
FHIR.logger.info "Configuring the client to use HTTP Basic authentication."
token = Base64.encode64("#{client}:#{secret}")
value = "Basic #{token}"
@security_headers = { 'Authorization' => value }
@use_oauth2_auth = false
@use_basic_auth = true
@client = RestClient
end
# Set the client to use Bearer Token Authentication
def set_bearer_token(token)
FHIR.logger.info "Configuring the client to use Bearer Token authentication."
value = "Bearer #{token}"
@security_headers = { 'Authorization' => value }
@use_oauth2_auth = false
@use_basic_auth = true
@client = RestClient
end
# Set the client to use OpenID Connect OAuth2 Authentication
# client -- client id
# secret -- client secret
# authorizePath -- absolute path of authorization endpoint
# tokenPath -- absolute path of token endpoint
def set_oauth2_auth(client,secret,authorizePath,tokenPath)
FHIR.logger.info "Configuring the client to use OpenID Connect OAuth2 authentication."
@use_oauth2_auth = true
@use_basic_auth = false
@security_headers = {}
options = {
:site => @baseServiceUrl,
:authorize_url => authorizePath,
:token_url => tokenPath,
:raise_errors => true
}
client = OAuth2::Client.new(client,secret,options)
@client = client.client_credentials.get_token
end
# Get the OAuth2 server and endpoints from the conformance statement
# (the server should not require OAuth2 or other special security to access
# the conformance statement).
# <rest>
# <mode value="server"/>
# <documentation value="All the functionality defined in FHIR"/>
# <security>
# <extension url="http:https://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris">
# <extension url="register">
# <valueUri value="https://authorize-dstu2.smarthealthit.org/register"/>
# </extension>
# <extension url="authorize">
# <valueUri value="https://authorize-dstu2.smarthealthit.org/authorize"/>
# </extension>
# <extension url="token">
# <valueUri value="https://authorize-dstu2.smarthealthit.org/token"/>
# </extension>
# </extension>
# <service>
# <coding>
# <system value="http:https://hl7.org/fhir/vs/restful-security-service"/>
# <code value="OAuth2"/>
# </coding>
# <text value="OAuth version 2 (see oauth.net)."/>
# </service>
# <description value="SMART on FHIR uses OAuth2 for authorization"/>
# </security>
def get_oauth2_metadata_from_conformance
options = {
:authorize_url => nil,
:token_url => nil
}
oauth_extension = 'http:https://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris'
authorize_extension = 'authorize'
token_extension = 'token'
begin
conformance = conformanceStatement
conformance.rest.each do |rest|
rest.security.service.each do |service|
service.coding.each do |coding|
if coding.code == 'SMART-on-FHIR'
rest.security.extension.where({url: oauth_extension}).first.extension.each do |ext|
case ext.url
when authorize_extension
options[:authorize_url] = ext.value.value
when "#{oauth_extension}\##{authorize_extension}"
options[:authorize_url] = ext.value.value
when token_extension
options[:token_url] = ext.value.value
when "#{oauth_extension}\##{token_extension}"
options[:token_url] = ext.value.value
end
end
end
end
end
end
rescue Exception => e
FHIR.logger.error 'Failed to locate SMART-on-FHIR OAuth2 Security Extensions.'
end
options.delete_if{|k,v|v.nil?}
options.clear if options.keys.size!=2
options
end
# Method returns a conformance statement for the system queried.
# @return
def conformanceStatement(format=FHIR::Formats::ResourceFormat::RESOURCE_XML)
if (@cached_conformance.nil? || format!=@default_format)
format = try_conformance_formats(format)
end
@cached_conformance
end
def try_conformance_formats(default_format)
formats = [FHIR::Formats::ResourceFormat::RESOURCE_XML,
FHIR::Formats::ResourceFormat::RESOURCE_JSON,
FHIR::Formats::ResourceFormat::RESOURCE_XML_DSTU2,
FHIR::Formats::ResourceFormat::RESOURCE_JSON_DSTU2,
'application/xml',
'application/json']
formats.insert(0, default_format)
@cached_conformance = nil
@default_format = nil
formats.each do |frmt|
reply = get 'metadata', fhir_headers({format: frmt})
if reply.code == 200
@cached_conformance = parse_reply(FHIR::Conformance, frmt, reply)
@default_format = frmt
break
end
end
@default_format = default_format if @default_format.nil?
@default_format
end
def resource_url(options)
FHIR::ResourceAddress.new.resource_url(options, @use_format_param)
end
def full_resource_url(options)
@baseServiceUrl + resource_url(options)
end
def fhir_headers(options={})
FHIR::ResourceAddress.new.fhir_headers(options, @use_format_param)
end
def parse_reply(klass, format, response)
FHIR.logger.info "Parsing response with {klass: #{klass}, format: #{format}, code: #{response.code}}."
return nil if ![200,201].include? response.code
res = nil
begin
res = FHIR.from_contents(response.body)
res.client = self if !res.nil?
FHIR.logger.warn "Expected #{klass} but got #{res.class}" if res.class!=klass
rescue Exception => e
FHIR.logger.error "Failed to parse #{format} as resource #{klass}: #{e.message} %n #{e.backtrace.join("\n")} #{response}"
nil
end
res
end
def strip_base(path)
path.gsub(@baseServiceUrl, '')
end
def reissue_request(request)
if [:get, :delete, :head].include?(request['method'])
method(request['method']).call(request['url'], request['headers'])
elsif [:post, :put].include?(request['method'])
resource = FHIR.from_contents(request['payload']) unless request['payload'].nil?
method(request['method']).call(request['url'], resource, request['headers'])
end
end
private
def base_path(path)
if path.start_with?('/')
if @baseServiceUrl.end_with?('/')
@baseServiceUrl.chop
else
@baseServiceUrl
end
else
@baseServiceUrl + '/'
end
end
# Extract the request payload in the specified format, defaults to XML
def request_payload(resource, headers)
if headers
format_specified = headers[:format] || headers["format"]
if format_specified.downcase.include?('xml')
resource.to_xml
elsif format_specified.downcase.include?('json')
resource.to_json
else
resource.to_xml
end
else
resource.to_xml
end
end
def request_patch_payload(patchset, format)
if (format == FHIR::Formats::PatchFormat::PATCH_JSON)
patchset.each do |patch|
# remove the resource name from the patch path, since the JSON representation doesn't have that
patch[:path] = patch[:path].slice(patch[:path].index('/')..-1)
end
patchset.to_json
elsif (format == FHIR::Formats::PatchFormat::PATCH_XML)
builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
patchset.each do |patch|
xml.diff {
# TODO: support other kinds besides just replace
xml.replace(patch[:value], sel: patch[:path] + '/@value') if patch[:op] == 'replace'
}
end
end
builder.to_xml
end
end
def clean_headers(headers)
headers.delete_if{|k,v|(k.nil? || v.nil?)}
headers.inject({}){|h,(k,v)| h[k.to_s]=v.to_s; h}
end
def scrubbed_response_headers(result)
result.each_key do |k|
v = result[k]
result[k] = v[0] if (v.is_a? Array)
end
end
def get(path, headers)
url = Addressable::URI.parse(build_url(path)).to_s
FHIR.logger.info "GETTING: #{url}"
headers = clean_headers(headers)
if @use_oauth2_auth
# @client.refresh!
begin
response = @client.get(url, {:headers=>headers})
rescue Exception => e
response = e.response if e.response
end
req = {
:method => :get,
:url => url,
:path => url.gsub(@baseServiceUrl,''),
:headers => headers,
:payload => nil
}
res = {
:code => response.status.to_s,
:headers => response.headers,
:body => response.body
}
FHIR.logger.info "GET - Request: #{req.to_s}, Response: #{response.body.force_encoding("UTF-8")}"
@reply = FHIR::ClientReply.new(req, res)
else
headers.merge!(@security_headers) if @use_basic_auth
begin
response = @client.get(url, headers)
rescue Exception => e
response = e.response if e.response
end
FHIR.logger.info "GET - Request: #{response.request.to_json}, Response: #{response.body.force_encoding("UTF-8")}"
response.request.args[:path] = response.request.args[:url].gsub(@baseServiceUrl,'')
headers = response.headers.inject({}){ |h,(k,v)| h[k.to_s.gsub('_','-')] = v.to_s; h}
res = {
:code => response.code,
:headers => scrubbed_response_headers(headers),
:body => response.body
}
@reply = FHIR::ClientReply.new(response.request.args, res)
end
end
def post(path, resource, headers)
url = URI(build_url(path)).to_s
FHIR.logger.info "POSTING: #{url}"
headers = clean_headers(headers)
payload = request_payload(resource, headers) if resource
if @use_oauth2_auth
# @client.refresh!
begin
response = @client.post(url, {:headers=>headers,:body=>payload})
rescue Exception => e
response = e.response if e.response
end
req = {
:method => :post,
:url => url,
:path => url.gsub(@baseServiceUrl,''),
:headers => headers,
:payload => payload
}
res = {
:code => response.status.to_s,
:headers => response.headers,
:body => response.body
}
FHIR.logger.info "POST - Request: #{req.to_s}, Response: #{response.body.force_encoding("UTF-8")}"
@reply = FHIR::ClientReply.new(req, res)
else
headers.merge!(@security_headers) if @use_basic_auth
@client.post(url, payload, headers){ |response, request, result|
FHIR.logger.info "POST - Request: #{request.to_json}, Response: #{response.force_encoding("UTF-8")}"
request.args[:path] = url.gsub(@baseServiceUrl,'')
res = {
:code => result.code,
:headers => scrubbed_response_headers(result.each_key{}),
:body => response
}
@reply = FHIR::ClientReply.new(request.args, res)
}
end
end
def put(path, resource, headers)
url = URI(build_url(path)).to_s
FHIR.logger.info "PUTTING: #{url}"
headers = clean_headers(headers)
payload = request_payload(resource, headers) if resource
if @use_oauth2_auth
# @client.refresh!
begin
response = @client.put(url, {:headers=>headers,:body=>payload})
rescue Exception => e
response = e.response if e.response
end
req = {
:method => :put,
:url => url,
:path => url.gsub(@baseServiceUrl,''),
:headers => headers,
:payload => payload
}
res = {
:code => response.status.to_s,
:headers => response.headers,
:body => response.body
}
FHIR.logger.info "PUT - Request: #{req.to_s}, Response: #{response.body.force_encoding("UTF-8")}"
@reply = FHIR::ClientReply.new(req, res)
else
headers.merge!(@security_headers) if @use_basic_auth
@client.put(url, payload, headers){ |response, request, result|
FHIR.logger.info "PUT - Request: #{request.to_json}, Response: #{response.force_encoding("UTF-8")}"
request.args[:path] = url.gsub(@baseServiceUrl,'')
res = {
:code => result.code,
:headers => scrubbed_response_headers(result.each_key{}),
:body => response
}
@reply = FHIR::ClientReply.new(request.args, res)
}
end
end
def patch(path, patchset, headers)
url = URI(build_url(path)).to_s
FHIR.logger.info "PATCHING: #{url}"
headers = clean_headers(headers)
payload = request_patch_payload(patchset, headers['format'])
if @use_oauth2_auth
# @client.refresh!
begin
response = @client.patch(url, {:headers=>headers,:body=>payload})
rescue Exception => e
response = e.response if e.response
end
req = {
:method => :patch,
:url => url,
:path => url.gsub(@baseServiceUrl,''),
:headers => headers,
:payload => payload
}
res = {
:code => response.status.to_s,
:headers => response.headers,
:body => response.body
}
FHIR.logger.info "PATCH - Request: #{req.to_s}, Response: #{response.body.force_encoding("UTF-8")}"
@reply = FHIR::ClientReply.new(req, res)
else
headers.merge!(@security_headers) if @use_basic_auth
# url = 'http:https://requestb.in/o8juy3o8'
@client.patch(url, payload, headers){ |response, request, result|
FHIR.logger.info "PATCH - Request: #{request.to_json}, Response: #{response.force_encoding("UTF-8")}"
request.args[:path] = url.gsub(@baseServiceUrl,'')
res = {
:code => result.code,
:headers => scrubbed_response_headers(result.each_key{}),
:body => response
}
@reply = FHIR::ClientReply.new(request.args, res)
}
end
end
def delete(path, headers)
url = URI(build_url(path)).to_s
FHIR.logger.info "DELETING: #{url}"
headers = clean_headers(headers)
if @use_oauth2_auth
# @client.refresh!
begin
response = @client.delete(url, {:headers=>headers})
rescue Exception => e
response = e.response if e.response
end
req = {
:method => :delete,
:url => url,
:path => url.gsub(@baseServiceUrl,''),
:headers => headers,
:payload => nil
}
res = {
:code => response.status.to_s,
:headers => response.headers,
:body => response.body
}
FHIR.logger.info "DELETE - Request: #{req.to_s}, Response: #{response.body.force_encoding("UTF-8")}"
@reply = FHIR::ClientReply.new(req, res)
else
headers.merge!(@security_headers) if @use_basic_auth
@client.delete(url, headers){ |response, request, result|
FHIR.logger.info "DELETE - Request: #{request.to_json}, Response: #{response.force_encoding("UTF-8")}"
request.args[:path] = url.gsub(@baseServiceUrl,'')
res = {
:code => result.code,
:headers => scrubbed_response_headers(result.each_key{}),
:body => response
}
@reply = FHIR::ClientReply.new(request.args, res)
}
end
end
def head(path, headers)
headers.merge!(@security_headers) unless @security_headers.blank?
url = URI(build_url(path)).to_s
FHIR.logger.info "HEADING: #{url}"
RestClient.head(url, headers){ |response, request, result|
FHIR.logger.info "HEAD - Request: #{request.to_json}, Response: #{response.force_encoding("UTF-8")}"
request.args[:path] = url.gsub(@baseServiceUrl,'')
res = {
:code => result.code,
:headers => scrubbed_response_headers(result.each_key{}),
:body => response
}
@reply = FHIR::ClientReply.new(request.args, res)
}
end
def build_url(path)
if path =~ /^\w+:\/\//
path
else
"#{base_path(path)}#{path}"
end
end
end
end