lib/signet/oauth_1/client.rb in signet-0.3.4 vs lib/signet/oauth_1/client.rb in signet-0.4.0

- old
+ new

@@ -10,11 +10,11 @@ # 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. -gem 'faraday', '~> 0.7.0' +gem 'faraday', '~> 0.8.1' require 'faraday' require 'faraday/utils' require 'stringio' require 'addressable/uri' @@ -536,11 +536,12 @@ end end options = { :signature_method => 'HMAC-SHA1', :additional_parameters => [], - :realm => nil + :realm => nil, + :connection => Faraday.default_connection }.merge(options) method = :post parameters = ::Signet::OAuth1.unsigned_temporary_credential_parameters( :client_credential_key => self.client_credential_key, :callback => self.callback, @@ -563,12 +564,14 @@ headers = [authorization_header] if method == :post headers << ['Content-Type', 'application/x-www-form-urlencoded'] headers << ['Content-Length', '0'] end - return Faraday::Request.create(method.to_s.downcase.to_sym) do |req| - req.url(Addressable::URI.parse(self.temporary_credential_uri.to_str)) + return options[:connection].build_request(method.to_s.downcase.to_sym) do |req| + req.url(Addressable::URI.parse( + self.temporary_credential_uri.to_str + ).normalize.to_s) req.headers = Faraday::Utils::Headers.new(headers) end end alias_method( :generate_request_token_request, @@ -601,10 +604,11 @@ # ) def fetch_temporary_credential(options={}) options[:connection] ||= Faraday.default_connection request = self.generate_temporary_credential_request(options) request_env = request.to_env(options[:connection]) + request_env[:request] ||= request response = options[:connection].app.call(request_env) if response.status.to_i == 200 return ::Signet::OAuth1.parse_form_encoded_credentials(response.body) elsif [400, 401, 403].include?(response.status.to_i) message = 'Authorization failed.' @@ -687,11 +691,12 @@ raise ArgumentError, "#{key} was not set." end end options = { :signature_method => 'HMAC-SHA1', - :realm => nil + :realm => nil, + :connection => Faraday.default_connection }.merge(options) method = :post parameters = ::Signet::OAuth1.unsigned_token_credential_parameters( :client_credential_key => self.client_credential_key, :temporary_credential_key => self.temporary_credential_key, @@ -716,12 +721,14 @@ headers << ['Cache-Control', 'no-store'] if method == :post headers << ['Content-Type', 'application/x-www-form-urlencoded'] headers << ['Content-Length', '0'] end - return Faraday::Request.create(method.to_s.downcase.to_sym) do |req| - req.url(Addressable::URI.parse(self.token_credential_uri.to_str)) + return options[:connection].build_request(method.to_s.downcase.to_sym) do |req| + req.url(Addressable::URI.parse( + self.token_credential_uri.to_str + ).normalize.to_s) req.headers = Faraday::Utils::Headers.new(headers) end end alias_method( :generate_access_token_request, @@ -752,10 +759,11 @@ # ) def fetch_token_credential(options={}) options[:connection] ||= Faraday.default_connection request = self.generate_token_credential_request(options) request_env = request.to_env(options[:connection]) + request_env[:request] ||= request response = options[:connection].app.call(request_env) if response.status.to_i == 200 return ::Signet::OAuth1.parse_form_encoded_credentials(response.body) elsif [400, 401, 403].include?(response.status.to_i) message = 'Authorization failed.' @@ -887,13 +895,12 @@ end if !body.kind_of?(String) raise TypeError, "Expected String, got #{body.class}." end method = method.to_s.downcase.to_sym - - request = Faraday::Request.create(method) do |req| - req.url(Addressable::URI.parse(uri)) + request = options[:connection].build_request(method) do |req| + req.url(Addressable::URI.parse(uri).normalize.to_s) req.headers = Faraday::Utils::Headers.new(headers) req.body = body end end @@ -976,9 +983,10 @@ # @return [Array] The response object. def fetch_protected_resource(options={}) options[:connection] ||= Faraday.default_connection request = self.generate_authenticated_request(options) request_env = request.to_env(options[:connection]) + request_env[:request] ||= request response = options[:connection].app.call(request_env) if response.status.to_i == 401 # When accessing a protected resource, we only want to raise an # error for 401 responses. message = 'Authorization failed.'