Class: Grafana::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/grafana/client/base_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(grafana_url: Grafana::Client.grafana_url, api_key: Grafana::Client.api_key) ⇒ BaseClient

Returns a new instance of BaseClient.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/grafana/client/base_client.rb', line 10

def initialize(grafana_url: Grafana::Client.grafana_url, api_key: Grafana::Client.api_key)
  retry_options = {
    max: 2,
    retry_statuses: [429]
  }

  @conn = Faraday.new(url: grafana_url) do |f|
    f.request :authorization, :Bearer, api_key
    f.request :json # encode req bodies as JSON
    f.request :retry, retry_options # retry transient failures
    f.response :follow_redirects # follow redirects
    f.response :json, content_type: /\bjson$/ # decode response bodies as JSON
  end
end

Instance Method Details

#delete(url, **options) ⇒ Object



37
38
39
40
41
# File 'lib/grafana/client/base_client.rb', line 37

def delete(url, **options)
  response = @conn.delete(url, **options)

  response.body
end

#get(url, **options) ⇒ Object



25
26
27
28
# File 'lib/grafana/client/base_client.rb', line 25

def get(url, **options)
  # TODO: error handling
  @conn.get(url, **options).body
end

#post(url, body, **options) ⇒ Object



30
31
32
33
34
35
# File 'lib/grafana/client/base_client.rb', line 30

def post(url, body, **options)
  response = @conn.post(url, body, **options)

  # TODO: handle errors in the response
  response.body
end