Sha256: a42c68426344e75b02ad9d3a18075ce738645d88ab1dcce6743b36e902220573

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require "uri"
require "net/http"
require "json"

module Anyway
  module Loaders
    class Doppler < Base
      class RequestError < StandardError; end

      class << self
        attr_accessor :download_url
        attr_writer :token

        def token
          @token || ENV["DOPPLER_TOKEN"]
        end
      end

      self.download_url = "https://api.doppler.com/v3/configs/config/secrets/download"

      def call(env_prefix:, **_options)
        env_payload = parse_doppler_response(url: Doppler.download_url, token: Doppler.token)

        env = ::Anyway::Env.new(type_cast: ::Anyway::NoCast, env_container: env_payload)

        env.fetch_with_trace(env_prefix).then do |(conf, trace)|
          Tracing.current_trace&.merge!(trace)
          conf
        end
      end

      private

      def parse_doppler_response(url:, token:)
        response = fetch_doppler_config(url, token)

        unless response.is_a?(Net::HTTPSuccess)
          raise RequestError, "#{response.code} #{response.message}"
        end

        JSON.parse(response.read_body)
      end

      def fetch_doppler_config(url, token)
        uri = URI.parse(url)
        raise "Doppler token is required to load configuration from Doppler" if token.nil?

        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true if uri.scheme == "https"

        request = Net::HTTP::Get.new(uri)
        request["Accept"] = "application/json"
        request["Authorization"] = "Bearer #{token}"

        http.request(request)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
runger_config-3.0.1 lib/anyway/loaders/doppler.rb
runger_config-3.0.0 lib/anyway/loaders/doppler.rb