Class: Mindee::ClientV2

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/client_v2.rb

Overview

Mindee V2 API Client.

Instance Method Summary collapse

Constructor Details

#initialize(api_key: '') ⇒ ClientV2

Returns a new instance of ClientV2.

Parameters:

  • api_key (String) (defaults to: '')


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(product, input_source, params) ⇒ Mindee::Parsing::V2::JobResponse

Enqueue a document for async parsing.

Parameters:

Returns:



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mindee/client_v2.rb', line 63

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_inference(input_source, params, disable_redundant_warnings: false) ⇒ Mindee::Parsing::V2::InferenceResponse

Enqueue a document for async parsing and automatically try to retrieve it.

Parameters:

Returns:



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/mindee/client_v2.rb', line 138

def enqueue_and_get_inference(input_source, params, disable_redundant_warnings: false)
  unless disable_redundant_warnings
    warn '[DEPRECATION] `enqueue_and_get_inference` is deprecated; use `enqueue_and_get_result` instead.',
         uplevel: 1
  end

  response = enqueue_and_get_result(Mindee::Parsing::V2::Inference, input_source, params)
  unless response.is_a?(Mindee::Parsing::V2::InferenceResponse)
    raise TypeError, "Invalid response type \"#{response.class}\""
  end

  response
end

#enqueue_and_get_result(product, input_source, params) ⇒ Mindee::Parsing::Common::ApiResponse

Enqueues to an asynchronous endpoint and automatically polls for a response.

Parameters:

Returns:

Raises:



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
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mindee/client_v2.rb', line 82

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::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.polling_options.initial_delay_sec)
  retry_counter = 1
  poll_results = get_job(job_id)

  while retry_counter < normalized_params.polling_options.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.polling_options.max_retries}.\n" \
      "Job status: #{poll_results.job.status}."
    )

    sleep(normalized_params.polling_options.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.polling_options.delay_sec * retry_counter
  raise Mindee::Errors::MindeeError,
        "Asynchronous parsing request timed out after #{sec_count} seconds"
end

#enqueue_inference(input_source, params, disable_redundant_warnings: false) ⇒ Mindee::Parsing::V2::JobResponse

Enqueue a document for async parsing.

Parameters:

Returns:



49
50
51
52
53
54
55
# File 'lib/mindee/client_v2.rb', line 49

def enqueue_inference(input_source, params, disable_redundant_warnings: false)
  unless disable_redundant_warnings
    warn '[DEPRECATION] `enqueue_inference` is deprecated; use `enqueue` instead.', uplevel: 1
  end
  normalized_params = normalize_parameters(Input::InferenceParameters, params)
  enqueue(Mindee::Parsing::V2::Inference, input_source, normalized_params)
end

#get_inference(inference_id) ⇒ Mindee::Parsing::V2::InferenceResponse

Retrieves an inference.

Parameters:

  • inference_id (String)

Returns:



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 from a given queue or URL to the job.

Parameters:

  • job_id (String)

    ID of the job.

Returns:



40
41
42
# File 'lib/mindee/client_v2.rb', line 40

def get_job(job_id)
  @mindee_api.req_get_job(job_id)
end

#get_result(product, resource) ⇒ Mindee::Parsing::V2::BaseResponse

Retrieves a result from a given queue or URL to the result.

Parameters:

Returns:

  • (Mindee::Parsing::V2::BaseResponse)


33
34
35
# File 'lib/mindee/client_v2.rb', line 33

def get_result(product, resource)
  @mindee_api.req_get_result(product, resource)
end

#normalize_parameters(param_class, params) ⇒ BaseParameters

If needed, converts the parsing options provided as a hash into a proper InferenceParameters object.

Parameters:

  • params (Hash, Class<BaseParameters>)

    Params.

Returns:

  • (BaseParameters)


155
156
157
158
159
# File 'lib/mindee/client_v2.rb', line 155

def normalize_parameters(param_class, params)
  return param_class.from_hash(params: params) if params.is_a?(Hash)

  params
end