Class: Hcloud::Client
- Inherits:
-
Object
- Object
- Hcloud::Client
- Defined in:
- lib/hcloud/client.rb
Constant Summary collapse
- MAX_ENTRIES_PER_PAGE =
50
Instance Attribute Summary collapse
-
#auto_pagination ⇒ Object
readonly
Returns the value of attribute auto_pagination.
-
#hydra ⇒ Object
readonly
Returns the value of attribute hydra.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #actions ⇒ Object
- #authorized? ⇒ Boolean
- #datacenters ⇒ Object
- #floating_ips ⇒ Object
- #images ⇒ Object
-
#initialize(token:, auto_pagination: false, concurrency: 20) ⇒ Client
constructor
A new instance of Client.
- #isos ⇒ Object
- #locations ⇒ Object
- #request(path, **options) ⇒ Object
- #server_types ⇒ Object
- #servers ⇒ Object
- #ssh_keys ⇒ Object
Constructor Details
#initialize(token:, auto_pagination: false, concurrency: 20) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 |
# File 'lib/hcloud/client.rb', line 9 def initialize(token:, auto_pagination: false, concurrency: 20) @token = token @auto_pagination = auto_pagination @concurrency = concurrency @hydra = Typhoeus::Hydra.new(max_concurrency: concurrency) end |
Instance Attribute Details
#auto_pagination ⇒ Object (readonly)
Returns the value of attribute auto_pagination.
8 9 10 |
# File 'lib/hcloud/client.rb', line 8 def auto_pagination @auto_pagination end |
#hydra ⇒ Object (readonly)
Returns the value of attribute hydra.
8 9 10 |
# File 'lib/hcloud/client.rb', line 8 def hydra @hydra end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
8 9 10 |
# File 'lib/hcloud/client.rb', line 8 def token @token end |
Instance Method Details
#actions ⇒ Object
27 28 29 |
# File 'lib/hcloud/client.rb', line 27 def actions ActionResource.new(client: self) end |
#authorized? ⇒ Boolean
16 17 18 19 20 21 |
# File 'lib/hcloud/client.rb', line 16 def request("server_types").run true rescue Error:: false end |
#datacenters ⇒ Object
39 40 41 |
# File 'lib/hcloud/client.rb', line 39 def datacenters DatacenterResource.new(client: self) end |
#floating_ips ⇒ Object
55 56 57 |
# File 'lib/hcloud/client.rb', line 55 def floating_ips FloatingIPResource.new(client: self) end |
#images ⇒ Object
35 36 37 |
# File 'lib/hcloud/client.rb', line 35 def images ImageResource.new(client: self) end |
#isos ⇒ Object
31 32 33 |
# File 'lib/hcloud/client.rb', line 31 def isos IsoResource.new(client: self) end |
#locations ⇒ Object
43 44 45 |
# File 'lib/hcloud/client.rb', line 43 def locations LocationResource.new(client: self) end |
#request(path, **options) ⇒ Object
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/hcloud/client.rb', line 59 def request(path, **) code = .delete(:code) if x = .delete(:j) [:body] = Oj.dump(x, mode: :compat) [:method] ||= :post end q = [] q << .delete(:ep).to_s if x = .delete(:q) q << x.to_param end path = path.dup path << "?"+q.join("&") r = Typhoeus::Request.new( "https://api.hetzner.cloud/v1/#{path}", { headers: { "Authorization" => "Bearer #{token}", "Content-Type" => "application/json", }, }.merge() ) r.on_complete do |response| case response.code when code raise Error::UnexpectedError, response.body when 401 raise Error:: when 0 raise Error::ServerError, "Connection error: #{response.return_code}" when 400...600 j = Oj.load(response.body) code = j.dig("error", "code") error_class = case code when "invalid_input" then Error::InvalidInput when "forbidden" then Error::Forbidden when "locked" then Error::Locked when "not_found" then Error::NotFound when "rate_limit_exceeded" then Error::RateLimitExceeded when "resource_unavailable" then Error::ResourceUnavilable when "service_error" then Error::ServiceError when "uniqueness_error" then Error::UniquenessError else Error::ServerError end raise error_class, j.dig("error", "message") end end r end |
#server_types ⇒ Object
47 48 49 |
# File 'lib/hcloud/client.rb', line 47 def server_types ServerTypeResource.new(client: self) end |
#servers ⇒ Object
23 24 25 |
# File 'lib/hcloud/client.rb', line 23 def servers ServerResource.new(client: self) end |
#ssh_keys ⇒ Object
51 52 53 |
# File 'lib/hcloud/client.rb', line 51 def ssh_keys SSHKeyResource.new(client: self) end |