Skip to content

Commit

Permalink
Merge pull request #55 from Scalingo/chore/maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
ksol authored Dec 27, 2023
2 parents 7edb64a + fae126e commit d9afca1
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.6', '2.7', '3.0', '3.2']
ruby-version: ['3.0', '3.1', '3.2', '3.3']
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
Expand Down
30 changes: 9 additions & 21 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
inherit_from: []

inherit_gem:
standard: config/base.yml

require:
- standard
- standard-custom
- standard-performance
- rubocop-performance

inherit_gem:
standard: config/base.yml
standard-custom: config/base.yml
standard-performance: config/base.yml

AllCops:
TargetRubyVersion: 3.0
NewCops: enable
SuggestExtensions: false
Exclude:
- 'node_modules/**/*'
- 'tmp/**/*'
- 'vendor/**/*'
- '.git/**/*'

Style/TrailingCommaInArguments:
Enabled: true
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArrayLiteral:
Enabled: true
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInHashLiteral:
Enabled: true
EnforcedStyleForMultiline: consistent_comma

Style/Alias:
Enabled: true
EnforcedStyle: prefer_alias_method
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

* Change: update Faraday to 2.x, released about two years ago. The public API of this gem doesn't change, therefore this is not a major release. However, if you manipulate directly faraday's objects, you may encounter breaking changes. Refer to [Faraday's website](https://lostisland.github.io/faraday/) for how to migrate.

## 3.4.0 - 2023-01-26

* New: Add `databases#upgrade` endpoint for database API ([#51](https://github.com/Scalingo/scalingo-ruby-api/pull/51))
Expand Down
21 changes: 6 additions & 15 deletions lib/scalingo/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def self.register_handlers!(handlers)

def self.register_handler!(method_name, klass)
define_method(method_name) do
value = instance_variable_get("@#{method_name}")
value = instance_variable_get(:"@#{method_name}")

if value.nil?
value = klass.new(self)
instance_variable_set("@#{method_name}", value)
instance_variable_set(:"@#{method_name}", value)
end

value
Expand All @@ -54,7 +54,7 @@ def inspect
def headers
hash = {
"User-Agent" => config.user_agent,
"Accept" => "application/json",
"Accept" => "application/json"
}

if (extra = config.additional_headers).present?
Expand All @@ -67,7 +67,7 @@ def headers
def connection_options
{
url: url,
headers: headers,
headers: headers
}
end

Expand Down Expand Up @@ -106,11 +106,7 @@ def authenticated_connection
@connection = Faraday.new(connection_options) { |conn|
conn.response :json, content_type: /\bjson$/, parser_options: {symbolize_names: true}
conn.request :json

if token_holder.token&.value
auth_header = Faraday::Request::Authorization.header "Bearer", token_holder.token.value
conn.headers[Faraday::Request::Authorization::KEY] = auth_header
end
conn.request :authorization, "Bearer", -> { token_holder.token&.value }

conn.adapter(config.faraday_adapter) if config.faraday_adapter
}
Expand All @@ -123,12 +119,7 @@ def database_connection(database_id)
@database_connections[database_id] ||= Faraday.new(connection_options) { |conn|
conn.response :json, content_type: /\bjson$/, parser_options: {symbolize_names: true}
conn.request :json

bearer_token = token_holder.database_tokens[database_id]&.value
if bearer_token
auth_header = Faraday::Request::Authorization.header "Bearer", bearer_token
conn.headers[Faraday::Request::Authorization::KEY] = auth_header
end
conn.request :authorization, "Bearer", -> { token_holder.database_tokens[database_id]&.value }

conn.adapter(config.faraday_adapter) if config.faraday_adapter
}
Expand Down
6 changes: 3 additions & 3 deletions lib/scalingo/api/response.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Scalingo
module API
class Response
def self.unpack(client, key: nil, keys: nil, &block)
response = block.call
def self.unpack(client, key: nil, keys: nil)
response = yield

body = response.body
has_hash_body = body.present? && body.respond_to?(:key)
Expand All @@ -24,7 +24,7 @@ def self.unpack(client, key: nil, keys: nil, &block)
headers: response.headers,
data: data,
meta: meta,
full_body: body,
full_body: body
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/scalingo/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Auth < API::Client
scm_integrations: ScmIntegrations,
tokens: Tokens,
two_factor_auth: TwoFactorAuth,
user: User,
user: User
)

alias_method :tfa, :two_factor_auth
Expand Down
4 changes: 2 additions & 2 deletions lib/scalingo/auth/tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class Auth::Tokens < API::Endpoint
def exchange(token, headers = nil, &block)
data = nil

authorization = Faraday::Request::BasicAuthentication.header("", token)
authorization = Faraday::Utils.basic_header_from("", token)

request_headers = {
Faraday::Request::Authorization::KEY => authorization,
Faraday::Request::Authorization::KEY => authorization
}

request_headers.update(headers) if headers
Expand Down
2 changes: 1 addition & 1 deletion lib/scalingo/billing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Billing < API::Client
require "scalingo/billing/profile"

register_handlers!(
profile: Profile,
profile: Profile
)
end
end
12 changes: 6 additions & 6 deletions lib/scalingo/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,44 @@ class Client < CoreClient
def auth
@auth ||= Auth.new(
"https://auth.scalingo.com/v1",
scalingo: self,
scalingo: self
)
end

def billing
@billing ||= Billing.new(
"https://cashmachine.scalingo.com",
scalingo: self,
scalingo: self
)
end

def osc_fr1
@osc_fr1 ||= Regional.new(
"https://api.osc-fr1.scalingo.com/v1",
scalingo: self,
scalingo: self
)
end
alias_method :apps_api_osc_fr1, :osc_fr1

def osc_secnum_fr1
@osc_secnum_fr1 ||= Regional.new(
"https://api.osc-secnum-fr1.scalingo.com/v1",
scalingo: self,
scalingo: self
)
end
alias_method :apps_api_osc_secnum_fr1, :osc_secnum_fr1

def db_api_osc_fr1
@db_api_osc_fr1 ||= RegionalDatabase.new(
"https://db-api.osc-fr1.scalingo.com/api",
scalingo: self,
scalingo: self
)
end

def db_api_osc_secnum_fr1
@db_api_osc_secnum_fr1 ||= RegionalDatabase.new(
"https://db-api.osc-secnum-fr1.scalingo.com/api",
scalingo: self,
scalingo: self
)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/scalingo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Configuration
:additional_headers,

# Specify an adapter for faraday. Leave nil for the default one (Net::HTTP)
:faraday_adapter,
:faraday_adapter
]

ATTRIBUTES.each { |attr| attr_accessor(attr) }
Expand All @@ -49,13 +49,13 @@ def self.default
exchanged_token_validity: 1.hour,
raise_on_missing_authentication: true,
raise_on_expired_token: false,
additional_headers: {},
additional_headers: {}
)
end

