Class: KamateraApiWrapper::Client

Inherits:
Object
  • Object
show all
Includes:
Mutex_m
Defined in:
lib/kamatera_api_wrapper/client.rb

Overview

Simple client for Kamatera public API (authentication + servers)

Example:

client = KamateraApiWrapper::Client.new(client_id: "...", client_secret: "...")
client.servers

Constant Summary collapse

AUTH_PATH =
"/service/authenticate".freeze
DEFAULT_BASE =
"https://console.kamatera.com".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, base_url: DEFAULT_BASE, timeout: 10, token_expiry_margin: 30, faraday: nil) ⇒ Client

options:

  • base_url: base host (default console.kamatera.com)

  • timeout: seconds for Faraday

  • token_expiry_margin: seconds to subtract from token expiry to refresh early



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kamatera_api_wrapper/client.rb', line 24

def initialize(client_id:, client_secret:, base_url: DEFAULT_BASE, timeout: 10, token_expiry_margin: 30, faraday: nil)
  super() # Mutex_m
  @client_id = client_id
  @client_secret = client_secret
  @base_url = base_url.chomp("/")
  @timeout = timeout
  @token_expiry_margin = token_expiry_margin

  @faraday = faraday || build_connection
  @token = nil
  @token_expires_at = nil
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



18
19
20
# File 'lib/kamatera_api_wrapper/client.rb', line 18

def base_url
  @base_url
end

#client_idObject (readonly)

Returns the value of attribute client_id.



18
19
20
# File 'lib/kamatera_api_wrapper/client.rb', line 18

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



18
19
20
# File 'lib/kamatera_api_wrapper/client.rb', line 18

def client_secret
  @client_secret
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



18
19
20
# File 'lib/kamatera_api_wrapper/client.rb', line 18

def timeout
  @timeout
end

#token_expiry_marginObject (readonly)

Returns the value of attribute token_expiry_margin.



18
19
20
# File 'lib/kamatera_api_wrapper/client.rb', line 18

def token_expiry_margin
  @token_expiry_margin
end

Instance Method Details

#authenticate!Object

Authenticate immediately (you may call this, but other methods auto-authenticate)



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
# File 'lib/kamatera_api_wrapper/client.rb', line 38

def authenticate!
  synchronize do
    resp = @faraday.post(AUTH_PATH) do |req|
      req.headers["Content-Type"] = "application/json"
      req.body = MultiJson.dump(clientId: client_id, secret: client_secret)
    end

    unless resp.success?
      raise Errors::AuthenticationError, "Authentication failed (status #{resp.status}): #{resp.body}"
    end

    parsed = parse_json(resp.body)
    token = parsed['authentication']
    token_expires_at = DateTime.strptime(parsed['expires'].to_s, '%s')

    unless token
      raise Errors::ResponseParseError, "No token found in authentication response: #{resp.body}"
    end

    @token = token
    if token_expires_at
      # seconds
      @token_expires_at = token_expires_at
    else
      # fallback: 1 hour
      @token_expires_at = Time.now + 3600
    end
  end

  true
end

#change_server_cpu(server_id:, body:, params: nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/kamatera_api_wrapper/client.rb', line 105

def change_server_cpu(server_id:, body:, params: nil)
  ensure_authenticated!

  resp = @faraday.put("/service/server/#{server_id}/cpu") do |req|
    req.headers["Authorization"] = "Bearer #{token}"
    req.headers['Accept'] = 'text/plain'
    req.headers['Content-Type'] =  'application/json'
    req.body = body
    req.params.update(params) if params
  end

  handle_response(resp)
end

#change_server_ram(server_id:, body:, params: nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/kamatera_api_wrapper/client.rb', line 119

def change_server_ram(server_id:,body: ,params: nil)
  ensure_authenticated!

  resp = @faraday.put("/service/server/#{server_id}/ram") do |req|
    req.headers["Authorization"] = "Bearer #{token}"
    req.headers['Accept'] = 'text/plain'
    req.headers['Content-Type'] =  'application/json'
    req.body = body
    req.params.update(params) if params
  end

  handle_response(resp)
end

#get(path, params: nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/kamatera_api_wrapper/client.rb', line 133

def get(path, params: nil)
  ensure_authenticated!

  resp = @faraday.get(path) do |req|
    req.headers["Authorization"] = "Bearer #{token}"
    req.params.update(params) if params
  end

  handle_response(resp)
end

#server(server_id:, params: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/kamatera_api_wrapper/client.rb', line 94

def server(server_id: ,params: nil)
  ensure_authenticated!

  resp = @faraday.get("/service/server/#{server_id}") do |req|
    req.headers["Authorization"] = "Bearer #{token}"
    req.params.update(params) if params
  end

  handle_response(resp)
end

#servers(params: nil) ⇒ Object

Returns array / parsed hash for GET /servers



71
72
73
74
75
76
77
78
79
80
# File 'lib/kamatera_api_wrapper/client.rb', line 71

def servers(params: nil)
  ensure_authenticated!

  resp = @faraday.get("/service/servers") do |req|
    req.headers["Authorization"] = "Bearer #{token}"
    req.params.update(params) if params
  end

  handle_response(resp)
end