Class: Mindee::V1::HTTP::Endpoint
- Defined in:
- lib/mindee/v1/http/endpoint.rb,
sig/mindee/v1/http/endpoint.rbs
Overview
Generic API endpoint for a product.
Instance Attribute Summary collapse
- #api_key ⇒ String readonly
- #base_url ⇒ String readonly
- #request_timeout ⇒ Integer readonly
- #url_root ⇒ String readonly
Instance Method Summary collapse
-
#check_api_key ⇒ void
Checks API key.
- #configure_ssl ⇒ void
- #document_queue_req_get(job_id) ⇒ Net::HTTPResponse?
- #document_queue_req_post(input_source, opts) ⇒ Net::HTTPResponse
-
#initialize(owner, url_name, version, api_key: '') ⇒ Endpoint
constructor
A new instance of Endpoint.
- #logger ⇒ Logger
-
#parse_async(job_id) ⇒ Array
Calls the parsed async doc.
-
#predict(input_source, opts) ⇒ Array
Call the prediction API.
-
#predict_async(input_source, opts) ⇒ Array
Call the prediction API.
- #predict_req_post(input_source, opts) ⇒ Net::HTTPResponse?
Constructor Details
#initialize(owner, url_name, version, api_key: '') ⇒ Endpoint
Returns a new instance of Endpoint.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mindee/v1/http/endpoint.rb', line 42 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 if api_key.nil? && !ENV.fetch(API_KEY_ENV_NAME, API_KEY_DEFAULT).to_s.empty? logger.debug('API key set from environment') end @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).chomp('/') @url_root = "#{@base_url}/products/#{@owner}/#{@url_name}/v#{@version}" end |
Instance Attribute Details
#api_key ⇒ String (readonly)
34 35 36 |
# File 'lib/mindee/v1/http/endpoint.rb', line 34 def api_key @api_key end |
#base_url ⇒ String (readonly)
40 41 42 |
# File 'lib/mindee/v1/http/endpoint.rb', line 40 def base_url @base_url end |
#request_timeout ⇒ Integer (readonly)
36 37 38 |
# File 'lib/mindee/v1/http/endpoint.rb', line 36 def request_timeout @request_timeout end |
#url_root ⇒ String (readonly)
38 39 40 |
# File 'lib/mindee/v1/http/endpoint.rb', line 38 def url_root @url_root end |
Instance Method Details
#check_api_key ⇒ void
This method returns an undefined value.
Checks API key
210 211 212 213 214 215 216 217 |
# File 'lib/mindee/v1/http/endpoint.rb', line 210 def check_api_key return unless @api_key.nil? || @api_key.empty? raise Error::MindeeAPIError, "Missing API key for product \"'#{@url_name}' v#{@version}\" (belonging to \"#{@owner}\"), " \ "check your Client Configuration.\nYou can set this using the " \ "'#{HTTP::API_KEY_ENV_NAME}' environment variable." end |
#configure_ssl ⇒ void
This method returns an undefined value.
37 |
# File 'sig/mindee/v1/http/endpoint.rbs', line 37 def configure_ssl: (Net::HTTP) -> void |
#document_queue_req_get(job_id) ⇒ Net::HTTPResponse?
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/mindee/v1/http/endpoint.rb', line 184 def document_queue_req_get(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 raise Error::MindeeError, 'Could not resolve server response.' if response.nil? 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 |
#document_queue_req_post(input_source, opts) ⇒ Net::HTTPResponse
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/mindee/v1/http/endpoint.rb', line 148 def document_queue_req_post(input_source, opts) uri = if opts.workflow_id URI("#{@base_url}/workflows/#{opts.workflow_id}/predict_async") else URI("#{@url_root}/predict_async") end params = {} # : Hash[String | Symbol, untyped] params[:cropper] = 'true' if opts.cropper params[:full_text_ocr] = 'true' if opts.full_text params[:rag] = 'true' if opts.rag 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]] # : Array[untyped] else [['document', *input_source.read_contents(close: opts.close_file)]] # : Array[untyped] end form_data.push ['include_mvision', 'true'] if opts.all_words req.set_form(form_data, 'multipart/form-data') req['Transfer-Encoding'] = 'chunked' Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http| return http.request(req) end raise Mindee::Error::MindeeError, 'Could not resolve server response.' end |
#logger ⇒ Logger
19 |
# File 'sig/mindee/v1/http/endpoint.rbs', line 19 def logger: () -> Logger |
#parse_async(job_id) ⇒ Array
Calls the parsed async doc.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mindee/v1/http/endpoint.rb', line 100 def parse_async(job_id) check_api_key response = document_queue_req_get(job_id) hashed_response = JSON.parse(response.body, object_class: Hash) return [hashed_response, response.body] if Mindee::HTTP::ResponseValidation.valid_async_response?(response) Mindee::HTTP::ResponseValidation.clean_request!(response) error = Mindee::HTTP::ErrorHandler.handle_error(@url_name, response) raise error end |
#predict(input_source, opts) ⇒ Array
Call the prediction API.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mindee/v1/http/endpoint.rb', line 59 def predict(input_source, opts) check_api_key response = predict_req_post( input_source, opts ) if !response.nil? && response.respond_to?(:body) hashed_response = JSON.parse(response.body, object_class: Hash) return [hashed_response, response.body] if Mindee::HTTP::ResponseValidation.valid_sync_response?(response) Mindee::HTTP::ResponseValidation.clean_request!(response) end raise Error::MindeeError, 'Could not resolve server response.' if response.nil? error = Mindee::HTTP::ErrorHandler.handle_error(@url_name, response) raise error end |
#predict_async(input_source, opts) ⇒ Array
Call the prediction API.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mindee/v1/http/endpoint.rb', line 82 def predict_async(input_source, opts) check_api_key response = document_queue_req_post(input_source, opts) if !response.nil? && response.respond_to?(:body) hashed_response = JSON.parse(response.body, object_class: Hash) return [hashed_response, response.body] if Mindee::HTTP::ResponseValidation.valid_async_response?(response) Mindee::HTTP::ResponseValidation.clean_request!(response) end raise Error::MindeeError, 'Could not resolve server response.' if response.nil? raise Mindee::HTTP::ErrorHandler.handle_error(@url_name, response) end |
#predict_req_post(input_source, opts) ⇒ Net::HTTPResponse?
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mindee/v1/http/endpoint.rb', line 116 def predict_req_post(input_source, opts) uri = URI("#{@url_root}/predict") params = {} # : Hash[String | Symbol, untyped] params[:cropper] = 'true' if opts.cropper params[:full_text_ocr] = 'true' if opts.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]] # : Array[untyped] else [['document', *input_source.read_contents(close: opts.close_file)]] # : Array[untyped] end form_data.push ['include_mvision', 'true'] if opts.all_words req.set_form(form_data, 'multipart/form-data') req['Transfer-Encoding'] = 'chunked' Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http| return http.request(req) end raise Mindee::Error::MindeeError, 'Could not resolve server response.' end |