lib/duracloud/client.rb in duracloud-client-0.0.1 vs lib/duracloud/client.rb in duracloud-client-0.0.2
- old
+ new
@@ -1,47 +1,51 @@
require "forwardable"
-require_relative "configuration"
-require_relative "connection"
-require_relative "content_request"
+require "duracloud/configuration"
+require "duracloud/connection"
+require "duracloud/error_handler"
+require "duracloud/rest_methods"
+
module Duracloud
class Client
extend Forwardable
+ extend RestMethods
+ include RestMethods
+ def self.execute(request_class, http_method, url, **options)
+ new.execute(request_class, http_method, url, **options)
+ end
+
def self.configure
yield Configuration
end
attr_reader :config
- delegate [:host, :port, :user, :password, :base_url] => :config
+ delegate [:host, :port, :user, :password, :base_url, :logger] => :config
def initialize(**options)
@config = Configuration.new(**options)
end
- def get_content(url, **options)
- execute ContentRequest, :get, url, **options
- #ContentRequest.get(self, url, **options)
+ def execute(request_class, http_method, url, **options)
+ request = request_class.new(self, http_method, url, **options)
+ response = request.execute
+ handle_response(response)
+ response
end
- def get_content_properties(url, **options)
- execute ContentRequest, :head, url, **options
- end
+ private
- def set_content_properties(url, **options)
- execute ContentRequest, :post, url, **options
- end
-
- def store_content(url, **options)
- execute ContentRequest, :put, url, **options
- end
-
- def delete_content(url, **options)
- execute ContentRequest, :delete, url, **options
- end
-
- def execute(request_class, http_method, url, **options)
- request_class.new(self, http_method, url, **options).execute
+ def handle_response(response)
+ logger.debug([self.class.to_s, response.request_method, response.url,
+ response.status, response.reason].join(' '))
+ if response.error?
+ ErrorHandler.call(response)
+ elsif %w(POST PUT DELETE).include?(response.request_method) &&
+ response.plain_text? &&
+ response.has_body?
+ logger.info(response.body)
+ end
end
end
end