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

Timeout handling #22

Closed
wants to merge 2 commits into from
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

* Bugfix: remove `https` configuration option (development artifact) (#20)
* Improv: handle timeouts. Breaks an internal API. (#21, #22)
* Improv: configure the faraday adapter to use (#20)
* Improv: prettier `inspect` for common objects (#20)
* New: Add `addons#token` endpoint (#20)
Expand Down
4 changes: 2 additions & 2 deletions lib/scalingo/api/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def inspect

private

def unpack(*args)
Response.unpack(client, *args)
def unpack(key = nil, &block)
Response.unpack(client, key: key, &block)
end
end
end
Expand Down
25 changes: 23 additions & 2 deletions lib/scalingo/api/response.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
module Scalingo
module API
class Response
def self.unpack(client, response, key: nil)
def self.timeout(client)
new(
client: client,
status: nil,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add another status for timeout ? There might be other reason for an empty status (DNS resolution failed, connection reset, etc...)

headers: nil,
data: nil,
meta: nil,
full_body: nil,
)
end

def self.unpack(client, key: nil, &block)
begin
response = block.call
rescue Faraday::ConnectionFailed
return timeout(client)
end

body = response.body
has_hash_body = body.present? && body.respond_to?(:key)

Expand Down Expand Up @@ -39,7 +56,11 @@ def inspect
end

def successful?
status >= 200 && status < 300
status.present? && status >= 200 && status < 300
end

def timeout?
status.nil?
end

def paginated?
Expand Down
8 changes: 4 additions & 4 deletions lib/scalingo/auth/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def all(headers = nil, &block)
&block
)

unpack(response, key: :keys)
unpack(:keys) { response }
end

def show(id, headers = nil, &block)
Expand All @@ -25,7 +25,7 @@ def show(id, headers = nil, &block)
&block
)

unpack(response, key: :key)
unpack(:key) { response }
end

def create(payload, headers = nil, &block)
Expand All @@ -38,7 +38,7 @@ def create(payload, headers = nil, &block)
&block
)

unpack(response, key: :key)
unpack(:key) { response }
end

def destroy(id, headers = nil, &block)
Expand All @@ -50,7 +50,7 @@ def destroy(id, headers = nil, &block)
&block
)

unpack(response)
unpack { response }
end
end
end
8 changes: 4 additions & 4 deletions lib/scalingo/auth/scm_integrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def all(headers = nil, &block)
&block
)

unpack(response, key: :scm_integrations)
unpack(:scm_integrations) { response }
end

def show(id, headers = nil, &block)
Expand All @@ -25,7 +25,7 @@ def show(id, headers = nil, &block)
&block
)

unpack(response, key: :scm_integration)
unpack(:scm_integration) { response }
end

def create(payload, headers = nil, &block)
Expand All @@ -38,7 +38,7 @@ def create(payload, headers = nil, &block)
&block
)

unpack(response, key: :scm_integration)
unpack(:scm_integration) { response }
end

def destroy(id, headers = nil, &block)
Expand All @@ -51,7 +51,7 @@ def destroy(id, headers = nil, &block)
&block
)

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

unpack(response)
unpack { response }
end

def all(headers = nil, &block)
Expand All @@ -33,7 +33,7 @@ def all(headers = nil, &block)
&block
)

unpack(response, key: :tokens)
unpack(:tokens) { response }
end

def create(payload, headers = nil, &block)
Expand All @@ -46,7 +46,7 @@ def create(payload, headers = nil, &block)
&block
)

unpack(response, key: :token)
unpack(:token) { response }
end

def renew(id, headers = nil, &block)
Expand All @@ -59,7 +59,7 @@ def renew(id, headers = nil, &block)
&block
)

unpack(response, key: :token)
unpack(:token) { response }
end

def destroy(id, headers = nil, &block)
Expand All @@ -72,7 +72,7 @@ def destroy(id, headers = nil, &block)
&block
)

unpack(response)
unpack { response }
end
end
end
8 changes: 4 additions & 4 deletions lib/scalingo/auth/two_factor_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def status(headers = nil, &block)
&block
)

