Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ dist/
*~
scripts/cert.json
scripts/apikey.txt
htmlcov/
.pytest_cache/
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

-
- [changed] Improved error handling in FCM by mapping more server-side
errors to client-side error codes.

# v2.9.0

Expand Down
21 changes: 21 additions & 0 deletions firebase_admin/__about__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2017 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.

"""About information (version, etc) for Firebase Admin SDK."""

__version__ = '2.9.0'
__title__ = 'firebase_admin'
__author__ = 'Firebase'
__license__ = 'Apache License 2.0'
__url__ = 'https://firebase.google.com/docs/admin/setup/'
5 changes: 1 addition & 4 deletions firebase_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
import six

from firebase_admin import credentials
from firebase_admin.__about__ import __version__


# Declaring module version as per https://www.python.org/dev/peps/pep-0396/#specification
# Update this accordingly for each release.
__version__ = '2.9.0'

_apps = {}
_apps_lock = threading.RLock()
_clock = datetime.datetime.utcnow
Expand Down
21 changes: 17 additions & 4 deletions firebase_admin/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,18 @@ class _MessagingService(object):
INTERNAL_ERROR = 'internal-error'
UNKNOWN_ERROR = 'unknown-error'
FCM_ERROR_CODES = {
'APNS_AUTH_ERROR': 'authentication-error',
# FCM v1 canonical error codes
'NOT_FOUND': 'registration-token-not-registered',
'PERMISSION_DENIED': 'mismatched-credential',
'RESOURCE_EXHAUSTED': 'message-rate-exceeded',
'UNAUTHENTICATED': 'invalid-apns-credentials',

# FCM v1 new error codes
'APNS_AUTH_ERROR': 'invalid-apns-credentials',
'INTERNAL': INTERNAL_ERROR,
'INVALID_ARGUMENT': 'invalid-argument',
'QUOTA_EXCEEDED': 'message-rate-exceeded',
'SENDER_ID_MISMATCH': 'authentication-error',
'SENDER_ID_MISMATCH': 'mismatched-credential',
'UNAVAILABLE': 'server-unavailable',
'UNREGISTERED': 'registration-token-not-registered',
}
Expand All @@ -741,6 +748,7 @@ def __init__(self, app):
'GCLOUD_PROJECT environment variable.')
self._fcm_url = _MessagingService.FCM_URL.format(project_id)
self._client = _http_client.JsonHttpClient(credential=app.credential.get_credential())
self._timeout = app.options.get('httpTimeout')

@classmethod
def encode_message(cls, message):
Expand All @@ -753,7 +761,7 @@ def send(self, message, dry_run=False):
if dry_run:
data['validate_only'] = True
try:
resp = self._client.body('post', url=self._fcm_url, json=data)
resp = self._client.body('post', url=self._fcm_url, json=data, timeout=self._timeout)
except requests.exceptions.RequestException as error:
if error.response is not None:
self._handle_fcm_error(error)
Expand Down Expand Up @@ -784,7 +792,12 @@ def make_topic_management_request(self, tokens, topic, operation):
url = '{0}/{1}'.format(_MessagingService.IID_URL, operation)
try:
resp = self._client.body(
'post', url=url, json=data, headers=_MessagingService.IID_HEADERS)
'post',
url=url,
json=data,
headers=_MessagingService.IID_HEADERS,
timeout=self._timeout
)
except requests.exceptions.RequestException as error:
if error.response is not None:
self._handle_iid_error(error)
Expand Down
5 changes: 4 additions & 1 deletion lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ set -o errexit
set -o nounset

SKIP_FOR_TESTS="redefined-outer-name,protected-access,missing-docstring,too-many-lines"
SKIP_FOR_SNIPPETS="${SKIP_FOR_TESTS},reimported,unused-variable"