def initialize(attributes = {}, parent = nil)
ATTRIBUTES.each do |name|
public_send("#{name}=", attributes.fetch(name, parent&.public_send(name)))
public_send(:"#{name}=", attributes.fetch(name, parent&.public_send(name)))
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/scalingo/core_client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "forwardable"
require "faraday"
require "faraday_middleware"
require "scalingo/token_holder"
require "scalingo/errors"

Expand Down Expand Up @@ -67,7 +66,7 @@ def authenticate_with(access_token: nil, bearer_token: nil, expires_at: nil)
self.token = BearerToken.new(
response.data[:token],
expires_at: expiration,
raise_on_expired: config.raise_on_expired_token,
raise_on_expired: config.raise_on_expired_token
)
end

Expand All @@ -78,7 +77,7 @@ def authenticate_with(access_token: nil, bearer_token: nil, expires_at: nil)
authenticate_with_bearer_token(
bearer_token,
expires_at: expires_at,
raise_on_expired_token: config.raise_on_expired_token,
raise_on_expired_token: config.raise_on_expired_token
)

true
Expand Down
2 changes: 1 addition & 1 deletion lib/scalingo/regional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Regional < API::Client
metrics: Metrics,
notifiers: Notifiers,
operations: Operations,
scm_repo_links: ScmRepoLinks,
scm_repo_links: ScmRepoLinks
)
end
end
2 changes: 1 addition & 1 deletion lib/scalingo/regional/addons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def authenticate!(app_id, addon_id, headers = nil, &block)
addon_id,
token,
expires_at: Time.now + 1.hour,
raise_on_expired_token: client.config.raise_on_expired_token,
raise_on_expired_token: client.config.raise_on_expired_token
)

