Class: Blockchain0x::Resources::APIKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/blockchain0x/resources/api_keys.rb

Instance Method Summary collapse

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.

Parameters:

  • agent_id (String)

    the agent the key is bound to.

  • label (String)

    human-readable label.

  • scopes (Array<String>)

    one or more wallet-scoped permissions.

  • expires_in_days (Integer, nil) (defaults to: nil)

    optional rotation hint.

  • idempotency_key (String, nil) (defaults to: nil)

    explicit override.

Returns:

  • (Hash)

    the raw envelope ‘{ ’id’, ‘secret’, ‘prefix’, … }‘.

Raises:

  • (ArgumentError)


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.

Parameters:

  • label (String)

    human-readable label shown in the dashboard.

  • workspace_scopes (Array<String>, nil) (defaults to: nil)

    zero or more workspace-level scopes.

  • wallet_assignments (Array<WalletAssignment>, nil) (defaults to: nil)

    zero or more per-wallet grants.

  • expires_in_days (Integer, nil) (defaults to: nil)

    optional rotation hint (7/30/60/90).

  • idempotency_key (String, nil) (defaults to: nil)

    explicit override; otherwise the client mints a fresh uuid4.

Returns:

  • (Hash)

    the raw envelope ‘{ ’id’, ‘secret’, ‘prefix’, … }‘. The `secret` field is the plaintext key returned ONCE; persist it now.

Raises:

  • (APIKeyError)

    when the RBAC ceiling rejects the grant (‘apikey.role_insufficient_for_grants`).



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

Parameters:

  • api_key_id (String)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


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

#listHash

Returns cursor-paginated page envelope.

Returns:

  • (Hash)

    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).

Raises:

  • (ArgumentError)


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