Sha256: e8db04f5d22365963178adac276a1c1d86aa9b915a7699787469cc84977a5454
Contents?: true
Size: 1.35 KB
Versions: 2
Compression:
Stored size: 1.35 KB
Contents
# frozen_string_literal: true class ControlplaneApiDirect API_METHODS = { get: Net::HTTP::Get, post: Net::HTTP::Post, put: Net::HTTP::Put, delete: Net::HTTP::Delete }.freeze API_HOSTS = { api: "https://api.cpln.io", logs: "https://logs.cpln.io" }.freeze # API_TOKEN_REGEX = Regexp.union( # /^[\w.]{155}$/, # CPLN_TOKEN format # /^[\w\-._]{1134}$/ # 'cpln profile token' format # ).freeze API_TOKEN_REGEX = /^[\w\-._]+$/.freeze def call(url, method:, host: :api) # rubocop:disable Metrics/MethodLength uri = URI("#{API_HOSTS[host]}#{url}") request = API_METHODS[method].new(uri) request["Content-Type"] = "application/json" request["Authorization"] = api_token response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(request) } case response when Net::HTTPOK JSON.parse(response.body) when Net::HTTPAccepted true when Net::HTTPNotFound nil else raise("#{response} #{response.body}") end end def api_token return @@api_token if defined?(@@api_token) @@api_token = ENV.fetch("CPLN_TOKEN", `cpln profile token`.chomp) # rubocop:disable Style/ClassVars return @@api_token if @@api_token.match?(API_TOKEN_REGEX) abort("ERROR: Unknown API token format. Please re-run 'cpln profile login' or set correct CPLN_TOKEN env variable") end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
cpl-0.2.0 | lib/core/controlplane_api_direct.rb |
cpl-0.1.0 | lib/core/controlplane_api_direct.rb |