Skip to content

Commit 53983ff

Browse files
committed
First commit
1 parent 37306a9 commit 53983ff

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

gcloudfunctionhttprequest/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Google Cloud Function HTTP request Python example
2+
3+
This folder contains a Google Cloud Function example in Python on Google Cloud Platform (GCP).
4+
5+
It handles a Google Cloud Function that responds to an HTTP request.
6+
7+
## Requirements
8+
9+
* You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account.
10+
11+
* The code was written for Python 3 and Google Cloud Python Client Library.
12+
13+
## Using the code
14+
15+
* Access the Google Cloud console.
16+
17+
* Create a Google Cloud Function:
18+
* Name: `<CLOUD_FUNCTION_NAME>`
19+
* Memory allocated: `256 MB`
20+
* Trigger: `HTTP`
21+
* URL: `https://<GOOGLE_CLOUD_REGION>-<PROJECT>.cloudfunctions.net/<CLOUD_FUNCTION_NAME>`
22+
* Source code. You can use 2 options:
23+
* Inline editor:
24+
Edit the code of the `main.py` in the browser.
25+
* ZIP upload:
26+
Upload a ZIP file containing the `main.py` and `requirements.txt` files.
27+
* ZIP file: `<ZIP_LOCAL_NAME>`
28+
* Stage bucket: `<BUCKET_NAME_FOR_STAGGING>`
29+
* Runtime: `Python 3.7 (Beta)`
30+
* Function to execute: `http_request`
31+
* Region: `<GOOGLE_CLOUD_REGION>`
32+
* Timeout: `60 seconds`
33+
34+
* Save the Google Cloud Function.
35+
36+
The function is deployed and run.
37+
38+
* Test the function.
39+
40+
You can test it in 2 ways:
41+
42+
* First way: Test the funcion functionality in the Google Cloud console.
43+
44+
Go to the `Function details` and select `Testing`.
45+
46+
Enter the `Triggering event` content:
47+
48+
{
49+
"message": "Hello Peter!"
50+
}
51+
52+
Click `Test the function`.
53+
54+
You should see the next message in the Google Cloud `console output`:
55+
56+
```bash
57+
Hello Peter!
58+
```
59+
60+
* Second way: Test the function with a browser.
61+
62+
Go to the URL: `https://<GOOGLE_CLOUD_REGION>-<PROJECT>.cloudfunctions.net/<CLOUD_FUNCTION_NAME>` using a browser.
63+
64+
You should see the response:
65+
66+
```bash
67+
Hello World!
68+
```

gcloudfunctionhttprequest/main.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# main.py
4+
# It handles a Google Cloud Function that responds to an HTTP request.
5+
6+
def http_request(request):
7+
"""Responds to any HTTP request.
8+
Args:
9+
request (flask.Request): HTTP request object.
10+
Returns:
11+
The response text or any set of values that can be turned into a
12+
Response object using
13+
`make_response <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>`.
14+
"""
15+
16+
request_json = request.get_json()
17+
if request.args and 'message' in request.args:
18+
return request.args.get('message')
19+
elif request_json and 'message' in request_json:
20+
return request_json['message']
21+
else:
22+
return f'Hello World!'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Function dependencies, for example:
2+
# package>=version

0 commit comments

Comments
 (0)