Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions incognia_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/incognia_api/api.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "faraday"
require "json"
require "logger"
require 'faraday_middleware'
require "faraday"

module Incognia
class Api
Expand Down
44 changes: 22 additions & 22 deletions lib/incognia_api/client.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -26,36 +20,43 @@ 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

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"
}
Comment on lines +55 to +59
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting r.body to a hash will not work correctly with the URL-encoded content type. The body should be URL-encoded string format or use Faraday's form encoding. Consider using URI.encode_www_form or let Faraday handle the encoding by using the :url_encoded request middleware properly.

Copilot uses AI. Check for mistakes.
end

response.success? ? build_credentials(response) : nil
Expand All @@ -67,14 +68,13 @@ 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
)

Credentials.from_hash(properties)
end

end
end
Loading