Skip to content

Added X-Firebase-Client header to FCM API calls #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2019
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
7 changes: 6 additions & 1 deletion firebase_admin/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import requests
import six

import firebase_admin
from firebase_admin import _http_client
from firebase_admin import _messaging_utils
from firebase_admin import _utils
Expand Down Expand Up @@ -235,6 +236,7 @@ def __init__(self, app):
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')
self._client_version = 'fire-admin-python/{0}'.format(firebase_admin.__version__)

@classmethod
def encode_message(cls, message):
Expand All @@ -247,7 +249,10 @@ def send(self, message, dry_run=False):
if dry_run:
data['validate_only'] = True
try:
headers = {'X-GOOG-API-FORMAT-VERSION': '2'}
headers = {
'X-GOOG-API-FORMAT-VERSION': '2',
'X-FIREBASE-CLIENT': self._client_version,
}
resp = self._client.body(
'post', url=self._fcm_url, headers=headers, json=data, timeout=self._timeout)
except requests.exceptions.RequestException as error:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ def test_topic_management_timeout(self):
class TestSend(object):

_DEFAULT_RESPONSE = json.dumps({'name': 'message-id'})
_CLIENT_VERSION = 'fire-admin-python/{0}'.format(firebase_admin.__version__)

@classmethod
def setup_class(cls):
Expand Down Expand Up @@ -1210,6 +1211,7 @@ def test_send_dry_run(self):
assert recorder[0].method == 'POST'
assert recorder[0].url == self._get_url('explicit-project-id')
assert recorder[0].headers['X-GOOG-API-FORMAT-VERSION'] == '2'
assert recorder[0].headers['X-FIREBASE-CLIENT'] == self._CLIENT_VERSION
body = {
'message': messaging._MessagingService.encode_message(msg),
'validate_only': True,
Expand All @@ -1225,6 +1227,7 @@ def test_send(self):
assert recorder[0].method == 'POST'
assert recorder[0].url == self._get_url('explicit-project-id')
assert recorder[0].headers['X-GOOG-API-FORMAT-VERSION'] == '2'
assert recorder[0].headers['X-FIREBASE-CLIENT'] == self._CLIENT_VERSION
assert recorder[0]._extra_kwargs['timeout'] is None
body = {'message': messaging._MessagingService.encode_message(msg)}
assert json.loads(recorder[0].body.decode()) == body
Expand All @@ -1242,6 +1245,7 @@ def test_send_error(self, status):
assert recorder[0].method == 'POST'
assert recorder[0].url == self._get_url('explicit-project-id')
assert recorder[0].headers['X-GOOG-API-FORMAT-VERSION'] == '2'
assert recorder[0].headers['X-FIREBASE-CLIENT'] == self._CLIENT_VERSION
body = {'message': messaging._MessagingService.JSON_ENCODER.default(msg)}
assert json.loads(recorder[0].body.decode()) == body

Expand Down