From 9d40f30ac01a9ca9549efc07312c5c75c48c5ba0 Mon Sep 17 00:00:00 2001 From: Wallace Soares Date: Thu, 18 Sep 2025 15:56:56 +0400 Subject: [PATCH] Upgrade Faraday lib to 2.9.0 --- Gemfile.lock | 41 +++++++++++------------------------ incognia_api.gemspec | 4 ++-- lib/incognia_api/api.rb | 3 +-- lib/incognia_api/client.rb | 44 +++++++++++++++++++------------------- 4 files changed, 37 insertions(+), 55 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index d620a1c..2cbafce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,8 +2,7 @@ PATH remote: . specs: incognia_api (2.2.0) - faraday (~> 1.10) - faraday_middleware (~> 1.2) + faraday (>= 2.9.0, < 3.0) GEM remote: https://rubygems.org/ @@ -14,34 +13,18 @@ GEM crack (0.4.5) rexml diff-lcs (1.4.4) - faraday (1.10.3) - faraday-em_http (~> 1.0) - faraday-em_synchrony (~> 1.0) - faraday-excon (~> 1.1) - faraday-httpclient (~> 1.0) - faraday-multipart (~> 1.0) - faraday-net_http (~> 1.0) - faraday-net_http_persistent (~> 1.0) - faraday-patron (~> 1.0) - faraday-rack (~> 1.0) - faraday-retry (~> 1.0) - ruby2_keywords (>= 0.0.4) - faraday-em_http (1.0.0) - faraday-em_synchrony (1.0.0) - faraday-excon (1.1.0) - faraday-httpclient (1.0.1) - faraday-multipart (1.0.4) - multipart-post (~> 2) - faraday-net_http (1.0.1) - faraday-net_http_persistent (1.2.0) - faraday-patron (1.0.0) - faraday-rack (1.0.0) - faraday-retry (1.0.3) - faraday_middleware (1.2.0) - faraday (~> 1.0) + faraday (2.13.4) + faraday-net_http (>= 2.0, < 3.5) + json + logger + faraday-net_http (3.4.1) + net-http (>= 0.5.0) hashdiff (1.0.1) + json (2.14.0) + logger (1.7.0) method_source (1.0.0) - multipart-post (2.3.0) + net-http (0.6.0) + uri pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -61,8 +44,8 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.10.0) rspec-support (3.10.2) - ruby2_keywords (0.0.5) timecop (0.9.4) + uri (1.0.3) webmock (3.14.0) addressable (>= 2.8.0) crack (>= 0.3.2) diff --git a/incognia_api.gemspec b/incognia_api.gemspec index ec8305c..93fe3fc 100644 --- a/incognia_api.gemspec +++ b/incognia_api.gemspec @@ -28,8 +28,8 @@ Gem::Specification.new do |spec| # Uncomment to register a new dependency of your gem # spec.add_dependency "example-gem", "~> 1.0" - spec.add_dependency('faraday', '~> 1.10') - spec.add_dependency('faraday_middleware', '~> 1.2') + spec.add_dependency "faraday", ">= 2.9.0", "< 3.0" + # For more information and examples about making a new gem, checkout our # guide at: https://bundler.io/guides/creating_gem.html diff --git a/lib/incognia_api/api.rb b/lib/incognia_api/api.rb index c536cef..8d7e435 100644 --- a/lib/incognia_api/api.rb +++ b/lib/incognia_api/api.rb @@ -1,7 +1,6 @@ -require "faraday" require "json" require "logger" -require 'faraday_middleware' +require "faraday" module Incognia class Api diff --git a/lib/incognia_api/client.rb b/lib/incognia_api/client.rb index a5b8ae1..9aa7614 100644 --- a/lib/incognia_api/client.rb +++ b/lib/incognia_api/client.rb @@ -1,22 +1,16 @@ -require "time" require "singleton" +require "time" module Incognia class Client include Singleton - # TODO: - # (ok) http/adapter specific code - # (ok) raises network/authentication errors - # (ok) handles token refreshing ok - # future: handles retrying def request(method, endpoint = nil, data = nil, headers = {}) json_data = JSON.generate(data) if data connection.send(method, endpoint, json_data, headers) do |r| - r.headers[Faraday::Request::Authorization::KEY] ||= Faraday::Request - .lookup_middleware(:authorization) - .header(:Bearer, credentials.access_token) + # Ensure Bearer token is set + r.headers["Authorization"] ||= "Bearer #{credentials.access_token}" end rescue Faraday::ClientError, Faraday::ServerError => e raise APIError.new(e.to_s, e.response) @@ -26,23 +20,29 @@ def request(method, endpoint = nil, data = nil, headers = {}) def credentials @credentials = request_credentials if @credentials.nil? || @credentials.stale? - @credentials end def connection return @connection if @connection - headers = { 'User-Agent' => "incognia-ruby/#{Incognia::VERSION} " \ - "({#{RbConfig::CONFIG['host']}}) " \ - "{#{RbConfig::CONFIG['arch']}} " \ - "Ruby/#{RbConfig::CONFIG['ruby_version']}" } + headers = { + "User-Agent" => "incognia-ruby/#{Incognia::VERSION} " \ + "(#{RbConfig::CONFIG['host']}) " \ + "#{RbConfig::CONFIG['arch']} " \ + "Ruby/#{RbConfig::CONFIG['ruby_version']}" + } @connection = Faraday.new(Incognia.config.host, headers: headers) do |faraday| faraday.request :json + faraday.request :url_encoded faraday.response :json, content_type: /\bjson$/ faraday.response :raise_error + faraday.request :authorization, :basic, + Incognia.config.client_id, + Incognia.config.client_secret + faraday.adapter Faraday.default_adapter end end @@ -50,12 +50,13 @@ def connection protected def request_credentials - basic_auth = Faraday::Request - .lookup_middleware(:basic_auth) - .header(Incognia.config.client_id, Incognia.config.client_secret) - - response = connection.send(:post, 'v2/token') do |r| - r.headers[Faraday::Request::Authorization::KEY] = basic_auth + response = connection.post("v2/token") do |r| + r.headers["Content-Type"] = "application/x-www-form-urlencoded" + r.body = { + client_id: Incognia.config.client_id, + client_secret: Incognia.config.client_secret, + grant_type: "client_credentials" + } end response.success? ? build_credentials(response) : nil @@ -67,7 +68,7 @@ def request_credentials def build_credentials(raw_response) body = raw_response.body - response_date = raw_response.headers['Date'] + response_date = raw_response.headers["Date"] properties = body.merge( generated_at: response_date ? Time.parse(response_date) : Time.now @@ -75,6 +76,5 @@ def build_credentials(raw_response) Credentials.from_hash(properties) end - end end