diff options
author | John Keiser <jkeiser@opscode.com> | 2013-10-09 12:26:27 -0700 |
---|---|---|
committer | John Keiser <jkeiser@opscode.com> | 2013-10-09 12:26:27 -0700 |
commit | a5337fcad5ed979078b6cf57c7e1e6ec8f0b76cf (patch) | |
tree | 11a5e98513f9496d3ce15a823e7a84cd4d178f58 /lib/chef/http | |
parent | 836705dee684032baa85920c808a3eca5f5a8a87 (diff) | |
download | chef-a5337fcad5ed979078b6cf57c7e1e6ec8f0b76cf.tar.gz |
Use new Chef::ServerAPI API class in upload/download/diff
Diffstat (limited to 'lib/chef/http')
-rw-r--r-- | lib/chef/http/json_input.rb | 56 | ||||
-rw-r--r-- | lib/chef/http/json_output.rb (renamed from lib/chef/http/json_to_model_inflater.rb) | 26 | ||||
-rw-r--r-- | lib/chef/http/json_to_model_output.rb | 34 |
3 files changed, 98 insertions, 18 deletions
diff --git a/lib/chef/http/json_input.rb b/lib/chef/http/json_input.rb new file mode 100644 index 0000000000..5f8b700e85 --- /dev/null +++ b/lib/chef/http/json_input.rb @@ -0,0 +1,56 @@ +#-- +# Author:: Daniel DeLeo (<dan@opscode.com>) +# Author:: John Keiser (<jkeiser@opscode.com>) +# Copyright:: Copyright (c) 2013 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/json_compat' + +class Chef + class HTTP + + # Middleware that takes json input and turns it into raw text + class JSONInput + + def initialize(opts={}) + @raw_input = opts[:raw_input] + end + + def handle_request(method, url, headers={}, data=false) + if data + headers["Content-Type"] = 'application/json' + if !@raw_input + data = Chef::JSONCompat.to_json(data) + # Force encoding to binary to fix SSL related EOFErrors + # cf. http://tickets.opscode.com/browse/CHEF-2363 + # http://redmine.ruby-lang.org/issues/5233 + data.force_encoding(Encoding::BINARY) if data.respond_to?(:force_encoding) + end + end + [method, url, headers, data] + end + + def handle_response(http_response, rest_request, return_value) + [http_response, rest_request, return_value] + end + + def stream_response_handler(response) + nil + end + + end + end +end diff --git a/lib/chef/http/json_to_model_inflater.rb b/lib/chef/http/json_output.rb index 31cd18a6b6..48407d933f 100644 --- a/lib/chef/http/json_to_model_inflater.rb +++ b/lib/chef/http/json_output.rb @@ -1,5 +1,6 @@ #-- # Author:: Daniel DeLeo (<dan@opscode.com>) +# Author:: John Keiser (<jkeiser@opscode.com>) # Copyright:: Copyright (c) 2013 Opscode, Inc. # License:: Apache License, Version 2.0 # @@ -17,36 +18,25 @@ # require 'chef/json_compat' +require 'chef/log' + class Chef class HTTP - # A Middleware-ish thing that takes an HTTP response, parses it as JSON if - # possible, and converts it into an appropriate model object if it contains - # a `json_class` key. - class JSONToModelInflater + # Middleware that takes an HTTP response, parses it as JSON if possible. + class JSONOutput def initialize(opts={}) - @raw_input = opts[:raw_input] @raw_output = opts[:raw_output] - @inflate_json_class = opts.has_key?(:inflate_json_class) ? opts[:inflate_json_class] : true + @inflate_json_class = opts[:inflate_json_class] end def handle_request(method, url, headers={}, data=false) # Ideally this should always set Accept to application/json, but # Chef::REST is sometimes used to make non-JSON requests, so it sets # Accept to the desired value before middlewares get called. - headers['Accept'] ||= "application/json" - headers["Content-Type"] = 'application/json' if data - if @raw_input - json_body = data || nil - else - json_body = data ? Chef::JSONCompat.to_json(data) : nil - end - # Force encoding to binary to fix SSL related EOFErrors - # cf. http://tickets.opscode.com/browse/CHEF-2363 - # http://redmine.ruby-lang.org/issues/5233 - json_body.force_encoding(Encoding::BINARY) if json_body.respond_to?(:force_encoding) - [method, url, headers, json_body] + headers['Accept'] ||= 'application/json' + [method, url, headers, data] end def handle_response(http_response, rest_request, return_value) diff --git a/lib/chef/http/json_to_model_output.rb b/lib/chef/http/json_to_model_output.rb new file mode 100644 index 0000000000..9bc90a52ae --- /dev/null +++ b/lib/chef/http/json_to_model_output.rb @@ -0,0 +1,34 @@ +#-- +# Author:: Daniel DeLeo (<dan@opscode.com>) +# Copyright:: Copyright (c) 2013 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/http/json_output' + +class Chef + class HTTP + + # A Middleware-ish thing that takes an HTTP response, parses it as JSON if + # possible, and converts it into an appropriate model object if it contains + # a `json_class` key. + class JSONToModelOutput < JSONOutput + def initialize(opts={}) + opts[:inflate_json_class] = true if !opts.has_key?(:inflate_json_class) + super + end + end + end +end |