response
Expand Down
2 changes: 1 addition & 1 deletion lib/scalingo/regional_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RegionalDatabase < API::Client

register_handlers!(
databases: Databases,
backups: Backups,
backups: Backups
)
end
end
2 changes: 1 addition & 1 deletion lib/scalingo/token_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def build_bearer_token(bearer_token, expires_at:, raise_on_expired_token:)
BearerToken.new(
token,
expires_at: expires_at,
raise_on_expired: raise_on_expired_token,
raise_on_expired: raise_on_expired_token
)
end
end
Expand Down
11 changes: 4 additions & 7 deletions scalingo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |s|
"changelog_uri" => "https://github.com/Scalingo/scalingo-ruby-api/blob/master/CHANGELOG.md",
"documentation_uri" => "https://developers.scalingo.com/",
"homepage_uri" => "https://www.scalingo.com/",
"source_code_uri" => "https://github.com/Scalingo/scalingo-ruby-api",
"source_code_uri" => "https://github.com/Scalingo/scalingo-ruby-api"
}

# Specify which files should be added to the gem when it is released.
Expand All @@ -32,18 +32,15 @@ Gem::Specification.new do |s|
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
s.require_paths = ["lib"]

s.test_files = Dir["spec/**/*_spec.rb"]

s.add_dependency "activesupport", [">= 5", "< 8"]
s.add_dependency "faraday", "~> 1.0"
s.add_dependency "faraday_middleware", "~> 1.0"
s.add_dependency "faraday", "~> 2.0"
s.add_dependency "multi_json", ">= 1.0.3", "~> 1.0"

s.add_development_dependency "bundler", "~> 2.0"
s.add_development_dependency "rake", "~> 13.0"
s.add_development_dependency "rspec", "~> 3.0"
s.add_development_dependency "standard", "~> 1.1.0"
s.add_development_dependency "standard", "~> 1.32"
s.add_development_dependency "rubocop-rspec"
s.add_development_dependency "pry", "~> 0.14.1"
s.add_development_dependency "webmock", "~> 3.12.2"
s.add_development_dependency "webmock", "~> 3.19"
end
19 changes: 15 additions & 4 deletions spec/scalingo/api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,13 @@
end

context "with bearer token" do
before { stub_request(:any, "localhost") }

it "has an authentication header set with a bearer scheme" do
expect(subject.connection.headers["Authorization"]).to eq "Bearer #{subject.token_holder.token.value}"
request_headers = subject.connection.get("/").env.request_headers
expected = "Bearer #{subject.token_holder.token.value}"

expect(request_headers["Authorization"]).to eq(expected)
end
end
end
Expand All @@ -184,14 +189,20 @@
end

context "with bearer token" do
before { stub_request(:any, "localhost") }

it "has an authentication header set with a bearer scheme" do
scalingo.authenticate_database_with_bearer_token(
database_id,
"1234",
expires_at: Time.now + 1.hour,
raise_on_expired_token: false,
raise_on_expired_token: false
)
expect(subject.database_connection(database_id).headers["Authorization"]).to eq "Bearer #{subject.token_holder.database_tokens[database_id].value}"

request_headers = subject.database_connection(database_id).get("/").env.request_headers
expected = "Bearer #{subject.token_holder.database_tokens[database_id].value}"

expect(request_headers["Authorization"]).to eq(expected)
end
end

Expand All @@ -202,7 +213,7 @@
database_id_2,
"1234",
expires_at: Time.now + 1.hour,
raise_on_expired_token: false,
raise_on_expired_token: false
)
expect {
subject.database_connection(database_id)
Expand Down
Loading

0 comments on commit d9afca1

Please sign in to comment.