Skip to content

chore(snippets): updated code samples to use ADC and supported APIs#752

Open
lahirumaramba wants to merge 1 commit intodevfrom
lm-update-snippet
Open

chore(snippets): updated code samples to use ADC and supported APIs#752
lahirumaramba wants to merge 1 commit intodevfrom
lm-update-snippet

Conversation

@lahirumaramba
Copy link
Member

Updated code samples in the snippets directory to use Application Default Credentials (ADC) wherever appropriate instead of requiring explicit service account keys. Also replaced the deprecated messaging APIs SendAll and SendMulticast with their modern, supported equivalents SendEach and SendEachForMulticast.

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors several code samples to align with best practices and updated API standards. It transitions Firebase app initialization from explicit service account keys to Application Default Credentials, simplifying authentication. Additionally, it modernizes Firebase Messaging usage by replacing deprecated batch and multicast send functions with their currently supported counterparts, ensuring the samples remain functional and demonstrate recommended API usage.

Highlights

  • Application Default Credentials (ADC): Updated code samples in snippets/db.go and snippets/storage.go to use Application Default Credentials (ADC) for Firebase app initialization, removing the explicit requirement for service account key files.
  • Firebase Messaging API Updates: Replaced deprecated Firebase Messaging APIs SendAll and SendMulticast with their modern equivalents, SendEach and SendEachForMulticast, in snippets/messaging.go.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • snippets/db.go
    • Removed the google.golang.org/api/option import.
    • Updated authenticateWithAdminPrivileges to initialize the Firebase app without option.WithCredentialsFile.
    • Updated authenticateWithLimitedPrivileges to initialize the Firebase app without option.WithCredentialsFile.
    • Updated authenticateWithGuestPrivileges to initialize the Firebase app without option.WithCredentialsFile.
  • snippets/messaging.go
    • Replaced client.SendAll with client.SendEach in the sendAll function.
    • Replaced client.SendMulticast with client.SendEachForMulticast in the sendMulticast function.
    • Replaced client.SendMulticast with client.SendEachForMulticast in the sendMulticastAndHandleErrors function.
  • snippets/storage.go
    • Removed the google.golang.org/api/option import.
    • Updated cloudStorage to initialize the Firebase app without option.WithCredentialsFile.
Activity
  • No specific activity (comments, reviews, progress) was provided in the context for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

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

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 updates code samples to use Application Default Credentials (ADC) and modern Firebase APIs, which is a great improvement. The changes to use ADC in db.go and storage.go are correct. The updates in messaging.go to replace deprecated SendAll and SendMulticast APIs are also correct.

My review includes a few suggestions for snippets/messaging.go to improve context handling and address code duplication that arose from these changes. Specifically, several functions are not propagating the context they receive, and some functions have become duplicates of others.

}

br, err := client.SendAll(context.Background(), messages)
br, err := client.SendEach(context.Background(), messages)

Choose a reason for hiding this comment

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

medium

The function receives a ctx context.Context parameter, but context.Background() is used here. To allow for proper context propagation (for cancellation, deadlines, tracing, etc.), you should use the ctx variable that is passed into the function.

Suggested change
br, err := client.SendEach(context.Background(), messages)
br, err := client.SendEach(ctx, messages)

}

br, err := client.SendAll(context.Background(), messages)
br, err := client.SendEach(context.Background(), messages)

Choose a reason for hiding this comment

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

medium

With this change, the sendAll function has become functionally identical to the existing sendEach function. This introduces code duplication, which can make maintenance more difficult. Please consider refactoring to remove the duplication, for example by removing this function and updating callers to use sendEach.

}

br, err := client.SendMulticast(context.Background(), message)
br, err := client.SendEachForMulticast(context.Background(), message)

Choose a reason for hiding this comment

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

medium

The function receives a ctx context.Context parameter, but context.Background() is used here. To allow for proper context propagation (for cancellation, deadlines, tracing, etc.), you should use the ctx variable that is passed into the function.

Suggested change
br, err := client.SendEachForMulticast(context.Background(), message)
br, err := client.SendEachForMulticast(ctx, message)

}

br, err := client.SendMulticast(context.Background(), message)
br, err := client.SendEachForMulticast(context.Background(), message)

Choose a reason for hiding this comment

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

medium

The function receives a ctx context.Context parameter, but context.Background() is used here. To allow for proper context propagation (for cancellation, deadlines, tracing, etc.), you should use the ctx variable that is passed into the function.

Suggested change
br, err := client.SendEachForMulticast(context.Background(), message)
br, err := client.SendEachForMulticast(ctx, message)

}

br, err := client.SendMulticast(context.Background(), message)
br, err := client.SendEachForMulticast(context.Background(), message)

Choose a reason for hiding this comment

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

medium

With this change, the sendMulticastAndHandleErrors function has become functionally identical to the existing sendEachForMulticastAndHandleErrors function. This introduces code duplication. To improve maintainability, please consider removing this function and updating any callers to use sendEachForMulticastAndHandleErrors.

// [END send_to_condition_golang]
}

func sendAll(ctx context.Context, client *messaging.Client) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can remove these 3 snippets completely since there are no longer referenced in devsite and we already have the migrated version of the snippets.

https://firebase.google.com/docs/cloud-messaging/send/admin-sdk#go_1

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants