From 639c3724f02a06310f2e5c1507e193b4091a71fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Kar=C3=A9kinian?= Date: Wed, 9 May 2018 16:01:14 +0200 Subject: [PATCH] Refactor the put_request method to have a return value --- lib/remote_storage/rest_provider.rb | 15 +++++++-------- lib/remote_storage/s3.rb | 17 ++++++++--------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/remote_storage/rest_provider.rb b/lib/remote_storage/rest_provider.rb index 621d456..3c5d38d 100644 --- a/lib/remote_storage/rest_provider.rb +++ b/lib/remote_storage/rest_provider.rb @@ -141,7 +141,7 @@ module RemoteStorage server.halt 412, "Precondition Failed" unless existing_metadata.empty? end - etag, timestamp = do_put_request_and_return_etag_and_last_modified(url, data, content_type) + etag, timestamp = do_put_request(url, data, content_type) metadata = { e: etag, @@ -392,16 +392,15 @@ module RemoteStorage def do_put_request(url, data, content_type) deal_with_unauthorized_requests do - RestClient.put(url, data, default_headers.merge({content_type: content_type})) + res = RestClient.put(url, data, default_headers.merge({content_type: content_type})) + + return [ + res.headers[:etag], + timestamp_for(res.headers[:last_modified]) + ] end end - def do_put_request_and_return_etag_and_last_modified(url, data, content_type) - res = do_put_request(url, data, content_type) - - return [res.headers[:etag], timestamp_for(res.headers[:last_modified])] - end - def do_get_request(url, &block) deal_with_unauthorized_requests do RestClient.get(url, default_headers, &block) diff --git a/lib/remote_storage/s3.rb b/lib/remote_storage/s3.rb index f2346d4..875f596 100644 --- a/lib/remote_storage/s3.rb +++ b/lib/remote_storage/s3.rb @@ -21,18 +21,17 @@ module RemoteStorage authorization_headers = authorization_headers_for( "PUT", url, md5, content_type ).merge({ "Content-Type" => content_type, "Content-Md5" => md5 }) - RestClient.put(url, data, authorization_headers) + res = RestClient.put(url, data, authorization_headers) + # S3 does not return a Last-Modified response header on PUTs + head_res = do_head_request(url) + + return [ + res.headers[:etag].delete('"'), + timestamp_for(head_res.headers[:last_modified]) + ] end end - def do_put_request_and_return_etag_and_last_modified(url, data, content_type) - res = do_put_request(url, data, content_type) - # S3 does not return a Last-Modified response header on PUTs - head_res = do_head_request(url) - - return [res.headers[:etag].delete('"'), timestamp_for(head_res.headers[:last_modified])] - end - def do_get_request(url, &block) deal_with_unauthorized_requests do authorization_headers = authorization_headers_for("GET", url)