Skip to content

Commit 688fcfa

Browse files
committed
Merge branch 'remote-config-versions'
Change-Id: Id10297670b17ba82251e1b98ec5d1c2ea506cdfc
2 parents 9a26b58 + bfbcf6f commit 688fcfa

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

config/README.md

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Firebase Remote Config REST API Python Quickstart
22
===============================================
33

4-
The Firebase Remote Config Python quickstart app demonstrates retrieving and
5-
updating the Firebase Remote Config template.
4+
The [Firebase Remote Config](https://firebase.google.com/docs/remote-config/) Python quickstart
5+
app demonstrates retrieving and updating the Firebase Remote Config template.
66

77
Introduction
88
------------
@@ -21,10 +21,11 @@ Getting started
2121
Run
2222
---
2323

24-
- From the `config` directory run `python configure.py --action=get` to retrieve the template.
25-
- The returned template is stored in a file named `config.json`.
26-
- Note the Etag printed to the console you will need to use it when publishing template updates.
27-
- Update the template.
24+
- Get active template
25+
- From the `config` directory run `python configure.py --action=get` to retrieve the template.
26+
- The returned template is stored in a file named `config.json`.
27+
- Note the Etag printed to the console you will need to use it when publishing template updates.
28+
- Update the template
2829
- If your template already has parameters, adjust one or more of the values.
2930
- If your template is empty, update it to look like this:
3031

@@ -58,28 +59,50 @@ Run
5859
}
5960
}
6061

61-
- From the `config` directory run `python configure.py --action=publish --etag=<LATEST_ETAG>` to update the template.
62-
- Be sure to set the etag to the one that was last printed in the console.
63-
- Confirm in the console that the template has been updated.
64-
- At this point mobile clients can fetch the updated values.
62+
- From the `config` directory run `python configure.py --action=publish --etag=<LATEST_ETAG>` to update the template.
63+
- Be sure to set the etag to the one that was last printed in the console.
64+
- Confirm in the console that the template has been updated.
65+
- At this point mobile clients can fetch the updated values.
66+
- View existing versions
67+
- From the `config` directory run `python configure.py --action=versions` to print the
68+
last 5 template versions.
69+
- Roll back to an existing template
70+
- From the `config` directory run `python configure.py --action=rollback --version=<TEMPLATE_VERSION_NUMBER>` to
71+
activate the template with the matching version number.
6572

6673
Best practices
6774
--------------
6875

6976
This section provides some additional information about how the Remote Config
7077
REST API should be used when retrieving and updating templates.
7178

72-
### Etags ###
79+
### [Versions](https://firebase.google.com/docs/remote-config/templates) ###
7380

74-
Eath time the Remote Config template is retrieved an Etag is included. This Etag is a
81+
Each time you update parameters, {{remote_config}} creates a
82+
new versioned {{remote_config}} template and stores the previous template as
83+
a version that you can retrieve or roll back to as needed.
84+
85+
All non-active versions expire and are removed if they are older than 90 days or if
86+
there are more than 300 newer template versions. Since template versions expire, any
87+
versions that need to be retrieved later on should be persisted externally.
88+
89+
Use the `listVersions` [query parameters](https://firebase.google.com/docs/reference/remote-config/rest/v1/projects.remoteConfig/listVersions#query-parameters)
90+
to filter the versions that are returned.
91+
92+
### ETags ###
93+
94+
Each time the Remote Config template it retrieved an ETag is included. This ETag is a
7595
unique identifier of the current template on the server. When submitting updates
76-
to the template you must include the latest Etag to ensure that your updates are consistent.
96+
to the template you must include the latest ETag to ensure that your updates are consistent.
7797

7898
In the event that you want to completely overwrite the server's template use
79-
an Etag of "\*". Use this with caution since this operation cannot be undone.
99+
an ETag of "\*". Use this with caution since this operation cannot be undone.
100+
101+
**NOTE:** To get the ETag your request must accept the gzip encoding. Add the header
102+
`Accept-Encoding: gzip` to receive the ETag in the response header `ETag`.
80103

81104
Support
82105
-------
83106

84-
- [Stack Overflow](https://stackoverflow.com/questions/tagged/firebase-cloud-messaging)
107+
- [Stack Overflow](https://stackoverflow.com/questions/tagged/firebase-remote-config)
85108
- [Firebase Support](https://firebase.google.com/support/)

config/configure.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from oauth2client.service_account import ServiceAccountCredentials
66

77

8-
PROJECT_ID = '<PROJECT_ID>'
8+
PROJECT_ID = 'PROJECT_ID'
99
BASE_URL = 'https://firebaseremoteconfig.googleapis.com'
1010
REMOTE_CONFIG_ENDPOINT = 'v1/projects/' + PROJECT_ID + '/remoteConfig'
1111
REMOTE_CONFIG_URL = BASE_URL + '/' + REMOTE_CONFIG_ENDPOINT
@@ -44,6 +44,41 @@ def _get():
4444
print('Unable to get template')
4545
print(resp.text)
4646

47+
def _listVersions():
48+
"""Print the last 5 Remote Config version's metadata."""
49+
headers = {
50+
'Authorization': 'Bearer ' + _get_access_token()
51+
}
52+
resp = requests.get(REMOTE_CONFIG_URL + ':listVersions?pageSize=5', headers=headers)
53+
54+
if resp.status_code == 200:
55+
print('Versions:')
56+
print(resp.text)
57+
else:
58+
print('Request to print template versions failed.')
59+
print(resp.text)
60+
61+
def _rollback(version):
62+
"""Roll back to an available version of Firebase Remote Config template.
63+
64+
:param version: The version of the template to roll back to.
65+
"""
66+
headers = {
67+
'Authorization': 'Bearer ' + _get_access_token()
68+
}
69+
70+
json = {
71+
"version_number": version
72+
}
73+
resp = requests.post(REMOTE_CONFIG_URL + ':rollback', headers=headers, json=json)
74+
75+
if resp.status_code == 200:
76+
print('Rolled back to version: ' + version)
77+
print(resp.text)
78+
print('ETag from server: {}'.format(resp.headers['ETag']))
79+
else:
80+
print('Request to roll back to version ' + version + ' failed.')
81+
print(resp.text)
4782

4883
def _publish(etag):
4984
"""Publish local template to Firebase server.
@@ -72,16 +107,23 @@ def main():
72107
parser = argparse.ArgumentParser()
73108
parser.add_argument('--action')
74109
parser.add_argument('--etag')
110+
parser.add_argument('--version')
75111
args = parser.parse_args()
76112

77113
if args.action and args.action == 'get':
78114
_get()
79115
elif args.action and args.action == 'publish' and args.etag:
80116
_publish(args.etag)
117+
elif args.action and args.action == 'versions':
118+
_listVersions()
119+
elif args.action and args.action == 'rollback' and args.version:
120+
_rollback(args.version)
81121
else:
82122
print('''Invalid command. Please use one of the following commands:
83123
python configure.py --action=get
84-
python configure.py --action=publish --etag=<LATEST_ETAG>''')
124+
python configure.py --action=publish --etag=<LATEST_ETAG>
125+
python configure.py --action=versions
126+
python configure.py --action=rollback --version=<TEMPLATE_VERSION_NUMBER>''')
85127

86128

87129

0 commit comments

Comments
 (0)