From 4cb85d29604aee9acbdfedae12ad601abb460558 Mon Sep 17 00:00:00 2001 From: Mikhael Rakauskas Date: Wed, 2 Jul 2025 11:31:58 -0400 Subject: [PATCH 1/7] Tool versions to stop my console screaming at me --- .ruby-version | 1 + .tool-versions | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 .ruby-version create mode 100644 .tool-versions diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..4d9d11cf --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +3.4.2 diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 00000000..e15f5d30 --- /dev/null +++ b/.tool-versions @@ -0,0 +1,2 @@ +nodejs 18.20.4 +ruby 3.1.6 From c9af371bc0893a394670124c8eba3cac742f1424 Mon Sep 17 00:00:00 2001 From: Mikhael Rakauskas Date: Wed, 2 Jul 2025 14:55:04 -0400 Subject: [PATCH 2/7] export controller --- app/controllers/export_controller.rb | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 app/controllers/export_controller.rb diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb new file mode 100644 index 00000000..b7aa73a2 --- /dev/null +++ b/app/controllers/export_controller.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true +require 'faraday' + +class ExportController < ApplicationController + skip_before_action :maybe_redirect_to_setup + skip_before_action :verify_authenticity_token + + # Template is sent as JSON already; we're just gonnna send it on to the third party + def export_template + conn = Faraday.new(url: 'https://api.thirdparty.com') do |faraday| + faraday.request :json + faraday.response :json + faraday.adapter Faraday.default_adapter + end + + response = conn.post('/endpoint') do |req| + req.headers['Authorization'] = 'Bearer YOUR_API_KEY' + # Pass along the entire JSON payload received in the request + req.body = request.raw_post.present? ? JSON.parse(request.raw_post) : params.to_unsafe_h + end + + if response.success? + redirect_to templates_path, alert: I18n.t('exports.templates.success') + else + Rollbar.error("Third party API error: #{response.status} - #{response.body}") if defined?(Rollbar) + redirect_to templates_path, alert: I18n.t('exports.templates.api_error') + end + rescue Faraday::Error => e + Rollbar.error("Failed to send to third party: #{e.message}") if defined?(Rollbar) + redirect_to templates_path, alert: I18n.t('exports.templates.api_error') + rescue StandardError => e + Rollbar.error(e) if defined?(Rollbar) + redirect_to templates_path, alert: I18n.t('exports.templates.error') + end +end From 53d0dee930fd29562b5d30dc9b13528e1a492313 Mon Sep 17 00:00:00 2001 From: Mikhael Rakauskas Date: Wed, 2 Jul 2025 16:50:24 -0400 Subject: [PATCH 3/7] Export location model and first pass export to configurable endpoint. --- app/controllers/export_controller.rb | 23 ++++++---- app/javascript/template_builder/builder.vue | 46 +++++++++++++++++++ app/models/export_location.rb | 22 +++++++++ config/locales/i18n.yml | 5 ++ config/routes.rb | 7 +++ .../20250702193415_create_export_locations.rb | 14 ++++++ ...317_add_auth_params_to_export_locations.rb | 5 ++ db/schema.rb | 17 +++++-- 8 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 app/models/export_location.rb create mode 100644 db/migrate/20250702193415_create_export_locations.rb create mode 100644 db/migrate/20250702204317_add_auth_params_to_export_locations.rb diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb index b7aa73a2..411daafd 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -2,33 +2,40 @@ require 'faraday' class ExportController < ApplicationController + skip_authorization_check skip_before_action :maybe_redirect_to_setup skip_before_action :verify_authenticity_token - # Template is sent as JSON already; we're just gonnna send it on to the third party + # Template is sent as JSON already; we're just gonnna send it on to the third party. + # It's assumed that any extra details required will also be passed through from the front end - + # set by the ExportLocation.extra_params def export_template - conn = Faraday.new(url: 'https://api.thirdparty.com') do |faraday| + export_location = ExportLocation.default_location + conn = Faraday.new(url: export_location.api_base_url) do |faraday| faraday.request :json faraday.response :json faraday.adapter Faraday.default_adapter end - response = conn.post('/endpoint') do |req| - req.headers['Authorization'] = 'Bearer YOUR_API_KEY' - # Pass along the entire JSON payload received in the request + response = conn.post(export_location.templates_endpoint) do |req| + # req.headers['Authorization'] = "Bearer #{export_location.authorization_token}" lol req.body = request.raw_post.present? ? JSON.parse(request.raw_post) : params.to_unsafe_h + req.body.merge!(export_location.extra_params) if export_location.extra_params end if response.success? - redirect_to templates_path, alert: I18n.t('exports.templates.success') + head :ok # alert: I18n.t('exports.templates.success') else - Rollbar.error("Third party API error: #{response.status} - #{response.body}") if defined?(Rollbar) - redirect_to templates_path, alert: I18n.t('exports.templates.api_error') + Rails.logger.error("Failed to send to third party Faraday: #{response.status}") + Rollbar.error("#{export_location.name} API error: #{response.status}") if defined?(Rollbar) + head :ok # templates_path, alert: I18n.t('exports.templates.api_error') end rescue Faraday::Error => e + Rails.logger.error("Failed to send to third party Faraday: #{e.message}") Rollbar.error("Failed to send to third party: #{e.message}") if defined?(Rollbar) redirect_to templates_path, alert: I18n.t('exports.templates.api_error') rescue StandardError => e + Rails.logger.error("Failed to send to third party: #{e.message}") Rollbar.error(e) if defined?(Rollbar) redirect_to templates_path, alert: I18n.t('exports.templates.error') end diff --git a/app/javascript/template_builder/builder.vue b/app/javascript/template_builder/builder.vue index df68917a..e36f8d6f 100644 --- a/app/javascript/template_builder/builder.vue +++ b/app/javascript/template_builder/builder.vue @@ -73,6 +73,25 @@ name="buttons" />