Module: Rubydora::RestApiClient

Extended by:
ActiveSupport::Concern, Deprecation
Includes:
ActiveSupport::Benchmarkable, FedoraUrlHelpers
Included in:
Fc3Service
Defined in:
lib/rubydora/rest_api_client.rb

Overview

Provide low-level access to the Fedora Commons REST API

Constant Summary collapse

DEFAULT_CONTENT_TYPE =
"application/octet-stream"
VALID_CLIENT_OPTIONS =
[:user, :password, :timeout, :open_timeout, :ssl_client_cert, :ssl_client_key]

Constants included from FedoraUrlHelpers

FedoraUrlHelpers::API_DOCUMENTATION

Instance Method Summary collapse

Methods included from FedoraUrlHelpers

#datastream_content_url, #datastream_history_url, #datastream_url, #datastreams_url, #describe_repository_url, #dissemination_url, #export_object_url, #find_objects_url, #new_object_relationship_url, #new_object_url, #next_pid_url, #object_relationship_url, #object_url, #object_versions_url, #object_xml_url, #url_for, #validate_object_url

Instance Method Details

#add_datastream(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rubydora/rest_api_client.rb', line 335

def add_datastream(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  dsid = query_options.delete(:dsid)
  file = query_options.delete(:content)
  content_type = query_options.delete(:content_type) || query_options[:mimeType] || file_content_type(file)
  run_hook :before_add_datastream, :pid => pid, :dsid => dsid, :file => file, :options => options
  str = file.respond_to?(:read) ? file.read : file
  file.rewind if file.respond_to?(:rewind)
  ProfileParser.parse_datastream_profile(client[datastream_url(pid, dsid, query_options)].post(str, :content_type => content_type.to_s, :multipart => true))
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#add_relationship(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


408
409
410
411
412
413
414
415
# File 'lib/rubydora/rest_api_client.rb', line 408

def add_relationship(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid) || query_options[:subject]
  run_hook :before_add_relationship, :pid => pid, :options => options
  client[new_object_relationship_url(pid, query_options)].post nil
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#client(config = {}) ⇒ RestClient::Resource

Create an authorized HTTP client for the Fedora REST API TODO trap for these errors specifically: RestClient::Request::Unauthorized, Errno::ECONNREFUSED

Parameters:

  • config (Hash) (defaults to: {})

Options Hash (config):

  • :url (String)
  • :user (String)
  • :password (String)

Returns:

  • (RestClient::Resource)


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubydora/rest_api_client.rb', line 51

def client(config = {})
  client_config = self.config.merge(config)
  client_config.symbolize_keys!
  if config.empty? || @config_hash.nil? || (client_config.hash == @config_hash)
    @config_hash = client_config.hash
    url = client_config[:url]
    client_config.delete_if { |k,v| !VALID_CLIENT_OPTIONS.include?(k) }
    client_config[:open_timeout] ||= client_config[:timeout]
    @client ||= RestClient::Resource.new(url, client_config)
  else
    raise ArgumentError, "Attemping to re-initialize #{self.class}#client with different configuration parameters"
  end
end

#datastream(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)
  • :dsid (String)
  • :asOfDateTime (String)
  • :validateChecksum (String)

Returns:

  • (String)


219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rubydora/rest_api_client.rb', line 219

def datastream(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  dsid = query_options.delete(:dsid)
  raise ArgumentError, "Missing required parameter :pid" unless pid

  if dsid.nil?
    #raise ArgumentError, "Missing required parameter :dsid" unless dsid
    Deprecation.warn(RestApiClient, "Calling Rubydora::RestApiClient#datastream without a :dsid is deprecated, use #datastreams instead")
    return datastreams(options)
  end
  query_options[:format] ||= 'xml'
  val = nil
  benchmark "Loaded datastream profile #{pid}/#{dsid}", :level=>:debug do
    val = client[datastream_url(pid, dsid, query_options)].get
  end

  val
rescue RestClient::Unauthorized => e
  Rubydora.logger.error "Unauthorized at #{client.url}/#{datastream_url(pid, dsid, query_options)}" if Rubydora.logger
  raise e
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#datastream_dissemination(options = {}, &block_response) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/rubydora/rest_api_client.rb', line 309

def datastream_dissemination(options = {}, &block_response)
  query_options = options.dup
  pid = query_options.delete(:pid)
  dsid = query_options.delete(:dsid)
  method = query_options.delete(:method)
  method ||= :get
  raise self.class.name + "#datastream_dissemination requires a DSID" unless dsid
  if block_given?
    resource = safe_subresource(datastream_content_url(pid, dsid, query_options), :block_response => block_response)
  else
    resource = client[datastream_content_url(pid, dsid, query_options)]
  end
  val = nil
  benchmark "Loaded datastream content #{pid}/#{dsid}", :level=>:debug do
    val = resource.send(method)
  end
  val
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#datastream_versions(options = {}) ⇒ String Also known as: datastream_history

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/rubydora/rest_api_client.rb', line 288

def datastream_versions(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  dsid = query_options.delete(:dsid)
  raise ArgumentError, "Must supply dsid" unless dsid
  query_options[:format] ||= 'xml'
  client[datastream_history_url(pid, dsid, query_options)].get
rescue RestClient::ResourceNotFound => e
  #404 Resource Not Found: No datastream history could be found. There is no datastream history for the digital object "changeme:1" with datastream ID of "descMetadata
  return nil
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#datastreams(options = {}) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rubydora/rest_api_client.rb', line 244

def datastreams(options = {})
  unless options[:dsid].nil?
    #raise ArgumentError, "Missing required parameter :dsid" unless dsid
    Deprecation.warn(RestApiClient, "Calling Rubydora::RestApiClient#datastreams with a :dsid is deprecated, use #datastream instead")
    return datastream(options)
  end
  query_options = options.dup
  pid = query_options.delete(:pid)
  raise ArgumentError, "Missing required parameter :pid" unless pid
  query_options[:format] ||= 'xml'
  val = nil
  benchmark "Loaded datastream list for #{pid}", :level=>:debug do
    val = client[datastreams_url(pid, query_options)].get
  end

  val
rescue RestClient::Unauthorized => e
  Rubydora.logger.error "Unauthorized at #{client.url}/#{datastreams_url(pid, query_options)}" if Rubydora.logger
  raise e
rescue Exception => exception
  rescue_with_handler(exception) || raise

end

#describe(options = {}) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/rubydora/rest_api_client.rb', line 65

def describe(options = {})
  query_options = options.dup
  query_options[:xml] ||= 'true'
  client[describe_repository_url(query_options)].get
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#dissemination(options = {}, &block_response) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)
  • :sdef (String)
  • :method (String)

Returns:

  • (String)


436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/rubydora/rest_api_client.rb', line 436

def dissemination(options = {}, &block_response)
  query_options = options.dup
  pid = query_options.delete(:pid)
  sdef = query_options.delete(:sdef)
  method = query_options.delete(:method)
  query_options[:format] ||= 'xml' unless pid && sdef && method
  if block_given?
    resource = safe_subresource(dissemination_url(pid,sdef,method,query_options), :block_response => block_response)
  else
    resource = client[dissemination_url(pid,sdef,method,query_options)]
  end
  resource.get

rescue Exception => exception
  rescue_with_handler(exception) || raise

end

#export(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


149
150
151
152
153
154
155
156
# File 'lib/rubydora/rest_api_client.rb', line 149

def export(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  raise ArgumentError, "Must have a pid" unless pid
  client[export_object_url(pid, query_options)].get
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#find_objects(options = {}, &block_response) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rubydora/rest_api_client.rb', line 87

def find_objects(options = {}, &block_response)
  query_options = options.dup
  raise ArgumentError,"Cannot have both :terms and :query parameters" if query_options[:terms] && query_options[:query]
  query_options[:resultFormat] ||= 'xml'

  resource = client[find_objects_url(query_options)]
  if block_given?
    resource.query_options[:block_response] = block_response
  end
  return resource.get
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#ingest(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rubydora/rest_api_client.rb', line 118

def ingest(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)

  if pid.nil?
    return mint_pid_and_ingest options
  end

  file = query_options.delete(:file)
  assigned_pid = client[object_url(pid, query_options)].post((file.dup if file), :content_type => 'text/xml')
  run_hook :after_ingest, :pid => assigned_pid, :file => file, :options => options
  assigned_pid
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#loggerObject

The logger is called by ActiveSupport::Benchmarkable



466
467
468
# File 'lib/rubydora/rest_api_client.rb', line 466

def logger
  Rubydora.logger
end

#mint_pid_and_ingest(options = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/rubydora/rest_api_client.rb', line 134

def mint_pid_and_ingest(options = {})
  query_options = options.dup
  file = query_options.delete(:file)

  assigned_pid = client[new_object_url(query_options)].post((file.dup if file), :content_type => 'text/xml')
  run_hook :after_ingest, :pid => assigned_pid, :file => file, :options => options
  assigned_pid
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#modify_datastream(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rubydora/rest_api_client.rb', line 354

def modify_datastream(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  dsid = query_options.delete(:dsid)
  file = query_options.delete(:content)
  content_type = query_options.delete(:content_type) || query_options[:mimeType] || file_content_type(file)
  rest_client_options = {}
  if file
    rest_client_options[:multipart] = true
    rest_client_options[:content_type] = content_type
  end

  run_hook :before_modify_datastream, :pid => pid, :dsid => dsid, :file => file, :content_type => content_type, :options => options
  str = file.respond_to?(:read) ? file.read : file
  file.rewind if file.respond_to?(:rewind)
  ProfileParser.parse_datastream_profile(client[datastream_url(pid, dsid, query_options)].put(str, rest_client_options))

rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#modify_object(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


162
163
164
165
166
167
168
169
# File 'lib/rubydora/rest_api_client.rb', line 162

def modify_object(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  run_hook :before_modify_object, :pid => pid, :options => options
  ProfileParser.canonicalize_date_string(client[object_url(pid, query_options)].put(nil))
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#next_pid(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)


76
77
78
79
80
81
82
# File 'lib/rubydora/rest_api_client.rb', line 76

def next_pid(options = {})
  query_options = options.dup
  query_options[:format] ||= 'xml'
  client[next_pid_url(query_options)].post nil
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#object(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


105
106
107
108
109
110
111
112
# File 'lib/rubydora/rest_api_client.rb', line 105

def object(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  query_options[:format] ||= 'xml'
  client[object_url(pid, query_options)].get
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#object_versions(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


188
189
190
191
192
193
194
195
196
# File 'lib/rubydora/rest_api_client.rb', line 188

def object_versions(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  query_options[:format] ||= 'xml'
  raise ArgumentError, "Must have a pid" unless pid
  client[object_versions_url(pid, query_options)].get
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#object_xml(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


202
203
204
205
206
207
208
209
210
# File 'lib/rubydora/rest_api_client.rb', line 202

def object_xml(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  raise ArgumentError, "Missing required parameter :pid" unless pid
  query_options[:format] ||= 'xml'
  client[object_xml_url(pid, query_options)].get
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#purge_datastream(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


380
381
382
383
384
385
386
387
388
# File 'lib/rubydora/rest_api_client.rb', line 380

def purge_datastream(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  dsid = query_options.delete(:dsid)
  run_hook :before_purge_datastream, :pid => pid, :dsid => dsid
  client[datastream_url(pid, dsid, query_options)].delete
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#purge_object(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


175
176
177
178
179
180
181
182
# File 'lib/rubydora/rest_api_client.rb', line 175

def purge_object(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  run_hook :before_purge_object, :pid => pid, :options => options
  client[object_url(pid, query_options)].delete
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#purge_relationship(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


421
422
423
424
425
426
427
428
# File 'lib/rubydora/rest_api_client.rb', line 421

def purge_relationship(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid) || query_options[:subject]
  run_hook :before_purge_relationship, :pid => pid, :options => options
  client[object_relationship_url(pid, query_options)].delete
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#relationships(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)

Returns:

  • (String)


394
395
396
397
398
399
400
401
402
# File 'lib/rubydora/rest_api_client.rb', line 394

def relationships(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid) || query_options[:subject]
  raise ArgumentError, "Missing required parameter :pid" unless pid
  query_options[:format] ||= 'xml'
  client[object_relationship_url(pid, query_options)].get
rescue Exception => exception
  rescue_with_handler(exception) || raise
end

#safe_subresource(subresource, options = Hash.new) ⇒ Object



454
455
456
457
458
459
460
461
462
463
# File 'lib/rubydora/rest_api_client.rb', line 454

def safe_subresource(subresource, options=Hash.new)
  url = client.concat_urls(client.url, subresource)
  options = client.options.dup.merge! options
  block = client.block
  if block
    client.class.new(url, options, &block)
  else
    client.class.new(url, options)
  end
end

#set_datastream_options(options = {}) ⇒ String

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pid (String)
  • :dsid (String)

Returns:

  • (String)


273
274
275
276
277
278
279
280
281
# File 'lib/rubydora/rest_api_client.rb', line 273

def set_datastream_options(options = {})
  query_options = options.dup
  pid = query_options.delete(:pid)
  dsid = query_options.delete(:dsid)
  run_hook :before_set_datastream_options, :pid => pid, :dsid => dsid, :options => options
  client[datastream_url(pid, dsid, query_options)].put nil
rescue Exception => exception
  rescue_with_handler(exception) || raise
end