Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions lib/booker/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def delete(path, params=nil, body=nil, booker_model=nil)
build_resources(booker_resources, booker_model)
end

def paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_all: true)
def paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_all: true, skip_corrupt_pages_without_errors: false)
page_size = params[:PageSize]
page_number = params[:PageNumber]

Expand All @@ -98,8 +98,13 @@ def paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_al

unless results.is_a?(Array)
error_msg = "Result from paginated request to #{path} with params: #{params} is not a collection"
raise Booker::MidPaginationError.new(message: error_msg, error_occurred_during_params: params,
results_fetched_prior_to_error: fetched)

if skip_corrupt_pages_without_errors
results = process_non_corrupt_pages(method: method, path: path, params: params, model: model)
else
raise Booker::MidPaginationError.new(message: error_msg, error_occurred_during_params: params,
results_fetched_prior_to_error: fetched)
end
end

fetched.concat(results)
Expand All @@ -119,6 +124,28 @@ def paginated_request(method:, path:, params:, model: nil, fetched: [], fetch_al
end
end

def process_non_corrupt_pages(method:, path:, params:, model: nil)
initial_page_size = params[:PageSize]
initial_page_number = params[:PageNumber]
return [] unless initial_page_size > 1 # if pagesize is one, there are no other non-corrupt pages

non_corrupt_data = []
pages_processed = (initial_page_number * initial_page_size)
new_params = params.deep_dup
new_params[:PageSize] = 1 # set to 1 to search for singular pages with noncorrupt data. Optimize noncorrupt data.

initial_page_size.times do
new_params[:PageNumber] = (pages_processed += 1)

single_page_results = paginated_request(method: method, path: path, params: new_params, model: model, fetch_all: false)
single_page_results = [] unless results.is_a?(Array) # if page is corrupted: skip

non_corrupt_data.concat(single_page_results)
end

non_corrupt_data
end

def get_booker_resources(http_method, path, params=nil, body=nil, booker_model=nil)
http_options = request_options(params, body)
url = full_url(path)
Expand Down
15 changes: 9 additions & 6 deletions lib/booker/v4/business_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,25 @@ def find_employees(booker_location_id:, fetch_all: true, params: {})
)
end

def find_orders(booker_location_id:, fetch_all: true, params: {})
def find_orders(booker_location_id:, fetch_all: true, params: {}, skip_corrupt_pages_without_errors: false)
paginated_request(
method: :post,
path: '/orders',
params: build_params({LocationID: booker_location_id}, params, true),
model: Booker::V4::Models::Order,
fetch_all: fetch_all
fetch_all: fetch_all,
skip_corrupt_pages_without_errors: skip_corrupt_pages_without_errors
)
end

def find_orders_partial(booker_location_id:, fetch_all: true, params: {})
def find_orders_partial(booker_location_id:, fetch_all: true, params: {}, skip_corrupt_pages_without_errors: false)
paginated_request(
method: :post,
path: '/orders/partial',
params: build_params({LocationID: booker_location_id}, params, true),
model: Booker::V4::Models::Order,
fetch_all: fetch_all
fetch_all: fetch_all,
skip_corrupt_pages_without_errors: skip_corrupt_pages_without_errors
)
end

Expand All @@ -71,7 +73,7 @@ def find_treatments(booker_location_id:, fetch_all: true, params: {})
)
end

def find_customers(booker_location_id:, fetch_all: true, params: {})
def find_customers(booker_location_id:, fetch_all: true, params: {}, skip_corrupt_pages_without_errors: false)
additional_params = {
'FilterByExactLocationID' => true,
'LocationID' => booker_location_id,
Expand All @@ -83,7 +85,8 @@ def find_customers(booker_location_id:, fetch_all: true, params: {})
path: '/customers',
params: build_params(additional_params, params, true),
model: Booker::V4::Models::Customer,
fetch_all: fetch_all
fetch_all: fetch_all,
skip_corrupt_pages_without_errors: skip_corrupt_pages_without_errors
)
end

Expand Down