@@ -34,6 +34,8 @@ class Endpoint
3434 attr_reader :request_timeout
3535 # @return [String]
3636 attr_reader :url_root
37+ # @return [String]
38+ attr_reader :base_url
3739
3840 def initialize ( owner , url_name , version , api_key : '' )
3941 @owner = owner
@@ -44,25 +46,19 @@ def initialize(owner, url_name, version, api_key: '')
4446 logger . debug ( 'API key set from environment' )
4547 end
4648 @api_key = api_key . nil? || api_key . empty? ? ENV . fetch ( API_KEY_ENV_NAME , API_KEY_DEFAULT ) : api_key
47- base_url = ENV . fetch ( BASE_URL_ENV_NAME , BASE_URL_DEFAULT )
48- @url_root = "#{ base_url . chomp ( '/' ) } /products/#{ @owner } /#{ @url_name } /v#{ @version } "
49+ @ base_url = ENV . fetch ( BASE_URL_ENV_NAME , BASE_URL_DEFAULT ) . chomp ( '/' )
50+ @url_root = "#{ @ base_url} /products/#{ @owner } /#{ @url_name } /v#{ @version } "
4951 end
5052
5153 # Call the prediction API.
5254 # @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
53- # @param all_words [bool] Whether the full word extraction needs to be performed
54- # @param full_text [bool] Whether to include the full OCR text response in compatible APIs
55- # @param close_file [bool] Whether the file will be closed after reading
56- # @param cropper [bool] Whether a cropping operation will be applied
55+ # @param opts [ParseOptions] Parse options.
5756 # @return [Array]
58- def predict ( input_source , all_words , full_text , close_file , cropper )
57+ def predict ( input_source , opts )
5958 check_api_key
6059 response = predict_req_post (
6160 input_source ,
62- all_words : all_words ,
63- full_text : full_text ,
64- close_file : close_file ,
65- cropper : cropper
61+ opts
6662 )
6763 if !response . nil? && response . respond_to? ( :body )
6864 hashed_response = JSON . parse ( response . body , object_class : Hash )
@@ -76,14 +72,11 @@ def predict(input_source, all_words, full_text, close_file, cropper)
7672
7773 # Call the prediction API.
7874 # @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
79- # @param all_words [bool] Whether the full word extraction needs to be performed
80- # @param full_text [bool] Whether to include the full OCR text response in compatible APIs.
81- # @param close_file [bool] Whether the file will be closed after reading
82- # @param cropper [bool] Whether a cropping operation will be applied
75+ # @param opts [ParseOptions, Hash] Parse options.
8376 # @return [Array]
84- def predict_async ( input_source , all_words , full_text , close_file , cropper )
77+ def predict_async ( input_source , opts )
8578 check_api_key
86- response = document_queue_req_get ( input_source , all_words , full_text , close_file , cropper )
79+ response = document_queue_req_post ( input_source , opts )
8780 if !response . nil? && response . respond_to? ( :body )
8881 hashed_response = JSON . parse ( response . body , object_class : Hash )
8982 return [ hashed_response , response . body ] if ResponseValidation . valid_async_response? ( response )
@@ -100,7 +93,7 @@ def predict_async(input_source, all_words, full_text, close_file, cropper)
10093 # @return [Array]
10194 def parse_async ( job_id )
10295 check_api_key
103- response = document_queue_req ( job_id )
96+ response = document_queue_req_get ( job_id )
10497 hashed_response = JSON . parse ( response . body , object_class : Hash )
10598 return [ hashed_response , response . body ] if ResponseValidation . valid_async_response? ( response )
10699
@@ -112,17 +105,14 @@ def parse_async(job_id)
112105 private
113106
114107 # @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
115- # @param all_words [bool] Whether the full word extraction needs to be performed
116- # @param full_text [bool] Whether to include the full OCR text response in compatible APIs.
117- # @param close_file [bool] Whether the file will be closed after reading
118- # @param cropper [bool] Whether a cropping operation will be applied
108+ # @param opts [ParseOptions] Parse options.
119109 # @return [Net::HTTPResponse, nil]
120- def predict_req_post ( input_source , all_words : false , full_text : false , close_file : true , cropper : false )
110+ def predict_req_post ( input_source , opts )
121111 uri = URI ( "#{ @url_root } /predict" )
122112
123113 params = { } # : Hash[Symbol | String, untyped]
124- params [ :cropper ] = 'true' if cropper
125- params [ :full_text_ocr ] = 'true' if full_text
114+ params [ :cropper ] = 'true' if opts . cropper
115+ params [ :full_text_ocr ] = 'true' if opts . full_text
126116 uri . query = URI . encode_www_form ( params )
127117
128118 headers = {
@@ -131,32 +121,33 @@ def predict_req_post(input_source, all_words: false, full_text: false, close_fil
131121 }
132122 req = Net ::HTTP ::Post . new ( uri , headers )
133123 form_data = if input_source . is_a? ( Mindee ::Input ::Source ::URLInputSource )
134- [ [ 'document' , input_source . url ] ]
124+ [ [ 'document' , input_source . url ] ] # : Array[untyped]
135125 else
136- [ input_source . read_contents ( close : close_file ) ]
126+ [ input_source . read_contents ( close : opts . close_file ) ] # : Array[untyped ]
137127 end
138- form_data . push [ 'include_mvision' , 'true' ] if all_words
128+ form_data . push [ 'include_mvision' , 'true' ] if opts . all_words
139129
140130 req . set_form ( form_data , 'multipart/form-data' )
141- response = nil
142131 Net ::HTTP . start ( uri . hostname , uri . port , use_ssl : true , read_timeout : @request_timeout ) do |http |
143- response = http . request ( req )
132+ return http . request ( req )
144133 end
145- response
134+ raise Mindee :: Errors :: MindeeError , 'Could not resolve server response.'
146135 end
147136
148137 # @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
149- # @param all_words [bool] Whether the full word extraction needs to be performed
150- # @param full_text [bool] Whether to include the full OCR text response in compatible APIs.
151- # @param close_file [bool] Whether the file will be closed after reading
152- # @param cropper [bool] Whether a cropping operation will be applied
153- # @return [Net::HTTPResponse, nil]
154- def document_queue_req_get ( input_source , all_words , full_text , close_file , cropper )
155- uri = URI ( "#{ @url_root } /predict_async" )
138+ # @param opts [ParseOptions] Parse options.
139+ # @return [Net::HTTPResponse]
140+ def document_queue_req_post ( input_source , opts )
141+ uri = if opts . workflow_id
142+ URI ( "#{ @base_url } /workflows/#{ opts . workflow_id } /predict_async" )
143+ else
144+ URI ( "#{ @url_root } /predict_async" )
145+ end
156146
157147 params = { } # : Hash[Symbol | String, untyped]
158- params [ :cropper ] = 'true' if cropper
159- params [ :full_text_ocr ] = 'true' if full_text
148+ params [ :cropper ] = 'true' if opts . cropper
149+ params [ :full_text_ocr ] = 'true' if opts . full_text
150+ params [ :rag ] = 'true' if opts . rag
160151 uri . query = URI . encode_www_form ( params )
161152
162153 headers = {
@@ -165,24 +156,23 @@ def document_queue_req_get(input_source, all_words, full_text, close_file, cropp
165156 }
166157 req = Net ::HTTP ::Post . new ( uri , headers )
167158 form_data = if input_source . is_a? ( Mindee ::Input ::Source ::URLInputSource )
168- [ [ 'document' , input_source . url ] ]
159+ [ [ 'document' , input_source . url ] ] # : Array[untyped]
169160 else
170- [ input_source . read_contents ( close : close_file ) ]
161+ [ input_source . read_contents ( close : opts . close_file ) ] # : Array[untyped ]
171162 end
172- form_data . push [ 'include_mvision' , 'true' ] if all_words
163+ form_data . push [ 'include_mvision' , 'true' ] if opts . all_words
173164
174165 req . set_form ( form_data , 'multipart/form-data' )
175166
176- response = nil
177167 Net ::HTTP . start ( uri . hostname , uri . port , use_ssl : true , read_timeout : @request_timeout ) do |http |
178- response = http . request ( req )
168+ return http . request ( req )
179169 end
180- response
170+ raise Mindee :: Errors :: MindeeError , 'Could not resolve server response.'
181171 end
182172
183173 # @param job_id [String]
184174 # @return [Net::HTTPResponse, nil]
185- def document_queue_req ( job_id )
175+ def document_queue_req_get ( job_id )
186176 uri = URI ( "#{ @url_root } /documents/queue/#{ job_id } " )
187177
188178 headers = {
0 commit comments