Class: Hcloud::Client
- Inherits:
-
Object
- Object
- Hcloud::Client
- Defined in:
- lib/hcloud/client.rb
Defined Under Namespace
Classes: ResourceFuture
Constant Summary collapse
- MAX_ENTRIES_PER_PAGE =
50
Class Attribute Summary collapse
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.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #actions ⇒ Object
- #authorized? ⇒ Boolean
- #concurrent ⇒ Object
- #concurrent? ⇒ Boolean
- #datacenters ⇒ Object
- #floating_ips ⇒ Object
- #images ⇒ Object
-
#initialize(token:, auto_pagination: false, concurrency: 20, user_agent: nil) ⇒ Client
constructor
A new instance of Client.
- #isos ⇒ Object
- #locations ⇒ Object
- #networks ⇒ Object
- #prepare_request(url, **args, &block) ⇒ Object
-
#request(path, **options) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #server_types ⇒ Object
- #servers ⇒ Object
- #ssh_keys ⇒ Object
- #volumes ⇒ Object
Constructor Details
#initialize(token:, auto_pagination: false, concurrency: 20, user_agent: nil) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 |
# File 'lib/hcloud/client.rb', line 21 def initialize(token:, auto_pagination: false, concurrency: 20, user_agent: nil) @token = token @user_agent = user_agent || "hcloud-ruby v#{VERSION}" @auto_pagination = auto_pagination @concurrency = concurrency @hydra = Typhoeus::Hydra.new(max_concurrency: concurrency) end |
Class Attribute Details
Instance Attribute Details
#auto_pagination ⇒ Object (readonly)
Returns the value of attribute auto_pagination.
20 21 22 |
# File 'lib/hcloud/client.rb', line 20 def auto_pagination @auto_pagination end |
#hydra ⇒ Object (readonly)
Returns the value of attribute hydra.
20 21 22 |
# File 'lib/hcloud/client.rb', line 20 def hydra @hydra end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
20 21 22 |
# File 'lib/hcloud/client.rb', line 20 def token @token end |
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
20 21 22 |
# File 'lib/hcloud/client.rb', line 20 def user_agent @user_agent end |
Instance Method Details
#actions ⇒ Object
58 59 60 |
# File 'lib/hcloud/client.rb', line 58 def actions ActionResource.new(client: self) end |
#authorized? ⇒ Boolean
47 48 49 50 51 52 |
# File 'lib/hcloud/client.rb', line 47 def request('server_types').run true rescue Error::Unauthorized false end |
#concurrent ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/hcloud/client.rb', line 29 def concurrent @concurrent = true ret = yield ret.each do |element| next unless element.is_a?(AbstractResource) element.run end hydra.run ret ensure @concurrent = nil end |
#concurrent? ⇒ Boolean
43 44 45 |
# File 'lib/hcloud/client.rb', line 43 def concurrent? !@concurrent.nil? end |
#datacenters ⇒ Object
70 71 72 |
# File 'lib/hcloud/client.rb', line 70 def datacenters DatacenterResource.new(client: self) end |
#floating_ips ⇒ Object
86 87 88 |
# File 'lib/hcloud/client.rb', line 86 def floating_ips FloatingIPResource.new(client: self) end |
#images ⇒ Object
66 67 68 |
# File 'lib/hcloud/client.rb', line 66 def images ImageResource.new(client: self) end |
#isos ⇒ Object
62 63 64 |
# File 'lib/hcloud/client.rb', line 62 def isos IsoResource.new(client: self) end |
#locations ⇒ Object
74 75 76 |
# File 'lib/hcloud/client.rb', line 74 def locations LocationResource.new(client: self) end |
#networks ⇒ Object
90 91 92 |
# File 'lib/hcloud/client.rb', line 90 def networks NetworkResource.new(client: self) end |
#prepare_request(url, **args, &block) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/hcloud/client.rb', line 108 def prepare_request(url, **args, &block) req = request(url, **args.merge(block: block)) return req.run.resource unless concurrent? hydra.queue req ResourceFuture.new(req) end |
#request(path, **options) ⇒ Object
rubocop:disable Metrics/MethodLength
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/hcloud/client.rb', line 116 def request(path, **) # rubocop:disable Metrics/MethodLength hcloud_attributes = TyphoeusExt.collect_attributes() 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}", 'User-Agent' => user_agent, 'Content-Type' => 'application/json' } }.merge() ) r.on_complete do |response| response.extend(TyphoeusExt) response.attributes = hcloud_attributes response.context.client = self response.check_for_error unless response.request.hydra end r end |
#server_types ⇒ Object
78 79 80 |
# File 'lib/hcloud/client.rb', line 78 def server_types ServerTypeResource.new(client: self) end |
#servers ⇒ Object
54 55 56 |
# File 'lib/hcloud/client.rb', line 54 def servers ServerResource.new(client: self) end |
#ssh_keys ⇒ Object
82 83 84 |
# File 'lib/hcloud/client.rb', line 82 def ssh_keys SSHKeyResource.new(client: self) end |
#volumes ⇒ Object
94 95 96 |
# File 'lib/hcloud/client.rb', line 94 def volumes VolumeResource.new(client: self) end |