summaryrefslogtreecommitdiff
path: root/lib/chef/rest.rb
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2013-10-03 17:50:06 -0700
committerdanielsdeleo <dan@opscode.com>2013-10-08 15:01:47 -0700
commitd586ce15f8bdbf4c08500f444a5e890ad0aab754 (patch)
tree212f1f4bd465ef89e81f52611de6560a50798dbd /lib/chef/rest.rb
parent4ab3df25941ab29f58109e2495d329b3d15dbc99 (diff)
downloadchef-d586ce15f8bdbf4c08500f444a5e890ad0aab754.tar.gz
DRY middleware application
Diffstat (limited to 'lib/chef/rest.rb')
-rw-r--r--lib/chef/rest.rb24
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/chef/rest.rb b/lib/chef/rest.rb
index cc57838df9..0e121ee78d 100644
--- a/lib/chef/rest.rb
+++ b/lib/chef/rest.rb
@@ -145,14 +145,10 @@ class Chef
# Runs an HTTP request to a JSON API with JSON body. File Download not supported.
def api_request(method, url, headers={}, data=false)
- method, url, headers, data = @chef_json_inflater.handle_request(method, url, headers, data)
- method, url, headers, data = @decompressor.handle_request(method, url, headers, data)
- method, url, headers, data = @authenticator.handle_request(method, url, headers, data)
+ method, url, headers, data = apply_request_middleware(method, url, headers, data)
response, rest_request, return_value = raw_http_request(method, url, headers, data)
- response, rest_request, return_value = @authenticator.handle_response(response, rest_request, return_value)
- response, rest_request, return_value = @decompressor.handle_response(response, rest_request, return_value)
- response, rest_request, return_value = @chef_json_inflater.handle_response(response, rest_request, return_value)
+ response, rest_request, return_value = apply_response_middleware(response, rest_request, return_value)
response.error! unless success_response?(response)
return_value
rescue Exception => exception
@@ -164,6 +160,22 @@ class Chef
raise
end
+ def middlewares
+ [@chef_json_inflater, @decompressor, @authenticator]
+ end
+
+ def apply_request_middleware(method, url, headers, data)
+ middlewares.inject([method, url, headers, data]) do |req_data, middleware|
+ middleware.handle_request(*req_data)
+ end
+ end
+
+ def apply_response_middleware(response, rest_request, return_value)
+ middlewares.reverse.inject([response, rest_request, return_value]) do |res_data, middleware|
+ middleware.handle_response(*res_data)
+ end
+ end
+
def log_failed_request(response, return_value)
return_value ||= {}
error_message = "HTTP Request Returned #{response.code} #{response.message}: "