Class: Myrr::Client
- Inherits:
-
Object
- Object
- Myrr::Client
- Defined in:
- lib/myrr/client.rb
Overview
Client for the Myrr protocol server API.
Wraps the register, challenge, verify, revoke, and lookup endpoints.
Also provides generic post, get, and delete methods for resource clients.
Instance Method Summary collapse
-
#challenge(did_key) ⇒ String
Get a challenge nonce for the given identity.
-
#delete(path) ⇒ Hash
Send a DELETE request to the protocol server.
-
#get(path, **params) ⇒ Hash
Send a GET request to the protocol server.
-
#initialize(server_url:, api_key: nil, open_timeout: 5, read_timeout: 10) ⇒ Client
constructor
A new instance of Client.
-
#lookup_by_owner(owner_type:, owner_value:) ⇒ Array<Hash>
Look up identities by owner.
-
#post(path, body, authenticated: false) ⇒ Hash
Send a POST request to the protocol server.
-
#register(public_key:, owner: nil) ⇒ Hash
Register a new agent identity.
-
#revoke(did_key) ⇒ Hash
Revoke an agent identity.
-
#verify(did_key) ⇒ Hash?
Verify an agent's identity.
Constructor Details
#initialize(server_url:, api_key: nil, open_timeout: 5, read_timeout: 10) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 |
# File 'lib/myrr/client.rb', line 20 def initialize(server_url:, api_key: nil, open_timeout: 5, read_timeout: 10) @server_url = server_url.chomp("/") @api_key = api_key @open_timeout = open_timeout @read_timeout = read_timeout end |
Instance Method Details
#challenge(did_key) ⇒ String
Get a challenge nonce for the given identity.
44 45 46 47 |
# File 'lib/myrr/client.rb', line 44 def challenge(did_key) resp = post("/v1/identities/challenge", { id: did_key }) resp["challenge"] end |
#delete(path) ⇒ Hash
Send a DELETE request to the protocol server.
124 125 126 127 128 129 130 131 |
# File 'lib/myrr/client.rb', line 124 def delete(path) uri = URI("#{@server_url}#{path}") http = build_http(uri) request = Net::HTTP::Delete.new(uri) response = http.request(request) handle_response(response) end |
#get(path, **params) ⇒ Hash
Send a GET request to the protocol server.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/myrr/client.rb', line 109 def get(path, **params) uri = URI("#{@server_url}#{path}") uri.query = URI.encode_www_form(params) if params.any? http = build_http(uri) request = Net::HTTP::Get.new(uri) response = http.request(request) handle_response(response) end |
#lookup_by_owner(owner_type:, owner_value:) ⇒ Array<Hash>
Look up identities by owner.
81 82 83 |
# File 'lib/myrr/client.rb', line 81 def lookup_by_owner(owner_type:, owner_value:) get("/v1/identities", owner_type: owner_type, owner_value: owner_value) end |
#post(path, body, authenticated: false) ⇒ Hash
Send a POST request to the protocol server.
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/myrr/client.rb', line 91 def post(path, body, authenticated: false) uri = URI("#{@server_url}#{path}") http = build_http(uri) request = Net::HTTP::Post.new(uri) request["Content-Type"] = "application/json" request["Authorization"] = "Bearer #{@api_key}" if authenticated && @api_key request.body = JSON.generate(body) response = http.request(request) handle_response(response) end |
#register(public_key:, owner: nil) ⇒ Hash
Register a new agent identity.
33 34 35 36 37 |
# File 'lib/myrr/client.rb', line 33 def register(public_key:, owner: nil) body = { public_key: public_key } body[:owner] = owner if owner post("/v1/identities/register", body) end |
#revoke(did_key) ⇒ Hash
Revoke an agent identity.
72 73 74 |
# File 'lib/myrr/client.rb', line 72 def revoke(did_key) post("/v1/identities/revoke", { id: did_key }, authenticated: true) end |
#verify(did_key) ⇒ Hash?
Verify an agent's identity. Performs a status check via the protocol server.
Returns a hash with did_key, status, and optional owner.
Returns nil if the identity is not found.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/myrr/client.rb', line 56 def verify(did_key) challenge(did_key) { "did_key" => did_key, "status" => "active" } rescue Error => e if e..include?("not_found") || e..include?("revoked") nil else raise end end |