Skip to content

Commit a6c2db5

Browse files
authored
New samples for Cloud Functions docs (GoogleCloudPlatform#132)
* Add HTTP Cloud Function. * Add more GCF http samples. * Change string * Added promise sample. * Add background functions tests. * Added more GCF tests. * Fix readme and make test work in Node 0.12 * Changed helloworld to camel case (GoogleCloudPlatform#133) * Changed helloworld to camel case (GoogleCloudPlatform#134) * Add BigQuery processing to SendGrid sample (still needs more tests). * Add basic auth check. * Add fix for property names * Make fixNames method recursive. * Changed helloworld to camel case (GoogleCloudPlatform#136) * Add small helloGET sample. * Tweak some hello world samples. * Finish sendgrid unit tests. * Add missing readme link. * Update comment. * Make sure a response gets sent. * Made requested fixes. * Final fixes. * Couple fixes to fix test flakiness.
1 parent 247e006 commit a6c2db5

29 files changed

+1555
-365
lines changed

functions/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ environment.
2020
## Samples
2121

2222
* [Hello World](helloworld/)
23+
* [Background](background/)
2324
* [Callbacks](messages/)
2425
* [Cloud Storage](gcs/)
2526
* [Cloud Datastore](datastore/)
2627
* [Cloud Pub/Sub](pubsub/)
27-
* [Dependencies](uid/)
28+
* [Dependencies](uuid/)
29+
* [HTTP](http/)
2830
* [Logging](log/)
2931
* [Modules](module/)
32+
* [OCR (Optical Character Recognition)](ocr/)
33+
* [SendGrid](sendgrid/)
File renamed without changes.

functions/background/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START helloworld]
17+
/**
18+
* Background Cloud Function.
19+
*
20+
* @param {Object} context Cloud Function context.
21+
* @param {Object} data Request data, provided by a trigger.
22+
* @param {string} data.message Message, provided by the trigger.
23+
*/
24+
exports.helloWorld = function helloWorld (context, data) {
25+
if (data.message === undefined) {
26+
// This is an error case, "message" is required
27+
context.failure('No message defined!');
28+
} else {
29+
// Everything is ok
30+
console.log(data.message);
31+
context.success();
32+
}
33+
};
34+
// [END helloworld]
35+
36+
// [START helloPromise]
37+
var request = require('request-promise');
38+
39+
/**
40+
* Background Cloud Function that returns a Promise.
41+
*
42+
* @param {Object} data Request data, provided by a trigger.
43+
* @returns {Promise}
44+
*/
45+
exports.helloPromise = function helloPromise (data) {
46+
return request({
47+
uri: data.endpoint
48+
});
49+
};
50+
// [END helloPromise]

functions/background/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "nodejs-docs-samples-functions",
3+
"description": "Node.js samples found on https://cloud.google.com",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
11+
},
12+
"dependencies": {
13+
"request-promise": "^3.0.0"
14+
}
15+
}

functions/helloworld/index.js

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,76 @@
1414
'use strict';
1515

