lib/promoted/ruby/client.rb
##
# Make a delivery request. If @perform_checks is set, input validation will occur and possibly raise errors.
Make a delivery request. If @perform_checks is set, input validation will occur and possibly raise errors.
Make a delivery request . If @perform_checks is set input validation will occur and possibly raise errors .
def deliver args, headers={}
          args = Promoted::Ruby::Client::Util.translate_hash(args)
          if !@enabled
            return {
              insertion: @pager.apply_paging(args[:full_insertion], Promoted::Ruby::Client::INSERTION_PAGING_TYPE['UNPAGED'], args[:request][:paging])
            }
          end
          delivery_request_builder = RequestBuilder.new
          delivery_request_builder.set_request_params(args)
          if @perform_checks
            perform_common_checks!(args)
            if args[:insertion_page_type] == Promoted::Ruby::Client::INSERTION_PAGING_TYPE['PRE_PAGED'] then
              err = DeliveryInsertionPageType.new
              @logger.error(err) if @logger
              raise err
            end
          end
          delivery_request_builder.ensure_client_timestamp
          response_insertions = []
          cohort_membership_to_log = nil
          insertions_from_delivery = false
          only_log = delivery_request_builder.only_log != nil ? delivery_request_builder.only_log : @default_only_log
          deliver_err = false
          begin
            @pager.validate_paging(delivery_request_builder.full_insertion, delivery_request_builder.request[:paging])
          rescue InvalidPagingError => err
            @logger.warn(err) if @logger
            return {
              insertion: err.default_insertions_page
            }
          end
          if !only_log
            cohort_membership_to_log = delivery_request_builder.new_cohort_membership_to_log
            if should_apply_treatment(cohort_membership_to_log)
              delivery_request_params = delivery_request_builder.delivery_request_params  
              begin
                response = send_request(delivery_request_params, @delivery_endpoint, @delivery_timeout_millis, @delivery_api_key, headers)
              rescue  StandardError => err
                deliver_err = true
                @logger.error("Error calling delivery: " + err.message) if @logger
              end
            elsif @send_shadow_traffic_for_control
              deliver_shadow_traffic args, headers
            end
            insertions_from_delivery = (response != nil && !deliver_err);
            response_insertions = delivery_request_builder.fill_details_from_response(
              response && response[:insertion] || [])
          end
          request_to_log = nil
          if !insertions_from_delivery then
            request_to_log = delivery_request_builder.request
            response_insertions = build_sdk_response_insertions(delivery_request_builder)
          end
          log_req = nil
          exec_server = (insertions_from_delivery ? Promoted::Ruby::Client::EXECUTION_SERVER['API'] : Promoted::Ruby::Client::EXECUTION_SERVER['SDK'])
          if request_to_log || cohort_membership_to_log
            log_request_builder = RequestBuilder.new
            log_request = {
              :full_insertion => response_insertions,
              :experiment => cohort_membership_to_log,
              :request => request_to_log
            }
            log_request_builder.set_request_params(log_request)
            log_request_builder.platform_id = delivery_request_builder.platform_id
            log_request_builder.timing      = delivery_request_builder.timing
            log_request_builder.user_info   = delivery_request_builder.user_info
            log_req = log_request_builder.log_request_params(
              include_delivery_log: !insertions_from_delivery, 
              exec_server: exec_server)
          end
          client_response = {
            insertion: response_insertions,
            log_request: log_req,
            execution_server: exec_server,
            client_request_id: delivery_request_builder.client_request_id
          }
          return client_response
        end
def deliver args , headers = { } args = Promoted :: Ruby :: Client :: Util . translate_hash ( args ) if ! @enabled return { insertion : @pager . apply_paging ( args [ :full_insertion ] , Promoted :: Ruby :: Client :: INSERTION_PAGING_TYPE [ 'UNPAGED' ] , args [ :request ] [ :paging ] ) } end delivery_request_builder = RequestBuilder . new delivery_request_builder . set_request_params ( args ) if @perform_checks perform_common_checks! ( args ) if args [ :insertion_page_type ] == Promoted :: Ruby :: Client :: INSERTION_PAGING_TYPE [ 'PRE_PAGED' ] then err = DeliveryInsertionPageType . new @logger . error ( err ) if @logger raise err end end delivery_request_builder . ensure_client_timestamp response_insertions = [ ] cohort_membership_to_log = nil insertions_from_delivery = false only_log = delivery_request_builder . only_log != nil ? delivery_request_builder . only_log : @default_only_log deliver_err = false begin @pager . validate_paging ( delivery_request_builder . full_insertion , delivery_request_builder . request [ :paging ] ) rescue InvalidPagingError => err @logger . warn ( err ) if @logger return { insertion : err . default_insertions_page } end if ! only_log cohort_membership_to_log = delivery_request_builder . new_cohort_membership_to_log if should_apply_treatment ( cohort_membership_to_log ) delivery_request_params = delivery_request_builder . delivery_request_params begin response = send_request ( delivery_request_params , @delivery_endpoint , @delivery_timeout_millis , @delivery_api_key , headers ) rescue StandardError => err deliver_err = true @logger . error ( "Error calling delivery: " + err . message ) if @logger end elsif @send_shadow_traffic_for_control deliver_shadow_traffic args , headers end insertions_from_delivery = ( response != nil && ! deliver_err ) ; response_insertions = delivery_request_builder . fill_details_from_response ( response && response [ :insertion ] || [ ] ) end request_to_log = nil if ! insertions_from_delivery then request_to_log = delivery_request_builder . request response_insertions = build_sdk_response_insertions ( delivery_request_builder ) end log_req = nil exec_server = ( insertions_from_delivery ? Promoted :: Ruby :: Client :: EXECUTION_SERVER [ 'API' ] : Promoted :: Ruby :: Client :: EXECUTION_SERVER [ 'SDK' ] ) if request_to_log || cohort_membership_to_log log_request_builder = RequestBuilder . new log_request = { :full_insertion => response_insertions , :experiment => cohort_membership_to_log , :request => request_to_log } log_request_builder . set_request_params ( log_request ) log_request_builder . platform_id = delivery_request_builder . platform_id log_request_builder . timing = delivery_request_builder . timing log_request_builder . user_info = delivery_request_builder . user_info log_req = log_request_builder . log_request_params ( include_delivery_log : ! insertions_from_delivery , exec_server : exec_server ) end client_response = { insertion : response_insertions , log_request : log_req , execution_server : exec_server , client_request_id : delivery_request_builder . client_request_id } return client_response end
Make a delivery request.
Make a delivery request .
# Respect the enabled state # No log request returned when disabled # perform_checks raises errors. # Invalid input, log and do SDK-side delivery. # No log request returned when no response insertions due to invalid paging # Call Delivery API to get insertions to use # Currently we don't propagate errors to the SDK caller, but rather default to returning # the request insertions. # Call Delivery API to send shadow traffic. This will create the request params with traffic type set. # We only return a log request if there's a request or cohort to log. # We can't count on these being set already since request_to_log may be nil. # On a successful delivery request, we don't log the insertions # or the request since they are logged on the server-side.
args
headers