我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用oauth2client.client.Credentials()。
def locked_get(self): """Retrieve Credential from datastore. Returns: oauth2client.Credentials """ credentials = None if self._cache: json = self._cache.get(self._key_name) if json: credentials = client.Credentials.new_from_json(json) if credentials is None: entity = self._get_entity() if entity is not None: credentials = getattr(entity, self._property_name) if self._cache: self._cache.set(self._key_name, credentials.to_json()) if credentials and hasattr(credentials, 'set_store'): credentials.set_store(self) return credentials
def _from_base_type(self, value): """Converts our stored JSON string back to the desired type. Args: value: A value from the datastore to be converted to the desired type. Returns: A deserialized Credentials (or subclass) object, else None if the value can't be parsed. """ if not value: return None try: # Uses the from_json method of the implied class of value credentials = client.Credentials.new_from_json(value) except ValueError: credentials = None return credentials
def test__to_json_with_strip(self): credentials = client.Credentials() credentials.foo = 'bar' credentials.baz = 'quux' to_strip = ['foo'] json_payload = credentials._to_json(to_strip) # str(bytes) in Python2 and str(unicode) in Python3 self.assertIsInstance(json_payload, str) payload = json.loads(json_payload) expected_payload = { '_class': client.Credentials.__name__, '_module': client.Credentials.__module__, 'token_expiry': None, 'baz': credentials.baz, } self.assertEqual(payload, expected_payload)
def test__to_json_to_serialize(self): credentials = client.Credentials() to_serialize = { 'foo': b'bar', 'baz': u'quux', 'st': set(['a', 'b']), } orig_vals = to_serialize.copy() json_payload = credentials._to_json([], to_serialize=to_serialize) # str(bytes) in Python2 and str(unicode) in Python3 self.assertIsInstance(json_payload, str) payload = json.loads(json_payload) expected_payload = { '_class': client.Credentials.__name__, '_module': client.Credentials.__module__, 'token_expiry': None, } expected_payload.update(to_serialize) # Special-case the set. expected_payload['st'] = list(expected_payload['st']) # Special-case the bytes. expected_payload['foo'] = u'bar' self.assertEqual(payload, expected_payload) # Make sure the method call didn't modify our dictionary. self.assertEqual(to_serialize, orig_vals)
def make_value_from_datastore(self, value): logger.info("make: Got type " + str(type(value))) if value is None: return None if len(value) == 0: return None try: credentials = client.Credentials.new_from_json(value) except ValueError: credentials = None return credentials
def validate(self, value): value = super(CredentialsProperty, self).validate(value) logger.info("validate: Got type " + str(type(value))) if value is not None and not isinstance(value, client.Credentials): raise db.BadValueError( 'Property {0} must be convertible ' 'to a Credentials instance ({1})'.format(self.name, value)) return value
def locked_put(self, credentials): """Write a Credentials to the datastore. Args: credentials: Credentials, the credentials to store. """ entity = self._model.get_or_insert(self._key_name) setattr(entity, self._property_name, credentials) entity.put() if self._cache: self._cache.set(self._key_name, credentials.to_json())
def get_credentials(self): """A thread local Credentials object. Returns: A client.Credentials object, or None if credentials hasn't been set in this thread yet, which may happen when calling has_credentials inside oauth_aware. """ return getattr(self._tls, 'credentials', None)
def _validate(self, value): """Validates a value as a proper credentials object. Args: value: A value to be set on the property. Raises: TypeError if the value is not an instance of Credentials. """ _LOGGER.info('validate: Got type %s', type(value)) if value is not None and not isinstance(value, client.Credentials): raise TypeError( 'Property {0} must be convertible to a credentials ' 'instance; received: {1}.'.format(self._name, value))
def setUp(self): self.fake_model = tests_models.CredentialsModel() self.fake_model_field = self.fake_model._meta.get_field('credentials') self.field = models.CredentialsField(null=True) self.credentials = client.Credentials() self.pickle_str = _helpers._from_bytes( base64.b64encode(pickle.dumps(self.credentials))) self.jsonpickle_str = _helpers._from_bytes( base64.b64encode(jsonpickle.encode(self.credentials).encode()))
def test_field_unpickled(self): self.assertIsInstance( self.field.to_python(self.pickle_str), client.Credentials)
def test_field_jsonunpickled(self): self.assertIsInstance( self.field.to_python(self.jsonpickle_str), client.Credentials)
def test_field_already_unpickled(self): self.assertIsInstance( self.field.to_python(self.credentials), client.Credentials)
def test_from_db_value(self): value = self.field.from_db_value( self.pickle_str, None, None, None) self.assertIsInstance(value, client.Credentials)
def test_validate_success(self, mock_logger): creds_prop = TestNDBModel.creds creds_val = client.Credentials() creds_prop._validate(creds_val) mock_logger.info.assert_called_once_with('validate: Got type %s', type(creds_val))
def test__to_base_type_valid_creds(self): creds_prop = TestNDBModel.creds creds = client.Credentials() creds_json = json.loads(creds_prop._to_base_type(creds)) self.assertDictEqual(creds_json, { '_class': 'Credentials', '_module': 'oauth2client.client', 'token_expiry': None, })
def test__from_base_type_valid_creds(self): creds_prop = TestNDBModel.creds creds_json = json.dumps({ '_class': 'Credentials', '_module': 'oauth2client.client', 'token_expiry': None, }) creds = creds_prop._from_base_type(creds_json) self.assertIsInstance(creds, client.Credentials)
def test_to_from_json(self): credentials = client.Credentials() json = credentials.to_json() client.Credentials.new_from_json(json)
def test_authorize_abstract(self): credentials = client.Credentials() http = object() with self.assertRaises(NotImplementedError): credentials.authorize(http)
def test_revoke_abstract(self): credentials = client.Credentials() http = object() with self.assertRaises(NotImplementedError): credentials.revoke(http)
def test_apply_abstract(self): credentials = client.Credentials() headers = {} with self.assertRaises(NotImplementedError): credentials.apply(headers)
def test__to_json_basic(self): credentials = client.Credentials() json_payload = credentials._to_json([]) # str(bytes) in Python2 and str(unicode) in Python3 self.assertIsInstance(json_payload, str) payload = json.loads(json_payload) expected_payload = { '_class': client.Credentials.__name__, '_module': client.Credentials.__module__, 'token_expiry': None, } self.assertEqual(payload, expected_payload)
def test_new_from_json_no_data(self): creds_data = {} json_data = json.dumps(creds_data) with self.assertRaises(KeyError): client.Credentials.new_from_json(json_data)
def test_new_from_json_basic_data(self): creds_data = { '_module': 'oauth2client.client', '_class': 'Credentials', } json_data = json.dumps(creds_data) credentials = client.Credentials.new_from_json(json_data) self.assertIsInstance(credentials, client.Credentials)
def test_new_from_json_old_name(self): creds_data = { '_module': 'oauth2client.googleapiclient.client', '_class': 'Credentials', } json_data = json.dumps(creds_data) credentials = client.Credentials.new_from_json(json_data) self.assertIsInstance(credentials, client.Credentials)
def test_new_from_json_bad_module(self): creds_data = { '_module': 'oauth2client.foobar', '_class': 'Credentials', } json_data = json.dumps(creds_data) with self.assertRaises(ImportError): client.Credentials.new_from_json(json_data)
def test_new_from_json_bad_class(self): creds_data = { '_module': 'oauth2client.client', '_class': 'NopeNotCredentials', } json_data = json.dumps(creds_data) with self.assertRaises(AttributeError): client.Credentials.new_from_json(json_data)