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

Fix #4. Detect HTTP RESPONSES different than 200 on Cursor objects #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 1 addition & 15 deletions lib/onelogin/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,8 @@ def clean_error
@error_description = 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')
message = status['message']
elsif status.has_key?('type')
message = status['type']
end
end
message
end

def expired?
Time.now.utc > @expiration
(!@expiration.nil?) && Time.now.utc > @expiration
end

def prepare_token
Expand Down
25 changes: 18 additions & 7 deletions lib/onelogin/api/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# Returns an enumerable object
class Cursor
include Enumerable
include OneLogin::Api::Util

attr_accessor :error, :error_description

# Create a new instance of the Cursor.
#
Expand All @@ -14,6 +17,8 @@ class Cursor
def initialize(url, options = {})
@url = url
@options = options
@error = nil
@error_description = nil

@model = options[:model]
@headers = options[:headers] || {}
Expand Down Expand Up @@ -55,16 +60,21 @@ def fetch_next_page
query: @params
)

json = response.parsed_response
results = json['data'].flatten
if response.code == 200
json = response.parsed_response
results = json['data'].flatten

@collection += if results_remaining < results.size
results.slice(0, results_remaining)
else
results
end

@collection += if results_remaining < results.size
results.slice(0, results_remaining)
@after_cursor = after_cursor(json)
else
results
@error = response.code.to_s
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesnt actually work. If you run this branch in the console using some Auth Only credentials you would expect it to produce a 401 error but in reality it just returns nil.

e.g. Run the sdk console

> bin/console
client = OneLogin::Api::Client.new(
  client_id: 'xxx',
  client_secret:'xxx',
  region: 'us'
)

users = client.get_users
# returns the cursor but no error yet as the users not fetched until each is called

users = client.get_users.each{|u|u.id}
# returns nil and since the error is applied to the cursor and not the client it is not visible 

@error_description = extract_error_message_from_response(response)
end

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

Expand All @@ -87,4 +97,5 @@ def fetch_completed?
def last?
@last_cursor_empty || fetch_completed?
end

end
14 changes: 14 additions & 0 deletions lib/onelogin/api/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ module Api
module Util
include OneLogin::Api::Util::Constants
include OneLogin::Api::Util::UrlBuilder

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')
message = status['message']
elsif status.has_key?('type')
message = status['type']
end
end
message
end
end
end
end