Sha256: dc50f8f0f8b545b2e929264ff8c0e231981540c49f35e4cd7cd30300ef1b39fc

Contents?: true

Size: 1.4 KB

Versions: 15

Compression:

Stored size: 1.4 KB

Contents

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "faraday"

module Gapic
  module Rest
    # Registers the middleware with Faraday
    module FaradayMiddleware
      ##
      # Request middleware that constructs the Authorization HTTP header
      # using ::Google::Auth::Credentials
      #
      class GoogleAuthorization < Faraday::Middleware
        ##
        # @param app [#call]
        # @param credentials [::Google::Auth::Credentials]
        def initialize app, credentials
          @credentials = credentials
          super app
        end

        # @param env [Faraday::Env]
        def call env
          auth_hash = @credentials.client.apply({})
          env.request_headers["Authorization"] = auth_hash[:authorization]

          @app.call env
        end
      end

      Faraday::Request.register_middleware google_authorization: -> { GoogleAuthorization }
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
gapic-common-0.13.0 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.12.0 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.11.1 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.11.0 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.10.0 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.9.0 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.6.1 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.8.0 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.7.0 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.6.0 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.5.0 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.4.3 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.4.2 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.4.1 lib/gapic/rest/faraday_middleware.rb
gapic-common-0.4.0 lib/gapic/rest/faraday_middleware.rb