Class: VagrantPlugins::DigitalOcean::Helpers::ApiClient
- Inherits:
-
Object
- Object
- VagrantPlugins::DigitalOcean::Helpers::ApiClient
- Includes:
- Vagrant::Util::Retryable
- Defined in:
- lib/vagrant-digitalocean/helpers/client.rb
Instance Method Summary collapse
- #delete(path, params = {}, method = :delete) ⇒ Object
-
#initialize(machine) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #post(path, params = {}, method = :post) ⇒ Object
- #request(path, params = {}, method = :get) ⇒ Object
- #wait_for_event(env, id) ⇒ Object
Constructor Details
#initialize(machine) ⇒ ApiClient
Returns a new instance of ApiClient.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/vagrant-digitalocean/helpers/client.rb', line 17 def initialize(machine) @logger = Log4r::Logger.new('vagrant::digitalocean::apiclient') @config = machine.provider_config @client = Faraday.new({ :url => 'https://api.digitalocean.com/', :ssl => { :ca_file => @config.ca_path } }) end |
Instance Method Details
#delete(path, params = {}, method = :delete) ⇒ Object
28 29 30 31 |
# File 'lib/vagrant-digitalocean/helpers/client.rb', line 28 def delete(path, params = {}, method = :delete) @client.request :url_encoded request(path, params, :delete) end |
#post(path, params = {}, method = :post) ⇒ Object
33 34 35 36 |
# File 'lib/vagrant-digitalocean/helpers/client.rb', line 33 def post(path, params = {}, method = :post) @client.headers['Content-Type'] = 'application/json' request(path, params, :post) end |
#request(path, params = {}, method = :get) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/vagrant-digitalocean/helpers/client.rb', line 38 def request(path, params = {}, method = :get) begin @logger.info "Request: #{path}" result = @client.send(method) do |req| req.url path, params req.headers['Authorization'] = "Bearer #{@config.token}" end rescue Faraday::Error::ConnectionFailed => e # TODO this is suspect but because farady wraps the exception # in something generic there doesn't appear to be another # way to distinguish different connection errors :( if e. =~ /certificate verify failed/ raise Errors::CertificateError end raise e end unless method == :delete begin body = JSON.parse(result.body) @logger.info "Response: #{body}" next_page = body["links"]["pages"]["next"] rescue nil unless next_page.nil? uri = URI.parse(next_page) new_path = path.split("?")[0] next_result = self.request("#{new_path}?#{uri.query}") req_target = new_path.split("/")[-1] body["#{req_target}"].concat(next_result["#{req_target}"]) end rescue JSON::ParserError => e raise(Errors::JSONError, { :message => e., :path => path, :params => params, :response => result.body }) end end unless /^2\d\d$/ =~ result.status.to_s raise(Errors::APIStatusError, { :path => path, :params => params, :status => result.status, :response => body.inspect }) end Result.new(body) end |
#wait_for_event(env, id) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vagrant-digitalocean/helpers/client.rb', line 90 def wait_for_event(env, id) retryable(:tries => 120, :sleep => 10) do # stop waiting if interrupted next if env[:interrupted] # check action status result = self.request("/v2/actions/#{id}") yield result if block_given? raise 'not ready' if result['action']['status'] != 'completed' end end |