def build_connection
      Faraday.new(config.options) do |builder|
        # response
        builder.use ZendeskAPI::Middleware::Response::RaiseError
        builder.use ZendeskAPI::Middleware::Response::Callback, self
        builder.use ZendeskAPI::Middleware::Response::Logger, config.logger if config.logger
        builder.use ZendeskAPI::Middleware::Response::ParseIsoDates
        builder.use ZendeskAPI::Middleware::Response::ParseJson
        builder.use ZendeskAPI::Middleware::Response::SanitizeResponse

        adapter = config.adapter || Faraday.default_adapter

        unless GZIP_EXCEPTIONS.include?(adapter)
          builder.use ZendeskAPI::Middleware::Response::Gzip
          builder.use ZendeskAPI::Middleware::Response::Deflate
        end

        # request
        if config.access_token && !config.url_based_access_token
          builder.authorization("Bearer", config.access_token)
        elsif config.access_token
          builder.use ZendeskAPI::Middleware::Request::UrlBasedAccessToken, config.access_token
        else
          builder.use Faraday::Request::BasicAuthentication, config.username, config.password
        end

        if config.cache
          builder.use ZendeskAPI::Middleware::Request::EtagCache, :cache => config.cache
        end

        builder.use ZendeskAPI::Middleware::Request::Upload
        builder.request :multipart
        builder.use ZendeskAPI::Middleware::Request::EncodeJson

        # Should always be first in the stack
        builder.use ZendeskAPI::Middleware::Request::Retry, :logger => config.logger if config.retry
        if config.raise_error_when_rate_limited
          builder.use ZendeskAPI::Middleware::Request::RaiseRateLimited, :logger => config.logger
        end

        builder.adapter(*adapter)
      end
    end
def build_connection
      Faraday.new(config.options) do |builder|
        # response
        builder.use ZendeskAPI::Middleware::Response::RaiseError
        builder.use ZendeskAPI::Middleware::Response::Callback, self
        builder.use ZendeskAPI::Middleware::Response::Logger, config.logger if config.logger
        builder.use ZendeskAPI::Middleware::Response::ParseIsoDates
        builder.use ZendeskAPI::Middleware::Response::ParseJson
        builder.use ZendeskAPI::Middleware::Response::SanitizeResponse

        adapter = config.adapter || Faraday.default_adapter

        unless GZIP_EXCEPTIONS.include?(adapter)
          builder.use ZendeskAPI::Middleware::Response::Gzip
          builder.use ZendeskAPI::Middleware::Response::Deflate
        end

        # request
        if config.access_token && !config.url_based_access_token
          builder.authorization("Bearer", config.access_token)
        elsif config.access_token
          builder.use ZendeskAPI::Middleware::Request::UrlBasedAccessToken, config.access_token
        else
          builder.use Faraday::Request::BasicAuthentication, config.username, config.password
        end

        if config.cache
          builder.use ZendeskAPI::Middleware::Request::EtagCache, :cache => config.cache
        end

        builder.use ZendeskAPI::Middleware::Request::Upload
        builder.request :multipart
        builder.use ZendeskAPI::Middleware::Request::EncodeJson

        # Should always be first in the stack
        builder.use ZendeskAPI::Middleware::Request::Retry, :logger => config.logger if config.retry
        if config.raise_error_when_rate_limited
          builder.use ZendeskAPI::Middleware::Request::RaiseRateLimited, :logger => config.logger
        end

        builder.adapter(*adapter)
      end
    end
Called by {#connection} to build a connection. Can be overwritten in a
 subclass to add additional middleware and make other configuration
 changes.

 Uses middleware according to configuration options.

 Request logger if logger is not nil

 Retry middleware if retry is true