Class: Huntress::Client

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

Overview

The Client class serves as a wrapper for the Hudu REST API, providing methods to interact with various Hudu resources.

This class dynamically defines methods to fetch, create, update, and manipulate Hudu resources such as companies, articles, assets, and more.

Examples:

Basic Usage

client.companies # Fetch all companies
client.company(1) # Fetch a company by ID
client.update_company(1, { name: "Updated Company" }) # Update a company
client.create_company({ name: "New Company" }) # Create a new company

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from API

#config, #initialize

Methods included from Authentication

#login

Constructor Details

This class inherits a constructor from Huntress::API

Class Method Details

.api_endpoint(method, singular_method = nil, path = method) ⇒ Object

Dynamically defines methods for interacting with Hudu API resources.

Depending on the arguments, this will define methods to:

  • Fetch all records for a resource

  • Fetch a specific record by ID

  • Update a record

  • Create a new record

Examples:

Defining endpoints

api_endpoint :companies, :company
# Defines:
# - `companies(params = {})` to fetch all companies.
# - `company(id, params = {})` to fetch a single company by ID.
# - `update_companies(id, params = {})` to update a company.
# - `create_companies(params = {})` to create a new company.

Parameters:

  • method (Symbol)

    The method name for fetching all records.

  • singular_method (Symbol, nil) (defaults to: nil)

    The method name for fetching a single record by ID. Optional.

  • path (String) (defaults to: method)

    The API path for the resource. Defaults to the method name.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/huntress/client.rb', line 39

def self.api_endpoint(method, singular_method = nil, path = method)
  if singular_method
    # Define method to fetch all records and one by id
    send(:define_method, method) do |params = {}|
      r = get_paged(api_url(path), params)
    end
    # Define method to fetch a single record by ID
    send(:define_method, singular_method) do |id, params = {}|
      r = get(api_url("#{path}/#{id}"), params)
    end
  else
    # Define simple method to fetch data
    send(:define_method, method) do |params = {}|
      get(api_url(path), params)
    end
  end
end

Instance Method Details

#account(options = {}) ⇒ Object



57
58
59
# File 'lib/huntress/client.rb', line 57

def (options = {})
  get(api_url('account'), options)
end

#actor(options = {}) ⇒ Object



61
62
63
# File 'lib/huntress/client.rb', line 61

def actor(options = {})
  get(api_url('actor'), options)
end

#api_url(path) ⇒ String

Constructs the full API URL for a given path.

Parameters:

  • path (String)

    The API path.

Returns:

  • (String)

    The full API URL.



87
88
89
# File 'lib/huntress/client.rb', line 87

def api_url(path)
  "/v1/#{path}"
end

#remediation(incident_report_id, remediation, options = {}) ⇒ Object



65
66
67
# File 'lib/huntress/client.rb', line 65

def remediation(incident_report_id, remediation, options = {})
  get_paged(api_url("incident_reports/#{incident_report_id}/remediations/#{remediation}"), options)
end

#remediations(incident_report_id, options = {}) ⇒ Object



69
70
71
72
# File 'lib/huntress/client.rb', line 69

def remediations(incident_report_id, options = {})
  #get_paged(api_url("incident_reports/#{incident_report_id}/remediations"), options)
  remediation(incident_report_id, nil, options)
end