mirror of
https://github.com/ubicloud/ubicloud.git
synced 2025-10-06 06:41:57 +08:00
Previously, the code only worked with Cloudflare User API tokens. Use Account API token endpoints if the GITHUB_CACHE_BLOB_STORAGE_USE_ACCOUNT_TOKEN environment variable is truthy.
27 lines
771 B
Ruby
27 lines
771 B
Ruby
# frozen_string_literal: true
|
|
|
|
require "excon"
|
|
require "json"
|
|
|
|
class CloudflareClient
|
|
def initialize(api_key)
|
|
@connection = Excon.new("https://api.cloudflare.com", headers: {"Authorization" => "Bearer #{api_key}"})
|
|
end
|
|
|
|
def create_token(name, policies)
|
|
response = @connection.post(path:, body: {name: name, policies: policies}.to_json, expects: 200)
|
|
data = JSON.parse(response.body)
|
|
[data["result"]["id"], data["result"]["value"]]
|
|
end
|
|
|
|
def delete_token(token_id)
|
|
@connection.delete(path: "#{path}/#{token_id}", expects: [200, 404]).status
|
|
end
|
|
|
|
private
|
|
|
|
def path
|
|
frag = Config.github_cache_blob_storage_use_account_token ? "accounts/#{Config.github_cache_blob_storage_account_id}" : "user"
|
|
"/client/v4/#{frag}/tokens"
|
|
end
|
|
end
|