From 726b38ae6f853515b49a994ddc8326ac4246263c Mon Sep 17 00:00:00 2001 From: Maximiliano Capodacqua Date: Fri, 30 Jun 2023 15:00:34 -0500 Subject: [PATCH 1/5] Adding first draft of wipe collection php example --- php/wipe_appwrite_collection/composer.json | 11 ++++ php/wipe_appwrite_collection/index.php | 77 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 php/wipe_appwrite_collection/composer.json create mode 100644 php/wipe_appwrite_collection/index.php diff --git a/php/wipe_appwrite_collection/composer.json b/php/wipe_appwrite_collection/composer.json new file mode 100644 index 00000000..d304dbf0 --- /dev/null +++ b/php/wipe_appwrite_collection/composer.json @@ -0,0 +1,11 @@ +{ + "name": "user/function", + "description": "", + "type": "library", + "license": "ISC", + "authors": [], + "require": { + "php": ">=8.0.0", + "appwrite/appwrite": "7.0.*" + } +} diff --git a/php/wipe_appwrite_collection/index.php b/php/wipe_appwrite_collection/index.php new file mode 100644 index 00000000..719872e0 --- /dev/null +++ b/php/wipe_appwrite_collection/index.php @@ -0,0 +1,77 @@ + false, + 'message' => 'Payload is invalid.' + ]; + try { + $payload = \json_decode($req['payload'], true); + $databaseId = \trim($payload['databaseId']); + $collectionId = \trim($payload['collectionId']); + } catch(\Exception $err) { + \var_dump($err); + $res->json($invalPayloadResponse); + return; + } + + if(empty($databaseId) || empty($collectionId)) { + $res->json($invalPayloadResponse); + return; + } + + // Make sure we have envirnment variables required to execute + if(!$req['variables']['APPWRITE_FUNCTION_ENDPOINT'] || + !$req['variables']['APPWRITE_FUNCTION_PROJECT_ID'] || + !$req['variables']['APPWRITE_FUNCTION_API_KEY']) { + $res->json([ + 'success' => false, + 'message' => 'Please provide all required variables.' + ]); + return; + } + + $client = new Client(); + $databases = new Databases($client); + + + $client + ->setEndpoint($req['variables']['APPWRITE_FUNCTION_ENDPOINT']) + ->setProject($req['variables']['APPWRITE_FUNCTION_PROJECT_ID']) + ->setKey($req['variables']['APPWRITE_FUNCTION_API_KEY']); + + $done = false; + $sum = 0; + try { + while (!$done) { + + $documents = $databases->listDocuments($databaseId, $collectionId)['documents']; + foreach($documents as $document) { + \var_dump($document); + $databases->deleteDocument($databaseId, $collectionId, $document['$id']); + $sum++; + } + + if (count($documents) === 0) { + $done = true; + } + } + } catch(\Exception $err) { + $res->json([ + 'success' => false, + 'message' => $err->getMessage() + ]); + return; + } + + // Return success result + $res->json([ + 'success' => true, + 'sum' => $sum + ]); +}; From 0d5cdd2bd620fa322c80dd9fa76407657ae1e9f4 Mon Sep 17 00:00:00 2001 From: Maximiliano Capodacqua Date: Fri, 30 Jun 2023 15:05:07 -0500 Subject: [PATCH 2/5] Fixing parameter of json_decode for error control --- php/wipe_appwrite_collection/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/wipe_appwrite_collection/index.php b/php/wipe_appwrite_collection/index.php index 719872e0..5d00b3c5 100644 --- a/php/wipe_appwrite_collection/index.php +++ b/php/wipe_appwrite_collection/index.php @@ -11,7 +11,7 @@ 'message' => 'Payload is invalid.' ]; try { - $payload = \json_decode($req['payload'], true); + $payload = \json_decode($req['payload'], true, flags: JSON_THROW_ON_ERROR); $databaseId = \trim($payload['databaseId']); $collectionId = \trim($payload['collectionId']); } catch(\Exception $err) { From f07c89022302da4f0ddee23c17ed97e06617f895 Mon Sep 17 00:00:00 2001 From: Maximiliano Capodacqua Date: Tue, 11 Jul 2023 18:39:20 -0500 Subject: [PATCH 3/5] Adding README file --- php/wipe_appwrite_collection/README.md | 89 ++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 php/wipe_appwrite_collection/README.md diff --git a/php/wipe_appwrite_collection/README.md b/php/wipe_appwrite_collection/README.md new file mode 100644 index 00000000..960ad8c5 --- /dev/null +++ b/php/wipe_appwrite_collection/README.md @@ -0,0 +1,89 @@ +# Wipe an Appwrite collection + +A PHP Cloud Function that wipes out all the documents inside a collection. + +_Example input:_ + +```json +{ + "databaseId": "stage", + "collectionId": "profiles" +} +``` + +_Example output (success):_ + +```json +{ + "success": true, + "sum": 2 +} +``` + +_Example output (failure):_ + +```json +{ + "success": false, + "message": "Collection not found." +} +``` + +## 📝 Environment Variables + +List of environment variables used by this cloud function: + +- **APPWRITE_FUNCTION_ENDPOINT** - Endpoint of your Appwrite server +- **APPWRITE_FUNCTION_API_KEY** - Appwrite API Key +- **APPWRITE_FUNCTION_PROJECT_ID** - Appwrite project ID. If running on Appwrite, this variable is provided automatically. + +## 🚀 Deployment + +1. Clone this repository, and enter this function folder: + +``` +$ git clone https://github.com/open-runtimes/examples.git && cd examples +$ cd php/wipe_appwrite_collection +``` + +2. Enter this function folder and build the code: +``` +docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/php:v2-8.1 sh /usr/local/src/build.sh +``` +As a result, a `code.tar.gz` file will be generated. + +3. Start the Open Runtime: +``` +docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_ENTRYPOINT=index.php --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/php:v2-8.1 sh /usr/local/src/start.sh +``` + +Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit PHP runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/php-8.1). + +4. Execute function: + +``` +curl http://localhost:3000/ -d '{"variables":{"APPWRITE_FUNCTION_ENDPOINT":"YOUR_ENDPOINT","APPWRITE_FUNCTION_PROJECT_ID":"YOUR_PROJECT_ID","APPWRITE_FUNCTION_API_KEY":"YOUR_API_KEY"},"payload":"{\"databaseId\":\"stage\",\"collectionId\":\"profiles\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" +``` + +## 🚀 Deployment using Appwrite + +1. Clone this repository, and enter this function folder: +``` +$ git clone https://github.com/open-runtimes/examples.git && cd examples +$ cd php/wipe_appwrite_collection +``` + +2. Enter this function folder and build the code: +``` +docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/php:v2-8.1 sh /usr/local/src/build.sh +``` +As a result, a `code.tar.gz` file will be generated. + +3. Add a new function to appwrite server. Upload the code.tar.gz file manually. Set index.php as entrypoint. + +4. Execute the function. Pass collection ID and database ID as shown in example input and execute the function. + + +## 📝 Notes + - This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions). + - This example is compatible with PHP 8.1. Other versions may work but are not guaranteed to work as they haven't been tested. \ No newline at end of file From c8cd52787d2f99acc61d79fa002b77614c752a08 Mon Sep 17 00:00:00 2001 From: Maximiliano Capodacqua Date: Tue, 11 Jul 2023 18:57:00 -0500 Subject: [PATCH 4/5] Updates main README with new example --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5447f630..f39823be 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Example functions to show off what you can achieve with Open Runtimes. | Compresss Imagse | | | | | |[✅](/ruby/compress-image)| | | Send a HTTP Request | | | | | | [✅](/ruby/send-http-request) | | | Wipe Appwrite Bucket | | | [✅](/node/wipe_appwrite_bucket) | | | | | +| Wipe Appwrite Collection | | | [✅](/node/wipe_appwrite_collection/) | | [✅](/php/wipe_appwrite_collection/) | | [✅](/ruby/wipe-appwrite-collection/) | | Detect Language with Deepgram | | | | | [✅](/python/deepgram-language-detection) | | | | Summarize Audio with Deepgram | | | | | |[✅](/ruby/deepgram-audio-summary)| | | Summarize Audio with Deepgram | | | | | [✅](/python/deepgram-audio-summary) | | | From 6b9793f66bbc09587f18c8a6ee264f8e55c2b09a Mon Sep 17 00:00:00 2001 From: Maximiliano Capodacqua Date: Tue, 11 Jul 2023 18:59:50 -0500 Subject: [PATCH 5/5] This table is hard to read --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f39823be..51a000d7 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Example functions to show off what you can achieve with Open Runtimes. | Compresss Imagse | | | | | |[✅](/ruby/compress-image)| | | Send a HTTP Request | | | | | | [✅](/ruby/send-http-request) | | | Wipe Appwrite Bucket | | | [✅](/node/wipe_appwrite_bucket) | | | | | -| Wipe Appwrite Collection | | | [✅](/node/wipe_appwrite_collection/) | | [✅](/php/wipe_appwrite_collection/) | | [✅](/ruby/wipe-appwrite-collection/) | +| Wipe Appwrite Collection | | | [✅](/node/wipe_appwrite_collection/) | [✅](/php/wipe_appwrite_collection/) | | [✅](/ruby/wipe-appwrite-collection/) | | | Detect Language with Deepgram | | | | | [✅](/python/deepgram-language-detection) | | | | Summarize Audio with Deepgram | | | | | |[✅](/ruby/deepgram-audio-summary)| | | Summarize Audio with Deepgram | | | | | [✅](/python/deepgram-audio-summary) | | |