Class: Mindee::V2::Client

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

Overview

Mindee V2 API Client.

Instance Method Summary collapse

Constructor Details

#initialize(api_key: '') ⇒ Client

Returns a new instance of Client.

Parameters:

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


17
18
19
# File 'lib/mindee/v2/client.rb', line 17

def initialize(api_key: '')
  @mindee_api = Mindee::V2::HTTP::MindeeApiV2.new(api_key: api_key)
end

Instance Method Details

#enqueue(product, input_source, params) ⇒ Mindee::V2::Parsing::JobResponse

Send a file to the asynchronous processing queue for inference.

Parameters:

Returns:



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mindee/v2/client.rb', line 27

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

Send a file to the inference queue and return the results when complete. rubocop:disable-next Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize

Parameters:

Returns:

Raises:



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mindee/v2/client.rb', line 81

def enqueue_and_get_result(
  product,
  input_source,
  params,
  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

  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.polling_options.initial_delay_sec)
  retry_counter = 1
  polling_url = enqueue_response.job.polling_url

  job_response = get_job_by_url(polling_url)

  while retry_counter < normalized_params.polling_options.max_retries
    if job_response.job.status == 'Failed'
      break
    elsif !job_response.job.result_url.nil?
      return get_result_by_url(product, job_response.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: #{job_response.job.status}."
    )

    raise Mindee::Error::MindeeError, 'Enqueueing of the document was canceled.' if cancellation_token&.canceled?

    sleep(normalized_params.polling_options.delay_sec)
    job_response = get_job_by_url(polling_url)
    retry_counter += 1
  end

  error = job_response.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.polling_options.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

Get the status of an inference that was previously enqueued. Prefer using get_job_by_url with the polling URL returned by enqueue.

Parameters:

  • job_id (String) —

    ID of the job.

Returns:



68
69
70
# File 'lib/mindee/v2/client.rb', line 68

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

#get_job_by_url(url) ⇒ Mindee::V2::Parsing::JobResponse

Get the status of an inference that was previously enqueued. Can be used for polling.

Parameters:

  • url (String) —

    HTTPS URL to the job.

Returns:



43
44
45
# File 'lib/mindee/v2/client.rb', line 43

def get_job_by_url(url)
  @mindee_api.req_get_job_by_url(url)
end

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

Get the result of an inference that was previously enqueued. Prefer using get_result_by_url using the result URL returned by the Job.

Parameters:

Returns:



60
61
62
# File 'lib/mindee/v2/client.rb', line 60

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

#get_result_by_url(product, url) ⇒ Mindee::V2::Parsing::BaseResponse

Get the result of an inference that was previously enqueued.

Parameters:

Returns:



51
52
53
# File 'lib/mindee/v2/client.rb', line 51

def get_result_by_url(product, url)
  @mindee_api.req_get_result_by_url(product, url)
end

#search(params) ⇒ Mindee::V2::Parsing::Search::BaseSearchResponse

Search for resources matching the given criteria.

Parameters:

Returns:



143
144
145
# File 'lib/mindee/v2/client.rb', line 143

def search(params)
  @mindee_api.req_get_search(params)
end

#search_models(model_name, model_type) ⇒ Mindee::V2::Search::Models::ModelSearchResponse

Deprecated.

Use #search instead.

Returns a list of models matching a criteria for the given API key.

Parameters:

  • model_name (String, nil) —

    Name filter.

  • model_type (String, nil) —

    Model type filter.

Returns:



152
153
154
# File 'lib/mindee/v2/client.rb', line 152

def search_models(model_name, model_type)
  search(Search::Models::ModelSearchParameters.new(name: model_name, model_type: model_type))
end