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) ⇒ 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_models(model_name, model_type) ⇒ Mindee::V2::Parsing::Search::SearchResponse
Searches for a list of available models for the given API key.
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_enqueue(input_source, normalized_params) end |
#enqueue_and_get_result(product, input_source, params) ⇒ Parsing::BaseResponse
Enqueues to an asynchronous endpoint and automatically polls for a response.
61 62 63 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 |
# File 'lib/mindee/v2/client.rb', line 61 def enqueue_and_get_result( product, input_source, params ) enqueue_response = enqueue(product, input_source, params) normalized_params = normalize_parameters(product.params_type, params) 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}.") 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}." ) 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_models(model_name, model_type) ⇒ Mindee::V2::Parsing::Search::SearchResponse
Searches for a list of available models for the given API key.
116 117 118 |
# File 'lib/mindee/v2/client.rb', line 116 def search_models(model_name, model_type) @mindee_api.search_models(model_name, model_type) end |