lib/dato/repo.rb in dato-0.7.5 vs lib/dato/repo.rb in dato-0.7.6

- old
+ new

@@ -52,50 +52,82 @@ placeholder = args.shift.to_s placeholders << placeholder placeholder end - response = if %i[post put].include?(link.method) - body = if link.schema - unserialized_body = args.shift + body = nil + query_string = nil - JsonApiSerializer.new(type, link).serialize( - unserialized_body, - link.method == :post ? nil : placeholders.last - ) - else - {} - end + if %i[post put].include?(link.method) + body = link.schema ? args.shift : {} + query_string = args.shift || {} - client.request(link.method, url, body) + elsif link.method == :delete + query_string = args.shift || {} - elsif link.method == :delete - client.request(:delete, url) + elsif link.method == :get + query_string = args.shift || {} + end - elsif link.method == :get - query_string = args.shift + options = args.any? ? args.shift.symbolize_keys : {} - all_pages = (args[0] || {}) - .symbolize_keys - .fetch(:all_pages, false) + if link.schema && %i[post put].include?(link.method) && options.fetch(:serialize_response, true) + body = JsonApiSerializer.new(type, link).serialize( + body, + link.method == :post ? nil : placeholders.last + ) + end - if all_pages + response = if %i[post put].include?(link.method) + client.send(link.method, url, body, query_string) + elsif link.method == :delete + client.delete(url, query_string) + elsif link.method == :get + if options.fetch(:all_pages, false) Paginator.new(client, url, query_string).response else - client.request(:get, url, query_string) + client.get(url, query_string) end end - options = if args.any? - args.shift.symbolize_keys - else - {} - end + if response && response[:data] && response[:data].is_a?(Hash) && response[:data][:type] == "job" + job_result = nil - if options.fetch(:deserialize_response, true) - JsonApiDeserializer.new(link).deserialize(response) + while !job_result do + begin + sleep(1) + job_result = client.job_result.find(response[:data][:id]) + rescue ApiError => error + if error.response[:status] != 404 + raise error + end + end + end + + if job_result[:status] < 200 || job_result[:status] >= 300 + error = ApiError.new( + status: job_result[:status], + body: JSON.dump(job_result[:payload]) + ) + + puts "====" + puts error.message + puts "====" + + raise error + end + + if options.fetch(:deserialize_response, true) + JsonApiDeserializer.new(link.job_schema).deserialize(job_result[:payload]) + else + job_result.payload + end else - response + if options.fetch(:deserialize_response, true) + JsonApiDeserializer.new(link.target_schema).deserialize(response) + else + response + end end end end end