Class: Mindee::ClientV2
- Inherits:
-
Object
- Object
- Mindee::ClientV2
- Defined in:
- lib/mindee/client_v2.rb
Overview
Mindee V2 API Client.
Instance Method Summary collapse
-
#enqueue_and_get_inference(input_source, params) ⇒ Mindee::Parsing::V2::InferenceResponse
Enqueue a document for async parsing and automatically try to retrieve it.
-
#enqueue_inference(input_source, params) ⇒ Mindee::Parsing::V2::JobResponse
Enqueue a document for async parsing.
-
#get_inference(inference_id) ⇒ Mindee::Parsing::V2::InferenceResponse
Retrieves an inference.
-
#get_job(job_id) ⇒ Mindee::Parsing::V2::JobResponse
Retrieves an inference.
-
#initialize(api_key: '') ⇒ ClientV2
constructor
A new instance of ClientV2.
-
#normalize_inference_parameters(params) ⇒ InferenceParameters
If needed, converts the parsing options provided as a hash into a proper InferenceParameters object.
Constructor Details
#initialize(api_key: '') ⇒ ClientV2
Returns a new instance of ClientV2.
18 19 20 |
# File 'lib/mindee/client_v2.rb', line 18 def initialize(api_key: '') @mindee_api = Mindee::HTTP::MindeeApiV2.new(api_key: api_key) end |
Instance Method Details
#enqueue_and_get_inference(input_source, params) ⇒ Mindee::Parsing::V2::InferenceResponse
Enqueue a document for async parsing and automatically try to retrieve it.
53 54 55 56 57 58 59 60 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 |
# File 'lib/mindee/client_v2.rb', line 53 def enqueue_and_get_inference(input_source, params) normalized_params = normalize_inference_parameters(params) normalized_params.validate_async_params enqueue_response = enqueue_inference(input_source, normalized_params) if enqueue_response.job.id.nil? || enqueue_response.job.id.empty? logger.error("Failed enqueueing:\n#{enqueue_response.raw_http}") raise Mindee::Errors::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.status == 'Processed' return get_inference(poll_results.job.id) 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: #{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::Errors::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::Errors::MindeeError, "Asynchronous parsing request timed out after #{sec_count} seconds" end |
#enqueue_inference(input_source, params) ⇒ Mindee::Parsing::V2::JobResponse
Enqueue a document for async parsing.
41 42 43 44 45 46 |
# File 'lib/mindee/client_v2.rb', line 41 def enqueue_inference(input_source, params) normalized_params = normalize_inference_parameters(params) logger.debug("Enqueueing document to model '#{normalized_params.model_id}'.") @mindee_api.req_post_inference_enqueue(input_source, normalized_params) end |
#get_inference(inference_id) ⇒ Mindee::Parsing::V2::InferenceResponse
Retrieves an inference.
25 26 27 |
# File 'lib/mindee/client_v2.rb', line 25 def get_inference(inference_id) @mindee_api.req_get_inference(inference_id) end |
#get_job(job_id) ⇒ Mindee::Parsing::V2::JobResponse
Retrieves an inference.
32 33 34 |
# File 'lib/mindee/client_v2.rb', line 32 def get_job(job_id) @mindee_api.req_get_job(job_id) end |
#normalize_inference_parameters(params) ⇒ InferenceParameters
If needed, converts the parsing options provided as a hash into a proper InferenceParameters object.
103 104 105 106 107 |
# File 'lib/mindee/client_v2.rb', line 103 def normalize_inference_parameters(params) return params if params.is_a?(Input::InferenceParameters) Input::InferenceParameters.from_hash(params: params) end |