Class: Blockchain0x::Resources::APIKeys
- Inherits:
-
Object
- Object
- Blockchain0x::Resources::APIKeys
- Defined in:
- lib/blockchain0x/resources/api_keys.rb
Instance Method Summary collapse
-
#create_agent_key(agent_id:, label:, scopes:, expires_in_days: nil, idempotency_key: nil) ⇒ Hash
Mint a legacy 21.1 agent-bound API key.
-
#create_workspace_key(label:, workspace_scopes: nil, wallet_assignments: nil, expires_in_days: nil, idempotency_key: nil) ⇒ Hash
Mint a sub-plan 21.3 workspace-flavor API key.
- #get(api_key_id) ⇒ Hash
-
#initialize(client) ⇒ APIKeys
constructor
A new instance of APIKeys.
-
#list ⇒ Hash
Cursor-paginated page envelope.
-
#revoke(api_key_id) ⇒ Object
Revoke an API key.
Constructor Details
#initialize(client) ⇒ APIKeys
Returns a new instance of APIKeys.
32 33 34 |
# File 'lib/blockchain0x/resources/api_keys.rb', line 32 def initialize(client) @client = client end |
Instance Method Details
#create_agent_key(agent_id:, label:, scopes:, expires_in_days: nil, idempotency_key: nil) ⇒ Hash
Mint a legacy 21.1 agent-bound API key.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/blockchain0x/resources/api_keys.rb', line 105 def create_agent_key( agent_id:, label:, scopes:, expires_in_days: nil, idempotency_key: nil ) raise ArgumentError, 'agent_id is required' if agent_id.nil? || agent_id.empty? raise ArgumentError, 'label is required' if label.nil? || label.empty? sc = Array(scopes) raise ArgumentError, 'at least one scope is required' if sc.empty? body = { 'agentId' => agent_id, 'label' => label, 'scopes' => sc, } body['expiresInDays'] = expires_in_days unless expires_in_days.nil? @client.post('/v1/api-keys', body: body, idempotency_key: idempotency_key) end |
#create_workspace_key(label:, workspace_scopes: nil, wallet_assignments: nil, expires_in_days: nil, idempotency_key: nil) ⇒ Hash
Mint a sub-plan 21.3 workspace-flavor API key.
At least one of ‘workspace_scopes` or `wallet_assignments` MUST be non-empty - a workspace-flavor key with neither rejects at the server with `request.invalid`. The client raises ArgumentError before issuing the wire call so the mistake is caught locally.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/blockchain0x/resources/api_keys.rb', line 73 def create_workspace_key( label:, workspace_scopes: nil, wallet_assignments: nil, expires_in_days: nil, idempotency_key: nil ) raise ArgumentError, 'label is required' if label.nil? || label.empty? ws = Array(workspace_scopes) wa = Array(wallet_assignments) if ws.empty? && wa.empty? raise ArgumentError, 'workspace-flavor key needs at least one workspace_scope OR wallet_assignment' end body = { 'label' => label } body['workspaceScopes'] = ws unless ws.empty? body['walletAssignments'] = wa.map { |a| a.respond_to?(:to_h_wire) ? a.to_h_wire : a } unless wa.empty? body['expiresInDays'] = expires_in_days unless expires_in_days.nil? @client.post('/v1/api-keys', body: body, idempotency_key: idempotency_key) end |
#get(api_key_id) ⇒ Hash
43 44 45 46 47 |
# File 'lib/blockchain0x/resources/api_keys.rb', line 43 def get(api_key_id) raise ArgumentError, 'api_key_id is required' if api_key_id.nil? || api_key_id.empty? @client.get("/v1/api-keys/#{api_key_id}") end |
#list ⇒ Hash
Returns cursor-paginated page envelope.
37 38 39 |
# File 'lib/blockchain0x/resources/api_keys.rb', line 37 def list @client.get('/v1/api-keys') end |
#revoke(api_key_id) ⇒ Object
Revoke an API key. Returns nil (204 No Content).
50 51 52 53 54 |
# File 'lib/blockchain0x/resources/api_keys.rb', line 50 def revoke(api_key_id) raise ArgumentError, 'api_key_id is required' if api_key_id.nil? || api_key_id.empty? @client.delete("/v1/api-keys/#{api_key_id}") end |