|
| 1 | +// Copyright 2026 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 | +// [START dartHttpImport] |
| 16 | +import 'package:firebase_functions/firebase_functions.dart'; |
| 17 | +// [END dartHttpImport] |
| 18 | + |
| 19 | +// [START dartHttpAdditionalImports] |
| 20 | +import 'dart:convert'; |
| 21 | +import 'package:intl/intl.dart'; |
| 22 | +// [END dartHttpAdditionalImports] |
| 23 | + |
| 24 | +// [START dartHttpAll] |
| 25 | +/// Returns the server's date. |
| 26 | +/// Options `timeoutSeconds` and `region` are optional. |
| 27 | +/// |
| 28 | +/// You must provide a `format` URL query parameter or `format` value in |
| 29 | +/// the request body with which we'll try to format the date. |
| 30 | +/// |
| 31 | +/// Format must follow the Dart intl library. See: https://pub.dev/packages/intl |
| 32 | +/// |
| 33 | +/// Example format: "MMMM d yyyy, h:mm:ss a". |
| 34 | +/// Example request using URL query parameters: |
| 35 | +/// https://date-<random-hash>.<region>.run.app?format=MMMM%20d%20yyyy%2C%20h%3Amm%3Ass%20a |
| 36 | +/// Example request using request body with cURL: |
| 37 | +/// curl -H 'Content-Type: application/json' / |
| 38 | +/// -d '{"format": "MMMM d yyyy, h:mm:ss a"}' / |
| 39 | +/// https://date-<random-hash>.<region>.run.app |
| 40 | +void main(List<String> args) async { |
| 41 | + await fireUp(args, (firebase) { |
| 42 | + // [START dartHttpTrigger] |
| 43 | + firebase.https.onRequest(name: 'date', (request) async { |
| 44 | + // [END dartHttpTrigger] |
| 45 | + |
| 46 | + // [START dartHttpSendError] |
| 47 | + // Forbidding PUT requests. |
| 48 | + if (request.method == 'PUT') { |
| 49 | + return Response.forbidden('Forbidden!'); |
| 50 | + } |
| 51 | + // [END dartHttpSendError] |
| 52 | + |
| 53 | + // Reading date format from URL query parameter. |
| 54 | + // [START dartHttpReadQueryParam] |
| 55 | + var format = request.url.queryParameters['format']; |
| 56 | + // [END dartHttpReadQueryParam] |
| 57 | + |
| 58 | + // Reading date format from request body query parameter |
| 59 | + if (format == null) { |
| 60 | + // [START dartHttpReadBodyParam] |
| 61 | + final bodyString = await request.readAsString(); |
| 62 | + try { |
| 63 | + if (bodyString.isNotEmpty) { |
| 64 | + final body = jsonDecode(bodyString) as Map<String, dynamic>; |
| 65 | + format = body['format'] as String?; |
| 66 | + } |
| 67 | + } catch (e) { |
| 68 | + return Response.badRequest(body: 'invalid JSON'); |
| 69 | + } |
| 70 | + // [END dartHttpReadBodyParam] |
| 71 | + } |
| 72 | + |
| 73 | + // Set a default format if none was provided |
| 74 | + format ??= 'MMMM d yyyy, h:mm:ss a'; |
| 75 | + |
| 76 | + // [START dartHttpSendResponse] |
| 77 | + final formattedDate = DateFormat(format).format(DateTime.now()); |
| 78 | + print('Sending formatted date: $formattedDate'); |
| 79 | + return Response.ok(formattedDate); |
| 80 | + // [END dartHttpSendResponse] |
| 81 | + }); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +// [END dartHttpAll] |
0 commit comments