Class: Lambda::MicroVMs::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda/microvms/endpoint.rb

Overview

HTTP client for a single Lambda MicroVM endpoint.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, token:, http: Net::HTTP) ⇒ Endpoint

Returns a new instance of Endpoint.



13
14
15
16
17
# File 'lib/lambda/microvms/endpoint.rb', line 13

def initialize(url:, token:, http: Net::HTTP)
  @url = url
  @token = token
  @http = http
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/lambda/microvms/endpoint.rb', line 11

def url
  @url
end

Instance Method Details

#get(path, headers: {}) ⇒ Object



19
20
21
# File 'lib/lambda/microvms/endpoint.rb', line 19

def get(path, headers: {})
  request(Net::HTTP::Get, path, headers:)
end

#post(path, json: nil, body: nil, headers: {}) ⇒ Object



23
24
25
# File 'lib/lambda/microvms/endpoint.rb', line 23

def post(path, json: nil, body: nil, headers: {})
  request(Net::HTTP::Post, path, json:, body:, headers:)
end

#request(klass, path, json: nil, body: nil, headers: {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lambda/microvms/endpoint.rb', line 27

def request(klass, path, json: nil, body: nil, headers: {})
  response = @http.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(build_request(klass, path, headers:, body:, json:))
  end

  success = response.code.to_i.between?(200, 299)
  unless success
    raise EndpointError.new("MicroVM endpoint returned HTTP #{response.code}", status: response.code.to_i,
                                                                               body: response.body)
  end

  parse_response(response)
end