# frozen_string_literal: true require 'faraday' require 'codat/faraday_codat_auth' require 'multi_json' module Codat class Client BASE_URLS = { production: 'https://api.codat.io/', uat: 'https://api-uat.codat.io/' }.freeze def initialize Faraday::Request.register_middleware codat_auth: -> { Codat::FaradayCodatAuth } end def get(path, params = {}) connection.get(path, params) end def post(path, params = {}) connection.post(path, MultiJson.dump(params)) end def connection @connection ||= Faraday.new(url: api_url) do |conn| conn.request :codat_auth, Codat.configuration.api_key conn.response :json, parser_options: { symbolize_names: true } # conn.response :logger # if Codat.configuration.debug? conn.adapter Faraday.default_adapter end end private def api_url BASE_URLS[Codat.configuration.environment.to_sym] end end end