Skip to content

Commit

Permalink
Fix issue with error code at regenerate_token method. Move extract_er…
Browse files Browse the repository at this point in the history
…ror_message_from_response and extract_error_attribute_from_response to an specific class. See #4 Create ApiException that will be raised on Cursor when OneLogin API returns an error
  • Loading branch information
pitbulk committed Nov 27, 2018
1 parent d5ba62f commit 5ab4b9d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 57 deletions.
10 changes: 10 additions & 0 deletions lib/onelogin/api/apiexception.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module OneLogin
module Api
class ApiException < Exception
def initialize(message, code)
super(message)
@code = code
end
end
end
end
65 changes: 16 additions & 49 deletions lib/onelogin/api/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'onelogin/version'
require 'onelogin/api/util'
require 'onelogin/api/apiexception'
require 'onelogin/api/cursor'
require 'onelogin/api/util'
require 'json'
require 'httparty'
require 'nokogiri'
Expand Down Expand Up @@ -63,40 +64,6 @@ def clean_error
@error_attribute = nil
end

def extract_error_message_from_response(response)
message = ''
content = JSON.parse(response.body)
if content && content.has_key?('status')
status = content['status']
if status.has_key?('message')
if status['message'].instance_of?(Hash)
if status['message'].has_key?('description')
message = status['message']['description']
end
else
message = status['message']
end
elsif status.has_key?('type')
message = status['type']
end
end
message
end

def extract_error_attribute_from_response(response)
attribute = nil
content = JSON.parse(response.body)
if content && content.has_key?('status')
status = content['status']
if status.has_key?('message') && status['message'].instance_of?(Hash)
if status['message'].has_key?('attribute')
attribute = status['message']['attribute']
end
end
end
attribute
end

def expired?
Time.now.utc > @expiration
end
Expand Down Expand Up @@ -207,7 +174,7 @@ def access_token
if response.code == 200
json_data = JSON.parse(response.body)
if json_data.has_key?('status')
@error = json_data['status']['error'].to_s
@error = json_data['status']['code'].to_s
@error_description = extract_error_message_from_response(response)
else
token = OneLogin::Api::Models::OneLoginToken.new(json_data)
Expand Down Expand Up @@ -1154,13 +1121,13 @@ def get_event_types
prepare_token

begin
options = {
model: OneLogin::Api::Models::EventType,
headers: authorized_headers,
max_results: @max_results
}
options = {
model: OneLogin::Api::Models::EventType,
headers: authorized_headers,
max_results: @max_results
}

return Cursor.new(self.class, url_for(GET_EVENT_TYPES_URL), options)
return Cursor.new(self.class, url_for(GET_EVENT_TYPES_URL), options)

rescue Exception => e
@error = '500'
Expand All @@ -1182,14 +1149,14 @@ def get_events(params={})
prepare_token

begin
options = {
model: OneLogin::Api::Models::Event,
headers: authorized_headers,
max_results: @max_results,
params: params
}
options = {
model: OneLogin::Api::Models::Event,
headers: authorized_headers,
max_results: @max_results,
params: params
}

return Cursor.new(self.class, url_for(GET_EVENTS_URL), options)
return Cursor.new(self.class, url_for(GET_EVENTS_URL), options)

rescue Exception => e
@error = '500'
Expand Down
26 changes: 18 additions & 8 deletions lib/onelogin/api/cursor.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require 'onelogin/api/apiexception'
require 'onelogin/api/util'

# Cursor
#
# Used for paginating requests to the OneLogin API
#
# Returns an enumerable object
class Cursor
include Enumerable
include OneLogin::Api::Util

# Create a new instance of the Cursor.
#
Expand Down Expand Up @@ -58,16 +62,22 @@ def fetch_next_page

json = response.parsed_response

results = json['data'].flatten

@collection += if results_remaining < results.size
results.slice(0, results_remaining)
if json.nil?
raise OneLogin::Api::ApiException.new("Response could not be parsed", 500)
elsif !json.key?("data") && json["status"]["error"] == true
raise OneLogin::Api::ApiException.new(extract_error_message_from_response(response), json["status"]["code"])
else
results
end
results = json['data'].flatten

@after_cursor = after_cursor(json)
@last_cursor_empty = @after_cursor.nil?
@collection += if results_remaining < results.size
results.slice(0, results_remaining)
else
results
end

@after_cursor = after_cursor(json)
@last_cursor_empty = @after_cursor.nil?
end
end

def after_cursor(json)
Expand Down
2 changes: 2 additions & 0 deletions lib/onelogin/api/util.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'onelogin/api/util/constants'
require 'onelogin/api/util/url_builder'
require 'onelogin/api/util/parser'

module OneLogin
module Api
module Util
include OneLogin::Api::Util::Constants
include OneLogin::Api::Util::UrlBuilder
include OneLogin::Api::Util::Parser
end
end
end
42 changes: 42 additions & 0 deletions lib/onelogin/api/util/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module OneLogin
module Api
module Util
module Parser
def extract_error_message_from_response(response)
message = ''
content = JSON.parse(response.body)
if content && content.has_key?('status')
status = content['status']
if status.has_key?('message')
if status['message'].instance_of?(Hash)
if status['message'].has_key?('description')
message = status['message']['description']
end
else
message = status['message']
end
elsif status.has_key?('type')
message = status['type']
end
end
message
end

def extract_error_attribute_from_response(response)
attribute = nil
content = JSON.parse(response.body)
if content && content.has_key?('status')
status = content['status']
if status.has_key?('message') && status['message'].instance_of?(Hash)
if status['message'].has_key?('attribute')
attribute = status['message']['attribute']
end
end
end
attribute
end

end
end
end
end

0 comments on commit 5ab4b9d

Please sign in to comment.