Sha256: 9861d022f68df0bc3f7ac60726aa89b6c5865b74e1cccb31d71e5b584c5a8099

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

module Smartsheet
  module API
    # Builds response objects from Faraday response environments
    class FaradayResponse
      def self.from_response_env(faraday_env)
        if faraday_env[:body].kind_of?(Hash) && faraday_env[:body].key?(:errorCode)
          FaradayErrorResponse.new(faraday_env[:body], faraday_env)
        elsif faraday_env.success?
          FaradaySuccessResponse.new(faraday_env[:body], faraday_env)
        else
          raise HttpResponseError.new(
              status_code: faraday_env[:status],
              reason_phrase: faraday_env[:reason_phrase],
              headers: faraday_env[:response_headers],
              message: "#{faraday_env[:status]} #{faraday_env[:reason_phrase]}"
          )
        end
      end

      attr_reader :status_code, :reason_phrase, :headers

      def initialize(faraday_env)
        @status_code = faraday_env[:status]
        @reason_phrase = faraday_env[:reason_phrase]
        @headers = faraday_env[:response_headers]
      end
    end

    # Provides a Smartsheet error response from a Faraday response environment
    class FaradayErrorResponse < FaradayResponse
      RETRYABLE_ERRORS = 4001..4004

      attr_reader :error_code, :message, :ref_id, :detail

      def initialize(result, faraday_env)
        super(faraday_env)
        @error_code = result[:errorCode]
        @message = result[:message]
        @ref_id = result[:refId]
        @detail = result[:detail]
      end

      def should_retry?
        RETRYABLE_ERRORS.include? error_code
      end

      def success?
        false
      end
    end

    # Provides a Smartsheet success response from a Faraday response environment
    class FaradaySuccessResponse < FaradayResponse
      attr_reader :result

      def initialize(result, faraday_env)
        super(faraday_env)
        @result = result
      end

      def should_retry?
        false
      end

      def success?
        true
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
smartsheet-2.77.0 lib/smartsheet/api/faraday_adapter/faraday_response.rb
smartsheet-1.3.0 lib/smartsheet/api/faraday_adapter/faraday_response.rb
smartsheet-1.2.0 lib/smartsheet/api/faraday_adapter/faraday_response.rb
smartsheet-1.1.0 lib/smartsheet/api/faraday_adapter/faraday_response.rb