Class: Everywhere::Platform::Client
- Inherits:
-
Object
- Object
- Everywhere::Platform::Client
- Defined in:
- lib/everywhere/platform/client.rb
Defined Under Namespace
Classes: Response
Constant Summary collapse
- NETWORK_ERRORS =
Everything a flaky link, a hung host or a TLS-intercepting proxy can throw at us. OpenSSL is an optional stdlib (net/http requires it best-effort), so its error class is only listed when it loaded.
[ SocketError, EOFError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::EPIPE, Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout, *(defined?(OpenSSL::SSL::SSLError) ? [OpenSSL::SSL::SSLError] : []) ].freeze
- OPEN_TIMEOUT =
15
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Class Method Summary collapse
Instance Method Summary collapse
- #delete(path) ⇒ Object
-
#download(url, dest, redirects: 5) ⇒ Object
GET a (possibly redirecting, signed) URL and stream it to dest.
- #get(path) ⇒ Object
-
#initialize(base_url, token: nil) ⇒ Client
constructor
A new instance of Client.
- #patch(path, body = {}) ⇒ Object
- #post(path, body = {}) ⇒ Object
-
#put_file(url, path, headers: {}) ⇒ Object
Stream a file with a raw PUT (Active Storage direct upload).
Constructor Details
#initialize(base_url, token: nil) ⇒ Client
Returns a new instance of Client.
54 55 56 57 58 |
# File 'lib/everywhere/platform/client.rb', line 54 def initialize(base_url, token: nil) @base_url = base_url @base = URI.parse("#{base_url}/") @token = token end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
52 53 54 |
# File 'lib/everywhere/platform/client.rb', line 52 def base_url @base_url end |
Class Method Details
.base_url(explicit = nil) ⇒ Object
48 49 50 |
# File 'lib/everywhere/platform/client.rb', line 48 def self.base_url(explicit = nil) (explicit || ENV["EVERYWHERE_PLATFORM_URL"] || DEFAULT_URL).chomp("/") end |
Instance Method Details
#delete(path) ⇒ Object
63 |
# File 'lib/everywhere/platform/client.rb', line 63 def delete(path) = request(Net::HTTP::Delete, path) |
#download(url, dest, redirects: 5) ⇒ Object
GET a (possibly redirecting, signed) URL and stream it to dest. Sends the bearer token only to the platform host (the first hop when the URL is a /runner or /cli endpoint); a redirect to a signed blob/S3 URL is followed without it.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/everywhere/platform/client.rb', line 85 def download(url, dest, redirects: 5) uri = URI.parse(url) req = Net::HTTP::Get.new(uri) req["Authorization"] = "Bearer #{@token}" if @token && uri.host == @base.host && uri.port == @base.port http(uri) do |conn| conn.request(req) do |res| case res when Net::HTTPRedirection raise Error, "too many redirects downloading #{url}" if redirects <= 0 return download(res["location"], dest, redirects: redirects - 1) when Net::HTTPSuccess File.open(dest, "wb") { |f| res.read_body { |chunk| f.write(chunk) } } else raise Error, "download failed (HTTP #{res.code})" end end end dest rescue *NETWORK_ERRORS => e raise Error, "download from #{uri.host} failed (#{e.class}: #{e.})" end |
#get(path) ⇒ Object
62 |
# File 'lib/everywhere/platform/client.rb', line 62 def get(path) = request(Net::HTTP::Get, path) |
#patch(path, body = {}) ⇒ Object
61 |
# File 'lib/everywhere/platform/client.rb', line 61 def patch(path, body = {}) = request(Net::HTTP::Patch, path, body) |
#post(path, body = {}) ⇒ Object
60 |
# File 'lib/everywhere/platform/client.rb', line 60 def post(path, body = {}) = request(Net::HTTP::Post, path, body) |
#put_file(url, path, headers: {}) ⇒ Object
Stream a file with a raw PUT (Active Storage direct upload). url is
absolute (from the reservation); headers come from the reservation too.
Returns the Net::HTTP response.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/everywhere/platform/client.rb', line 68 def put_file(url, path, headers: {}) uri = URI.parse(url) req = Net::HTTP::Put.new(uri) headers.each { |k, v| req[k] = v } req["Content-Length"] = File.size(path).to_s File.open(path, "rb") do |io| req.body_stream = io http(uri) { |conn| conn.request(req) } end rescue *NETWORK_ERRORS => e raise Error, "upload to #{uri.host} failed (#{e.class}: #{e.})" end |