Skip to content

Commit 9740962

Browse files
committed
fix: Resolved issues from gemini review
1 parent 43f956a commit 9740962

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

firebase_admin/functions.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ def __init__(self, app: App):
117117
'projectId option, or use service account credentials. Alternatively, set the '
118118
'GOOGLE_CLOUD_PROJECT environment variable.')
119119

120-
if _get_emulator_host():
120+
self._emulator_host = _get_emulator_host()
121+
if self._emulator_host:
121122
self._credential = _utils.EmulatorAdminCredentials()
122123
else:
123124
self._credential = app.credential.get_credential()
@@ -127,7 +128,8 @@ def __init__(self, app: App):
127128
def task_queue(self, function_name: str, extension_id: Optional[str] = None) -> TaskQueue:
128129
"""Creates a TaskQueue instance."""
129130
return TaskQueue(
130-
function_name, extension_id, self._project_id, self._credential, self._http_client)
131+
function_name, extension_id, self._project_id, self._credential, self._http_client,
132+
self._emulator_host)
131133

132134
@classmethod
133135
def handle_functions_error(cls, error: Any):
@@ -143,7 +145,8 @@ def __init__(
143145
extension_id: Optional[str],
144146
project_id,
145147
credential,
146-
http_client
148+
http_client,
149+
emulator_host: Optional[str] = None
147150
) -> None:
148151

149152
# Validate function_name
@@ -152,6 +155,7 @@ def __init__(
152155
self._project_id = project_id
153156
self._credential = credential
154157
self._http_client = http_client
158+
self._emulator_host = emulator_host
155159
self._function_name = function_name
156160
self._extension_id = extension_id
157161
# Parse resources from function_name
@@ -198,7 +202,10 @@ def enqueue(self, task_data: Any, opts: Optional[TaskOptions] = None) -> str:
198202
if self._is_emulated():
199203
# Emulator returns a response with format {task: {name: <task_name>}}
200204
# The task name also has an extra '/' at the start compared to prod
201-
task_name = resp.get('task').get('name')[1:]
205+
task_info = resp.get('task') or {}
206+
task_name = task_info.get('name')
207+
if task_name:
208+
task_name = task_name[1:]
202209
else:
203210
# Production returns a response with format {name: <task_name>}
204211
task_name = resp.get('name')
@@ -221,6 +228,7 @@ def delete(self, task_id: str) -> None:
221228
the Cloud Functions service.
222229
ValueError: If the input arguments are invalid.
223230
"""
231+
_Validators.check_non_empty_string('task_id', task_id)
224232
emulator_url = self._get_emulator_url(self._resource)
225233
if emulator_url:
226234
service_url = emulator_url + f'/{task_id}'
@@ -354,15 +362,14 @@ def _update_task_payload(self, task: Task, resource: Resource, extension_id: str
354362
return task
355363

356364
def _get_emulator_url(self, resource: Resource):
357-
emulator_host = _get_emulator_host()
358-
if emulator_host:
359-
emulator_url_format = f'http://{emulator_host}/' + _CLOUD_TASKS_API_RESOURCE_PATH
365+
if self._emulator_host:
366+
emulator_url_format = f'http://{self._emulator_host}/' + _CLOUD_TASKS_API_RESOURCE_PATH
360367
url = self._get_url(resource, emulator_url_format)
361368
return url
362369
return None
363370

364371
def _is_emulated(self):
365-
return _get_emulator_host() is not None
372+
return self._emulator_host is not None
366373

367374

368375
class _Validators:

tests/test_functions.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,12 @@ def test_task_enqueue_without_emulator_host_error(self, monkeypatch):
255255
assert len(recorder) == 0
256256

257257
def test_get_emulator_url_invalid_format(self, monkeypatch):
258-
_, recorder = self._instrument_functions_service()
259258
monkeypatch.setenv('CLOUD_TASKS_EMULATOR_HOST', 'http://localhost:8124')
260-
queue = functions.task_queue('test-function-name')
259+
app = firebase_admin.initialize_app(
260+
testutils.MockCredential(), {'projectId': 'test-project'}, name='invalid-host-app')
261261
with pytest.raises(ValueError) as excinfo:
262-
queue.enqueue(_DEFAULT_DATA)
262+
functions.task_queue('test-function-name', app=app)
263263
assert 'Invalid CLOUD_TASKS_EMULATOR_HOST' in str(excinfo.value)
264-
assert len(recorder) == 0
265264

266265
class TestTaskQueueOptions:
267266

0 commit comments

Comments
 (0)