# frozen_string_literal: true

require 'faraday'
require 'json'
require_relative '../response/api_error'
require_relative '../error/api_error'

module ONEAccess
  module API
    class ApiBase
      class << self
        def send_get(method, params = {})
          Http::HttpClient.get(api_url(method), params)
        end

        def send_post(method, body)
          Http::HttpClient.post(api_url(method), body, 'Content-Type' => 'application/json')
        end

        def resource_api_path
          const_get('RESOURCE_API_PATH')
        rescue StandardError
          nil
        end

        def api_url(method = nil)
          url = "#{base_url}#{resource_api_path}"
          return url if method.nil?
          "#{url}/#{method}"
        end

        protected

        def api_path(path)
          raise 'API path has already been set' if resource_api_path
          self.resource_api_path = path
        end

        private

        def base_url
          raise 'base_url must be implemented'
        end

        def resource_api_path=(path)
          const_set('RESOURCE_API_PATH', path)
        end
      end
    end
  end
end