Class: Mindee::HTTP::WorkflowEndpoint
- Inherits:
-
Object
- Object
- Mindee::HTTP::WorkflowEndpoint
- Defined in:
- lib/mindee/http/workflow_endpoint.rb
Overview
Handles the routing for workflow calls.
Instance Attribute Summary collapse
- #api_key ⇒ String readonly
- #request_timeout ⇒ Integer readonly
- #url ⇒ String readonly
Instance Method Summary collapse
-
#check_api_key ⇒ Object
Checks API key.
-
#execute_workflow(input_source, opts) ⇒ Array
Sends a document to the workflow.
-
#initialize(workflow_id, api_key: '') ⇒ WorkflowEndpoint
constructor
A new instance of WorkflowEndpoint.
- #workflow_execution_req_post(input_source, opts) ⇒ Net::HTTPResponse?
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_key ⇒ String (readonly)
12 13 14 |
# File 'lib/mindee/http/workflow_endpoint.rb', line 12 def api_key @api_key end |
#request_timeout ⇒ Integer (readonly)
14 15 16 |
# File 'lib/mindee/http/workflow_endpoint.rb', line 14 def request_timeout @request_timeout end |
#url ⇒ String (readonly)
16 17 18 |
# File 'lib/mindee/http/workflow_endpoint.rb', line 16 def url @url end |
Instance Method Details
#check_api_key ⇒ Object
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 Errors::MindeeConfigurationError, "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, opts) ⇒ Array
Sends a document to the workflow.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mindee/http/workflow_endpoint.rb', line 29 def execute_workflow(input_source, opts) check_api_key response = workflow_execution_req_post(input_source, opts) if response.nil? raise Mindee::Errors::MindeeHTTPError.new( { code: 0, details: 'Server response was nil.', message: 'Unknown error.' }, @url, 0 ) end 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 = Mindee::HTTP::ErrorHandler.handle_error(@url_name, response) raise error end |
#workflow_execution_req_post(input_source, opts) ⇒ Net::HTTPResponse?
49 50 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 49 def workflow_execution_req_post(input_source, opts) uri = URI(@url) params = {} # : Hash[Symbol | String, untyped] params[:full_text_ocr] = 'true' if opts.full_text params[:rag] = 'true' if opts.rag uri.query = URI.encode_www_form(params) if params.any? headers = { 'Authorization' => "Token #{@api_key}", 'User-Agent' => USER_AGENT, } req = Net::HTTP::Post.new(uri, headers) form_data = [] # : Array[untyped] if input_source.is_a?(Mindee::Input::Source::URLInputSource) form_data.push ['document', input_source.url] else form_data.push input_source.read_contents end form_data.push ['alias', opts.document_alias] if opts.document_alias form_data.push ['public_url', opts.public_url] if opts.public_url form_data.push ['priority', opts.priority.to_s] if opts.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 |