if [[ "$#" -eq 1 && "$1" = "all" ]]
then
CHECK_ALL=true
CHECK_ALL=true
elif [[ "$#" -eq 0 ]]
then
CHECK_ALL=false
Expand All @@ -49,8 +50,10 @@ then
lintAllFiles "firebase_admin" ""
lintAllFiles "tests" "$SKIP_FOR_TESTS"
lintAllFiles "integration" "$SKIP_FOR_TESTS"
lintAllFiles "snippets" "$SKIP_FOR_SNIPPETS"
else
lintChangedFiles "firebase_admin" ""
lintChangedFiles "tests" "$SKIP_FOR_TESTS"
lintChangedFiles "integration" "$SKIP_FOR_TESTS"
lintChangedFiles "snippets" "$SKIP_FOR_SNIPPETS"
fi
10 changes: 5 additions & 5 deletions scripts/prepare_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ if ! parseVersion "$VERSION"; then
exit 1
fi

CUR_VERSION=$(grep "^__version__ =" ../firebase_admin/__init__.py | awk '{print $3}' | sed "s/'//g")
CUR_VERSION=$(grep "^__version__ =" ../firebase_admin/__about__.py | awk '{print $3}' | sed "s/'//g")
if [ -z "$CUR_VERSION" ]; then
echo "[ERROR] Failed to find the current version. Check firebase_admin/__init__.py for version declaration."
echo "[ERROR] Failed to find the current version. Check firebase_admin/__version__.py for version declaration."
exit 1
fi
if ! parseVersion "$CUR_VERSION"; then
Expand Down Expand Up @@ -119,12 +119,12 @@ fi
##################################

HOST=$(uname)
echo "[INFO] Updating __init__.py and CHANGELOG.md"
echo "[INFO] Updating __about__.py and CHANGELOG.md"
if [ $HOST == "Darwin" ]; then
sed -i "" -e "s/__version__ = '$CUR_VERSION'/__version__ = '$VERSION'/" "../firebase_admin/__init__.py"
sed -i "" -e "s/__version__ = '$CUR_VERSION'/__version__ = '$VERSION'/" "../firebase_admin/__about__.py"
sed -i "" -e "1 s/# Unreleased//" "../CHANGELOG.md"
else
sed -i -e "s/__version__ = '$CUR_VERSION'/__version__ = '$VERSION'/" "../firebase_admin/__init__.py"
sed -i -e "s/__version__ = '$CUR_VERSION'/__version__ = '$VERSION'/" "../firebase_admin/__about__.py"
sed -i -e "1 s/# Unreleased//" "../CHANGELOG.md"
fi

Expand Down
21 changes: 12 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
from setuptools import find_packages
from setuptools import setup

import firebase_admin


if sys.version_info < (2, 7):
print('firebase_admin requires python2 version >= 2.7 or python3.', file=sys.stderr)
sys.exit(1)

# Read in the package meta data per recommendations from:
# https://packaging.python.org/guides/single-sourcing-package-version/
about_path = path.join(path.dirname(path.abspath(__file__)), 'firebase_admin', '__about__.py')
about = {}
with open(about_path) as fp:
exec(fp.read(), about) # pylint: disable=exec-used


long_description = ('The Firebase Admin Python SDK enables server-side (backend) Python developers '
'to integrate Firebase into their services and applications.')
Expand All @@ -43,16 +48,14 @@
':python_version<"3.4"': ('enum34>=1.0.4',),
}

version = firebase_admin.__version__

setup(
name='firebase_admin',
version=version,
name=about['__title__'],
version=about['__version__'],
description='Firebase Admin Python SDK',
long_description=long_description,
url='https://firebase.google.com/docs/admin/setup/',
author='Firebase',
license='Apache License 2.0',
url=about['__url__'],
author=about['__author__'],
license=about['__license__'],
keywords='firebase cloud development',
extras_require=extras_require,
install_requires=install_requires,
Expand Down
Empty file added snippets/__init__.py
Empty file.
Empty file added snippets/auth/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions snippets/auth/get_service_account_tokens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.

# [START get_service_account_tokens]
from firebase_admin import credentials

cred = credentials.Certificate('path/to/serviceAccountKey.json')
access_token_info = cred.get_access_token()

access_token = access_token_info.access_token
expiration_time = access_token_info.expiry
# Attach access_token to HTTPS request in the "Authorization: Bearer" header
# After expiration_time, you must generate a new access token
# [END get_service_account_tokens]

print 'The access token {} expires at {}'.format(access_token, expiration_time)
Loading