diff --git a/lib/mindee/http/endpoint.rb b/lib/mindee/http/endpoint.rb index 53fc1766b..806524663 100644 --- a/lib/mindee/http/endpoint.rb +++ b/lib/mindee/http/endpoint.rb @@ -128,6 +128,8 @@ def predict_req_post(input_source, opts) 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 @@ -163,6 +165,7 @@ def document_queue_req_post(input_source, opts) 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) diff --git a/lib/mindee/http/workflow_endpoint.rb b/lib/mindee/http/workflow_endpoint.rb index 41edb2f13..1c239ce20 100644 --- a/lib/mindee/http/workflow_endpoint.rb +++ b/lib/mindee/http/workflow_endpoint.rb @@ -69,6 +69,7 @@ def workflow_execution_req_post(input_source, opts) form_data.push ['priority', opts.priority.to_s] if opts.priority req.set_form(form_data, 'multipart/form-data') + req['Transfer-Encoding'] = 'chunked' response = nil Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http| diff --git a/sig/custom/net_http.rbs b/sig/custom/net_http.rbs index fa179ec62..5bfa33318 100644 --- a/sig/custom/net_http.rbs +++ b/sig/custom/net_http.rbs @@ -15,6 +15,7 @@ module Net def initialize: (untyped, Hash[String, String]?) -> void def set_form: (untyped, String?) -> void def new: (untyped, untyped) -> void + def []=: (?untyped, ?untyped) -> bool end # Stub for the HTTP GET request class. diff --git a/spec/input/sources/url_input_source_integration.rb b/spec/input/sources/url_input_source_integration.rb index 9f8d25cd0..e66d137d3 100644 --- a/spec/input/sources/url_input_source_integration.rb +++ b/spec/input/sources/url_input_source_integration.rb @@ -3,9 +3,9 @@ require 'mindee' describe Mindee::Input::Source::URLInputSource do + let(:client) { Mindee::Client.new(api_key: ENV.fetch('MINDEE_API_KEY')) } + it 'retrieves response from a remote file' do - api_key = ENV.fetch('MINDEE_API_KEY', nil) - client = Mindee::Client.new(api_key: api_key) remote_input = Mindee::Input::Source::URLInputSource.new('https://github.com/mindee/client-lib-test-data/blob/main/products/invoice_splitter/invoice_5p.pdf?raw=true') local_input = remote_input.as_local_input_source @@ -14,4 +14,17 @@ result = client.parse(local_input, Mindee::Product::Invoice::InvoiceV4) expect(result.document.n_pages).to eq(5) end + + it 'streams with chunked transfer‐encoding without creating temp files' do + remote_input = Mindee::Input::Source::URLInputSource + .new('https://upload.wikimedia.org/wikipedia/commons/1/1d/Blank_Page.pdf') + allow(Tempfile).to receive(:new).and_call_original + allow(Tempfile).to receive(:create).and_call_original + + result = client.parse(remote_input, Mindee::Product::Invoice::InvoiceV4) + + expect(result.document.n_pages).to eq(1) + expect(Tempfile).not_to have_received(:new) + expect(Tempfile).not_to have_received(:create) + end end