12
13
14
15
16
17
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
|
# File 'lib/browserstack/fetch_download_source_url.rb', line 12
def self.call(auth_token:, user_agent:, fallback: false, error_message: nil,
proxy_host: nil, proxy_port: nil)
uri = URI::HTTPS.build(host: BS_HOST, path: ENDPOINT_PATH)
body = { 'auth_token' => auth_token }
body['error_message'] = error_message if fallback && error_message
http_class = if proxy_host && proxy_port
Net::HTTP::Proxy(proxy_host, proxy_port.to_i)
else
Net::HTTP
end
http = http_class.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.open_timeout = 10
http.read_timeout = 15
req = Net::HTTP::Post.new(uri.request_uri)
req['Content-Type'] = 'application/json'
req['User-Agent'] = user_agent
req['X-Local-Fallback-Cloudflare'] = 'true' if fallback
req.body = JSON.dump(body)
res = http.request(req)
begin
parsed = JSON.parse(res.body.to_s)
rescue JSON::ParserError => e
raise BrowserStack::LocalException.new(
"Failed to parse binary endpoint API response (HTTP #{res.code}): #{e.message}"
)
end
if parsed.is_a?(Hash) && parsed['error']
raise BrowserStack::LocalException.new(
"Binary endpoint API returned error: #{parsed['error']}"
)
end
endpoint = parsed.is_a?(Hash) ? parsed.dig('data', 'endpoint') : nil
if endpoint.nil? || endpoint.to_s.empty?
raise BrowserStack::LocalException.new(
"Binary endpoint API returned no endpoint (HTTP #{res.code})"
)
end
endpoint
end
|