Skip to content

Commit 81f7f17

Browse files
author
chenyumic
authored
Added sample for Using Environment Variables. (#1653)
* Added sample for Using Environment Variables. * Added default value.
1 parent 58522c2 commit 81f7f17

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

functions/env_vars/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Cloud Functions - Using Environment Variables sample
4+
5+
See:
6+
* [Cloud Functions Using Environment Variables tutorial][tutorial]
7+
* [Cloud Functions Using Environment Variables sample source code][code]
8+
[tutorial]: https://cloud.google.com/functions/docs/env-var#functions_env_var-python
9+
[code]: main.py

functions/env_vars/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
18+
# [START functions_env_vars]
19+
def env_vars(request):
20+
return os.environ.get('FOO', 'Specified environment variable is not set.')
21+
# [END functions_env_vars]

functions/env_vars/main_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import flask
18+
import pytest
19+
20+
import main
21+
22+
23+
# Create a fake "app" for generating test request contexts.
24+
@pytest.fixture(scope="module")
25+
def app():
26+
return flask.Flask(__name__)
27+
28+
29+
def test_env_vars(app):
30+
with app.test_request_context():
31+
os.environ['FOO'] = 'bar'
32+
res = main.env_vars(flask.request)
33+
assert res == 'bar'

0 commit comments

Comments
 (0)