Class: Mindee::HTTP::Endpoint
- Inherits:
-
Object
- Object
- Mindee::HTTP::Endpoint
- Defined in:
- lib/mindee/http/endpoint.rb
Overview
Generic API endpoint for a product.
Instance Attribute Summary collapse
- #api_key ⇒ String readonly
- #request_timeout ⇒ Integer readonly
- #url_root ⇒ String readonly
Instance Method Summary collapse
-
#initialize(owner, url_name, version, api_key: '') ⇒ Endpoint
constructor
A new instance of Endpoint.
-
#parse_async(job_id) ⇒ Array
Calls the parsed async doc.
-
#predict(input_source, all_words, full_text, close_file, cropper) ⇒ Array
Call the prediction API.
-
#predict_async(input_source, all_words, full_text, close_file, cropper) ⇒ Array
Call the prediction API.
Constructor Details
#initialize(owner, url_name, version, api_key: '') ⇒ Endpoint
Returns a new instance of Endpoint.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mindee/http/endpoint.rb', line 38 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) @url_root = "#{base_url.chomp('/')}/products/#{@owner}/#{@url_name}/v#{@version}" end |
Instance Attribute Details
#api_key ⇒ String (readonly)
32 33 34 |
# File 'lib/mindee/http/endpoint.rb', line 32 def api_key @api_key end |
#request_timeout ⇒ Integer (readonly)
34 35 36 |
# File 'lib/mindee/http/endpoint.rb', line 34 def request_timeout @request_timeout end |
#url_root ⇒ String (readonly)
36 37 38 |
# File 'lib/mindee/http/endpoint.rb', line 36 def url_root @url_root end |
Instance Method Details
#parse_async(job_id) ⇒ Array
Calls the parsed async doc.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mindee/http/endpoint.rb', line 101 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 = ErrorHandler.handle_error(@url_name, response) raise error end |
#predict(input_source, all_words, full_text, close_file, cropper) ⇒ Array
Call the prediction API.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mindee/http/endpoint.rb', line 58 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 ) if !response.nil? && response.respond_to?(:body) hashed_response = JSON.parse(response.body, object_class: Hash) return [hashed_response, response.body] if ResponseValidation.valid_sync_response?(response) ResponseValidation.clean_request!(response) end error = ErrorHandler.handle_error(@url_name, response) raise error end |
#predict_async(input_source, all_words, full_text, close_file, cropper) ⇒ Array
Call the prediction API.
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/mindee/http/endpoint.rb', line 84 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) if !response.nil? && response.respond_to?(:body) hashed_response = JSON.parse(response.body, object_class: Hash) return [hashed_response, response.body] if ResponseValidation.valid_async_response?(response) ResponseValidation.clean_request!(response) end error = ErrorHandler.handle_error(@url_name, response) raise error end |