diff --git a/.editorconfig b/.editorconfig index 42059db5f..eec6ef891 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,23 +1,29 @@ root = true +# All files [*] -charset = utf-8 -end_of_line = lf +indent_style = space insert_final_newline = true +end_of_line = lf +charset = utf-8 trim_trailing_whitespace = true -[*.md] -trim_trailing_whitespace = false -[*.{rb,rake,gemspec,rbs,ru}] -indent_style = space +# Config files +[*.{yml,yaml,toml}] indent_size = 2 -[*.{yml,yaml,json,toml}] -indent_style = space +# JSON files +[*.json] +indent_size = 2 + +# Markdown files +[*.{md,markdown}] +trim_trailing_whitespace = false + +[*.{rb,rake,gemspec,rbs,ru}] indent_size = 2 [*.sh] -indent_style = space indent_size = 2 [Makefile] diff --git a/lib/mindee.rb b/lib/mindee.rb index a1416021c..f81763968 100644 --- a/lib/mindee.rb +++ b/lib/mindee.rb @@ -14,25 +14,14 @@ module Dependencies module Error end - # Custom extraction module - module Extraction + # Custom geometry module + module Geometry end # Mindee internal http module. module HTTP end - # PDF-specific operations. - module PDF - end - - # Document handling. - module Input - # Document source handling. - module Source - end - end - module Image # Miscellaneous image operations. module ImageUtils @@ -43,35 +32,27 @@ module ImageCompressor end end - # Logging - module Logging - end - - # Document input-related internals. + # Document handling. module Input - end - - # Collection of all internal products. - module Product - # Europe-specific products. - module EU - end - - # France-specific products. - module FR + # Document source handling. + module Source end + end - # Indian-specific products. - module IND - end + # Logging + module Logging + end - # US-specific products. - module US - end + # PDF-specific operations. + module PDF end # V1-specific module. module V1 + # Custom extraction for V1. + module Extraction + end + # HTTP module for V1. module HTTP end @@ -101,10 +82,6 @@ module FR # V2-specific module. module V2 - # Mindee internal http module. - module HTTP - end - # File operations. module FileOperation # Crop operations. @@ -116,8 +93,8 @@ module Split end end - # Product-specific module. - module Product + # HTTP module for V2. + module HTTP end # V2 parsing module. @@ -126,6 +103,14 @@ module Parsing module Search end end + + # Product-specific module. + module Product + end + + # V2 search module. + module Search + end end end diff --git a/lib/mindee/input/polling_options.rb b/lib/mindee/input/polling_options.rb index 8cba695b3..4dc7f7429 100644 --- a/lib/mindee/input/polling_options.rb +++ b/lib/mindee/input/polling_options.rb @@ -20,6 +20,38 @@ def initialize(initial_delay_sec: 2.0, delay_sec: 1.5, max_retries: 80) @initial_delay_sec = initial_delay_sec @delay_sec = delay_sec @max_retries = max_retries + validate_settings + end + + # Loads from a Hash. + def self.from_hash(polling_options: {}) + polling_options = polling_options + .transform_keys(&:to_sym) + .slice(:initial_delay_sec, :delay_sec, :max_retries) + .compact + new(**polling_options) + end + + private + + # Validates the parameters for async auto-polling + def validate_settings + min_delay_sec = 1 + min_initial_delay_sec = 1 + min_retries = 2 + + if @delay_sec < min_delay_sec + raise ArgumentError, + "Cannot set auto-poll delay to less than #{min_delay_sec} second(s)" + end + if @initial_delay_sec < min_initial_delay_sec + raise ArgumentError, + "Cannot set initial parsing delay to less than #{min_initial_delay_sec} second(s)" + end + return unless @max_retries < min_retries + + raise ArgumentError, + "Cannot set auto-poll retries to less than #{min_retries}" end end end diff --git a/lib/mindee/v1/client.rb b/lib/mindee/v1/client.rb index 5c5b831f1..1a5de1405 100644 --- a/lib/mindee/v1/client.rb +++ b/lib/mindee/v1/client.rb @@ -89,7 +89,13 @@ def initialize(api_key: '') # * `:max_retries` [Integer] Maximum number of retries. Defaults to 80. # @param enqueue [bool] Whether to enqueue the file. # @return [Mindee::V1::Parsing::Common::ApiResponse] - def parse(input_source, product_class, endpoint: nil, options: {}, enqueue: true) + def parse( + input_source, + product_class, + endpoint: nil, + options: {}, + enqueue: true + ) opts = normalize_parse_options(options) process_pdf_if_required(input_source, opts) if input_source.is_a?(Input::Source::LocalInputSource) endpoint ||= initialize_endpoint(product_class) @@ -122,7 +128,12 @@ def parse(input_source, product_class, endpoint: nil, options: {}, enqueue: true # * `:cropper` [bool, nil] Whether to include cropper results for each page. # This performs a cropping operation on the server and will increase response time. # @return [Mindee::V1::Parsing::Common::ApiResponse] - def parse_sync(input_source, product_class, endpoint, options) + def parse_sync( + input_source, + product_class, + endpoint, + options + ) logger.debug("Parsing document as '#{endpoint.url_root}'") prediction, raw_http = endpoint.predict( @@ -159,7 +170,12 @@ def parse_sync(input_source, product_class, endpoint, options) # * `:workflow_id` [String, nil] ID of the workflow to use. # @param endpoint [Mindee::V1::HTTP::Endpoint] Endpoint of the API. # @return [Mindee::V1::Parsing::Common::ApiResponse] - def enqueue(input_source, product_class, endpoint: nil, options: {}) + def enqueue( + input_source, + product_class, + endpoint: nil, + options: {} + ) opts = normalize_parse_options(options) endpoint ||= initialize_endpoint(product_class) logger.debug("Enqueueing document as '#{endpoint.url_root}'") @@ -369,7 +385,12 @@ def validate_async_params(initial_delay_sec, delay_sec, max_retries) # standard (off the shelf) endpoint. # @param version [String] For custom endpoints, version of the product. # @return [Mindee::V1::HTTP::Endpoint] - def initialize_endpoint(product_class, endpoint_name: '', account_name: '', version: '') + def initialize_endpoint( + product_class, + endpoint_name: '', + account_name: '', + version: '' + ) if (endpoint_name.nil? || endpoint_name.empty?) && product_class == Mindee::V1::Product::Universal::Universal raise Mindee::Error::MindeeConfigurationError, 'Missing argument endpoint_name when using custom class' end @@ -421,7 +442,7 @@ def process_pdf_if_required(input_source, opts) opts.page_options.on_min_pages && input_source.pdf? - input_source.process_pdf(opts.page_options) + input_source.apply_page_options(opts.page_options) end private :parse_sync, :validate_async_params, :initialize_endpoint, :fix_endpoint_name, :fix_version, diff --git a/lib/mindee/v2/client.rb b/lib/mindee/v2/client.rb index 324d5a39e..c75cb468f 100644 --- a/lib/mindee/v2/client.rb +++ b/lib/mindee/v2/client.rb @@ -29,11 +29,18 @@ def enqueue( input_source, params ) - normalized_params = normalize_parameters(product.params_type, params) - normalized_params.validate_async_params - logger.debug("Enqueueing document to model '#{normalized_params.model_id}'.") - - @mindee_api.req_post_product_enqueue(input_source, normalized_params) + params_instance = case params + when Hash + product.params_type.from_hash(params: params) + when Mindee::V2::ClientOptions::BaseProductParameters + params + else + raise ArgumentError, "Expected Hash or BaseProductParameters, got #{params.class}" + end + + logger.debug("Enqueueing document to model '#{params_instance.model_id}'.") + + @mindee_api.req_post_product_enqueue(input_source, params_instance) end # Get the status of an inference that was previously enqueued. @@ -70,14 +77,14 @@ def get_job(job_id) end # Send a file to the inference queue and return the results when complete. - # @param product [Class] The return class. + # @param product [Class] The product class. # @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource] # The source of the input document (local file or URL). # @param params [Hash, ClientOptions::BaseProductParameters] Parameters for the inference. # @param polling_options [Hash, PollingOptions, nil] Parameters for polling. # @param cancellation_token [Mindee::HTTP::CancellationToken, nil] Token for cancellation. # @return [Parsing::BaseResponse] - # rubocop:disable-next Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize + # rubocop:disable-next Metrics/CyclomaticComplexity def enqueue_and_get_result( product, input_source, @@ -85,9 +92,25 @@ def enqueue_and_get_result( polling_options = nil, cancellation_token = nil ) - enqueue_response = enqueue(product, input_source, params) - normalized_params = normalize_parameters(product.params_type, params, polling_options: polling_options) - normalized_params.validate_async_params + params_instance = case params + when Hash + product.params_type.from_hash(params: params) + when Mindee::V2::ClientOptions::BaseProductParameters + params + else + raise ArgumentError, "Expected Hash or BaseProductParameters, got #{params.class}" + end + + polling_options_instance = case polling_options + when Mindee::Input::PollingOptions + polling_options + when Hash + Mindee::Input::PollingOptions.from_hash(polling_options: polling_options) + else + Mindee::Input::PollingOptions.new + end + + enqueue_response = enqueue(product, input_source, params_instance) if enqueue_response.job.id.nil? || enqueue_response.job.id.empty? logger.error("Failed enqueueing:\n#{enqueue_response.raw_http}") @@ -99,13 +122,47 @@ def enqueue_and_get_result( raise Mindee::Error::MindeeError, 'Enqueueing of the document was canceled.' if cancellation_token&.canceled? - sleep(normalized_params.polling_options.initial_delay_sec) - retry_counter = 1 - polling_url = enqueue_response.job.polling_url + poll_on_job(product, enqueue_response, polling_options_instance, cancellation_token) + end + + # Search for resources matching the given criteria. + # @param params [ClientOptions::BaseSearchParameters] Search parameters. + # @return [Mindee::V2::Parsing::Search::BaseSearchResponse] A search response containing the matching resources. + def search(params) + @mindee_api.req_get_search(params) + end + + # Returns a list of models matching a criteria for the given API key. + # @deprecated Use {#search} instead. + # @param model_name [String, nil] Name filter. + # @param model_type [String, nil] Model type filter. + # @return [Mindee::V2::Search::Models::ModelSearchResponse] + def search_models(model_name, model_type) + search(Search::Models::ModelSearchParameters.new(name: model_name, model_type: model_type)) + end + + private + + # Polls a job until it is processed or the maximum number of tries is reached. + # @param product [Class] + # @param initial_response [V2::Parsing::JobResponse] + # @param polling_options [Input::PollingOptions] + # @param cancellation_token [Mindee::HTTP::CancellationToken, nil] Token for cancellation. + # @return [Parsing::BaseResponse] + def poll_on_job( + product, + initial_response, + polling_options, + cancellation_token + ) + sleep(polling_options.initial_delay_sec) + try_counter = 1 + max_retries = polling_options.max_retries + polling_url = initial_response.job.polling_url job_response = get_job_by_url(polling_url) - while retry_counter < normalized_params.polling_options.max_retries + while try_counter < max_retries if job_response.job.status == 'Failed' break elsif !job_response.job.result_url.nil? @@ -113,16 +170,14 @@ def enqueue_and_get_result( end logger.debug( - "Successfully enqueued inference with job id: #{job_id}.\n" \ - "Attempt n°#{retry_counter}/#{normalized_params.polling_options.max_retries}.\n" \ - "Job status: #{job_response.job.status}." + "Poll attempt #{try_counter + 1} of #{max_retries}." ) raise Mindee::Error::MindeeError, 'Enqueueing of the document was canceled.' if cancellation_token&.canceled? - sleep(normalized_params.polling_options.delay_sec) + sleep(polling_options.delay_sec) job_response = get_job_by_url(polling_url) - retry_counter += 1 + try_counter += 1 end error = job_response.job.error @@ -132,43 +187,10 @@ def enqueue_and_get_result( raise err_to_raise end - sec_count = normalized_params.polling_options.delay_sec * retry_counter + sec_count = polling_options.delay_sec * try_counter raise Mindee::Error::MindeeError, "Asynchronous parsing request timed out after #{sec_count} seconds" end - - # Search for resources matching the given criteria. - # @param params [ClientOptions::BaseSearchParameters] Search parameters. - # @return [Mindee::V2::Parsing::Search::BaseSearchResponse] A search response containing the matching resources. - def search(params) - @mindee_api.req_get_search(params) - end - - # Returns a list of models matching a criteria for the given API key. - # @deprecated Use {#search} instead. - # @param model_name [String, nil] Name filter. - # @param model_type [String, nil] Model type filter. - # @return [Mindee::V2::Search::Models::ModelSearchResponse] - def search_models(model_name, model_type) - search(Search::Models::ModelSearchParameters.new(name: model_name, model_type: model_type)) - end - - private - - # If needed, converts the parsing options provided as a hash into a proper BaseParameters subclass object. - # @param params [Hash, Class] Params. - # @param polling_options [Hash, PollingOptions, nil] Polling options. - # @return [BaseParameters] - def normalize_parameters(param_class, params, polling_options: nil) - if params.is_a?(Hash) - params[:polling_options] = polling_options if polling_options - elsif params.is_a?(Mindee::V2::ClientOptions::BaseProductParameters) && !polling_options.nil? - params.polling_options = polling_options - end - return param_class.from_hash(params: params) if params.is_a?(Hash) - - params - end end end end diff --git a/lib/mindee/v2/client_options/base_product_parameters.rb b/lib/mindee/v2/client_options/base_product_parameters.rb index 14956503e..db395460e 100644 --- a/lib/mindee/v2/client_options/base_product_parameters.rb +++ b/lib/mindee/v2/client_options/base_product_parameters.rb @@ -17,23 +17,17 @@ class BaseProductParameters # If empty, no webhooks will be used. attr_reader :webhook_ids - # @return [Input::PollingOptions] Options for polling. Set only if having timeout issues. - attr_reader :polling_options - # @return [Boolean, nil] Whether to close the file after parsing. attr_reader :close_file # @param [String] model_id ID of the model # @param [String, nil] file_alias File alias, if applicable. # @param [Array, nil] webhook_ids List of webhook IDs to propagate the API response to. - # @param [Hash, Input::PollingOptions, nil] polling_options Options for polling. Set only if having timeout - # issues. # @param [Boolean, nil] close_file Whether to close the file after parsing. def initialize( model_id, file_alias: nil, webhook_ids: nil, - polling_options: nil, close_file: true ) raise Error::MindeeInputError, 'Model ID is required.' if model_id.nil? || model_id.empty? @@ -41,17 +35,9 @@ def initialize( @model_id = model_id @file_alias = file_alias @webhook_ids = webhook_ids || [] - @polling_options = get_clean_polling_options(polling_options) @close_file = close_file.nil? || close_file end - # Sets polling options after normalizing hash inputs. - # @param [Hash, Input::PollingOptions, nil] polling_options - # @return [Input::PollingOptions] - def polling_options=(polling_options) - @polling_options = get_clean_polling_options(polling_options) - end - # @return [String] Slug for the endpoint. def self.slug if self == BaseProductParameters @@ -75,7 +61,6 @@ def self.from_hash(params: {}) params[:model_id], file_alias: params[:file_alias], webhook_ids: params[:webhook_ids], - polling_options: params[:polling_options], close_file: params[:close_file] ) end @@ -90,15 +75,6 @@ def self.load_from_hash(params: {}) raise Error::MindeeInputError, 'Model ID is required.' end - polling_options_input = params.fetch(:page_options, Input::PollingOptions.new) - if polling_options_input.is_a?(Hash) - polling_options_input = polling_options_input.transform_keys(&:to_sym) - Input::PollingOptions.new( - initial_delay_sec: polling_options_input.fetch(:initial_delay_sec, 2.0), - delay_sec: polling_options_input.fetch(:delay_sec, 1.5), - max_retries: polling_options_input.fetch(:max_retries, 80) - ) - end params end @@ -111,51 +87,6 @@ def append_form_data(form_data) form_data.push(['webhook_ids', webhook_ids.join(',')]) unless @webhook_ids.nil? || webhook_ids.empty? form_data end - - # Validates the parameters for async auto-polling - def validate_async_params - min_delay_sec = 1 - min_initial_delay_sec = 1 - min_retries = 2 - - if @polling_options.delay_sec < min_delay_sec - raise ArgumentError, - "Cannot set auto-poll delay to less than #{min_delay_sec} second(s)" - end - if @polling_options.initial_delay_sec < min_initial_delay_sec - raise ArgumentError, - "Cannot set initial parsing delay to less than #{min_initial_delay_sec} second(s)" - end - return unless @polling_options.max_retries < min_retries - - raise ArgumentError, - "Cannot set auto-poll retries to less than #{min_retries}" - end - - private - - # Cleans a proper polling options object potentially from a hash. - # @param [Hash, Input::PollingOptions, nil] polling_options Polling options. - # @return [Input::PollingOptions] Valid polling options object. - def get_clean_polling_options(polling_options) - return Input::PollingOptions.new if polling_options.is_a?(Input::PollingOptions) - - if polling_options.is_a?(Hash) - polling_options = polling_options.transform_keys(&:to_sym) - output_polling_options = Input::PollingOptions.new( - initial_delay_sec: polling_options.fetch(:initial_delay_sec, 2.0), - delay_sec: polling_options.fetch(:delay_sec, 1.5), - max_retries: polling_options.fetch(:max_retries, 80) - ) - else - output_polling_options = if polling_options.is_a?(Input::PollingOptions) - polling_options || Input::PollingOptions.new - else - Input::PollingOptions.new - end - end - output_polling_options - end end end end diff --git a/lib/mindee/v2/http/mindee_api_v2.rb b/lib/mindee/v2/http/mindee_api_v2.rb index dc9e72cda..75efad8bd 100644 --- a/lib/mindee/v2/http/mindee_api_v2.rb +++ b/lib/mindee/v2/http/mindee_api_v2.rb @@ -150,24 +150,6 @@ def poll(url) raise Mindee::Error::MindeeError, 'Could not resolve server response.' end - # Handle parameters for the enqueue form - # @param form_data [Array] Array of form fields - # @param params [V2::Product::Extraction::Params::ExtractionParameters] Inference options. - def enqueue_form_options(form_data, params) - form_data.push(['rag', params.rag.to_s]) unless params.rag.nil? - form_data.push(['raw_text', params.raw_text.to_s]) unless params.raw_text.nil? - form_data.push(['polygon', params.polygon.to_s]) unless params.polygon.nil? - form_data.push(['confidence', params.confidence.to_s]) unless params.confidence.nil? - form_data.push ['text_context', params.text_context] if params.text_context - form_data.push ['data_schema', params.data_schema.to_s] if params.data_schema - unless params.webhook_ids.nil? || params.webhook_ids.empty? - params.webhook_ids.each do |webhook_id| - form_data.push ['webhook_ids[]', webhook_id] - end - end - form_data - end - # @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource] # @param params [ClientOptions::BaseProductParameters] Inference options. # @return [Net::HTTPResponse, nil] @@ -181,10 +163,6 @@ def enqueue(input_source, params) [['file', file_data, file_metadata]] # : Array[Array[untyped]] end form_data.push(['model_id', params.model_id]) - if params.is_a?(V2::Product::Extraction::Params::ExtractionParameters) - form_data = enqueue_form_options(form_data, params) - end - form_data = params.append_form_data(form_data) headers = { diff --git a/lib/mindee/v2/product/classification/params/classification_parameters.rb b/lib/mindee/v2/product/classification/params/classification_parameters.rb index 786d27327..a6d41fdef 100644 --- a/lib/mindee/v2/product/classification/params/classification_parameters.rb +++ b/lib/mindee/v2/product/classification/params/classification_parameters.rb @@ -16,13 +16,11 @@ def self.slug # @param [String] model_id ID of the model # @param [String, nil] file_alias File alias, if applicable. # @param [Array, nil] webhook_ids List of webhook IDs to propagate the API response to. - # @param [Hash, PollingOptions, nil] polling_options Options for polling. Set only if having timeout issues. # @param [Boolean, nil] close_file Whether to close the file after parsing. def initialize( model_id, file_alias: nil, webhook_ids: nil, - polling_options: nil, close_file: true ) super diff --git a/lib/mindee/v2/product/crop/params/crop_parameters.rb b/lib/mindee/v2/product/crop/params/crop_parameters.rb index 43958b027..e4d613bd0 100644 --- a/lib/mindee/v2/product/crop/params/crop_parameters.rb +++ b/lib/mindee/v2/product/crop/params/crop_parameters.rb @@ -16,13 +16,11 @@ def self.slug # @param [String] model_id ID of the model # @param [String, nil] file_alias File alias, if applicable. # @param [Array, nil] webhook_ids List of webhook IDs to propagate the API response to. - # @param [Hash, PollingOptions, nil] polling_options Options for polling. Set only if having timeout issues. # @param [Boolean, nil] close_file Whether to close the file after parsing. def initialize( model_id, file_alias: nil, webhook_ids: nil, - polling_options: nil, close_file: true ) super diff --git a/lib/mindee/v2/product/extraction/params/extraction_parameters.rb b/lib/mindee/v2/product/extraction/params/extraction_parameters.rb index 7b9b8c263..e7c14bdf6 100644 --- a/lib/mindee/v2/product/extraction/params/extraction_parameters.rb +++ b/lib/mindee/v2/product/extraction/params/extraction_parameters.rb @@ -46,7 +46,6 @@ def self.slug # @param [String, nil] file_alias File alias, if applicable. # @param [Array, nil] webhook_ids # @param [String, nil] text_context - # @param [Hash, Input::PollingOptions, nil] polling_options # @param [Boolean, nil] close_file # @param [DataSchemaField, String, Hash nil] data_schema def initialize( @@ -58,7 +57,6 @@ def initialize( file_alias: nil, webhook_ids: nil, text_context: nil, - polling_options: nil, close_file: true, data_schema: nil ) @@ -66,7 +64,6 @@ def initialize( model_id, file_alias: file_alias, webhook_ids: webhook_ids, - polling_options: polling_options, close_file: close_file ) @@ -108,13 +105,16 @@ def self.from_hash(params: {}) model_id = new_params.fetch(:model_id) ExtractionParameters.new( - model_id, rag: rag, - raw_text: raw_text, - polygon: polygon, - confidence: confidence, - file_alias: params.fetch(:file_alias, nil), - webhook_ids: params.fetch(:webhook_ids, nil), - close_file: params.fetch(:close_file, true) + model_id, + rag: rag, + raw_text: raw_text, + polygon: polygon, + confidence: confidence, + file_alias: params.fetch(:file_alias, nil), + webhook_ids: params.fetch(:webhook_ids, nil), + text_context: base_params.fetch(:text_context, nil), + data_schema: base_params.fetch(:data_schema, nil), + close_file: params.fetch(:close_file, true) ) end end diff --git a/lib/mindee/v2/product/ocr/params/ocr_parameters.rb b/lib/mindee/v2/product/ocr/params/ocr_parameters.rb index 92b86cc22..af75bf715 100644 --- a/lib/mindee/v2/product/ocr/params/ocr_parameters.rb +++ b/lib/mindee/v2/product/ocr/params/ocr_parameters.rb @@ -16,13 +16,11 @@ def self.slug # @param [String] model_id ID of the model # @param [String, nil] file_alias File alias, if applicable. # @param [Array, nil] webhook_ids List of webhook IDs to propagate the API response to. - # @param [Hash, PollingOptions, nil] polling_options Options for polling. Set only if having timeout issues. # @param [Boolean, nil] close_file Whether to close the file after parsing. def initialize( model_id, file_alias: nil, webhook_ids: nil, - polling_options: nil, close_file: true ) super diff --git a/lib/mindee/v2/product/split/params/split_parameters.rb b/lib/mindee/v2/product/split/params/split_parameters.rb index 08fd39d36..410e6eac8 100644 --- a/lib/mindee/v2/product/split/params/split_parameters.rb +++ b/lib/mindee/v2/product/split/params/split_parameters.rb @@ -17,13 +17,11 @@ def self.slug # @param [String] model_id ID of the model # @param [String, nil] file_alias File alias, if applicable. # @param [Array, nil] webhook_ids List of webhook IDs to propagate the API response to. - # @param [Hash, PollingOptions, nil] polling_options Options for polling. Set only if having timeout issues. # @param [Boolean, nil] close_file Whether to close the file after parsing. def initialize( model_id, file_alias: nil, webhook_ids: nil, - polling_options: nil, close_file: true ) super diff --git a/sig/mindee/input/polling_options.rbs b/sig/mindee/input/polling_options.rbs index 6a550c688..a27b204dc 100644 --- a/sig/mindee/input/polling_options.rbs +++ b/sig/mindee/input/polling_options.rbs @@ -6,7 +6,13 @@ module Mindee attr_reader initial_delay_sec: Integer | Float attr_reader max_retries: Integer - def initialize: (?initial_delay_sec: Float, ?delay_sec: Float, ?max_retries: Integer) -> Integer + def initialize: (?initial_delay_sec: Float, ?delay_sec: Float, ?max_retries: Integer) -> void + + def self.from_hash: (?polling_options: Hash[String | Symbol, untyped]) -> PollingOptions + + private + + def validate_settings: -> void end end end diff --git a/sig/mindee/v2/client.rbs b/sig/mindee/v2/client.rbs index 2516936d4..23a89ee9c 100644 --- a/sig/mindee/v2/client.rbs +++ b/sig/mindee/v2/client.rbs @@ -19,18 +19,35 @@ module Mindee def get_job_by_url: (String url) -> V2::Parsing::JobResponse - def enqueue: [T] (HTTP::_ProductClass[T] product, Input::Source::LocalInputSource | Input::Source::URLInputSource, Hash[String | Symbol, untyped] | ClientOptions::BaseProductParameters params) -> V2::Parsing::JobResponse - - def enqueue_and_get_result: [T] (HTTP::_ProductClass[T] product, Input::Source::LocalInputSource | Input::Source::URLInputSource, Hash[String | Symbol, untyped] | ClientOptions::BaseProductParameters params, ?Hash[String | Symbol, untyped] | Input::PollingOptions | nil) -> T + def enqueue: [T] ( + HTTP::_ProductClass[T] product, + Input::Source::LocalInputSource | Input::Source::URLInputSource input_source, + Hash[String | Symbol, untyped] | ClientOptions::BaseProductParameters params + ) -> V2::Parsing::JobResponse + + def enqueue_and_get_result: [T] ( + HTTP::_ProductClass[T] product, + Input::Source::LocalInputSource | Input::Source::URLInputSource input_source, + Hash[String | Symbol, untyped] | ClientOptions::BaseProductParameters params, + ?Hash[String | Symbol, untyped] | Input::PollingOptions | nil polling_options, + ?Mindee::HTTP::CancellationToken | nil cancellation_token + ) -> T def search: [T] (HTTP::_SearchParameters[T] params) -> T # Deprecated: use `search` instead. - def search_models: (String?, String?) -> Mindee::V2::Search::Models::ModelSearchResponse + def search_models: (String?, String?) -> Search::Models::ModelSearchResponse def validate_async_params: (Integer | Float, Integer | Float, Integer) -> void - def normalize_parameters: (singleton(ClientOptions::BaseProductParameters) param_class, Hash[String | Symbol, untyped] | ClientOptions::BaseProductParameters params, ?polling_options: Hash[String | Symbol, untyped] | Input::PollingOptions | nil) -> ClientOptions::BaseProductParameters + private + + def poll_on_job: [T] ( + HTTP::_ProductClass[T] product, + V2::Parsing::JobResponse initial_response, + Input::PollingOptions polling_options, + Mindee::HTTP::CancellationToken | nil cancellation_token + ) -> T end end end diff --git a/sig/mindee/v2/client_options/base_product_parameters.rbs b/sig/mindee/v2/client_options/base_product_parameters.rbs index 2077e48f3..c3a62ff55 100644 --- a/sig/mindee/v2/client_options/base_product_parameters.rbs +++ b/sig/mindee/v2/client_options/base_product_parameters.rbs @@ -15,26 +15,16 @@ module Mindee attr_reader model_id: String attr_reader file_alias: String? attr_reader webhook_ids: Array[String]? - attr_reader polling_options: Input::PollingOptions attr_reader close_file: bool def initialize: ( String, ?file_alias: String?, ?webhook_ids: Array[String]?, - ?polling_options: Hash[Symbol | String, untyped] | Input::PollingOptions | nil, ?close_file: bool? ) -> void def append_form_data: (Array[Array[untyped]]) -> Array[Array[untyped]] - - def validate_async_params: () -> void - - def polling_options=: (Hash[Symbol | String, untyped] | Input::PollingOptions | nil) -> Input::PollingOptions - - private - - def get_clean_polling_options: (Hash[String | Symbol, untyped] | Input::PollingOptions | nil) -> Input::PollingOptions end end end diff --git a/sig/mindee/v2/file_operation/crop.rbs b/sig/mindee/v2/file_operation/crop.rbs index 901b33a91..6e7a92b39 100644 --- a/sig/mindee/v2/file_operation/crop.rbs +++ b/sig/mindee/v2/file_operation/crop.rbs @@ -2,8 +2,15 @@ module Mindee module V2 module FileOperation module Crop - def self.extract_single_crop: (Input::Source::LocalInputSource, Parsing::Field::FieldLocation) -> Image::ExtractedImage - def self.extract_crops: (Input::Source::LocalInputSource, Array[Product::Crop::CropItem]) -> Image::ExtractedImages + def self.extract_single_crop: ( + Input::Source::LocalInputSource, + Parsing::Field::FieldLocation + ) -> Image::ExtractedImage + + def self.extract_crops: ( + Input::Source::LocalInputSource, + Array[Product::Crop::CropItem] + ) -> Image::ExtractedImages end end end diff --git a/sig/mindee/v2/http/mindee_api_v2.rbs b/sig/mindee/v2/http/mindee_api_v2.rbs index 04d4e7b3e..d9fe14c9f 100644 --- a/sig/mindee/v2/http/mindee_api_v2.rbs +++ b/sig/mindee/v2/http/mindee_api_v2.rbs @@ -29,7 +29,10 @@ module Mindee def req_get_result_by_id: [T] (_ProductClass[T] product, String inference_id) -> T - def req_post_product_enqueue: (Input::Source::LocalInputSource | Input::Source::URLInputSource, ClientOptions::BaseProductParameters) -> V2::Parsing::JobResponse + def req_post_product_enqueue: ( + Input::Source::LocalInputSource | Input::Source::URLInputSource, + ClientOptions::BaseProductParameters + ) -> V2::Parsing::JobResponse def req_get_job_by_id: (String job_id) -> V2::Parsing::JobResponse @@ -41,14 +44,15 @@ module Mindee def poll: (String) -> Net::HTTPResponse - def enqueue: (Input::Source::LocalInputSource | Input::Source::URLInputSource, ClientOptions::BaseProductParameters) -> Net::HTTPResponse? + def enqueue: ( + Input::Source::LocalInputSource | Input::Source::URLInputSource, + ClientOptions::BaseProductParameters + ) -> Net::HTTPResponse? def req_get_search: [T] (_SearchParameters[T] params) -> T private - def enqueue_form_options: (Array[Array[untyped]], V2::Product::Extraction::Params::ExtractionParameters) -> Array[Array[untyped]] - def validate_https_url!: (String) -> void end end diff --git a/sig/mindee/v2/parsing/base_response.rbs b/sig/mindee/v2/parsing/base_response.rbs index 803afaf20..1b49aeaf4 100644 --- a/sig/mindee/v2/parsing/base_response.rbs +++ b/sig/mindee/v2/parsing/base_response.rbs @@ -3,7 +3,7 @@ module Mindee module V2 module Parsing - class BaseResponse[T] < Mindee::V2::Parsing::CommonResponse + class BaseResponse[T] < CommonResponse attr_reader inference: T end end diff --git a/sig/mindee/v2/parsing/field/base_field.rbs b/sig/mindee/v2/parsing/field/base_field.rbs index b05002072..80ec8e4b9 100644 --- a/sig/mindee/v2/parsing/field/base_field.rbs +++ b/sig/mindee/v2/parsing/field/base_field.rbs @@ -9,7 +9,10 @@ module Mindee attr_reader locations: Array[FieldLocation] def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void - def self.create_field: (Hash[String | Symbol, untyped], ?Integer) -> (ListField | Field::ObjectField | Field::SimpleField) + def self.create_field: ( + Hash[String | Symbol, untyped], + ?Integer + ) -> (ListField | Field::ObjectField | Field::SimpleField) end end end diff --git a/sig/mindee/v2/product/classification/classification_response.rbs b/sig/mindee/v2/product/classification/classification_response.rbs index 6e43050cd..b1ea8595b 100644 --- a/sig/mindee/v2/product/classification/classification_response.rbs +++ b/sig/mindee/v2/product/classification/classification_response.rbs @@ -8,7 +8,7 @@ module Mindee self.@slug: String self.@_params_type: singleton(Params::ClassificationParameters) - attr_reader inference: Mindee::V2::Product::Classification::ClassificationInference + attr_reader inference: ClassificationInference def initialize: (Hash[String | Symbol, untyped]) -> void def _params_type: -> singleton(Params::ClassificationParameters) diff --git a/sig/mindee/v2/product/classification/params/classification_parameters/classification_parameters.rbs b/sig/mindee/v2/product/classification/params/classification_parameters.rbs similarity index 85% rename from sig/mindee/v2/product/classification/params/classification_parameters/classification_parameters.rbs rename to sig/mindee/v2/product/classification/params/classification_parameters.rbs index ae89ac7d4..566d213c2 100644 --- a/sig/mindee/v2/product/classification/params/classification_parameters/classification_parameters.rbs +++ b/sig/mindee/v2/product/classification/params/classification_parameters.rbs @@ -12,7 +12,6 @@ module Mindee String, ?file_alias: String?, ?webhook_ids: Array[String]?, - ?polling_options: Hash[Symbol | String, untyped] | Input::PollingOptions | nil, ?close_file: bool? ) -> void end diff --git a/sig/mindee/v2/product/crop/crop_response.rbs b/sig/mindee/v2/product/crop/crop_response.rbs index 773045a92..5530ef1d4 100644 --- a/sig/mindee/v2/product/crop/crop_response.rbs +++ b/sig/mindee/v2/product/crop/crop_response.rbs @@ -8,7 +8,7 @@ module Mindee self.@slug: String self.@_params_type: singleton(Params::CropParameters) - attr_reader inference: Mindee::V2::Product::Crop::CropInference + attr_reader inference: CropInference def initialize: (Hash[String | Symbol, untyped]) -> void def _params_type: -> singleton(Params::CropParameters) diff --git a/sig/mindee/v2/product/crop/params/crop_parameters/crop_parameters.rbs b/sig/mindee/v2/product/crop/params/crop_parameters.rbs similarity index 84% rename from sig/mindee/v2/product/crop/params/crop_parameters/crop_parameters.rbs rename to sig/mindee/v2/product/crop/params/crop_parameters.rbs index ad5d44daa..161b1f7cd 100644 --- a/sig/mindee/v2/product/crop/params/crop_parameters/crop_parameters.rbs +++ b/sig/mindee/v2/product/crop/params/crop_parameters.rbs @@ -12,7 +12,6 @@ module Mindee String, ?file_alias: String?, ?webhook_ids: Array[String]?, - ?polling_options: Hash[Symbol | String, untyped] | Input::PollingOptions | nil, ?close_file: bool? ) -> void end diff --git a/sig/mindee/v2/product/extraction/extraction.rbs b/sig/mindee/v2/product/extraction/extraction.rbs index 197a5f7e5..262db4b57 100644 --- a/sig/mindee/v2/product/extraction/extraction.rbs +++ b/sig/mindee/v2/product/extraction/extraction.rbs @@ -5,8 +5,8 @@ module Mindee module Product module Extraction class Extraction - self.@params_type: singleton(Mindee::V2::Product::Extraction::Params::ExtractionParameters) - self.@response_type: singleton(Mindee::V2::Product::Extraction::ExtractionResponse) + self.@params_type: singleton(Params::ExtractionParameters) + self.@response_type: singleton(ExtractionResponse) self.@slug: String end end diff --git a/sig/mindee/v2/product/extraction/extraction_inference.rbs b/sig/mindee/v2/product/extraction/extraction_inference.rbs index 2facf4f77..0ec8a3f14 100644 --- a/sig/mindee/v2/product/extraction/extraction_inference.rbs +++ b/sig/mindee/v2/product/extraction/extraction_inference.rbs @@ -7,7 +7,7 @@ module Mindee attr_reader self.response_type: singleton(ExtractionResponse) attr_reader self.slug: String - attr_reader active_options: Mindee::V2::Parsing::InferenceActiveOptions + attr_reader active_options: Parsing::InferenceActiveOptions attr_reader result: ExtractionResult def initialize: (Hash[String | Symbol, untyped]) -> void diff --git a/sig/mindee/v2/product/extraction/params/extraction_parameters.rbs b/sig/mindee/v2/product/extraction/params/extraction_parameters.rbs index 39e84d99e..190adad43 100644 --- a/sig/mindee/v2/product/extraction/params/extraction_parameters.rbs +++ b/sig/mindee/v2/product/extraction/params/extraction_parameters.rbs @@ -23,7 +23,6 @@ module Mindee ?file_alias: String?, ?text_context: String?, ?webhook_ids: Array[String]?, - ?polling_options: Hash[Symbol | String, untyped] | Input::PollingOptions | nil, ?close_file: bool?, ?data_schema: V2::Product::Extraction::Params::DataSchema|String|Hash[Symbol | String, untyped]? ) -> void diff --git a/sig/mindee/v2/product/ocr/params/ocr_parameters/ocr_parameters.rbs b/sig/mindee/v2/product/ocr/params/ocr_parameters.rbs similarity index 84% rename from sig/mindee/v2/product/ocr/params/ocr_parameters/ocr_parameters.rbs rename to sig/mindee/v2/product/ocr/params/ocr_parameters.rbs index 944317cfa..07a5bcef1 100644 --- a/sig/mindee/v2/product/ocr/params/ocr_parameters/ocr_parameters.rbs +++ b/sig/mindee/v2/product/ocr/params/ocr_parameters.rbs @@ -12,7 +12,6 @@ module Mindee String, ?file_alias: String?, ?webhook_ids: Array[String]?, - ?polling_options: Hash[Symbol | String, untyped] | Input::PollingOptions | nil, ?close_file: bool? ) -> void end diff --git a/sig/mindee/v2/product/split/params/split_parameters/split_parameters.rbs b/sig/mindee/v2/product/split/params/split_parameters.rbs similarity index 84% rename from sig/mindee/v2/product/split/params/split_parameters/split_parameters.rbs rename to sig/mindee/v2/product/split/params/split_parameters.rbs index f984c8af4..7f8fe943c 100644 --- a/sig/mindee/v2/product/split/params/split_parameters/split_parameters.rbs +++ b/sig/mindee/v2/product/split/params/split_parameters.rbs @@ -12,7 +12,6 @@ module Mindee String, ?file_alias: String?, ?webhook_ids: Array[String]?, - ?polling_options: Hash[Symbol | String, untyped] | Input::PollingOptions | nil, ?close_file: bool? ) -> void end diff --git a/spec/v2/client_options/polling_options_spec.rb b/spec/v2/client_options/polling_options_spec.rb new file mode 100644 index 000000000..94e064907 --- /dev/null +++ b/spec/v2/client_options/polling_options_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'mindee' +require 'mindee/input' + +describe Mindee::Input::PollingOptions do + it 'provides sensible defaults' do + options = Mindee::Input::PollingOptions.new + expect(options.initial_delay_sec).to eq(2.0) + expect(options.delay_sec).to eq(1.5) + expect(options.max_retries).to eq(80) + end + + it 'allows custom values' do + options = Mindee::Input::PollingOptions.new( + initial_delay_sec: 3.0, + delay_sec: 5.0, + max_retries: 100 + ) + expect(options.initial_delay_sec).to eq(3.0) + expect(options.delay_sec).to eq(5.0) + expect(options.max_retries).to eq(100) + end + + it 'raises an error with invalid options' do + expect do + Mindee::Input::PollingOptions.new( + initial_delay_sec: -1.0, + delay_sec: 1.5, + max_retries: 80 + ) + end.to raise_error(ArgumentError) + + expect do + Mindee::Input::PollingOptions.new( + initial_delay_sec: 2.0, + delay_sec: -1.5, + max_retries: 80 + ) + end.to raise_error(ArgumentError) + + expect do + Mindee::Input::PollingOptions.new( + initial_delay_sec: 2.0, + delay_sec: 1.5, + max_retries: -1 + ) + end.to raise_error(ArgumentError) + end +end diff --git a/spec/v2/client_v2_integration.rb b/spec/v2/client_v2_integration.rb index cce9cab28..67ba6225c 100644 --- a/spec/v2/client_v2_integration.rb +++ b/spec/v2/client_v2_integration.rb @@ -10,34 +10,34 @@ let(:api_key) { ENV.fetch('MINDEE_V2_API_KEY') } let(:model_id) { ENV.fetch('MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID') } let(:blank_pdf_url) { ENV.fetch('MINDEE_V2_SE_TESTS_BLANK_PDF_URL') } - let(:client) { Mindee::V2::Client.new(api_key: api_key) } + context 'An input file' do it 'parses an empty multi-page PDF successfully' do src_path = File.join(FILE_TYPES_DIR, 'pdf', 'multipage_cut-2.pdf') - input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'multipage_cut-2.pdf') + input = Mindee::Input::Source::FileInputSource.new( + File.open(src_path, 'rb'), 'multipage_cut-2.pdf' + ) - polling = Mindee::Input::PollingOptions.new( + polling_options = Mindee::Input::PollingOptions.new( initial_delay_sec: 3.0, delay_sec: 1.5, max_retries: 80 ) - - inference_params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( - model_id, + extraction_params = { + model_id: model_id, rag: false, raw_text: true, polygon: false, confidence: false, file_alias: 'rb_integration_test', - text_context: 'this is a test' - ) - + text_context: 'this is a test', + } response = client.enqueue_and_get_result( Mindee::V2::Product::Extraction::Extraction, input, - inference_params, - polling + extraction_params, + polling_options ) expect(response).not_to be_nil @@ -72,46 +72,26 @@ expect(result.fields).not_to be_nil end - it 'parses with legacy polling options successfully' do - src_path = File.join(V1_PRODUCT_DATA_DIR, 'financial_document', 'default_sample.jpg') - input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'multipage_cut-2.pdf') - - polling = Mindee::Input::PollingOptions.new( - initial_delay_sec: 3.0, - delay_sec: 1.5, - max_retries: 80 - ) - - inference_params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( - model_id, - rag: false, - raw_text: true, - polygon: false, - confidence: false, - file_alias: 'rb_integration_test', - polling_options: polling, - text_context: 'this is a test' - ) - response = client.enqueue_and_get_result(Mindee::V2::Product::Extraction::Extraction, input, inference_params) - expect(response).not_to be_nil - expect(response.inference).not_to be_nil - expect(response.inference.file.file_alias).to eq('rb_integration_test') - end - it 'parses a filled single-page image successfully' do src_path = File.join(V1_PRODUCT_DATA_DIR, 'financial_document', 'default_sample.jpg') - input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'default_sample.jpg') + input = Mindee::Input::Source::FileInputSource.new( + File.open(src_path, 'rb'), 'default_sample.jpg' + ) - inference_params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( - model_id, + extraction_params = { + model_id: model_id, raw_text: false, polygon: false, confidence: false, rag: false, - file_alias: 'rb_integration_test' - ) + file_alias: 'rb_integration_test', + } - response = client.enqueue_and_get_result(Mindee::V2::Product::Extraction::Extraction, input, inference_params) + response = client.enqueue_and_get_result( + Mindee::V2::Product::Extraction::Extraction, + input, + extraction_params + ) expect(response).not_to be_nil file = response.inference.file @@ -150,31 +130,31 @@ src_path = File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf') input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'blank_1.pdf') - inference_params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new('INVALID_MODEL_ID') + extraction_params = { model_id: 'INVALID_MODEL_ID' } expect do - client.enqueue(Mindee::V2::Product::Extraction::Extraction, input, inference_params) - end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |e| expect(e.status).to eq(422) } + client.enqueue(Mindee::V2::Product::Extraction::Extraction, input, extraction_params) + end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |err| expect(err.status).to eq(422) } end it 'raises MindeeHTTPErrorV2 (422) on invalid webhook id' do src_path = File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf') input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'blank_1.pdf') - params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( - model_id, - webhook_ids: ['INVALID_WEBHOOK_ID'] - ) + extraction_params = { + model_id: model_id, + webhook_ids: ['INVALID_WEBHOOK_ID'], + } expect do - client.enqueue(Mindee::V2::Product::Extraction::Extraction, input, params) - end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |e| - expect(e.status).to eq(422) - expect(e.code).to start_with('422-') - expect(e.detail).to_not be_nil - expect(e.title).to_not be_nil - expect(e.errors).to be_an_instance_of(Array) - expect(e.errors.count).to be_positive + client.enqueue(Mindee::V2::Product::Extraction::Extraction, input, extraction_params) + end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |err| + expect(err.status).to eq(422) + expect(err.code).to start_with('422-') + expect(err.detail).to_not be_nil + expect(err.title).to_not be_nil + expect(err.errors).to be_an_instance_of(Array) + expect(err.errors.count).to be_positive } end @@ -182,34 +162,34 @@ src_path = File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf') input = Mindee::Input::Source::FileInputSource.new(File.open(src_path, 'rb'), 'blank_1.pdf') - inference_params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( - model_id, - webhook_ids: ['fc405e37-4ba4-4d03-aeba-533a8d1f0f21', 'fc405e37-4ba4-4d03-aeba-533a8d1f0f21'] - ) + extraction_params = { + model_id: model_id, + webhook_ids: ['fc405e37-4ba4-4d03-aeba-533a8d1f0f21', 'fc405e37-4ba4-4d03-aeba-533a8d1f0f21'], + } expect do - client.enqueue(Mindee::V2::Product::Extraction::Extraction, input, inference_params) - end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |e| - expect(e.status).to eq(422) - expect(e.code).to start_with('422-') - expect(e.detail).to_not be_nil - expect(e.title).to_not be_nil - expect(e.errors).to be_an_instance_of(Array) - expect(e.errors.count).to be_positive - expect(e.errors[0]).to be_an_instance_of(Mindee::V2::Parsing::ErrorItem) + client.enqueue(Mindee::V2::Product::Extraction::Extraction, input, extraction_params) + end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |err| + expect(err.status).to eq(422) + expect(err.code).to start_with('422-') + expect(err.detail).to_not be_nil + expect(err.title).to_not be_nil + expect(err.errors).to be_an_instance_of(Array) + expect(err.errors.count).to be_positive + expect(err.errors[0]).to be_an_instance_of(Mindee::V2::Parsing::ErrorItem) } end it 'raises MindeeHTTPErrorV2 on invalid job id' do expect do client.get_result(Mindee::V2::Product::Extraction::Extraction, 'INVALID_JOB_ID') - end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |e| - expect(e.status).to eq(422) - expect(e.code).to start_with('422-') - expect(e.detail).to_not be_nil - expect(e.title).to_not be_nil - expect(e.errors).to be_an_instance_of(Array) - expect(e.errors.count).to be_positive + end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |err| + expect(err.status).to eq(422) + expect(err.code).to start_with('422-') + expect(err.detail).to_not be_nil + expect(err.title).to_not be_nil + expect(err.errors).to be_an_instance_of(Array) + expect(err.errors.count).to be_positive } end @@ -221,21 +201,21 @@ 'default_sample.jpg' ) - inference_params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( - 'fc405e37-4ba4-4d03-aeba-533a8d1f0f21', + extraction_params = { + model_id: 'fc405e37-4ba4-4d03-aeba-533a8d1f0f21', raw_text: false, polygon: false, confidence: false, rag: false, - file_alias: 'rb_integration_test' - ) - client.enqueue_and_get_result(Mindee::V2::Product::Extraction::Extraction, input, inference_params) - end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |e| - expect(e.status).to eq(404) - expect(e.code).to start_with('404-') - expect(e.detail).to_not be_nil - expect(e.title).to_not be_nil - expect(e.errors).to be_an_instance_of(Array) + file_alias: 'rb_integration_test', + } + client.enqueue_and_get_result(Mindee::V2::Product::Extraction::Extraction, input, extraction_params) + end.to raise_error(Mindee::Error::MindeeHTTPErrorV2) { |err| + expect(err.status).to eq(404) + expect(err.code).to start_with('404-') + expect(err.detail).to_not be_nil + expect(err.title).to_not be_nil + expect(err.errors).to be_an_instance_of(Array) } end end @@ -244,9 +224,12 @@ it 'parses an URL input source without errors' do url_input = Mindee::Input::Source::URLInputSource.new(blank_pdf_url) - inference_params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new(model_id) + extraction_params = { model_id: model_id } - response = client.enqueue_and_get_result(Mindee::V2::Product::Extraction::Extraction, url_input, inference_params) + response = client.enqueue_and_get_result( + Mindee::V2::Product::Extraction::Extraction, + url_input, extraction_params + ) expect(response).not_to be_nil expect(response.inference).not_to be_nil @@ -255,21 +238,28 @@ context 'A Data Schema Override' do it 'Overrides successfully' do - data_schema_replace = File.read(File.join(V2_PRODUCT_DATA_DIR, 'extraction', - 'data_schema_replace_param.json')) - input = Mindee::Input::Source::PathInputSource.new(File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf')) + data_schema_replace = File.read( + File.join(V2_PRODUCT_DATA_DIR, 'extraction', 'data_schema_replace_param.json') + ) + input = Mindee::Input::Source::PathInputSource.new( + File.join(FILE_TYPES_DIR, 'pdf', 'blank_1.pdf') + ) - inference_params = Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( - model_id, + extraction_params = { + model_id: model_id, raw_text: false, polygon: false, confidence: false, rag: false, file_alias: 'rb_integration_data_schema_replace', - data_schema: data_schema_replace - ) + data_schema: data_schema_replace, + } - response = client.enqueue_and_get_result(Mindee::V2::Product::Extraction::Extraction, input, inference_params) + response = client.enqueue_and_get_result( + Mindee::V2::Product::Extraction::Extraction, + input, + extraction_params + ) expect(response).not_to be_nil model = response.inference.model diff --git a/spec/v2/client_v2_spec.rb b/spec/v2/client_v2_spec.rb index 495b777da..24a144cdb 100644 --- a/spec/v2/client_v2_spec.rb +++ b/spec/v2/client_v2_spec.rb @@ -154,8 +154,7 @@ def stub_next_request_with(method, hash:, status_code: 0) end let(:params) do Mindee::V2::Product::Extraction::Params::ExtractionParameters.new( - 'dummy-model', - polling_options: Mindee::Input::PollingOptions.new(initial_delay_sec: 2, delay_sec: 1.5, max_retries: 2) + 'dummy-model' ) end @@ -171,7 +170,11 @@ def stub_next_request_with(method, hash:, status_code: 0) expect do client.enqueue_and_get_result( - Mindee::V2::Product::Extraction::Extraction, input_doc, params, nil, token + Mindee::V2::Product::Extraction::Extraction, + input_doc, + params, + Mindee::Input::PollingOptions.new(initial_delay_sec: 2, delay_sec: 1.5, max_retries: 2), + token ) end.to raise_error(Mindee::Error::MindeeError, %r{canceled}) end diff --git a/spec/v2/file_operations/crop_operation_integration.rb b/spec/v2/file_operations/crop_operation_integration.rb index a2239bf98..15c230101 100644 --- a/spec/v2/file_operations/crop_operation_integration.rb +++ b/spec/v2/file_operations/crop_operation_integration.rb @@ -54,12 +54,12 @@ def check_findoc_return(findoc_response) expect(extracted_images[0].filename).to eq('default_sample.jpg_page0-0.jpg') expect(extracted_images[1].filename).to eq('default_sample.jpg_page0-1.jpg') - findoc_params = { model_id: findoc_model_id, close_file: false } + extraction_params = { model_id: findoc_model_id, close_file: false } invoice0 = v2_client.enqueue_and_get_result( Mindee::V2::Product::Extraction::Extraction, extracted_images[0].as_input_source, - findoc_params + extraction_params ) check_findoc_return(invoice0) diff --git a/spec/v2/file_operations/split_operation_integration.rb b/spec/v2/file_operations/split_operation_integration.rb index 1a2376a34..d18ad6ce0 100644 --- a/spec/v2/file_operations/split_operation_integration.rb +++ b/spec/v2/file_operations/split_operation_integration.rb @@ -58,12 +58,12 @@ def check_findoc_return(findoc_response) expect(extracted_pdfs[0].filename).to eq('default_sample_001-001.pdf') expect(extracted_pdfs[1].filename).to eq('default_sample_002-002.pdf') - findoc_params = { model_id: findoc_model_id, close_file: false } + extraction_params = { model_id: findoc_model_id, close_file: false } invoice0 = v2_client.enqueue_and_get_result( Mindee::V2::Product::Extraction::Extraction, extracted_pdfs[0].as_input_source, - findoc_params + extraction_params ) check_findoc_return(invoice0) diff --git a/spec/v2/product/classification/classification_integration.rb b/spec/v2/product/classification/classification_integration.rb index f7e14e542..efbdf6771 100644 --- a/spec/v2/product/classification/classification_integration.rb +++ b/spec/v2/product/classification/classification_integration.rb @@ -17,12 +17,12 @@ File.join(V2_PRODUCT_DATA_DIR, 'classification', 'default_sample.jpg') ) - params = { model_id: classification_model_id } + classification_params = { model_id: classification_model_id } response = v2_client.enqueue_and_get_result( Mindee::V2::Product::Classification::Classification, input_source, - params + classification_params ) expect(response.inference).not_to be_nil diff --git a/spec/v2/product/crop/crop_integration.rb b/spec/v2/product/crop/crop_integration.rb index 653a9ebab..1012297ce 100644 --- a/spec/v2/product/crop/crop_integration.rb +++ b/spec/v2/product/crop/crop_integration.rb @@ -17,12 +17,12 @@ File.join(V2_PRODUCT_DATA_DIR, 'crop', 'default_sample.jpg') ) - params = { model_id: crop_model_id } + crop_params = { model_id: crop_model_id } response = v2_client.enqueue_and_get_result( Mindee::V2::Product::Crop::Crop, input_source, - params + crop_params ) expect(response.inference).not_to be_nil diff --git a/spec/v2/product/ocr/ocr_integration.rb b/spec/v2/product/ocr/ocr_integration.rb index 3d525a279..d9fdd7e20 100644 --- a/spec/v2/product/ocr/ocr_integration.rb +++ b/spec/v2/product/ocr/ocr_integration.rb @@ -17,12 +17,12 @@ File.join(V2_PRODUCT_DATA_DIR, 'ocr', 'default_sample.jpg') ) - params = { model_id: ocr_model_id } + ocr_params = { model_id: ocr_model_id } response = v2_client.enqueue_and_get_result( Mindee::V2::Product::OCR::OCR, input_source, - params + ocr_params ) expect(response.inference).not_to be_nil diff --git a/spec/v2/product/split/split_integration.rb b/spec/v2/product/split/split_integration.rb index d328cc914..0fe389141 100644 --- a/spec/v2/product/split/split_integration.rb +++ b/spec/v2/product/split/split_integration.rb @@ -17,12 +17,12 @@ File.join(V2_PRODUCT_DATA_DIR, 'split', 'default_sample.pdf') ) - params = { model_id: split_model_id } + split_params = { model_id: split_model_id } response = v2_client.enqueue_and_get_result( Mindee::V2::Product::Split::Split, input_source, - params + split_params ) expect(response.inference).not_to be_nil