Module: Grafana::HttpRequest

Included in:
Client
Defined in:
lib/grafana/http_request.rb

Instance Method Summary collapse

Instance Method Details

#_issue_request(method_type = 'GET', endpoint = '/', data = {}) ⇒ Object



30
31
32
33
34
35
36
37
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
# File 'lib/grafana/http_request.rb', line 30

def _issue_request(method_type='GET', endpoint='/', data={})
  
  begin
    resp = nil
    case method_type.upcase
    when 'GET'
      resp = @api_instance[endpoint].get(@headers)
    when 'POST'
      resp = @api_instance[endpoint].post(data,@headers)
    when 'PATCH'
      resp = @api_instance[endpoint].patch(data,@headers)
    when 'PUT'
      resp = @api_instance[endpoint].put(data,@headers)
    when 'DELETE'
      resp = @api_instance[endpoint].delete(@headers)
    else
      @logger.error("Error: #{__method__} is not a valid request method.") if @debug
      return false
    end

    if (resp.code.to_i >= 200 && resp.code.to_i <= 299) || (resp.code.to_i >= 400 && resp.code.to_i <= 499)
      return JSON.parse(resp.body)
    else
      @logger.error("#{__method__} on #{endpoint} failed: HTTP #{resp.code} - #{resp.body}") if @debug
      return false
    end

  rescue => e
   @logger.error("Error: #{__method__} #{endpoint} error: #{e}") if @debug
   return false
  end

end

#delete_request(endpoint) ⇒ Object



20
21
22
23
# File 'lib/grafana/http_request.rb', line 20

def delete_request(endpoint)
  @logger.info("Running: Grafana::HttpRequest::#{__method__} on #{endpoint}") if @debug
  return _issue_request('DELETE', endpoint)
end

#get_request(endpoint) ⇒ Object



5
6
7
8
# File 'lib/grafana/http_request.rb', line 5

def get_request(endpoint)
  @logger.info("Running: Grafana::HttpRequest::#{__method__} on #{endpoint}") if @debug
  return _issue_request('GET', endpoint)
end

#patch_request(endpoint, patchdata = {}) ⇒ Object



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

def patch_request(endpoint, patchdata={})
  @logger.info("Running: Grafana::HttpRequest::#{__method__} on #{endpoint}") if @debug
  return _issue_request('PATCH', endpoint, patchdata)
end

#post_request(endpoint, postdata = {}) ⇒ Object



10
11
12
13
# File 'lib/grafana/http_request.rb', line 10

def post_request(endpoint, postdata={})
  @logger.info("Running: Grafana::HttpRequest::#{__method__} on #{endpoint}") if @debug
  return _issue_request('POST', endpoint, postdata)
end

#put_request(endpoint, putdata = {}) ⇒ Object



15
16
17
18
# File 'lib/grafana/http_request.rb', line 15

def put_request(endpoint, putdata={})
  @logger.info("Running: Grafana::HttpRequest::#{__method__} on #{endpoint}") if @debug
  return _issue_request('PUT', endpoint, putdata)
end