From 53d0dee930fd29562b5d30dc9b13528e1a492313 Mon Sep 17 00:00:00 2001 From: Mikhael Rakauskas Date: Wed, 2 Jul 2025 16:50:24 -0400 Subject: [PATCH] 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" />