Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
**/.firebase
**/.firebaserc
**/.runtimeconfig.json
**/functions.yaml
**/firestore-debug.log
*/npm-debug.log
lerna-debug.log
*~
Expand Down
5 changes: 5 additions & 0 deletions Dart/quickstarts/https-increment-number/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.dart_tool/
.packages
build/
*.dart_tool
pubspec.lock
48 changes: 48 additions & 0 deletions Dart/quickstarts/https-increment-number/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# HTTPS Increment Number Quickstart

This quickstart demonstrates how to handle HTTP requests for Cloud Functions using Dart.
It features two endpoints:
- `incrementLocal`: Simple POST request that takes a local count and returns it incremented by 1.
- `incrementSynced`: GET and POST endpoints that sync a counter variable to Firestore, returning the newly updated count.



## Local Testing

First, fetch dependencies:
```bash
dart pub get
```

Then, you can use the Firebase CLI to test the function locally. To ensure both endpoints work properly, make sure to start both the functions and firestore emulators:
```bash
firebase emulators:start --project="demo-example" --only firestore,functions
```

### Testing `incrementLocal`

In a separate terminal, use cURL to POST to the function (replace the URL with the one provided by the emulator):

```bash
curl -X POST http://127.0.0.1:5001/demo-example/us-central1/increment-local \
-H "Content-Type: application/json" \
-d '{"count": 5}'
```

You should see:
```json
{"message":"Local increment complete!","newCount":6}
```

### Testing `incrementSynced`

To test using Firestore, fetch the initial count:
```bash
curl -X GET http://127.0.0.1:5001/demo-example/us-central1/increment-synced
```

Then increment it using POST:
```bash
curl -X POST http://127.0.0.1:5001/demo-example/us-central1/increment-synced \
-H "Content-Type: application/json"
```
65 changes: 65 additions & 0 deletions Dart/quickstarts/https-increment-number/bin/server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'dart:convert';
import 'package:firebase_functions/firebase_functions.dart';
import 'package:google_cloud_firestore/google_cloud_firestore.dart'
show FieldValue;
import 'package:shared/shared.dart';

void main(List<String> args) async {
await fireUp(args, (firebase) {
// Listen for calls to the http request and name defined in the shared package.
firebase.https.onRequest(name: incrementCallable, (request) async {
// In a production app, verify the user with request.auth?.uid here.
print('Incrementing counter on the server...');

// Get firestore database instance
final firestore = firebase.adminApp.firestore();

// Get a reference to the counter document
final counterDoc = firestore.collection('counters').doc('global');

// Get the current snapshot for the count data
final snapshot = await counterDoc.get();

// Increment response we will send back
IncrementResponse incrementResponse;

// Check for the current count and if the snapshot exists
if (snapshot.data() case {'count': int value} when snapshot.exists) {
if (request.method == 'GET') {
// Get the current result
incrementResponse = IncrementResponse(
success: true,
message: 'Read-only sync complete',
newCount: value,
);
} else if (request.method == 'POST') {
// Increment count by one
final step = request.url.queryParameters['step'] as int? ?? 1;
await counterDoc.update({'count': FieldValue.increment(step)});
incrementResponse = IncrementResponse(
success: true,
message: 'Atomic increment complete',
newCount: value + 1,
);
} else {
return Response(405, body: 'Method Not Allowed');
}
} else {
// Create a new document with a count of 1
await counterDoc.set({'count': 1});
incrementResponse = const IncrementResponse(
success: true,
message: 'Cloud-sync complete',
newCount: 1,
);
}

// Return the response as JSON
return Response(
200,
body: jsonEncode(incrementResponse.toJson()),
headers: {'Content-Type': 'application/json'},
);
});
});
}
9 changes: 9 additions & 0 deletions Dart/quickstarts/https-increment-number/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"functions": {
"source": ".",
"codebase": "dart-quickstarts-https-increment-number"
},
"firestore": {
"rules": "firestore.rules"
}
}
8 changes: 8 additions & 0 deletions Dart/quickstarts/https-increment-number/firestore.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if true;
}
}
}
40 changes: 40 additions & 0 deletions Dart/quickstarts/https-increment-number/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: https_increment_number
description: HTTPS trigger examples showcasing state increment operations.
publish_to: none

environment:
sdk: ^3.11.0

dependencies:
shared:
path: ../shared
firebase_functions:
git:
url: https://github.com/firebase/firebase-functions-dart
ref: main
dart_firebase_admin:
git:
url: https://github.com/firebase/firebase-admin-dart
path: packages/dart_firebase_admin
ref: main
google_cloud_firestore:
git:
url: https://github.com/firebase/firebase-admin-dart
path: packages/google_cloud_firestore
ref: main

dev_dependencies:
build_runner: ^2.10.5
lints: ^6.0.0

dependency_overrides:
dart_firebase_admin:
git:
url: https://github.com/firebase/firebase-admin-dart
path: packages/dart_firebase_admin
ref: main
google_cloud_firestore:
git:
url: https://github.com/firebase/firebase-admin-dart
path: packages/google_cloud_firestore
ref: main
5 changes: 5 additions & 0 deletions Dart/quickstarts/shared/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.dart_tool/
.packages
build/
*.dart_tool
pubspec.lock
19 changes: 19 additions & 0 deletions Dart/quickstarts/shared/lib/shared.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class IncrementResponse {
final bool success;
final String message;
final int newCount;

const IncrementResponse({
required this.success,
required this.message,
required this.newCount,
});

Map<String, dynamic> toJson() => {
'success': success,
'message': message,
'newCount': newCount,
};
}

const String incrementCallable = 'incrementSynced';
6 changes: 6 additions & 0 deletions Dart/quickstarts/shared/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: shared
description: Shared classes.
publish_to: none

environment:
sdk: ^3.11.0
Loading