Class: Kandji::Client

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

Constant Summary collapse

API_VERSION =
"v1"

Class Method Summary collapse

Class Method Details

.build_url(path, params) ⇒ Object



46
47
48
49
50
# File 'lib/kandji/client.rb', line 46

def self.build_url(path, params)
  url = "#{Kandji.organization_url}/api/#{API_VERSION}/#{path}"
  url += "?#{URI.encode_www_form(params)}" unless params.empty?
  url
end

.get(path, params = {}) ⇒ Object



10
11
12
13
14
# File 'lib/kandji/client.rb', line 10

def self.get(path, params = {})
  url = build_url(path, params)
  response = RestClient.get(url, headers)
  JSON.parse(response.body)
end

.headersObject



52
53
54
55
56
57
# File 'lib/kandji/client.rb', line 52

def self.headers
  {
    Accept: "application/json",
    Authorization: "Bearer #{Kandji.api_token}"
  }
end

.paginated_cursor_get(path, params = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kandji/client.rb', line 32

def self.paginated_cursor_get(path, params = {})
  params[:limit] = params[:limit] || 500
  url = build_url(path, params)

  all_results = []
  loop do
    response = JSON.parse(RestClient.get(url, headers))
    all_results.concat(response["results"])
    url = response["next"]
    break unless url
  end
  all_results
end

.paginated_get(path, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kandji/client.rb', line 16

def self.paginated_get(path, params = {})
  params[:offset] = params[:offset] || 0
  params[:limit] = params[:limit] || 300

  all_results = []
  while (response = get(path, params))
    # The Kandji API can return different structures, so we handle both cases
    result = response.is_a?(Array) ? response : response["data"]
    all_results.concat(result)
    break if result.size < params[:limit]

    params[:offset] += params[:limit]
  end
  all_results
end