From 8ad95d17f0333a2b25642ef01e11277e8bd355e5 Mon Sep 17 00:00:00 2001 From: egenny Date: Mon, 23 Oct 2023 15:33:25 -0400 Subject: [PATCH 1/2] Allow inclusion of Faraday Middleware Allow the constructor to accept a block and store it in an instance variable as a proc for configuring Faraday. The block allows the inclusion of Faraday Middleware, for example: FacebookAds::Session.new( access_token: "a_token", app_secret: "a_secret ) { |faraday| faraday.response :my_faraday_middleware } --- lib/facebook_ads/session.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/facebook_ads/session.rb b/lib/facebook_ads/session.rb index 28c19375..ceadf334 100644 --- a/lib/facebook_ads/session.rb +++ b/lib/facebook_ads/session.rb @@ -11,16 +11,18 @@ module FacebookAds class Session - attr_accessor :access_token, :app_secret, :api_version, :server_host + attr_accessor :access_token, :app_secret, :api_version, :server_host, :connection_setup def initialize(access_token: nil, app_secret: nil, server_host: FacebookAds.config.server_host, - api_version: FacebookAds.config.api_version) + api_version: FacebookAds.config.api_version, + &connection_setup) @access_token = access_token @app_secret = app_secret @server_host = server_host @api_version = api_version + @connection_setup = connection_setup end def request(method, path, params = nil) @@ -52,7 +54,7 @@ def api_conn # TODO Json Request # TODO URL Encode - stringify json faraday.request :url_encoded - + @connection_setup&.call(faraday) faraday.response :logger, Utils.logger, bodies: FacebookAds.config.log_api_bodies faraday.adapter Faraday.default_adapter end From 33965ceeaeadc89f23f337ccadba03e4efef6b5d Mon Sep 17 00:00:00 2001 From: egenny Date: Tue, 2 Jul 2024 10:42:48 -0400 Subject: [PATCH 2/2] EventRequest accepts an optional session parameter execute will use the passed in session if available or create a session from the access_token if no session is available. This allows Faraday middleware to be used by EventRequest. session = FacebookAds::Session.new(access_token: token) { |faraday| faraday.response :some_faraday_middleware } FacebookAds::ServerSide::EventRequestAsync.new( pixel_id: pixel_id, events: [event], session: session, ) --- .../ad_objects/server_side/event_request.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/facebook_ads/ad_objects/server_side/event_request.rb b/lib/facebook_ads/ad_objects/server_side/event_request.rb index 0bcdb35c..51deb438 100644 --- a/lib/facebook_ads/ad_objects/server_side/event_request.rb +++ b/lib/facebook_ads/ad_objects/server_side/event_request.rb @@ -57,6 +57,9 @@ class EventRequest # The HttpServiceInterface client to use for executing the request. attr_accessor :http_service_client + # AdObject Session + attr_accessor :session + # @param [String] pixel_id # @param [Array(FacebookAds::ServerSide::Event)] events # @param [String] test_event_code @@ -69,7 +72,7 @@ class EventRequest # @param [HttpServiceInterface] http_service_client def initialize(pixel_id: nil, events: nil, test_event_code: nil, partner_agent: nil, namespace_id: nil, upload_id: nil, upload_tag: nil, upload_source: nil, - access_token: nil, http_service_client: nil) + access_token: nil, http_service_client: nil, session: nil) unless pixel_id.nil? self.pixel_id = pixel_id end @@ -100,6 +103,9 @@ def initialize(pixel_id: nil, events: nil, test_event_code: nil, partner_agent: unless http_service_client.nil? self.http_service_client = http_service_client end + unless session.nil? + self.session = session + end end # build the object using the input hash @@ -160,7 +166,7 @@ def execute end params = get_params() params[:data] = normalize - session = FacebookAds::Session.new(access_token: access_token) if access_token + session = self.session || (access_token ? FacebookAds::Session.new(access_token: access_token) : nil) ads_pixel = FacebookAds::AdsPixel.get(pixel_id, session) response = ads_pixel.events.create(params) json_response_object = JSON.parse(JSON.generate(response), object_class: OpenStruct)