class Mindee::HTTP::Endpoint
Generic API endpoint for a product.
def check_api_key
def check_api_key return unless @api_key.nil? || @api_key.empty? raise "Missing API key for product \"'#{@url_name}' v#{@version}\" (belonging to \"#{@owner}\"), " \ "check your Client Configuration.\n" \ 'You can set this using the ' \ "'#{HTTP::API_KEY_ENV_NAME}' environment variable." end
def document_queue_req(job_id)
-
(Net::HTTPResponse, nil)
-
Parameters:
-
job_id
(String
) --
def document_queue_req(job_id) uri = URI("#{@url_root}/documents/queue/#{job_id}") headers = { 'Authorization' => "Token #{@api_key}", 'User-Agent' => USER_AGENT, } req = Net::HTTP::Get.new(uri, headers) response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http| http.request(req) end if response.code.to_i > 299 && response.code.to_i < 400 req = Net::HTTP::Get.new(response['location'], headers) Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http| response = http.request(req) end end response end
def document_queue_req_get(input_source, all_words, full_text, close_file, cropper)
-
(Net::HTTPResponse, nil)
-
Parameters:
-
cropper
(Boolean
) -- Whether a cropping operation will be applied -
close_file
(Boolean
) -- Whether the file will be closed after reading -
full_text
(Boolean
) -- Whether to include the full OCR text response in compatible APIs. -
all_words
(Boolean
) -- Whether the full word extraction needs to be performed -
input_source
(Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::UrlInputSource
) --
def document_queue_req_get(input_source, all_words, full_text, close_file, cropper) uri = URI("#{@url_root}/predict_async") params = {} params[:cropper] = 'true' if cropper params[:full_text_ocr] = 'true' if full_text uri.query = URI.encode_www_form(params) headers = { 'Authorization' => "Token #{@api_key}", 'User-Agent' => USER_AGENT, } req = Net::HTTP::Post.new(uri, headers) form_data = if input_source.is_a?(Mindee::Input::Source::UrlInputSource) [['document', input_source.url]] else [input_source.read_document(close: close_file)] end form_data.push ['include_mvision', 'true'] if all_words req.set_form(form_data, 'multipart/form-data') response = nil Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http| response = http.request(req) end response end
def initialize(owner, url_name, version, api_key: '')
def initialize(owner, url_name, version, api_key: '') @owner = owner @url_name = url_name @version = version @request_timeout = ENV.fetch(REQUEST_TIMEOUT_ENV_NAME, TIMEOUT_DEFAULT).to_i @api_key = api_key.nil? || api_key.empty? ? ENV.fetch(API_KEY_ENV_NAME, API_KEY_DEFAULT) : api_key base_url = ENV.fetch(BASE_URL_ENV_NAME, BASE_URL_DEFAULT) @url_root = "#{base_url.chomp('/')}/products/#{@owner}/#{@url_name}/v#{@version}" end
def parse_async(job_id)
-
(Array)
-
Parameters:
-
job_id
(String
) --
def parse_async(job_id) check_api_key response = document_queue_req(job_id) hashed_response = JSON.parse(response.body, object_class: Hash) return [hashed_response, response.body] if ResponseValidation.valid_async_response?(response) ResponseValidation.clean_request!(response) error = Error.handle_error(@url_name, response) raise error end
def predict(input_source, all_words, full_text, close_file, cropper)
-
(Array)
-
Parameters:
-
cropper
(Boolean
) -- Whether a cropping operation will be applied -
close_file
(Boolean
) -- Whether the file will be closed after reading -
full_text
(Boolean
) -- Whether to include the full OCR text response in compatible APIs -
all_words
(Boolean
) -- Whether the full word extraction needs to be performed -
input_source
(Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::UrlInputSource
) --
def predict(input_source, all_words, full_text, close_file, cropper) check_api_key response = predict_req_post( input_source, all_words: all_words, full_text: full_text, close_file: close_file, cropper: cropper ) hashed_response = JSON.parse(response.body, object_class: Hash) return [hashed_response, response.body] if ResponseValidation.valid_sync_response?(response) ResponseValidation.clean_request!(response) error = Error.handle_error(@url_name, response) raise error end
def predict_async(input_source, all_words, full_text, close_file, cropper)
-
(Array)
-
Parameters:
-
cropper
(Boolean
) -- Whether a cropping operation will be applied -
close_file
(Boolean
) -- Whether the file will be closed after reading -
full_text
(Boolean
) -- Whether to include the full OCR text response in compatible APIs. -
all_words
(Boolean
) -- Whether the full word extraction needs to be performed -
input_source
(Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::UrlInputSource
) --
def predict_async(input_source, all_words, full_text, close_file, cropper) check_api_key response = document_queue_req_get(input_source, all_words, full_text, close_file, cropper) hashed_response = JSON.parse(response.body, object_class: Hash) return [hashed_response, response.body] if ResponseValidation.valid_async_response?(response) ResponseValidation.clean_request!(response) error = Error.handle_error(@url_name, response) raise error end
def predict_req_post(input_source, all_words: false, full_text: false, close_file: true, cropper: false)
-
(Net::HTTPResponse, nil)
-
Parameters:
-
cropper
(Boolean
) -- Whether a cropping operation will be applied -
close_file
(Boolean
) -- Whether the file will be closed after reading -
full_text
(Boolean
) -- Whether to include the full OCR text response in compatible APIs. -
all_words
(Boolean
) -- Whether the full word extraction needs to be performed -
input_source
(Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::UrlInputSource
) --
def predict_req_post(input_source, all_words: false, full_text: false, close_file: true, cropper: false) uri = URI("#{@url_root}/predict") params = {} params[:cropper] = 'true' if cropper params[:full_text_ocr] = 'true' if full_text uri.query = URI.encode_www_form(params) headers = { 'Authorization' => "Token #{@api_key}", 'User-Agent' => USER_AGENT, } req = Net::HTTP::Post.new(uri, headers) form_data = if input_source.is_a?(Mindee::Input::Source::UrlInputSource) [['document', input_source.url]] else [input_source.read_document(close: close_file)] end form_data.push ['include_mvision', 'true'] if all_words req.set_form(form_data, 'multipart/form-data') response = nil Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http| response = http.request(req) end response end