Skip to content

Commit a3f096a

Browse files
author
unabris
committed
Add client_credentials token method
1 parent 9b333dc commit a3f096a

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lib/beyond_api/resources/token.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class InvalidSessionError < StandardError; end
88

99
def initialize(session)
1010
@session = session
11+
1112
raise InvalidSessionError.new("Invalid session") unless session.is_a? BeyondApi::Session
1213
raise InvalidSessionError.new("Session api_url cannot be nil") if session.api_url.nil?
1314
end
@@ -17,22 +18,31 @@ def create(code)
1718
grant_type: "authorization_code",
1819
code: code)
1920

20-
if status.between?(200, 299)
21-
@session.access_token = response["access_token"]
22-
@session.refresh_token = response["refresh_token"]
23-
else
24-
BeyondApi::Error.new(response)
25-
end
21+
handle_response(response, status)
2622
end
2723

2824
def refresh
2925
response, status = BeyondApi::Request.token(@session.api_url + "/oauth/token",
3026
grant_type: "refresh_token",
3127
refresh_token: @session.refresh_token)
3228

29+
handle_response(response, status)
30+
end
31+
32+
def client_credentials
33+
response, status = BeyondApi::Request.token(@session.api_url + "/oauth/token",
34+
grant_type: "client_credentials")
35+
36+
handle_response(response, status)
37+
end
38+
39+
private
40+
41+
def handle_response(response, status)
3342
if status.between?(200, 299)
3443
@session.access_token = response["access_token"]
3544
@session.refresh_token = response["refresh_token"]
45+
@session
3646
else
3747
BeyondApi::Error.new(response)
3848
end

0 commit comments

Comments
 (0)