Class: Mindee::V2::Client
- Inherits:
-
Object
- Object
- Mindee::V2::Client
- Defined in:
- lib/mindee/v2/client.rb
Overview
Mindee V2 API Client.
Instance Method Summary collapse
-
#enqueue(product, input_source, params) ⇒ Mindee::V2::Parsing::JobResponse
Enqueue a document for async parsing.
-
#enqueue_and_get_result(product, input_source, params, polling_options = nil, cancellation_token = nil) ⇒ Parsing::BaseResponse
Enqueues to an asynchronous endpoint and automatically polls for a response.
-
#get_job(job_id) ⇒ Mindee::V2::Parsing::JobResponse
Retrieves an inference from a given queue or URL to the job.
-
#get_result(product, resource) ⇒ Mindee::V2::Parsing::BaseResponse
Retrieves a result from a given queue or URL to the result.
-
#initialize(api_key: '') ⇒ Client
constructor
A new instance of Client.
-
#search(params) ⇒ Mindee::V2::Parsing::Search::BaseSearchResponse
Search for resources matching the given criteria.
-
#search_models(model_name, model_type) ⇒ Mindee::V2::Search::Models::ModelSearchResponse
deprecated
Deprecated.
Use #search instead.
Constructor Details
Instance Method Details
#enqueue(product, input_source, params) ⇒ Mindee::V2::Parsing::JobResponse
Enqueue a document for async parsing.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mindee/v2/client.rb', line 42 def enqueue( product, 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) end |
#enqueue_and_get_result(product, input_source, params, polling_options = nil, cancellation_token = nil) ⇒ Parsing::BaseResponse
Enqueues to an asynchronous endpoint and automatically polls for a response.
rubocop:disable-next Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mindee/v2/client.rb', line 64 def enqueue_and_get_result( product, input_source, params, = nil, cancellation_token = nil ) enqueue_response = enqueue(product, input_source, params) normalized_params = normalize_parameters(product.params_type, params, polling_options: ) normalized_params.validate_async_params if enqueue_response.job.id.nil? || enqueue_response.job.id.empty? logger.error("Failed enqueueing:\n#{enqueue_response.raw_http}") raise Mindee::Error::MindeeError, 'Enqueueing of the document failed.' end job_id = enqueue_response.job.id logger.debug("Successfully enqueued document with job id: #{job_id}.") raise Mindee::Error::MindeeError, 'Enqueueing of the document was canceled.' if cancellation_token&.canceled? sleep(normalized_params..initial_delay_sec) retry_counter = 1 poll_results = get_job(job_id) while retry_counter < normalized_params..max_retries if poll_results.job.status == 'Failed' break elsif !poll_results.job.result_url.nil? return get_result(product, poll_results.job.result_url) end logger.debug( "Successfully enqueued inference with job id: #{job_id}.\n" \ "Attempt n°#{retry_counter}/#{normalized_params..max_retries}.\n" \ "Job status: #{poll_results.job.status}." ) raise Mindee::Error::MindeeError, 'Enqueueing of the document was canceled.' if cancellation_token&.canceled? sleep(normalized_params..delay_sec) poll_results = get_job(job_id) retry_counter += 1 end error = poll_results.job.error unless error.nil? err_to_raise = Mindee::Error::MindeeHTTPErrorV2.new(error) # NOTE: purposefully decoupled from the line above, otherwise rubocop thinks `error` is a `message` param. raise err_to_raise end sec_count = normalized_params..delay_sec * retry_counter raise Mindee::Error::MindeeError, "Asynchronous parsing request timed out after #{sec_count} seconds" end |
#get_job(job_id) ⇒ Mindee::V2::Parsing::JobResponse
Retrieves an inference from a given queue or URL to the job.
32 33 34 |
# File 'lib/mindee/v2/client.rb', line 32 def get_job(job_id) @mindee_api.req_get_job(job_id) end |
#get_result(product, resource) ⇒ Mindee::V2::Parsing::BaseResponse
Retrieves a result from a given queue or URL to the result.
25 26 27 |
# File 'lib/mindee/v2/client.rb', line 25 def get_result(product, resource) @mindee_api.req_get_result(product, resource) end |
#search(params) ⇒ Mindee::V2::Parsing::Search::BaseSearchResponse
Search for resources matching the given criteria.
124 125 126 |
# File 'lib/mindee/v2/client.rb', line 124 def search(params) @mindee_api.req_get_search(params) end |
#search_models(model_name, model_type) ⇒ Mindee::V2::Search::Models::ModelSearchResponse
Use #search instead.
Returns a list of models matching a criteria for the given API key.
133 134 135 |
# File 'lib/mindee/v2/client.rb', line 133 def search_models(model_name, model_type) search(Search::Models::ModelSearchParameters.new(name: model_name, model_type: model_type)) end |