Class: VagrantPlugins::DigitalOcean::Helpers::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-digitalocean/helpers/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



9
10
11
# File 'lib/vagrant-digitalocean/helpers/client.rb', line 9

def initialize
  @client = Faraday.new(:url => "https://api.digitalocean.com/")
end

Instance Method Details

#request(path, params = {}) ⇒ Object



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
# File 'lib/vagrant-digitalocean/helpers/client.rb', line 13

def request(path, params = {})
  # create the key
  result = @client.get(path, params = params.merge({
    :client_id => ENV["DO_CLIENT_ID"],
    :api_key => ENV["DO_API_KEY"]
  }))

  # remove the api key in case an error gets dumped to the console
  params[:api_key] = "REMOVED"

  begin
    body = JSON.parse(result.body)
  rescue JSON::ParserError => e
    raise(Errors::JSONError, {
      :message => e.message,
      :path => path,
      :params => params,
      :response => result.body
    })
  end

  if body["status"] != "OK"
    raise(Errors::APIStatusError, {
      :path => path,
      :params => params,
      :status => body["status"],
      :response => body.inspect
    })
  end

  Result.new(body)
end