ubicloud/routes/project/inference_api_key.rb
Jeremy Evans aeacc6f0bb Allow authorization methods to accept objects in addition to ids
Previously, project_id and subject_id needed to be a uuid, and
object_id could be a uuid or ubid. This allows accepting an
object for all three. This simplifies almost all callers of
these methods. It also doesn't require either parsing a ubid
or generating a ubid. Previously, even if a uuid was given, it
would turn it into a ubid in order to extract the class, and
guess ApiKey if it was an et ubid.
2025-09-18 02:56:28 +09:00

58 lines
1.5 KiB
Ruby

# frozen_string_literal: true
class Clover
hash_branch(:project_prefix, "inference-api-key") do |r|
r.is do
r.get do
@inference_api_keys = inference_api_key_ds.all
if web?
@remaining_free_quota = FreeQuota.remaining_free_quota("inference-tokens", @project.id)
@free_quota_unit = "inference tokens"
@has_valid_payment_method = @project.has_valid_payment_method?
view "inference/api_key/index"
else
{items: @inference_api_keys.map { {id: it.ubid, key: it.key} }}
end
end
r.post do
authorize("InferenceApiKey:create", @project)
iak = nil
DB.transaction do
iak = ApiKey.create_inference_api_key(@project)
audit_log(iak, "create")
end
if web?
flash["notice"] = "Created Inference API Key with id #{iak.ubid}. It may take a few minutes to sync."
r.redirect @project, "/inference-api-key"
else
{id: iak.ubid, key: iak.key}
end
end
end
r.is :ubid_uuid do |id|
iak = inference_api_key_ds.with_pk(id)
r.get api? do
if iak
{id: iak.ubid, key: iak.key}
end
end
r.delete do
if iak
authorize("InferenceApiKey:delete", iak)
DB.transaction do
iak.destroy
audit_log(iak, "destroy")
end
flash["notice"] = "Inference API Key deleted successfully" if web?
end
204
end
end
end
end