Skip to content

Commit

Permalink
Merge pull request #340 from olucurious/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
olucurious authored Jun 20, 2024
2 parents c519b15 + 19ea6c6 commit 062abf0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ from pyfcm import FCMNotification

fcm = FCMNotification(service_account_file="<service-account-json-path>", project_id="<project-id>")

# Google oauth2 credentials(such as ADC, impersonate credentials) can be used instead of service account file.

fcm = FCMNotification(
service_account_file=None, credentials=your_credentials, project_id="<project-id>"
)

# OR initialize with proxies

proxy_dict = {
Expand Down
2 changes: 1 addition & 1 deletion pyfcm/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__summary__ = "Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web)"
__url__ = "https://github.com/olucurious/pyfcm"

__version__ = "2.0.3"
__version__ = "2.0.4"

__author__ = "Emmanuel Adegbite"
__email__ = "[email protected]"
Expand Down
29 changes: 21 additions & 8 deletions pyfcm/baseapi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
import os
import time
Expand Down Expand Up @@ -26,8 +28,9 @@ class BaseAPI(object):

def __init__(
self,
service_account_file: str,
service_account_file: str | None,
project_id: str,
credentials=None,
proxy_dict=None,
env=None,
json_encoder=None,
Expand All @@ -38,6 +41,7 @@ def __init__(
Attributes:
service_account_file (str): path to service account JSON file
project_id (str): project ID of Google account
credentials (Credentials): Google oauth2 credentials instance, such as ADC
proxy_dict (dict): proxy settings dictionary, use proxy (keys: `http`, `https`)
env (dict): environment settings dictionary, for example "app_engine"
json_encoder (BaseJSONEncoder): JSON encoder
Expand All @@ -49,10 +53,11 @@ def __init__(
self.FCM_REQ_PROXIES = None
self.custom_adapter = adapter
self.thread_local = threading.local()
self.credentials = credentials

if not service_account_file:
if not service_account_file and not credentials:
raise AuthenticationError(
"Please provide a service account file path in the constructor"
"Please provide a service account file path or credentials in the constructor"
)

if (
Expand Down Expand Up @@ -85,7 +90,12 @@ def requests_session(self):
self.thread_local.requests_session = requests.Session()
self.thread_local.requests_session.mount("http://", adapter)
self.thread_local.requests_session.mount("https://", adapter)
self.thread_local.token_expiry = 0

current_timestamp = time.time()
if self.thread_local.token_expiry < current_timestamp:
self.thread_local.requests_session.headers.update(self.request_headers())
self.thread_local.token_expiry = current_timestamp + 1800
return self.thread_local.requests_session

def send_request(self, payload=None, timeout=None):
Expand Down Expand Up @@ -120,17 +130,20 @@ def send_async_request(self, params_list, timeout):

def _get_access_token(self):
"""
Generates access from refresh token that contains in the service_account_file.
Generates access token from credentials.
If token expires then new access token is generated.
Returns:
str: Access token
"""
# get OAuth 2.0 access token
try:
credentials = service_account.Credentials.from_service_account_file(
self.service_account_file,
scopes=["https://www.googleapis.com/auth/firebase.messaging"],
)
if self.service_account_file:
credentials = service_account.Credentials.from_service_account_file(
self.service_account_file,
scopes=["https://www.googleapis.com/auth/firebase.messaging"],
)
else:
credentials = self.credentials
request = google.auth.transport.requests.Request()
credentials.refresh(request)
return credentials.token
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
aiohttp>=3.6.2
cachetools==5.3.3
google-auth==2.29.0
google-auth==2.22.0
pyasn1==0.6.0
pyasn1-modules==0.4.0
rsa==4.9
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def test_push_service_without_credentials():
try:
FCMNotification(service_account_file="", project_id="")
FCMNotification(service_account_file="", project_id="", credentials=None)
assert False, "Should raise AuthenticationError without credentials"
except errors.AuthenticationError:
pass
Expand Down

0 comments on commit 062abf0

Please sign in to comment.