Skip to content

feat(fcm): Migrate topic management to FCM v1 API - #980

Open
lahirumaramba wants to merge 4 commits into
mainfrom
lm-fcm-topics
Open

lahirumaramba wants to merge 4 commits into
mainfrom
lm-fcm-topics

Conversation

@lahirumaramba

Copy link
Copy Markdown
Member

Migrates subscribe_to_topic and unsubscribe_from_topic in the messaging module from the legacy Instance ID (IID) API to the FCM v1 Topic Subscriptions API.

Key changes:

  • Updated subscribe_to_topic and unsubscribe_from_topic to call the FCM v1 endpoints (/v1/projects/{projectId}/registrations/{token}/topicSubscriptions).
  • Added asynchronous counterparts subscribe_to_topic_async and unsubscribe_from_topic_async utilizing HttpxAsyncClient (HTTP/2).
  • Added deprecated legacy methods subscribe_to_topic_legacy and unsubscribe_from_topic_legacy with deprecation warnings.
  • Bounded client-side concurrency to 100 simultaneous requests.
  • Handled idempotency: ALREADY_EXISTS (HTTP 409) is treated as success for topic subscriptions, while NOT_FOUND (HTTP 404) is recorded as a failure for topic unsubscriptions.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request migrates the topic management functionality in the Firebase Cloud Messaging module from the legacy Instance ID (IID) API to the FCM v1 API, introducing new synchronous and asynchronous methods (subscribe_to_topic, subscribe_to_topic_async, unsubscribe_from_topic, and unsubscribe_from_topic_async) while deprecating the legacy versions. The feedback highlights a potential concurrency race condition in the multi-threaded request execution where a shared headers dictionary is mutated, and suggests extracting duplicated error-handling logic into a single helper method to improve maintainability.

Comment thread firebase_admin/messaging.py
Comment thread firebase_admin/messaging.py Outdated
…ic error parsing

- Pass a copy of self._fcm_headers in topic management requests to prevent concurrent mutation race conditions in ThreadPoolExecutor.
- Extract common error parsing logic from _build_topic_subscription_result_from_requests_error and _build_topic_subscription_result_from_httpx_error into _build_topic_subscription_result.
- Hoist URL-encoded topic computation out of the per-token request loops.
- Mount an HTTPAdapter with a connection pool size of 100 on the FCM client session.
- Fix line length and method signature override lint warnings in test_messaging.py.
- Added type annotations to public topic management functions and internal methods
- Updated HTTP status code mapping (401->UNAUTHENTICATED, 503->UNAVAILABLE, 408/504->DEADLINE_EXCEEDED) and prioritized status codes over free-form message strings
- Updated topic regex with \Z to reject trailing newlines
- Passed header copies in send, send_each, and send_each_async to prevent concurrency mutations
- Added test coverage for status code mapping, async argument validation, prefixed topics, and async batch unsubscribe
@lahirumaramba

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request migrates the topic subscription and unsubscription features to the FCM v1 API, introducing both synchronous and asynchronous implementations (subscribe_to_topic, subscribe_to_topic_async, unsubscribe_from_topic, and unsubscribe_from_topic_async). The previous Instance ID API implementations have been renamed to subscribe_to_topic_legacy and unsubscribe_from_topic_legacy and marked as deprecated. Comprehensive unit tests have been added to cover the new functionality, validation rules, and error handling. There are no review comments to address, and I have no additional feedback to provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants