Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch responses to pure-ish faraday #57

Merged
merged 9 commits into from
Feb 12, 2024
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unreleased

** Breaking change: automatic digging of the value if the reponse body is an object with a single key
** Breaking change: remove `Scalingo::API::Reponse` in favor of `Faraday::Response`

## 3.5.0 - 2023-12-28

* 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.
Expand Down
12 changes: 11 additions & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
require "bundler/setup"
require "scalingo"

begin
require "dotenv"
Dotenv.load(".env.local")
rescue LoadError
puts("dotenv not available - no .env.local loading")
end

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
require "pry"

Pry.start
client = Scalingo::Client.new
client.authenticate_with(access_token: ENV["SCALINGO_API_TOKEN"]) if ENV["SCALINGO_API_TOKEN"].present?

Pry.start(binding, quiet: true)
10 changes: 10 additions & 0 deletions lib/scalingo/api/client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require "scalingo/token_holder"
require "scalingo/faraday/response"
require "scalingo/faraday/extract_meta"
require "scalingo/faraday/extract_root_value"
require "active_support/core_ext/hash"

module Scalingo
module API
Expand Down Expand Up @@ -84,6 +88,8 @@ def connection(fallback_to_guest: false)

def unauthenticated_connection
@unauthenticated_conn ||= Faraday.new(connection_options) { |conn|
conn.response :extract_root_value
conn.response :extract_meta
conn.response :json, content_type: /\bjson$/, parser_options: {symbolize_names: true}
conn.request :json

Expand All @@ -104,6 +110,8 @@ def authenticated_connection
end

@connection = Faraday.new(connection_options) { |conn|
conn.response :extract_root_value
conn.response :extract_meta
conn.response :json, content_type: /\bjson$/, parser_options: {symbolize_names: true}
conn.request :json
conn.request :authorization, "Bearer", -> { token_holder.token&.value }
Expand All @@ -117,6 +125,8 @@ def database_connection(database_id)

@database_connections ||= {}
@database_connections[database_id] ||= Faraday.new(connection_options) { |conn|
conn.response :extract_root_value
conn.response :extract_meta
conn.response :json, content_type: /\bjson$/, parser_options: {symbolize_names: true}
conn.request :json
conn.request :authorization, "Bearer", -> { token_holder.database_tokens[database_id]&.value }
Expand Down
7 changes: 0 additions & 7 deletions lib/scalingo/api/endpoint.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "forwardable"
require "scalingo/api/response"

module Scalingo
module API
Expand All @@ -23,12 +22,6 @@ def inspect
str << ">"
str
end

private

def unpack(*keys, &block)
Response.unpack(client, keys: keys, &block)
end
end
end
end
69 changes: 0 additions & 69 deletions lib/scalingo/api/response.rb

This file was deleted.

17 changes: 5 additions & 12 deletions lib/scalingo/auth/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,45 @@ class Auth::Keys < API::Endpoint
def all(headers = nil, &block)
data = nil

response = connection.get(
connection.get(
"keys",
data,
headers,
&block
)

unpack(:keys) { response }
end

def show(id, headers = nil, &block)
data = nil

response = connection.get(
connection.get(
"keys/#{id}",
data,
headers,
&block
)

unpack(:key) { response }
end

def create(payload, headers = nil, &block)
data = {key: payload}

response = connection.post(
connection.post(
"keys",
data,
headers,
&block
)

unpack(:key) { response }
end

def destroy(id, headers = nil, &block)
data = nil
response = connection.delete(

connection.delete(
"keys/#{id}",
data,
headers,
&block
)

unpack { response }
end
end
end
16 changes: 4 additions & 12 deletions lib/scalingo/auth/scm_integrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,45 @@ class Auth::ScmIntegrations < API::Endpoint
def all(headers = nil, &block)
data = nil

response = connection.get(
connection.get(
"scm_integrations",
data,
headers,
&block
)

unpack(:scm_integrations) { response }
end

def show(id, headers = nil, &block)
data = nil

response = connection.get(
connection.get(
"scm_integrations/#{id}",
data,
headers,
&block
)

unpack(:scm_integration) { response }
end

def create(payload, headers = nil, &block)
data = {scm_integration: payload}

response = connection.post(
connection.post(
"scm_integrations",
data,
headers,
&block
)

unpack(:scm_integration) { response }
end

def destroy(id, headers = nil, &block)
data = nil

response = connection.delete(
connection.delete(
"scm_integrations/#{id}",
data,
headers,
&block
)

unpack { response }
end
end
end
20 changes: 5 additions & 15 deletions lib/scalingo/auth/tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,66 +13,56 @@ def exchange(token, headers = nil, &block)

request_headers.update(headers) if headers

response = client.unauthenticated_connection.post(
client.unauthenticated_connection.post(
"tokens/exchange",
data,
request_headers,
&block
)

unpack { response }
end

def all(headers = nil, &block)
data = nil

response = connection.get(
connection.get(
"tokens",
data,
headers,
&block
)

unpack(:tokens) { response }
end

def create(payload, headers = nil, &block)
data = {token: payload}

response = connection.post(
connection.post(
"tokens",
data,
headers,
&block
)

unpack(:token) { response }
end

def renew(id, headers = nil, &block)
data = nil

response = connection.patch(
connection.patch(
"tokens/#{id}/renew",
data,
headers,
&block
)

unpack(:token) { response }
end

def destroy(id, headers = nil, &block)
data = nil

response = connection.delete(
connection.delete(
"tokens/#{id}",
data,
headers,
&block
)

unpack { response }
end
end
end
Loading