Skip to content

Commit

Permalink
refactor: remove unpack from API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ksol committed Feb 11, 2024
1 parent d5db9ce commit 1cd8a92
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 289 deletions.
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
16 changes: 4 additions & 12 deletions lib/scalingo/auth/two_factor_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,45 @@ class Auth::TwoFactorAuth < API::Endpoint
def status(headers = nil, &block)
data = nil

response = connection.get(
connection.get(
"client/tfa",
data,
headers,
&block
)

unpack(:tfa) { response }
end

def initiate(provider = DEFAULT_PROVIDER, headers = nil, &block)
data = {tfa: {provider: provider}}

response = connection.post(
connection.post(
"client/tfa",
data,
headers,
&block
)

unpack(:tfa) { response }
end

def validate(attempt, headers = nil, &block)
data = {tfa: {attempt: attempt}}

response = connection.post(
connection.post(
"client/tfa/validate",
data,
headers,
&block
)

unpack(:tfa) { response }
end

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

response = connection.delete(
connection.delete(
"client/tfa",
data,
headers,
&block
)

unpack(:tfa) { response }
end
end
end
12 changes: 3 additions & 9 deletions lib/scalingo/auth/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,34 @@ class Auth::User < API::Endpoint
def self(headers = nil, &block)
data = nil

response = connection.get(
connection.get(
"users/self",
data,
headers,
&block
)

unpack(:user) { response }
end

def update(payload, headers = nil, &block)
data = {user: payload}

response = connection.put(
connection.put(
"users/account",
data,
headers,
&block
)

unpack(:user) { response }
end

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

response = connection.post(
connection.post(
"users/stop_free_trial",
data,
headers,
&block
)

unpack { response }
end
end
end
12 changes: 3 additions & 9 deletions lib/scalingo/billing/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,34 @@ class Billing::Profile < API::Endpoint
def show(headers = nil, &block)
data = nil

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

unpack(:profile) { response }
end

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

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

unpack(:profile) { response }
end

def update(id, payload = {}, headers = nil, &block)
data = {profile: payload}

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

unpack(:profile) { response }
end

alias_method :self, :show
Expand Down
2 changes: 1 addition & 1 deletion lib/scalingo/core_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def authenticate_with(access_token: nil, bearer_token: nil, expires_at: nil)

if response.successful?
self.token = BearerToken.new(
response.data[:token],
response.data,
expires_at: expiration,
raise_on_expired: config.raise_on_expired_token
)
Expand Down
Loading

0 comments on commit 1cd8a92

Please sign in to comment.