Class: Cabal::Client
- Inherits:
-
Object
- Object
- Cabal::Client
- Defined in:
- lib/cabal/client.rb
Overview
A client interface for an upstream Cabal::API
Constant Summary collapse
- NotFoundError =
The requested upstream resource was not found
Class.new(StandardError)
Class.new(StandardError)
Instance Attribute Summary collapse
-
#api_base ⇒ String
readonly
The base URL for the v3 API.
Instance Method Summary collapse
-
#clusters ⇒ Array<String>
Retrieves a list of cluster names from the upstream API.
-
#get(endpoint) ⇒ Object
Does an HTTP GET against the upstream API for the provided endpoint.
-
#initialize(options = {}) ⇒ Client
constructor
Create a new client instance.
-
#private_key(cluster_name) ⇒ String
Retrieves a private SSH key from the upstream API.
-
#public_key(cluster_name) ⇒ String
Retrieves a public SSH key from teh upstream API.
Constructor Details
#initialize(options = {}) ⇒ Client
Create a new client instance
24 25 26 27 28 29 30 31 32 |
# File 'lib/cabal/client.rb', line 24 def initialize( = {}) @api_base = "#{[:api_base]}/v3" @access_key = [:access_key] @secret_key = [:secret_key] @connector = Faraday.new(url: api_base) do |faraday| faraday.request :url_encoded faraday.adapter Faraday.default_adapter end end |
Instance Attribute Details
#api_base ⇒ String (readonly)
Returns the base URL for the v3 API.
14 15 16 |
# File 'lib/cabal/client.rb', line 14 def api_base @api_base end |
Instance Method Details
#clusters ⇒ Array<String>
Retrieves a list of cluster names from the upstream API
36 37 38 |
# File 'lib/cabal/client.rb', line 36 def clusters get("clusters").map {|cluster| cluster['name']} end |
#get(endpoint) ⇒ Object
Does an HTTP GET against the upstream API for the provided endpoint
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cabal/client.rb', line 61 def get(endpoint) response = connector.get {|req| req.url endpoint req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' req.headers['Authorization'] = } case response.status when 200 Oj.load(response.body) when 404 raise Cabal::Client::NotFoundError when 401 raise Cabal::Client::UnauthorizedError end end |
#private_key(cluster_name) ⇒ String
Retrieves a private SSH key from the upstream API
43 44 45 |
# File 'lib/cabal/client.rb', line 43 def private_key(cluster_name) get("private-key/#{URI.encode(cluster_name)}")['private_ssh_key'] end |
#public_key(cluster_name) ⇒ String
Retrieves a public SSH key from teh upstream API
50 51 52 |
# File 'lib/cabal/client.rb', line 50 def public_key(cluster_name) get("key/#{URI.encode(cluster_name)}")['public_ssh_key'] end |