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