split storages in own adapter classes, added redis adapter
This commit is contained in:
		
							parent
							
								
									bd7cf179f3
								
							
						
					
					
						commit
						c2135d9dcf
					
				| 
						 | 
					@ -11,7 +11,7 @@ Gem::Specification.new do |s|
 | 
				
			||||||
  s.email = "sbazyl@google.com"
 | 
					  s.email = "sbazyl@google.com"
 | 
				
			||||||
  s.executables = ["google-api"]
 | 
					  s.executables = ["google-api"]
 | 
				
			||||||
  s.extra_rdoc_files = ["README.md"]
 | 
					  s.extra_rdoc_files = ["README.md"]
 | 
				
			||||||
  s.files = ["lib/cacerts.pem", "lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/compute_service_account.rb", "lib/google/api_client/auth/file_storage.rb", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/gzip.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/gzip_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/request_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIBUTING.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"]
 | 
					  s.files = ["lib/cacerts.pem", "lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/compute_service_account.rb", "lib/google/api_client/auth/storage.rb", "lib/google/api_client/auth/storages/*.rb", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/gzip.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/gzip_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/request_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIBUTING.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"]
 | 
				
			||||||
  s.homepage = "http://code.google.com/p/google-api-ruby-client/"
 | 
					  s.homepage = "http://code.google.com/p/google-api-ruby-client/"
 | 
				
			||||||
  s.licenses = ["Apache 2.0"]
 | 
					  s.licenses = ["Apache 2.0"]
 | 
				
			||||||
  s.rdoc_options = ["--main", "README.md"]
 | 
					  s.rdoc_options = ["--main", "README.md"]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,7 +12,6 @@
 | 
				
			||||||
# See the License for the specific language governing permissions and
 | 
					# See the License for the specific language governing permissions and
 | 
				
			||||||
# limitations under the License.
 | 
					# limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require 'json'
 | 
					 | 
				
			||||||
require 'signet/oauth_2/client'
 | 
					require 'signet/oauth_2/client'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module Google
 | 
					module Google
 | 
				
			||||||
| 
						 | 
					@ -22,34 +21,31 @@ module Google
 | 
				
			||||||
    # JSON serialized file. Meant to resemble the serialized format
 | 
					    # JSON serialized file. Meant to resemble the serialized format
 | 
				
			||||||
    # http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
 | 
					    # http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    class FileStorage
 | 
					    class Storage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/auth'
 | 
					      AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/auth'
 | 
				
			||||||
      TOKEN_CREDENTIAL_URI = 'https://accounts.google.com/o/oauth2/token'
 | 
					      TOKEN_CREDENTIAL_URI = 'https://accounts.google.com/o/oauth2/token'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      # @return [String] Path to the credentials file.
 | 
					      # @return [Object] Storage object.
 | 
				
			||||||
      attr_accessor :path
 | 
					      attr_accessor :store
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      # @return [Signet::OAuth2::Client] Path to the credentials file.
 | 
					
 | 
				
			||||||
 | 
					      # @return [Signet::OAuth2::Client]
 | 
				
			||||||
      attr_reader :authorization
 | 
					      attr_reader :authorization
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      ##
 | 
					      ##
 | 
				
			||||||
      # Initializes the FileStorage object.
 | 
					      # Initializes the Storage object.
 | 
				
			||||||
      #
 | 
					      #
 | 
				
			||||||
      # @param [String] path
 | 
					      # @params [Object] Storage object
 | 
				
			||||||
      #    Path to the credentials file.
 | 
					      def initialize(store)
 | 
				
			||||||
      def initialize(path)
 | 
					        @store= store
 | 
				
			||||||
        @path= path
 | 
					 | 
				
			||||||
        self.authorize
 | 
					        self.authorize
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      ##
 | 
					      ##
 | 
				
			||||||
      # Attempt to read in credentials from the specified file.
 | 
					      # Attempt to read in credentials from the specified store.
 | 
				
			||||||
      def load_credentials
 | 
					      def load_credentials
 | 
				
			||||||
        if File.exists?(@path) && File.readable?(@path) && File.writable?(@path)
 | 
					        store.load_credentials
 | 
				
			||||||
          credentials = File.open(path, 'r') { |f| JSON.parse(f.read) }
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
        credentials
 | 
					 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      def authorize
 | 
					      def authorize
 | 
				
			||||||
| 
						 | 
					@ -62,12 +58,14 @@ module Google
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      ##
 | 
					      ##
 | 
				
			||||||
      # refresh credentials and save them to file
 | 
					      # refresh credentials and save them to store
 | 
				
			||||||
      def refresh_authorization
 | 
					      def refresh_authorization
 | 
				
			||||||
        @authorization.refresh!
 | 
					        @authorization.refresh!
 | 
				
			||||||
        self.write_credentials(@authorization)
 | 
					        self.write_credentials
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ##
 | 
				
			||||||
 | 
					      # @return [Hash] with credentials
 | 
				
			||||||
      def credentials_hash
 | 
					      def credentials_hash
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
          :access_token          => @authorization.access_token,
 | 
					          :access_token          => @authorization.access_token,
 | 
				
			||||||
| 
						 | 
					@ -82,17 +80,15 @@ module Google
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      ##
 | 
					      ##
 | 
				
			||||||
      # Write the credentials to the specified file.
 | 
					      # Write the credentials to the specified store.
 | 
				
			||||||
      #
 | 
					      #
 | 
				
			||||||
      # @param [Signet::OAuth2::Client] authorization
 | 
					      # @params [Signet::OAuth2::Client] authorization
 | 
				
			||||||
      #    Optional authorization instance. If not provided, the authorization
 | 
					      #    Optional authorization instance. If not provided, the authorization
 | 
				
			||||||
      #    already associated with this instance will be written.
 | 
					      #    already associated with this instance will be written.
 | 
				
			||||||
      def write_credentials(authorization=nil)
 | 
					      def write_credentials(authorization=nil)
 | 
				
			||||||
        @authorization = authorization if authorization
 | 
					        @authorization = authorization if authorization
 | 
				
			||||||
        if @authorization.refresh_token
 | 
					        if @authorization.refresh_token
 | 
				
			||||||
          File.open(self.path, 'w') do |file|
 | 
					          store.write_credentials(credentials_hash)
 | 
				
			||||||
            file.write(credentials_hash.to_json)
 | 
					 | 
				
			||||||
          end
 | 
					 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,59 @@
 | 
				
			||||||
 | 
					# Copyright 2013 Google Inc.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# 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
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					#      http://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 'json'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module Google
 | 
				
			||||||
 | 
					  class APIClient
 | 
				
			||||||
 | 
					    ##
 | 
				
			||||||
 | 
					    # Represents cached OAuth 2 tokens stored on local disk in a
 | 
				
			||||||
 | 
					    # JSON serialized file. Meant to resemble the serialized format
 | 
				
			||||||
 | 
					    # http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html
 | 
				
			||||||
 | 
					    #
 | 
				
			||||||
 | 
					    class FileStore
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      attr_accessor :path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ##
 | 
				
			||||||
 | 
					      # Initializes the FileStorage object.
 | 
				
			||||||
 | 
					      #
 | 
				
			||||||
 | 
					      # @param [String] path
 | 
				
			||||||
 | 
					      #    Path to the credentials file.
 | 
				
			||||||
 | 
					      def initialize(path)
 | 
				
			||||||
 | 
					        @path= path
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ##
 | 
				
			||||||
 | 
					      # Attempt to read in credentials from the specified file.
 | 
				
			||||||
 | 
					      def load_credentials
 | 
				
			||||||
 | 
					        if File.exists?(path) && File.readable?(path) && File.writable?(path)
 | 
				
			||||||
 | 
					          credentials = File.open(path, 'r') { |f| JSON.parse(f.read) }
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					        credentials
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ##
 | 
				
			||||||
 | 
					      # Write the credentials to the specified file.
 | 
				
			||||||
 | 
					      #
 | 
				
			||||||
 | 
					      # @param [Signet::OAuth2::Client] authorization
 | 
				
			||||||
 | 
					      #    Optional authorization instance. If not provided, the authorization
 | 
				
			||||||
 | 
					      #    already associated with this instance will be written.
 | 
				
			||||||
 | 
					      def write_credentials(credentials_hash)
 | 
				
			||||||
 | 
					        File.open(self.path, 'w') do |file|
 | 
				
			||||||
 | 
					          file.write(credentials_hash.to_json)
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,51 @@
 | 
				
			||||||
 | 
					# Copyright 2013 Google Inc.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# 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
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					#      http://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 'json'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module Google
 | 
				
			||||||
 | 
					  class APIClient
 | 
				
			||||||
 | 
					    class RedisStore
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      attr_accessor :redis
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ##
 | 
				
			||||||
 | 
					      # Initializes the RedisStore object.
 | 
				
			||||||
 | 
					      #
 | 
				
			||||||
 | 
					      # @params [Object] Redis instance
 | 
				
			||||||
 | 
					      def initialize(redis)
 | 
				
			||||||
 | 
					        @redis= redis
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ##
 | 
				
			||||||
 | 
					      # Attempt to read in credentials from redis.
 | 
				
			||||||
 | 
					      def load_credentials
 | 
				
			||||||
 | 
					        credentials = redis.get redis_credentials_key
 | 
				
			||||||
 | 
					        JSON.parse(credentials) if credentials
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      def redis_credentials_key
 | 
				
			||||||
 | 
					        "google_api_credentials"
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      ##
 | 
				
			||||||
 | 
					      # Write the credentials to redis.
 | 
				
			||||||
 | 
					      #
 | 
				
			||||||
 | 
					      # @params [Hash] credentials
 | 
				
			||||||
 | 
					      def write_credentials(credentials_hash)
 | 
				
			||||||
 | 
					        redis.set(redis_credentials_key, credentials_hash.to_json)
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
		Loading…
	
		Reference in New Issue