-
Notifications
You must be signed in to change notification settings - Fork 401
Fcm GAPIC type conversion #1354
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
maceonthompson
merged 36 commits into
maceo-fcm-gapic-client
from
maceo-fcm-gapic-type-conversion
Aug 9, 2021
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
8fe5e05
Import generated types & convert Message -> generated Message
maceonthompson a005e7d
Add JSDoc to `convertToClientMessage` method
maceonthompson f7e0b1b
Fix error where private Message type was used as object
maceonthompson 96a5675
Add support for different public Message types
maceonthompson de08189
Add `getAppKey()` as a precursor to using the generated client
maceonthompson b53df09
Switch to using generated client in `send()`
maceonthompson b9f6913
Remove TODOS
maceonthompson 5c86e2c
Temporary adjustment to generated client to avoid errors at runtime.
maceonthompson a81cdc2
Import generated types & convert Message -> generated Message
maceonthompson 3bc7f20
Add JSDoc to `convertToClientMessage` method
maceonthompson 91f00f2
Fix error where private Message type was used as object
maceonthompson 66b6a18
Add support for different public Message types
maceonthompson 69173a4
Add `getAppKey()` as a precursor to using the generated client
maceonthompson 01ddff8
Switch to using generated client in `send()`
maceonthompson f76848b
Remove TODOS
maceonthompson 4acfe87
Temporary adjustment to generated client to avoid errors at runtime.
maceonthompson f0225a9
Merge branch 'maceo-fcm-gapic-type-conversion' of github.com:firebase…
maceonthompson 7d1dfa4
Convert send non error expects to use sinon stub
maceonthompson 73da589
refactor Sinon behavior to be more in line with original code
maceonthompson 43b9cde
Add `createFirebaseErrorFromGapicError`
maceonthompson 4cdb520
Add error handling for `send()` with the generated client
maceonthompson 146bfa1
Simplify `createFirebaseErrorFromGapicError`
maceonthompson 134c621
Address casting comment on PR in messaging.ts
maceonthompson cc435ab
Fix unit tests to accomodate new generated client.
maceonthompson 4bd6059
Use the same `client` instance for multiple calls
maceonthompson b798f9d
Remove commented out/redundant tests
maceonthompson aac6778
Remove redundancy in `send` test `afterEach` logic
maceonthompson d5347ea
Add test for send error properties in addition to rejection message
maceonthompson 393760d
Address comments on message serialization
maceonthompson f766736
Add missing return statement
maceonthompson 60a2bef
Move gapic imports closer to other imports
maceonthompson 453cae6
Use original type names from generated client
maceonthompson b7f0b9b
Instantiate client with projectId instead of having client infer it
maceonthompson c2e7dc9
Fix typo on 462, examle -> example
maceonthompson 4fcd759
Address redudant gapic return constant by making it module-level
maceonthompson 25da011
Remove commented out code + unnecessary function call
maceonthompson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,12 @@ import { validateMessage, BLACKLISTED_DATA_PAYLOAD_KEYS, BLACKLISTED_OPTIONS_KEY | |
import { messaging } from './index'; | ||
import { FirebaseMessagingRequestHandler } from './messaging-api-request-internal'; | ||
import { ErrorInfo, MessagingClientErrorCode, FirebaseMessagingError } from '../utils/error'; | ||
import { createFirebaseErrorFromGapicError } from './messaging-errors-internal'; | ||
import { ServiceAccountCredential } from '../credential/credential-internal'; | ||
import * as utils from '../utils'; | ||
import * as validator from '../utils/validator'; | ||
import * as protos from '../generated/messaging/protos/protos'; | ||
import { FcmServiceClient } from '../generated/messaging/src/v1/fcm_service_client' | ||
|
||
import MessagingInterface = messaging.Messaging; | ||
import Message = messaging.Message; | ||
|
@@ -41,6 +45,10 @@ import MessagingConditionResponse = messaging.MessagingConditionResponse; | |
import DataMessagePayload = messaging.DataMessagePayload; | ||
import NotificationMessagePayload = messaging.NotificationMessagePayload; | ||
|
||
// GAPIC Generated client types | ||
import IMessage = protos.google.firebase.fcm.v1.IMessage; | ||
import ISendRequest = protos.google.firebase.fcm.v1.ISendMessageRequest; | ||
|
||
/* eslint-disable @typescript-eslint/camelcase */ | ||
|
||
// FCM endpoints | ||
|
@@ -193,6 +201,7 @@ export class Messaging implements MessagingInterface { | |
private urlPath: string; | ||
private readonly appInternal: FirebaseApp; | ||
private readonly messagingRequestHandler: FirebaseMessagingRequestHandler; | ||
private fcmServiceClient: FcmServiceClient; | ||
|
||
/** | ||
* Gets the {@link messaging.Messaging `Messaging`} service for the | ||
|
@@ -228,6 +237,77 @@ export class Messaging implements MessagingInterface { | |
return this.appInternal; | ||
} | ||
|
||
|
||
/** | ||
* Converts from the public {@link messaging.Message `Message`} type to the | ||
* internal/generated {@link protos.google.firebase.fcm.v1.Message `clientMessage`} | ||
* type. | ||
* | ||
* @param message The {@link messaging.Message `Message`} to be converted | ||
* @returns A {@link protos.google.firebase.fcm.v1.Message `clientMessage`}, | ||
* where like fields are the same as the inptuted message. | ||
*/ | ||
private convertToIMessage(message: Message): IMessage { | ||
//TODO: Add conversion for android, webpush, apns | ||
|
||
const convertedMessage: IMessage = { | ||
data: message.data, | ||
notification: message.notification, | ||
fcmOptions: message.fcmOptions | ||
} | ||
|
||
if ('token' in message) { | ||
convertedMessage.token = message.token; | ||
} else if ('topic' in message) { | ||
convertedMessage.topic = message.topic; | ||
} else if ('condition' in message) { | ||
convertedMessage.condition = message.condition; | ||
} | ||
|
||
return convertedMessage; | ||
} | ||
|
||
/** | ||
* Method that returns a fully instantiated service client. | ||
* | ||
* @returns an instantiated service client with either the application's | ||
* service account credentials or the default credentials | ||
*/ | ||
private getFcmServiceClient(): Promise<FcmServiceClient> { | ||
if (this.fcmServiceClient) { | ||
return Promise.resolve(this.fcmServiceClient); | ||
} | ||
|
||
const credential = this.app.options.credential; | ||
const options: { credentials?: object; fallback: 'rest'; projectId?: string } = { | ||
fallback: 'rest' | ||
}; | ||
|
||
if (credential instanceof ServiceAccountCredential) { | ||
options.credentials = { | ||
private_key: credential.privateKey, | ||
client_email: credential.clientEmail | ||
}; | ||
} | ||
|
||
return utils.findProjectId(this.appInternal).then(projectId => { | ||
if (!validator.isNonEmptyString(projectId)) { | ||
// Assert for an explicit project ID (either via AppOptions or the cert itself). | ||
throw new FirebaseMessagingError( | ||
MessagingClientErrorCode.INVALID_ARGUMENT, | ||
'Failed to determine project ID for Messaging. Initialize the ' | ||
+ 'SDK with service account credentials or set project ID as an app option. ' | ||
+ 'Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.', | ||
); | ||
} | ||
|
||
options.projectId = projectId; | ||
}).then(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can drop this line safely, and it would still work. |
||
this.fcmServiceClient = new FcmServiceClient(options); | ||
return this.fcmServiceClient; | ||
}) | ||
} | ||
|
||
/** | ||
* Sends the given message via FCM. | ||
* | ||
|
@@ -245,16 +325,27 @@ export class Messaging implements MessagingInterface { | |
throw new FirebaseMessagingError( | ||
MessagingClientErrorCode.INVALID_ARGUMENT, 'dryRun must be a boolean'); | ||
} | ||
return this.getUrlPath() | ||
.then((urlPath) => { | ||
const request: { message: Message; validate_only?: boolean } = { message: copy }; | ||
if (dryRun) { | ||
request.validate_only = true; | ||
} | ||
return this.messagingRequestHandler.invokeRequestHandler(FCM_SEND_HOST, urlPath, request); | ||
}) | ||
.then((response) => { | ||
return (response as any).name; | ||
|
||
const IMessage = this.convertToIMessage(copy); | ||
|
||
return this.getFcmServiceClient() | ||
.then(client => { | ||
return client.getProjectId() | ||
.then((projectId) => { | ||
const parent = `projects/${projectId}`; | ||
|
||
const request: ISendRequest = { | ||
parent: parent, | ||
message: IMessage, | ||
validateOnly: dryRun | ||
}; | ||
return client.sendMessage(request); | ||
}) | ||
.then(([response]) => { | ||
return response.name!; | ||
}).catch(err => { | ||
throw createFirebaseErrorFromGapicError(err); | ||
}); | ||
}); | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.