class Kentico::Kontent::Delivery::RequestManager

Constants

INITIAL_DELAY
MAX_ATTEMPTS
RETRY_WHEN_CODE

Public Class Methods

start(query, headers) click to toggle source
# File lib/delivery/client/request_manager.rb, line 13
def start(query, headers)
  @query = query
  @headers = headers
  @times_run = 1
  @delay = INITIAL_DELAY
  @url = @query.provide_url
  continue
end

Private Class Methods

continue() click to toggle source
# File lib/delivery/client/request_manager.rb, line 35
def continue
  if ENV['TEST'] == '1'
    resp = Kentico::Kontent::Delivery::Tests::FakeResponder.get_response @query, @url, @headers
    return resp if resp.is_a? Kentico::Kontent::Delivery::Responses::ResponseBase

    make_response resp # resp is pure JSON
  else
    begin
      resp = RestClient.get @url, @headers
    rescue RestClient::ExceptionWithResponse => err
      should_retry Kentico::Kontent::Delivery::Responses::ResponseBase.new err.http_code, err.response
    rescue RestClient::SSLCertificateNotVerified => err
      should_retry Kentico::Kontent::Delivery::Responses::ResponseBase.new 500, err
    rescue SocketError => err
      should_retry Kentico::Kontent::Delivery::Responses::ResponseBase.new 500, err.message
    else
      make_response resp
    end
  end
end
make_response(response) click to toggle source

Converts a standard REST response based on the type of query.

# File lib/delivery/client/request_manager.rb, line 60
def make_response(response)
  case @query.query_type
  when Kentico::Kontent::Delivery::QUERY_TYPE_ITEMS
    respond_item response
  when Kentico::Kontent::Delivery::QUERY_TYPE_TYPES
    respond_type response
  when Kentico::Kontent::Delivery::QUERY_TYPE_TAXONOMIES
    respond_taxonomy response
  when Kentico::Kontent::Delivery::QUERY_TYPE_ELEMENT
    Kentico::Kontent::Delivery::Responses::DeliveryElementResponse.new JSON.parse(response)
  end
end
respond_item(response) click to toggle source
# File lib/delivery/client/request_manager.rb, line 89
def respond_item(response)
  if @query.code_name.nil?
    Kentico::Kontent::Delivery::Responses::DeliveryItemListingResponse.new(
      JSON.parse(response),
      @query.content_link_url_resolver,
      @query.inline_content_item_resolver
    )
  else
    Kentico::Kontent::Delivery::Responses::DeliveryItemResponse.new(
      JSON.parse(response),
      @query.content_link_url_resolver,
      @query.inline_content_item_resolver
    )
  end
end
respond_taxonomy(response) click to toggle source
# File lib/delivery/client/request_manager.rb, line 81
def respond_taxonomy(response)
  if @query.code_name.nil?
    Kentico::Kontent::Delivery::Responses::DeliveryTaxonomyListingResponse.new JSON.parse(response)
  else
    Kentico::Kontent::Delivery::Responses::DeliveryTaxonomyResponse.new JSON.parse(response)
  end
end
respond_type(response) click to toggle source
# File lib/delivery/client/request_manager.rb, line 73
def respond_type(response)
  if @query.code_name.nil?
    Kentico::Kontent::Delivery::Responses::DeliveryTypeListingResponse.new JSON.parse(response)
  else
    Kentico::Kontent::Delivery::Responses::DeliveryTypeResponse.new JSON.parse(response)
  end
end
should_retry(potential_response) click to toggle source
# File lib/delivery/client/request_manager.rb, line 24
def should_retry(potential_response)
  return potential_response if @times_run == MAX_ATTEMPTS ||
                              !RETRY_WHEN_CODE.include?(potential_response.http_code) ||
                              !@query.with_retry_policy

  @times_run += 1
  @delay *= 2
  sleep(@delay)
  continue
end