Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 7 additions & 46 deletions lib/mindee/client_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,27 @@ def initialize(api_key: '')
@mindee_api = Mindee::HTTP::MindeeApiV2.new(api_key: api_key)
end

# Retrieves an inference.
# @param inference_id [String]
# @return [Mindee::Parsing::V2::InferenceResponse]
def get_inference(inference_id)
@mindee_api.req_get_inference(inference_id)
end

# Retrieves a result from a given queue or URL to the result.
# @param product [Class<Mindee::V2::Product::BaseProduct>] The return class.
# @param resource [String] ID of the inference or URL to the result.
# @return [Mindee::Parsing::V2::BaseResponse]
# @return [Mindee::V2::Parsing::BaseResponse]
def get_result(product, resource)
@mindee_api.req_get_result(product, resource)
end

# Retrieves an inference from a given queue or URL to the job.
# @param job_id [String] ID of the job.
# @return [Mindee::Parsing::V2::JobResponse]
# @return [Mindee::V2::Parsing::JobResponse]
def get_job(job_id)
@mindee_api.req_get_job(job_id)
end

# Enqueue a document for async parsing.
# @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
# The source of the input document (local file or URL).
# @param params [Hash, InferenceParameters]
# @return [Mindee::Parsing::V2::JobResponse]
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

