Sha256: d83e43f0b3af5cada2964417279b3748680a52cd71769fb24d256a0b28fd51e6
Contents?: true
Size: 1.93 KB
Versions: 2
Compression:
Stored size: 1.93 KB
Contents
autoload :Typhoeus, "typhoeus" autoload :Oj, "oj" module Hcloud class Client attr_reader :token def initialize(token:) @token = token end def authorized? request("server_types").run true rescue Error::Unauthorized false end def servers ServerResource.new(client: self) end def request(path, **options) code = options.delete(:code) if x = options.delete(:j) options[:body] = Oj.dump(x, mode: :compat) options[:method] ||= :post end r = Typhoeus::Request.new( "https://api.hetzner.cloud/v1/#{path}", { headers: { "Authorization" => "Bearer #{token}", "Content-Type" => "application/json", }, }.merge(options) ) r.on_complete do |response| case response.code when code raise Error::UnexpectedError, response.body when 401 raise Error::Unauthorized 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 end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
hcloud-0.1.0.pre.alpha1 | lib/hcloud/client.rb |
hcloud-0.1.0.pre.alpha0 | lib/hcloud/client.rb |