Class: Mindee::HTTP::WorkflowEndpoint

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

Overview

Handles the routing for workflow calls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow_id, api_key: '') ⇒ WorkflowEndpoint

Returns a new instance of WorkflowEndpoint.



18
19
20
21
22
23
# File 'lib/mindee/http/workflow_endpoint.rb', line 18

def initialize(workflow_id, api_key: '')
  @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 = "#{base_url.chomp('/')}/workflows/#{workflow_id}/executions"
end

Instance Attribute Details

#api_keyString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/mindee/http/workflow_endpoint.rb', line 12

def api_key
  @api_key
end

#request_timeoutInteger (readonly)

Returns:

  • (Integer)


14
15
16
# File 'lib/mindee/http/workflow_endpoint.rb', line 14

def request_timeout
  @request_timeout
end

#urlString (readonly)

Returns:

  • (String)


16
17
18
# File 'lib/mindee/http/workflow_endpoint.rb', line 16

def url
  @url
end

Instance Method Details

#check_api_keyObject

Checks API key



81
82
83
84
85
86
87
# File 'lib/mindee/http/workflow_endpoint.rb', line 81

def check_api_key
  return unless @api_key.nil? || @api_key.empty?

  raise "Missing API key. Check your Client Configuration.\n" \
        'You can set this using the ' \
        "'#{HTTP::API_KEY_ENV_NAME}' environment variable."
end

#execute_workflow(input_source, full_text, document_alias, priority, public_url) ⇒ Array

Sends a document to the workflow. requiring authentication.

Parameters:

  • input_source (Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::UrlInputSource)
  • document_alias (String, nil)

    Alias to give to the document.

  • priority (Symbol, nil)

    Priority to give to the document.

  • full_text (Boolean)

    Whether to include the full OCR text response in compatible APIs.

  • public_url (String, nil)

    A unique, encrypted URL for accessing the document validation interface without

Returns:

  • (Array)


33
34
35
36
37
38
39
40
41
42
# File 'lib/mindee/http/workflow_endpoint.rb', line 33

def execute_workflow(input_source, full_text, document_alias, priority, public_url)
  check_api_key
  response = workflow_execution_req_post(input_source, document_alias, priority, full_text, public_url)
  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

#workflow_execution_req_post(input_source, document_alias, priority, full_text, public_url) ⇒ Net::HTTPResponse?

requiring authentication.

Parameters:

  • input_source (Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::UrlInputSource)
  • document_alias (String, nil)

    Alias to give to the document.

  • priority (Symbol, nil)

    Priority to give to the document.

  • full_text (Boolean)

    Whether to include the full OCR text response in compatible APIs.

  • public_url (String, nil)

    A unique, encrypted URL for accessing the document validation interface without

Returns:

  • (Net::HTTPResponse, nil)


51
52
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
# File 'lib/mindee/http/workflow_endpoint.rb', line 51

def workflow_execution_req_post(input_source, document_alias, priority, full_text, public_url)
  uri = URI(@url)
  params = {}
  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]
              end
  form_data.push ['alias', document_alias] if document_alias
  form_data.push ['public_url', public_url] if public_url
  form_data.push ['priority', priority.to_s] if priority

  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