Skip to content

Commit edb4163

Browse files
♻️ switch to 'Transfer-Encoding: chunked' to prevent Net::HTTP from writing temporary files (#189)
1 parent 98c4ba4 commit edb4163

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

lib/mindee/http/endpoint.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def predict_req_post(input_source, opts)
128128
form_data.push ['include_mvision', 'true'] if opts.all_words
129129

130130
req.set_form(form_data, 'multipart/form-data')
131+
req['Transfer-Encoding'] = 'chunked'
132+
131133
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http|
132134
return http.request(req)
133135
end
@@ -163,6 +165,7 @@ def document_queue_req_post(input_source, opts)
163165
form_data.push ['include_mvision', 'true'] if opts.all_words
164166

165167
req.set_form(form_data, 'multipart/form-data')
168+
req['Transfer-Encoding'] = 'chunked'
166169

167170
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http|
168171
return http.request(req)

lib/mindee/http/workflow_endpoint.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def workflow_execution_req_post(input_source, opts)
6969
form_data.push ['priority', opts.priority.to_s] if opts.priority
7070

7171
req.set_form(form_data, 'multipart/form-data')
72+
req['Transfer-Encoding'] = 'chunked'
7273

7374
response = nil
7475
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http|

sig/custom/net_http.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Net
1515
def initialize: (untyped, Hash[String, String]?) -> void
1616
def set_form: (untyped, String?) -> void
1717
def new: (untyped, untyped) -> void
18+
def []=: (?untyped, ?untyped) -> bool
1819
end
1920

2021
# Stub for the HTTP GET request class.

spec/input/sources/url_input_source_integration.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
require 'mindee'
44

55
describe Mindee::Input::Source::URLInputSource do
6+
let(:client) { Mindee::Client.new(api_key: ENV.fetch('MINDEE_API_KEY')) }
7+
68
it 'retrieves response from a remote file' do
7-
api_key = ENV.fetch('MINDEE_API_KEY', nil)
8-
client = Mindee::Client.new(api_key: api_key)
99
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')
1010

1111
local_input = remote_input.as_local_input_source
@@ -14,4 +14,17 @@
1414
result = client.parse(local_input, Mindee::Product::Invoice::InvoiceV4)
1515
expect(result.document.n_pages).to eq(5)
1616
end
17+
18+
it 'streams with chunked transfer‐encoding without creating temp files' do
19+
remote_input = Mindee::Input::Source::URLInputSource
20+
.new('https://upload.wikimedia.org/wikipedia/commons/1/1d/Blank_Page.pdf')
21+
allow(Tempfile).to receive(:new).and_call_original
22+
allow(Tempfile).to receive(:create).and_call_original
23+
24+
result = client.parse(remote_input, Mindee::Product::Invoice::InvoiceV4)
25+
26+
expect(result.document.n_pages).to eq(1)
27+
expect(Tempfile).not_to have_received(:new)
28+
expect(Tempfile).not_to have_received(:create)
29+
end
1730
end

0 commit comments

Comments
 (0)