Class: Everywhere::Platform::Client
- Inherits:
-
Object
- Object
- Everywhere::Platform::Client
- Defined in:
- lib/everywhere/platform/client.rb
Defined Under Namespace
Classes: Response
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.
36 37 38 39 40 |
# File 'lib/everywhere/platform/client.rb', line 36 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.
34 35 36 |
# File 'lib/everywhere/platform/client.rb', line 34 def base_url @base_url end |
Class Method Details
.base_url(explicit = nil) ⇒ Object
30 31 32 |
# File 'lib/everywhere/platform/client.rb', line 30 def self.base_url(explicit = nil) (explicit || ENV["EVERYWHERE_PLATFORM_URL"] || DEFAULT_URL).chomp("/") end |
Instance Method Details
#delete(path) ⇒ Object
45 |
# File 'lib/everywhere/platform/client.rb', line 45 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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/everywhere/platform/client.rb', line 67 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 end |
#get(path) ⇒ Object
44 |
# File 'lib/everywhere/platform/client.rb', line 44 def get(path) = request(Net::HTTP::Get, path) |
#patch(path, body = {}) ⇒ Object
43 |
# File 'lib/everywhere/platform/client.rb', line 43 def patch(path, body = {}) = request(Net::HTTP::Patch, path, body) |
#post(path, body = {}) ⇒ Object
42 |
# File 'lib/everywhere/platform/client.rb', line 42 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.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/everywhere/platform/client.rb', line 50 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 SocketError, Errno::ECONNREFUSED, Errno::ETIMEDOUT => e raise Error, "upload to #{uri.host} failed (#{e.class})" end |