# Enqueue a document for async parsing.
# @param product [Class<Mindee::V2::Product::BaseProduct>] The return class.
# @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
# The source of the input document (local file or URL).
# @param params [Hash, InferenceParameters] Parameters for the inference.
# @return [Mindee::Parsing::V2::JobResponse]
# @param params [Hash, Input::BaseParameters] Parameters for the inference.
# @return [Mindee::V2::Parsing::JobResponse]
def enqueue(
product,
input_source,
Expand All @@ -77,8 +57,8 @@ def enqueue(
# @param product [Class<Mindee::V2::Product::BaseProduct>] The return class.
# @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
# The source of the input document (local file or URL).
# @param params [Hash, InferenceParameters] Parameters for the inference.
# @return [Mindee::Parsing::Common::ApiResponse]
# @param params [Hash, Input::BaseParameters] Parameters for the inference.
# @return [Parsing::BaseResponse]
def enqueue_and_get_result(
product,
input_source,
Expand Down Expand Up @@ -130,26 +110,7 @@ def enqueue_and_get_result(
"Asynchronous parsing request timed out after #{sec_count} seconds"
end

# Enqueue a document for async parsing and automatically try to retrieve it.
# @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
# The source of the input document (local file or URL).
# @param params [Hash, InferenceParameters] Parameters for the inference.
# @return [Mindee::Parsing::V2::InferenceResponse]
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

# If needed, converts the parsing options provided as a hash into a proper InferenceParameters object.
# If needed, converts the parsing options provided as a hash into a proper BaseParameters subclass object.
# @param params [Hash, Class<BaseParameters>] Params.
# @return [BaseParameters]
def normalize_parameters(param_class, params)
Expand Down
8 changes: 4 additions & 4 deletions lib/mindee/errors/mindee_http_error_v2.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative 'mindee_error'
require_relative '../parsing/v2/error_item'
require_relative '../v2/parsing/error_item'

module Mindee
module Errors
Expand All @@ -18,9 +18,9 @@ class MindeeHTTPErrorV2 < MindeeError
# @return [Array<ErrorItem>] A list of explicit error details.
attr_reader :errors

# @param http_error [Hash, Mindee::Parsing::V2::ErrorResponse]
# @param http_error [Hash, Mindee::V2::Parsing::ErrorResponse]
def initialize(http_error)
if http_error.is_a?(Parsing::V2::ErrorResponse)
if http_error.is_a?(V2::Parsing::ErrorResponse)
http_error = { 'detail' => http_error.detail,
'status' => http_error.status,
'title' => http_error.title,
Expand All @@ -33,7 +33,7 @@ def initialize(http_error)
@code = http_error['code']
@errors = if http_error.key?('errors')
http_error['errors'].map do |error|
Mindee::Parsing::V2::ErrorItem.new(error)
Mindee::V2::Parsing::ErrorItem.new(error)
end
else
[]
Expand Down
51 changes: 12 additions & 39 deletions lib/mindee/http/mindee_api_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require_relative 'api_settings_v2'
require_relative '../input'
require_relative '../errors'
require_relative '../parsing/v2'

module Mindee
module HTTP
Expand All @@ -17,33 +16,25 @@ def initialize(api_key: nil)
@settings = ApiSettingsV2.new(api_key: api_key)
end

# Sends a file to the inference queue.
# Sends a file to the queue.
#
# @param input_source [Input::Source::LocalInputSource, Input::Source::URLInputSource]
# @param params [Input::BaseParameters]
# @return [Mindee::Parsing::V2::JobResponse]
# @return [Mindee::V2::Parsing::JobResponse]
# @raise [Mindee::Errors::MindeeHttpErrorV2]
def req_post_enqueue(input_source, params)
@settings.check_api_key
response = enqueue(
input_source,
params
)
Mindee::Parsing::V2::JobResponse.new(process_response(response))
end

# Retrieves a queued inference.
#
# @param inference_id [String]
# @return [Mindee::Parsing::V2::InferenceResponse]
def req_get_inference(inference_id)
req_get_result(Parsing::V2::Inference, inference_id)
Mindee::V2::Parsing::JobResponse.new(process_response(response))
end

# Retrieves a result from a given queue.
# @param product [Class<Mindee::V2::Product::BaseProduct>] The return class.
# @param resource [String] ID of the inference or URL to the result.
# @return [Mindee::Parsing::V2::BaseResponse]
# @return [Mindee::V2::Parsing::BaseResponse]
def req_get_result(product, resource)
return req_get_result_url(product.response_type, resource) if uri?(resource)

Expand All @@ -58,32 +49,30 @@ def req_get_result(product, resource)
# Retrieves a queued job.
#
# @param job_id [String] ID of the job or URL to the job.
# @return [Mindee::Parsing::V2::JobResponse]
# @return [Mindee::V2::Parsing::JobResponse]
def req_get_job(job_id)
@settings.check_api_key
response = inference_job_req_get(
job_id
)
Mindee::Parsing::V2::JobResponse.new(process_response(response))
response = poll("#{@settings.base_url}/jobs/#{job_id}")
Mindee::V2::Parsing::JobResponse.new(process_response(response))
end

private

# Retrieves a queued job.
#
# @param url [String]
# @return [Mindee::Parsing::V2::JobResponse]
# @return [Mindee::V2::Parsing::JobResponse]
def req_get_job_url(url)
@settings.check_api_key
response = poll(url)
Mindee::Parsing::V2::JobResponse.new(process_response(response))
Mindee::V2::Parsing::JobResponse.new(process_response(response))
end

# Retrieves a queued job.
#
# @param result_class [Mindee::V2::Parsing::BaseResponse]
# @param result_class [Class<Mindee::V2::Parsing::BaseResponse>]
# @param url [String]
# @return [Mindee::Parsing::V2::JobResponse]
# @return [Mindee::V2::Parsing::BaseResponse]
def req_get_result_url(result_class, url)
@settings.check_api_key
response = poll(url)
Expand Down Expand Up @@ -137,22 +126,6 @@ def poll(url)
raise Mindee::Errors::MindeeError, 'Could not resolve server response.'
end

# Polls the API for the status of a job.
#
# @param job_id [String] ID of the job.
# @return [Net::HTTPResponse]
def inference_job_req_get(job_id)
poll("#{@settings.base_url}/jobs/#{job_id}")
end

# Polls the API for the result of an inference.
#
# @param queue_id [String] ID of the queue.
# @return [Net::HTTPResponse]
def inference_result_req_get(queue_id)
poll("#{@settings.base_url}/inferences/#{queue_id}")
end

# Polls the API for the result of an inference.
#
# @param queue_id [String] ID of the queue.
Expand All @@ -164,7 +137,7 @@ def result_req_get(queue_id, product)

# Handle parameters for the enqueue form
# @param form_data [Array] Array of form fields
# @param params [Input::InferenceParameters] Inference options.
# @param params [Input::BaseParameters] Inference options.
def enqueue_form_options(form_data, params)
# deal with optional features
form_data.push(['rag', params.rag.to_s]) unless params.rag.nil?
Expand Down
1 change: 0 additions & 1 deletion lib/mindee/input.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require_relative 'input/data_schema'
require_relative 'input/inference_parameters'
require_relative 'input/polling_options'
require_relative 'input/sources'
119 changes: 0 additions & 119 deletions lib/mindee/input/inference_parameters.rb

This file was deleted.

8 changes: 3 additions & 5 deletions lib/mindee/input/local_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ def valid_hmac_signature?(secret_key, signature)
end

# Deserializes a loaded response
# @param response_class [Parsing::V2::CommonResponse] class to return.
# @return [Parsing::V2::JobResponse, Mindee::V2::Parsing::CommonResponse]
# @param response_class [Class<V2::Parsing::BaseResponse>] class to return.
# @return [V2::Parsing::JobResponse, Mindee::V2::Parsing::BaseResponse]
def deserialize_response(response_class)
response_class.new(as_hash) # : Mindee::Parsing::V2::JobResponse | Mindee::Parsing::V2::InferenceResponse
rescue StandardError
raise Errors::MindeeInputError, 'Invalid response provided.'
response_class.new(as_hash) # : Mindee::V2::Parsing::JobResponse | Mindee::V2::Parsing::BaseResponse
end
end
end
Expand Down
16 changes: 0 additions & 16 deletions lib/mindee/parsing/v2.rb

This file was deleted.

Loading