Class: Agent99::RegistryClient

Inherits:
Object
  • Object
show all
Defined in:
lib/agent99/registry_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: ENV.fetch('AGENT99_REGISTRY_URL', 'http://localhost:4567'), logger: Logger.new($stdout)) ⇒ RegistryClient

Returns a new instance of RegistryClient.



10
11
12
13
14
15
16
17
18
# File 'lib/agent99/registry_client.rb', line 10

def initialize(
    base_url: ENV.fetch('AGENT99_REGISTRY_URL', 'http://localhost:4567'),
    logger:   Logger.new($stdout)
  )
  @base_url     = base_url
  @logger       = logger
  uri = URI.parse(base_url)
  @http_client  = Net::HTTP.new(uri.host, uri.port)
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/agent99/registry_client.rb', line 8

def logger
  @logger
end

Instance Method Details

#discover(capability:) ⇒ Object



34
35
36
37
38
# File 'lib/agent99/registry_client.rb', line 34

def discover(capability:)
  encoded_capability = URI.encode_www_form_component(capability)
  request = create_request(:get, "/discover?capability=#{encoded_capability}")
  send_request(request)
end

#fetch_all_agentsObject



41
42
43
44
# File 'lib/agent99/registry_client.rb', line 41

def fetch_all_agents
  request   = create_request(:get, "/")
  response  = send_request(request)
end

#register(info:) ⇒ Object



20
21
22
23
24
# File 'lib/agent99/registry_client.rb', line 20

def register(info:)
  payload = info
  request = create_request(:post, "/register", payload)
  @id     = send_request(request)
end

#withdraw(id) ⇒ Object



26
27
28
29
30
31
# File 'lib/agent99/registry_client.rb', line 26

def withdraw(id)
  return logger.warn("Agent not registered") unless id

  request = create_request(:delete, "/withdraw/#{id}")
  send_request(request)
end