18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/fetch/client.rb', line 18
def fetch(resource, method:, headers:, body:, redirect:, redirected: false)
uri = URI.parse(resource)
req = Net::HTTP.const_get(method.capitalize).new(uri)
.each do |k, v|
req[k.to_s] = v.to_s
end
case body
when FormData
req.set_form body.map {|k, v|
if v.is_a?(File)
[k, v, {
filename: File.basename(v.path),
content_type: MiniMime.lookup_by_filename(v.path)&.content_type || 'application/octet-stream'
}]
else
[k, v]
end
}, 'multipart/form-data'
when URLSearchParams
req.set_form_data body.entries
else
req.body = body
end
res = pool.with_connection(uri) { _1.request(req) }
case res
when Net::HTTPRedirection
case redirect.to_s
when 'follow'
location = res['Location']
fetch(location, method:, headers:, body:, redirect:, redirected: true)
when 'error'
raise RedirectError, "redirected to #{res['Location']}"
when 'manual'
to_response(resource, res, redirected)
else
raise ArgumentError, "invalid redirect option: #{redirect.inspect}"
end
else
to_response(resource, res, redirected)
end
end
|