unpack(response, key: :tfa)
unpack(:tfa) { response }
end

def initiate(provider = DEFAULT_PROVIDER, headers = nil, &block)
Expand All @@ -29,7 +29,7 @@ def initiate(provider = DEFAULT_PROVIDER, headers = nil, &block)
&block
)

unpack(response, key: :tfa)
unpack(:tfa) { response }
end

def validate(attempt, headers = nil, &block)
Expand All @@ -42,7 +42,7 @@ def validate(attempt, headers = nil, &block)
&block
)

unpack(response, key: :tfa)
unpack(:tfa) { response }
end

def disable(headers = nil, &block)
Expand All @@ -55,7 +55,7 @@ def disable(headers = nil, &block)
&block
)

unpack(response, key: :tfa)
unpack(:tfa) { response }
end
end
end
4 changes: 2 additions & 2 deletions lib/scalingo/auth/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self(headers = nil, &block)
&block
)

unpack(response, key: :user)
unpack(:user) { response }
end

def update(payload, headers = nil, &block)
Expand All @@ -25,7 +25,7 @@ def update(payload, headers = nil, &block)
&block
)

unpack(response, key: :user)
unpack(:user) { response }
end
end
end
6 changes: 3 additions & 3 deletions lib/scalingo/billing/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def show(headers = nil, &block)
&block
)

unpack(response, key: :profile)
unpack(:profile) { response }
end

def create(payload = {}, headers = nil, &block)
Expand All @@ -25,7 +25,7 @@ def create(payload = {}, headers = nil, &block)
&block
)

unpack(response, key: :profile)
unpack(:profile) { response }
end

def update(id, payload = {}, headers = nil, &block)
Expand All @@ -38,7 +38,7 @@ def update(id, payload = {}, headers = nil, &block)
&block
)

unpack(response, key: :profile)
unpack(:profile) { response }
end

alias_method :self, :show
Expand Down
18 changes: 9 additions & 9 deletions lib/scalingo/regional/addons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def for(app_id, headers = nil, &block)
&block
)

unpack(response, key: :addons)
unpack(:addons) { response }
end

def find(app_id, addon_id, headers = nil, &block)
Expand All @@ -25,7 +25,7 @@ def find(app_id, addon_id, headers = nil, &block)
&block
)

unpack(response, key: :addon)
unpack(:addon) { response }
end

def provision(app_id, payload = {}, headers = nil, &block)
Expand All @@ -38,7 +38,7 @@ def provision(app_id, payload = {}, headers = nil, &block)
&block
)

unpack(response, key: :addon)
unpack(:addon) { response }
end

def update(app_id, addon_id, payload = {}, headers = nil, &block)
Expand All @@ -51,7 +51,7 @@ def update(app_id, addon_id, payload = {}, headers = nil, &block)
&block
)

unpack(response, key: :addon)
unpack(:addon) { response }
end

def destroy(app_id, addon_id, headers = nil, &block)
Expand All @@ -64,7 +64,7 @@ def destroy(app_id, addon_id, headers = nil, &block)
&block
)

unpack(response)
unpack { response }
end

def sso(app_id, addon_id, headers = nil, &block)
Expand All @@ -77,7 +77,7 @@ def sso(app_id, addon_id, headers = nil, &block)
&block
)

unpack(response, key: :addon)
unpack(:addon) { response }
end

def token(app_id, addon_id, headers = nil, &block)
Expand All @@ -90,7 +90,7 @@ def token(app_id, addon_id, headers = nil, &block)
&block
)

unpack(response, key: :addon)
unpack(:addon) { response }
end

def categories(headers = nil, &block)
Expand All @@ -103,7 +103,7 @@ def categories(headers = nil, &block)
&block
)

unpack(response, key: :addon_categories)
unpack(:addon_categories) { response }
end

def providers(headers = nil, &block)
Expand All @@ -116,7 +116,7 @@ def providers(headers = nil, &block)
&block
)

unpack(response, key: :addon_providers)
unpack(:addon_providers) { response }
end
end
end
Loading