Skip to content

Commit cd62fb5

Browse files
committed
Add code snippets for Remote Config
1 parent 9cf1a35 commit cd62fb5

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package com.google.firebase.samples.config;
2+
3+
import com.google.auth.oauth2.GoogleCredentials;
4+
import com.google.firebase.FirebaseApp;
5+
import com.google.firebase.FirebaseOptions;
6+
import com.google.firebase.remoteconfig.Condition;
7+
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
8+
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException;
9+
import com.google.firebase.remoteconfig.ListVersionsPage;
10+
import com.google.firebase.remoteconfig.Parameter;
11+
import com.google.firebase.remoteconfig.ParameterGroup;
12+
import com.google.firebase.remoteconfig.ParameterValue;
13+
import com.google.firebase.remoteconfig.TagColor;
14+
import com.google.firebase.remoteconfig.Template;
15+
import com.google.firebase.remoteconfig.Version;
16+
17+
import java.io.FileInputStream;
18+
import java.io.IOException;
19+
import java.util.concurrent.ExecutionException;
20+
21+
/**
22+
* Remote Config snippets for documentation.
23+
*
24+
* See:
25+
* https://firebase.google.com/docs/remote-config/automate-rc
26+
*/
27+
public class RemoteConfigSnippets {
28+
29+
//Get the current Remote Config Template
30+
public static Template getRemoteConfig() throws ExecutionException, InterruptedException {
31+
// [START get_rc_template]
32+
Template template = FirebaseRemoteConfig.getInstance().getTemplateAsync().get();
33+
// See the ETag of the fetched template.
34+
System.out.println("ETag from server: " + template.getETag());
35+
// [END get_rc_template]
36+
return template;
37+
}
38+
39+
// Modify Remote Config parameters
40+
public static void addParameterToGroup(Template template) {
41+
// [START add_rc_parameter]
42+
template.getParameterGroups().get("new_menu").getParameters()
43+
.put("spring_season", new Parameter()
44+
.setDefaultValue(ParameterValue.inAppDefault())
45+
.setDescription("spring season menu visibility.")
46+
);
47+
// [END add_rc_parameter]
48+
}
49+
50+
// Modify Remote Config conditions
51+
public static void addNewCondition(Template template) {
52+
// [START add_rc_condition]
53+
template.getConditions().add(new Condition("android_en",
54+
"device.os == 'android' && device.country in ['us', 'uk']", TagColor.BLUE));
55+
// [END add_rc_condition]
56+
}
57+
58+
// Validate the Remote Config template
59+
public static void validateTemplate(Template template) throws InterruptedException {
60+
// [START validate_rc_template]
61+
try {
62+
Template validatedTemplate = FirebaseRemoteConfig.getInstance()
63+
.validateTemplateAsync(template).get();
64+
System.out.println("Template was valid and safe to use");
65+
} catch (ExecutionException e) {
66+
if (e.getCause() instanceof FirebaseRemoteConfigException) {
67+
FirebaseRemoteConfigException rcError = (FirebaseRemoteConfigException) e.getCause();
68+
System.out.println("Template is invalid and cannot be published");
69+
System.out.println(rcError.getMessage());
70+
}
71+
}
72+
// [END validate_rc_template]
73+
}
74+
75+
// Publish the Remote Config template
76+
public static void publishTemplate(Template template) throws InterruptedException {
77+
// [START publish_rc_template]
78+
try {
79+
Template publishedTemplate = FirebaseRemoteConfig.getInstance()
80+
.publishTemplateAsync(template).get();
81+
System.out.println("Template has been published");
82+
// See the ETag of the published template.
83+
System.out.println("ETag from server: " + publishedTemplate.getETag());
84+
} catch (ExecutionException e) {
85+
if (e.getCause() instanceof FirebaseRemoteConfigException) {
86+
FirebaseRemoteConfigException rcError = (FirebaseRemoteConfigException) e.getCause();
87+
System.out.println("Unable to publish template.");
88+
System.out.println(rcError.getMessage());
89+
}
90+
}
91+
// [END publish_rc_template]
92+
}
93+
94+
/**
95+
* Remote Config snippets for Manage Remote Config template versions documentation.
96+
*
97+
* See:
98+
* https://firebase.google.com/docs/remote-config/templates
99+
*/
100+
// List all stored versions of the Remote Config template
101+
public static void listAllVersions() throws InterruptedException, ExecutionException {
102+
// [START list_all_versions]
103+
ListVersionsPage page = FirebaseRemoteConfig.getInstance().listVersionsAsync().get();
104+
while (page != null) {
105+
for (Version version : page.getValues()) {
106+
System.out.println("Version: " + version.getVersionNumber());
107+
}
108+
page = page.getNextPage();
109+
}
110+
111+
// Iterate through all versions. This will still retrieve versions in batches.
112+
page = FirebaseRemoteConfig.getInstance().listVersionsAsync().get();
113+
for (Version version : page.iterateAll()) {
114+
System.out.println("Version: " + version.getVersionNumber());
115+
}
116+
// [END list_all_versions]
117+
}
118+
119+
// Retrieve a specific version of the Remote Config template
120+
public static void getRemoteConfigAtVersion(long versionNumber) throws ExecutionException, InterruptedException {
121+
// [START get_rc_template_at_version]
122+
Template template = FirebaseRemoteConfig.getInstance().getTemplateAtVersionAsync(versionNumber).get();
123+
// See the ETag of the fetched template.
124+
System.out.println("Successfully fetched the template with ETag: " + template.getETag());
125+
// [END get_rc_template_at_version]
126+
}
127+
128+
// Roll back to a specific stored version of the Remote Config template
129+
public static void rollbackToVersion(long versionNumber) throws InterruptedException {
130+
// [START rollback_rc_template]
131+
try {
132+
Template template = FirebaseRemoteConfig.getInstance().rollbackAsync(versionNumber).get();
133+
System.out.println("Successfully rolled back to template version: " + versionNumber);
134+
System.out.println("New ETag: " + template.getETag());
135+
} catch (ExecutionException e) {
136+
if (e.getCause() instanceof FirebaseRemoteConfigException) {
137+
FirebaseRemoteConfigException rcError = (FirebaseRemoteConfigException) e.getCause();
138+
System.out.println("Error trying to rollback template.");
139+
System.out.println(rcError.getMessage());
140+
}
141+
}
142+
// [END rollback_rc_template]
143+
}
144+
145+
public static void main(String[] args) throws ExecutionException, InterruptedException {
146+
System.out.println("Hello, RemoteConfigSnippets!");
147+
148+
// Initialize Firebase
149+
try {
150+
// [START initialize]
151+
FileInputStream serviceAccount = new FileInputStream("service-account.json");
152+
FirebaseOptions options = FirebaseOptions.builder()
153+
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
154+
.build();
155+
FirebaseApp.initializeApp(options);
156+
// [END initialize]
157+
} catch (IOException e) {
158+
System.out.println("ERROR: invalid service account credentials. See README.");
159+
System.out.println(e.getMessage());
160+
161+
System.exit(1);
162+
}
163+
164+
// Smoke test
165+
getRemoteConfig();
166+
Template template = new Template();
167+
template.getParameterGroups().put("new_menu", new ParameterGroup());
168+
addParameterToGroup(template);
169+
addNewCondition(template);
170+
validateTemplate(template);
171+
publishTemplate(template);
172+
listAllVersions();
173+
getRemoteConfigAtVersion(6);
174+
rollbackToVersion(6);
175+
System.out.println("Done!");
176+
}
177+
}

0 commit comments

Comments
 (0)