Skip to content

Commit ff67e08

Browse files
refactor: address PR review feedback
* Link from root README.md * Fix clone directory path in Dart sample README * Fix sample run.app url format in README * Fix sample run.app url format in sample documentation blocks * Return 400 Bad Request on invalid JSON * Apply a default format string of "MMMM d yyyy, h:mm:ss a" prior to the return block Co-authored-by: jhuleatt <3759507+jhuleatt@users.noreply.github.com>
1 parent ea51836 commit ff67e08

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

Dart/quickstarts/https-time-server/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Further reading:
1616

1717
### 1. Clone this repo
1818

19-
Clone or download this repo and open the `quickstarts/time-server` directory.
19+
Clone or download this repo and open the `Dart/quickstarts/https-time-server` directory.
2020

2121

2222
### 2. Create a Firebase project and configure the quickstart
@@ -67,21 +67,21 @@ Alteratively, you can call `firebase emulators:start` to test the functions on t
6767

6868
After deploying the function, check the CLI's output to see the URL for your function.
6969

70-
It will look something like: `https://<function-name>-<random-hash>-<region>.a.run.app`
70+
It will look something like: `https://<function-name>-<random-hash>.<region>.run.app`
7171

7272
You can also send the format in the request body. For instance using cURL in the command line:
7373

7474
```bash
7575
curl -H 'Content-Type: application/json' /
7676
-d '{"format": "MMMM d yyyy, h:mm:ss a"}' /
77-
<function url>/date
77+
<function url>
7878
```
7979
Formatted dates should be displayed.
8080

8181
We are responding with a 403 error in case of PUT requests:
8282

8383
```bash
84-
curl -X PUT -d '{"format": "MMMM d yyyy, h:mm:ss a"}' <function-url>/date
84+
curl -X PUT -d '{"format": "MMMM d yyyy, h:mm:ss a"}' <function-url>
8585
```
8686

8787

Dart/quickstarts/https-time-server/bin/server.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import 'package:intl/intl.dart';
3232
///
3333
/// Example format: "MMMM d yyyy, h:mm:ss a".
3434
/// Example request using URL query parameters:
35-
/// https://us-central1-<project-id>.cloudfunctions.net/date?format=MMMM%20d%20yyyy%2C%20h%3Amm%3Ass%20a
35+
/// https://date-<random-hash>.<region>.run.app?format=MMMM%20d%20yyyy%2C%20h%3Amm%3Ass%20a
3636
/// Example request using request body with cURL:
3737
/// curl -H 'Content-Type: application/json' /
3838
/// -d '{"format": "MMMM d yyyy, h:mm:ss a"}' /
39-
/// https://us-central1-<project-id>.cloudfunctions.net/date
39+
/// https://date-<random-hash>.<region>.run.app
4040
void main(List<String> args) async {
4141
await fireUp(args, (firebase) {
4242
// [START dartHttpTrigger]
@@ -65,15 +65,16 @@ void main(List<String> args) async {
6565
format = body['format'] as String?;
6666
}
6767
} catch (e) {
68-
// Ignore JSON parsing errors
68+
return Response.badRequest(body: 'invalid JSON');
6969
}
7070
// [END dartHttpReadBodyParam]
7171
}
7272

73+
// Set a default format if none was provided
74+
format ??= 'MMMM d yyyy, h:mm:ss a';
75+
7376
// [START dartHttpSendResponse]
74-
final formattedDate = format != null
75-
? DateFormat(format).format(DateTime.now())
76-
: DateTime.now().toString();
77+
final formattedDate = DateFormat(format).format(DateTime.now());
7778
print('Sending formatted date: $formattedDate');
7879
return Response.ok(formattedDate);
7980
// [END dartHttpSendResponse]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This quickstart sample demonstrates using **Cloud Functions** triggered by **Fir
3737

3838
### HTTPS trigger quickstart: Time Server
3939

40+
- [Dart](/Dart/quickstarts/https-time-server/)
4041
- [Node 2nd gen](/Node/quickstarts/https-time-server/)
4142
- [Python](/Python/quickstarts/https-time-server/)
4243
- [Node 1st gen](/Node-1st-gen/quickstarts/https-time-server/)

0 commit comments

Comments
 (0)