PageRenderTime 24ms CodeModel.GetById 16ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/gdata/alt/app_engine.py

http://radioappz.googlecode.com/
Python | 101 lines | 50 code | 6 blank | 45 comment | 8 complexity | 5b61db5f77ea282786150f59e42ddcba MD5 | raw file
  1#!/usr/bin/python
  2#
  3# Copyright (C) 2009 Google Inc.
  4#
  5# Licensed under the Apache License, Version 2.0 (the "License");
  6# you may not use this file except in compliance with the License.
  7# You may obtain a copy of the License at
  8#
  9#      http://www.apache.org/licenses/LICENSE-2.0
 10#
 11# Unless required by applicable law or agreed to in writing, software
 12# distributed under the License is distributed on an "AS IS" BASIS,
 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14# See the License for the specific language governing permissions and
 15# limitations under the License.
 16
 17
 18"""Provides functions to persist serialized auth tokens in the datastore.
 19
 20The get_token and set_token functions should be used in conjunction with
 21gdata.gauth's token_from_blob and token_to_blob to allow auth token objects
 22to be reused across requests. It is up to your own code to ensure that the
 23token key's are unique.
 24"""
 25
 26__author__ = 'j.s@google.com (Jeff Scudder)'
 27
 28
 29from google.appengine.ext import db
 30from google.appengine.api import memcache
 31
 32
 33class Token(db.Model):
 34  """Datastore Model which stores a serialized auth token."""
 35  t = db.BlobProperty()
 36
 37
 38def get_token(unique_key):
 39  """Searches for a stored token with the desired key.
 40
 41  Checks memcache and then the datastore if required.
 42
 43  Args:
 44    unique_key: str which uniquely identifies the desired auth token.
 45
 46  Returns:
 47    A string encoding the auth token data. Use gdata.gauth.token_from_blob to
 48    convert back into a usable token object. None if the token was not found
 49    in memcache or the datastore.
 50  """
 51  token_string = memcache.get(unique_key)
 52  if token_string is None:
 53    # The token wasn't in memcache, so look in the datastore.
 54    token = Token.get_by_key_name(unique_key)
 55    if token is None:
 56      return None
 57    return token.t
 58  return token_string
 59
 60
 61def set_token(unique_key, token_str):
 62  """Saves the serialized auth token in the datastore.
 63
 64  The token is also stored in memcache to speed up retrieval on a cache hit.
 65
 66  Args:
 67    unique_key: The unique name for this token as a string. It is up to your
 68        code to ensure that this token value is unique in your application.
 69        Previous values will be silently overwitten.
 70    token_str: A serialized auth token as a string. I expect that this string
 71        will be generated by gdata.gauth.token_to_blob.
 72
 73  Returns:
 74    True if the token was stored sucessfully, False if the token could not be
 75    safely cached (if an old value could not be cleared). If the token was
 76    set in memcache, but not in the datastore, this function will return None.
 77    However, in that situation an exception will likely be raised.
 78
 79  Raises:
 80    Datastore exceptions may be raised from the App Engine SDK in the event of
 81    failure.
 82  """
 83  # First try to save in memcache.
 84  result = memcache.set(unique_key, token_str)
 85  # If memcache fails to save the value, clear the cached value.
 86  if not result:
 87    result = memcache.delete(unique_key)
 88    # If we could not clear the cached value for this token, refuse to save.
 89    if result == 0:
 90      return False
 91  # Save to the datastore.
 92  if Token(key_name=unique_key, t=token_str).put():
 93    return True
 94  return None
 95
 96
 97def delete_token(unique_key):
 98  # Clear from memcache.
 99  memcache.delete(unique_key)
100  # Clear from the datastore.
101  Token(key_name=unique_key).delete()