Sha256: 9c0d730281d5a5a97931fb5ad9ef62437f025df9ceef1acf2594ae120678db39

Contents?: true

Size: 931 Bytes

Versions: 33

Compression:

Stored size: 931 Bytes

Contents

# frozen_string_literal: true

module Hetzner
  class Client
    BASE_URI = 'https://api.hetzner.cloud/v1'

    attr_reader :token

    def initialize(token:)
      @token = token
    end

    def get(path)
      make_request do
        JSON.parse HTTParty.get(BASE_URI + path, headers: headers).body
      end
    end

    def post(path, data)
      make_request do
        HTTParty.post(BASE_URI + path, body: data.to_json, headers: headers)
      end
    end

    def delete(path, id)
      make_request do
        HTTParty.delete("#{BASE_URI}#{path}/#{id}", headers: headers)
      end
    end

    private

    def headers
      {
        'Authorization' => "Bearer #{@token}",
        'Content-Type' => 'application/json'
      }
    end

    def make_request(&block)
      retries ||= 0

      Timeout.timeout(30) do
        block.call
      end
    rescue Timeout::Error
      retry if (retries += 1) < 3
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
hetzner-k3s-0.6.2 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.2.pre1 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.1 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre29 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre28 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre27 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre26 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre25 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre24 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre23 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre22 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre21 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre20 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre19 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre18 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre17 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre16 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre15 lib/hetzner/infra/client.rb
hetzner-k3s-0.6.0.pre14 lib/hetzner/infra/client.rb