1616
// [START helloworld]
17-
exports.helloworld = function (context, data) {
18-
context.success('Hello World!');
17+
/**
18+
* Cloud Function.
19+
*
20+
* @param {Object} context Cloud Function context.
21+
* @param {Object} data Request data, provided by a trigger.
22+
*/
23+
exports.helloWorld = function helloWorld (context, data) {
24+
console.log('My Cloud Function: ' + data.message);
25+
context.success();
1926
};
2027
// [END helloworld]
28+
29+
// [START helloGET]
30+
/**
31+
* HTTP Cloud Function.
32+
*
33+
* @param {Object} req Cloud Function request context.
34+
* @param {Object} res Cloud Function response context.
35+
*/
36+
exports.helloGET = function helloGET (req, res) {
37+
res.send('Hello World!');
38+
};
39+
// [END helloGET]
40+
41+
// [START helloHttp]
42+
/**
43+
* HTTP Cloud Function.
44+
*
45+
* @param {Object} req Cloud Function request context.
46+
* @param {Object} res Cloud Function response context.
47+
*/
48+
exports.helloHttp = function helloHttp (req, res) {
49+
res.send('Hello ' + (req.body.name || 'World') + '!');
50+
};
51+
// [END helloHttp]
52+
53+
// [START helloBackground]
54+
/**
55+
* Background Cloud Function.
56+
*
57+
* @param {Object} context Cloud Function context.
58+
* @param {Object} data Request data, provided by a trigger.
59+
*/
60+
exports.helloBackground = function helloBackground (context, data) {
61+
context.success('Hello ' + (data.name || 'World') + '!');
62+
};
63+
// [END helloBackground]
64+
65+
// [START helloPubSub]
66+
/**
67+
* Background Cloud Function to be triggered by Pub/Sub.
68+
*
69+
* @param {Object} context Cloud Function context.
70+
* @param {Object} data Request data, provided by a Pub/Sub trigger.
71+
*/
72+
exports.helloPubSub = function helloPubSub (context, data) {
73+
console.log('Hello ' + (data.name || 'World') + '!');
74+
context.success();
75+
};
76+
// [END helloPubSub]
77+
78+
// [START helloGCS]
79+
/**
80+
* Background Cloud Function to be triggered by Cloud Storage.
81+
*
82+
* @param {Object} context Cloud Function context.
83+
* @param {Object} data Request data, provided by a Cloud Storage trigger.
84+
*/
85+
exports.helloGCS = function helloGCS (context, data) {
86+
console.log('Hello ' + (data.name || 'World') + '!');
87+
context.success();
88+
};
89+
// [END helloGCS]

functions/http/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 HTTP sample
4+
5+
This recipe shows you how to respond to HTTP requests with a Cloud Function.
6+
7+
View the [source code][code].
8+
9+
[code]: index.js
10+
11+
## Deploy and Test
12+
13+
1. Follow the [Cloud Functions quickstart guide][quickstart] to setup Cloud
14+
Functions for your project.
15+
16+
1. Clone this repository:
17+
18+
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
19+
cd nodejs-docs-samples/functions/http
20+
21+
1. Create a Cloud Storage Bucket to stage our deployment:
22+
23+
gsutil mb gs://[YOUR_BUCKET_NAME]
24+
25+
* Replace `[YOUR_BUCKET_NAME]` with the name of your Cloud Storage Bucket.
26+
27+
1. Deploy the "helloGET" function with an HTTP trigger
28+
29+
gcloud alpha functions deploy publish --bucket [YOUR_BUCKET_NAME] --trigger-http
30+
31+
1. Call the "helloGET" function:
32+
33+
curl https://[YOUR_PROJECT_REGION].[YOUR_PROJECT_ID].cloudfunctions.net/helloGET
34+
35+
[quickstart]: https://cloud.google.com/functions/quickstart

functions/http/index.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START helloworld]
17+
/**
18+
* Responds to any HTTP request that can provide a "message" field in the body.
19+
*
20+
* @param {Object} req Cloud Function request context.
21+
* @param {Object} res Cloud Function response context.
22+
*/
23+
exports.helloWorld = function helloWorld (req, res) {
24+
if (req.body.message === undefined) {
25+
// This is an error case, as "message" is required
26+
res.status(400).send('No message defined!');
27+
} else {
28+
// Everything is ok
29+
console.log(req.body.message);
30+
res.status(200).end();
31+
}
32+
};
33+
// [END helloworld]
34+
35+
// [START helloHttp]
36+
function handleGET (req, res) {
37+
// Do something with the GET request
38+
res.status(200).send('Hello World!');
39+
}
40+
41+
function handlePUT (req, res) {
42+
// Do something with the PUT request
43+
res.status(403).send('Forbidden!');
44+
}
45+
46+
/**
47+
* Responds to a GET request with "Hello World!". Forbids a PUT request.
48+
*
49+
* @example
50+
* gcloud alpha functions call helloHttp
51+
*
52+
* @param {Object} req Cloud Function request context.
53+
* @param {Object} res Cloud Function response context.
54+
*/
55+
exports.helloHttp = function helloHttp (req, res) {
56+
switch (req.method) {
57+
case 'GET':
58+
handleGET(req, res);
59+
break;
60+
case 'PUT':
61+
handlePUT(req, res);
62+
break;
63+
default:
64+
res.status(500).send({ error: 'Something blew up!' });
65+
break;
66+
}
67+
};
68+
// [END helloHttp]
69+
70+
// [START helloContent]
71+
/**
72+
* Responds to any HTTP request that can provide a "message" field in the body.
73+
*
74+
* @param {Object} req Cloud Function request context.
75+
* @param {Object} res Cloud Function response context.
76+
*/
77+
exports.helloContent = function helloContent (req, res) {
78+
var name;
79+
80+
console.log(req.get('content-type'));
81+
switch (req.get('content-type')) {
82+
// '{"name":"John"}'
83+
case 'application/json':
84+
name = req.body.name;
85+
break;
86+
87+
// 'John', stored in a Buffer
88+
case 'application/octet-stream':
89+
name = req.body.toString(); // Convert buffer to a string
90+
break;
91+
92+
// 'John'
93+
case 'text/plain':
94+
name = req.body;
95+
break;
96+
97+
// 'name=John'
98+
case 'application/x-www-form-urlencoded':
99+
name = req.body.name;
100+
break;
101+
}
102+
103+
res.status(200).send('Hello ' + (name || 'World') + '!');
104+
};
105+
// [END helloContent]

functions/log2/README.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

functions/log2/index.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)