1
+ from __future__ import annotations
2
+
1
3
import json
2
4
import os
3
5
import time
@@ -26,8 +28,9 @@ class BaseAPI(object):
26
28
27
29
def __init__ (
28
30
self ,
29
- service_account_file : str ,
31
+ service_account_file : str | None ,
30
32
project_id : str ,
33
+ credentials = None ,
31
34
proxy_dict = None ,
32
35
env = None ,
33
36
json_encoder = None ,
@@ -38,6 +41,7 @@ def __init__(
38
41
Attributes:
39
42
service_account_file (str): path to service account JSON file
40
43
project_id (str): project ID of Google account
44
+ credentials (Credentials): Google oauth2 credentials instance, such as ADC
41
45
proxy_dict (dict): proxy settings dictionary, use proxy (keys: `http`, `https`)
42
46
env (dict): environment settings dictionary, for example "app_engine"
43
47
json_encoder (BaseJSONEncoder): JSON encoder
@@ -49,10 +53,11 @@ def __init__(
49
53
self .FCM_REQ_PROXIES = None
50
54
self .custom_adapter = adapter
51
55
self .thread_local = threading .local ()
56
+ self .credentials = credentials
52
57
53
- if not service_account_file :
58
+ if not service_account_file and not credentials :
54
59
raise AuthenticationError (
55
- "Please provide a service account file path in the constructor"
60
+ "Please provide a service account file path or credentials in the constructor"
56
61
)
57
62
58
63
if (
@@ -85,7 +90,12 @@ def requests_session(self):
85
90
self .thread_local .requests_session = requests .Session ()
86
91
self .thread_local .requests_session .mount ("http://" , adapter )
87
92
self .thread_local .requests_session .mount ("https://" , adapter )
93
+ self .thread_local .token_expiry = 0
94
+
95
+ current_timestamp = time .time ()
96
+ if self .thread_local .token_expiry < current_timestamp :
88
97
self .thread_local .requests_session .headers .update (self .request_headers ())
98
+ self .thread_local .token_expiry = current_timestamp + 1800
89
99
return self .thread_local .requests_session
90
100
91
101
def send_request (self , payload = None , timeout = None ):
@@ -120,17 +130,20 @@ def send_async_request(self, params_list, timeout):
120
130
121
131
def _get_access_token (self ):
122
132
"""
123
- Generates access from refresh token that contains in the service_account_file .
133
+ Generates access token from credentials .
124
134
If token expires then new access token is generated.
125
135
Returns:
126
136
str: Access token
127
137
"""
128
138
# get OAuth 2.0 access token
129
139
try :
130
- credentials = service_account .Credentials .from_service_account_file (
131
- self .service_account_file ,
132
- scopes = ["https://www.googleapis.com/auth/firebase.messaging" ],
133
- )
140
+ if self .service_account_file :
141
+ credentials = service_account .Credentials .from_service_account_file (
142
+ self .service_account_file ,
143
+ scopes = ["https://www.googleapis.com/auth/firebase.messaging" ],
144
+ )
145
+ else :
146
+ credentials = self .credentials
134
147
request = google .auth .transport .requests .Request ()
135
148
credentials .refresh (request )
136
149
return credentials .token
0 commit comments