From 4deefa26750f9512db482ee0795910dea897c9a5 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 29 Aug 2023 14:58:48 +0100 Subject: [PATCH 1/9] fix: response helper examples --- app/views/docs/functions-develop.phtml | 339 ++++++++++++------------- 1 file changed, 167 insertions(+), 172 deletions(-) diff --git a/app/views/docs/functions-develop.phtml b/app/views/docs/functions-develop.phtml index 4f5adbb6..dc192922 100644 --- a/app/views/docs/functions-develop.phtml +++ b/app/views/docs/functions-develop.phtml @@ -841,8 +841,8 @@ public class Main {
export default async ({ req, res, log }) => {
 
     switch (req.query.type) {
-        case 'text': 
-            return res.send("This is a text response", 200);
+        case 'empty': 
+            return res.empty();
         case 'json':
             return res.json({"type": "This is a JSON response"}, 200);
         case 'redirect':
@@ -853,7 +853,7 @@ public class Main {
                     "content-type": "text/html"
                 });
         default:
-            return res.empty();
+            return res.send("This is a text response", 200);
     }
 }
@@ -865,8 +865,8 @@ public class Main { return function ($context) { switch ($context->req->query['type']) { - case 'text': - return $context->res->send("This is a text response", 200); + case 'empty': + return $context->res->empty(); case 'json': return $context->res->json(["type" => "This is a JSON response"], 200); case 'redirect': @@ -876,7 +876,7 @@ return function ($context) { "content-type" => "text/html" ]); default: - return $context->res->empty(); + return $context->res->send("This is a text response", 200); } }; @@ -886,8 +886,8 @@ return function ($context) {
def main(context):
     switch context.req.query['type']:
-        case 'text':
-            return context.res.send("This is a text response", 200)
+        case 'empty':
+            return context.res.empty()
         case 'json':
             return context.res.json({"type": "This is a JSON response"}, 200)
         case 'redirect':
@@ -897,7 +897,7 @@ return function ($context) {
                 "content-type": "text/html"
             })
         default:
-            return context.res.empty()
+ return context.res.send("This is a text response", 200)
  • @@ -905,8 +905,8 @@ return function ($context) {
    def main(context)
         case context.req.query['type'] 
    -        when 'text'
    -            return context.res.send("This is a text response", 200)
    +        when 'empty'
    +            return context.res.empty()
             when 'json'
                 return context.res.json({"type": "This is a JSON response"}, 200)
             when 'redirect'
    @@ -916,7 +916,8 @@ return function ($context) {
                     "content-type": "text/html"
                 })
             else
    -            return context.res.empty()
    +            return context.res.send("This is a text response", 200)
    +            
         end
     end
    @@ -927,8 +928,8 @@ end
    export default async ({ req, res, log }) => {
     
         switch (req.query.type) {
    -        case 'text':
    -            return res.send("This is a text response", 200);
    +        case 'empty':
    +            return res.empty();
             case 'json':
                 return res.json({type": "This is a JSON response"}, 200);
             case 'redirect':
    @@ -939,7 +940,7 @@ end
    "content-type": "text/html" }); default: - return res.empty(); + return res.send("This is a text response", 200); } } @@ -951,24 +952,18 @@ end Future<dynamic> main(final context) async { switch (context.req.query['type']) { - case 'text': - return context.res - .send('This is a text response', 200); + case 'empty': + return context.res.empty(); case 'json': - return context.res - .json({'type': 'This is a JSON response'}); + return context.res.json({'type': 'This is a JSON response'}); case 'redirect': - return context.res - .redirect('https://appwrite.io', 301); + return context.res.redirect('https://appwrite.io', 301); case 'html': - return context.res - .send('<h1>This is an HTML response</h1>', 200, { - 'content-type': 'text/html' - }); + return context.res.send('<h1>This is an HTML response</h1>', + 200, {'content-type': 'text/html'}); default: - return context.res - .empty(); - } + return context.res.send('This is a text response', 200); + } }
  • @@ -979,18 +974,18 @@ Future<dynamic> main(final context) async { func main(context: RuntimeContext) async throws -> RuntimeOutput { switch context.req.query["type"] { - case "text": - return try await context.send("This is a text response", 200) + case "empty": + return try await context.res.empty() case "json": - return try await context.send(["type": "This is a JSON response"], 200) + return try await context.res.send(["type": "This is a JSON response"], 200) case "redirect": - return try await context.redirect("https://appwrite.io", 301) + return try await context.res.redirect("https://appwrite.io", 301) case "html": - return try await context.send("<h1>This is an HTML response</h1>", 200, [ + return try await context.res.send("<h1>This is an HTML response</h1>", 200, [ "content-type": "text/html" ]) default: - return try await context.empty() + return try await context.res.send("This is a text response", 200) } } @@ -1005,18 +1000,18 @@ public class Handler { { switch (Context.Request.Query["type"]) { - case "text": - return await Context.Send("This is a text response", 200); + case "empty": + return await Context.Res.Empty(); case "json": - return await Context.Send(new Dictionary<string, object>() { { "type", "This is a JSON response" } }, 200); + return await Context.Res.Send(new Dictionary<string, object>() { { "type", "This is a JSON response" } }, 200); case "redirect": - return await Context.Redirect("https://appwrite.io", 301); + return await Context.Res.Redirect("https://appwrite.io", 301); case "html": - return await Context.Send("<h1>This is an HTML response</h1>", 200, new Dictionary<string, string>() { + return await Context.Res.Send("<h1>This is an HTML response</h1>", 200, new Dictionary<string, string>() { { "content-type", "text/html" } }); - default: - return await Context.Empty(); + default: + return await Context.Res.Send("This is a text response", 200); } } } @@ -1033,11 +1028,11 @@ import io.openruntimes.kotlin.RuntimeOutput class Main { fun main(context: RuntimeContext): RuntimeOutput { when (context.req.query["type"]) { - "text" -> return context.send("This is a text response", 200) - "json" -> return context.send(mapOf("type" to "This is a JSON response"), 200) - "redirect" -> return context.redirect("https://appwrite.io", 301) - "html" -> return context.send("<h1>This is an HTML response</h1>", 200, mapOf("content-type" to "text/html")) - else -> return context.empty() + "empty" -> return context.res.empty() + "json" -> return context.res.send(mapOf("type" to "This is a JSON response"), 200) + "redirect" -> return context.res.redirect("https://appwrite.io", 301) + "html" -> return context.res.send("<h1>This is an HTML response</h1>", 200, mapOf("content-type" to "text/html")) + else -> return context.res.send("This is a text response", 200) } } } @@ -1057,17 +1052,17 @@ public class Main { public RuntimeOutput main(RuntimeContext context) throws Exception { switch (context.getReq().getQuery()["type"]) { case "text": - return context.send("This is a text response", 200); + return context.getRes().empty(); case "json": HashMap<String, Object> data = new HashMap<>(); data.put("type", "This is a JSON response"); - return context.send(data, 200); + return context.getRes().send(data, 200); case "redirect": - return context.redirect("https://appwrite.io", 301); + return context.getRes().redirect("https://appwrite.io", 301); case "html": - return context.send("<h1>This is an HTML response</h1>", 200, Map.of("content-type", "text/html")); + return context.getRes().send("<h1>This is an HTML response</h1>", 200, Map.of("content-type", "text/html")); default: - return context.empty(); + return context.getRes().send("This is a text response", 200); } } } @@ -1087,20 +1082,20 @@ namespace runtime { static RuntimeOutput main(RuntimeContext &context) { std::string type = context.req.query["type"]; - if (type == "text") { - return context.send("This is a text response", 200); + if (type == "empty") { + return context.res.empty(); } else if (type == "json") { Json::Value data; data["type"] = "This is a JSON response"; - return context.send(data, 200); + return context.res.send(data, 200); } else if (type == "redirect") { - return context.redirect("https://appwrite.io", 301); + return context.res.redirect("https://appwrite.io", 301); } else if (type == "html") { Json::Value headers; headers["content-type"] = "text/html"; - return context.send("<h1>This is an HTML response</h1>", 200, headers); + return context.res.send("<h1>This is an HTML response</h1>", 200, headers); } else { - return context.empty(); + return context.res.send("This is a text response", 200); } } }; @@ -1193,7 +1188,7 @@ return function ($context) { context.log(f"This function was called with {context.req.method} method") context.error("This is an error, use for logging errors to console") - return context.send("Check the Appwrite Console to see logs and errors!") + return context.res.send("Check the Appwrite Console to see logs and errors!")
  • @@ -1204,7 +1199,7 @@ return function ($context) { context.log("This function was called with #{context.req.method} method") context.error("This is an error, use for logging errors to console") - return context.send("Check the Appwrite Console to see logs and errors!") + return context.res.send("Check the Appwrite Console to see logs and errors!") end
  • @@ -1230,7 +1225,7 @@ Future<dynamic> main(final context) async { context.log("This function was called with ${context.req.method} method"); context.error("This is an error, use for logging errors to console"); - return context.send("Check the Appwrite Console to see logs and errors!"); + return context.res.send("Check the Appwrite Console to see logs and errors!"); } @@ -1244,7 +1239,7 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput { context.log("This function was called with \(context.req.method) method") context.error("This is an error, use for logging errors to console") - return try context.send("Check the Appwrite Console to see logs and errors!") + return context.res.send("Check the Appwrite Console to see logs and errors!") } @@ -1260,7 +1255,7 @@ public class Handler { Context.Log($"This function was called with {Context.Req.Method} method"); Context.Error("This is an error, use for logging errors to console"); - return await Context.Send("Check the Appwrite Console to see logs and errors!"); + return await Context.Res.Send("Check the Appwrite Console to see logs and errors!"); } } @@ -1279,7 +1274,7 @@ class Main { context.log("This function was called with ${context.req.method} method") context.error("This is an error, use for logging errors to console") - return context.send("Check the Appwrite Console to see logs and errors!") + return context.res.send("Check the Appwrite Console to see logs and errors!") } } @@ -1298,7 +1293,7 @@ public class Main { context.log("This function was called with " + context.req.method + " method"); context.error("This is an error, use for logging errors to console"); - return context.send("Check the Appwrite Console to see logs and errors!"); + return context.getRes().send("Check the Appwrite Console to see logs and errors!"); } } @@ -1319,7 +1314,7 @@ namespace runtime { context.log("This function was called with " + context.req.method + " method"); context.error("This is an error, use for logging errors to console"); - return context.send("Check the Appwrite Console to see logs and errors!"); + return context.res.send("Check the Appwrite Console to see logs and errors!"); } }; } @@ -1486,7 +1481,7 @@ Future<dynamic> main(final context) async {
    import Foundation
     
     func main(context: RuntimeContext) async throws -> RuntimeOutput {
    -    return try await context.send(ProcessInfo.processInfo.environment["MY_VAR"], 200)
    +    return await context.res.send(ProcessInfo.processInfo.environment["MY_VAR"], 200)
     }
    @@ -1498,7 +1493,7 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput { public class Handler { public async Task<RuntimeOutput> Main(RuntimeContext Context) { - return await Context.Send(Environment.GetEnvironmentVariable("MY_VAR"), 200); + return await Context.Res.Send(Environment.GetEnvironmentVariable("MY_VAR"), 200); } } @@ -1513,7 +1508,7 @@ import io.openruntimes.kotlin.RuntimeOutput class Main { fun main(context: RuntimeContext): RuntimeOutput { - return context.send(System.getenv("MY_VAR"), 200) + return context.res.send(System.getenv("MY_VAR"), 200) } } @@ -1528,7 +1523,7 @@ import io.openruntimes.java.RuntimeOutput; public class Main { public RuntimeOutput main(RuntimeContext context) throws Exception { - return context.send(System.getenv("MY_VAR"), 200); + return context.getRes().send(System.getenv("MY_VAR"), 200); } } @@ -1546,7 +1541,7 @@ namespace runtime { public: static RuntimeOutput main(RuntimeContext &context) { - return context.send(std::getenv("MY_VAR"), 200); + return context.res.send(std::getenv("MY_VAR"), 200); }; } @@ -1559,114 +1554,114 @@ namespace runtime { Your function's dependencies should be managed by the package manager of each language. By default, we include the following package managers in each runtime:

    - - +
    + + + + + + + + - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    LanguagePackage ManagerCommands
    LanguagePackage ManagerCommands + Node icon + Node.jsnpmnpm install
    - Node icon - Node.jsnpmnpm install
    - PHP icon - PHPComposercomposer install
    - Python icon - Pythonpippip install -r requirements.txt
    - Ruby icon - RubyBundlerbundle install
    - Deno icon - Denodenodeno cache <ENTRYPOINT_FILE>
    - Dart icon - Dartpubpub get
    - Swift icon - SwiftSwift Package ManagerN/A
    - Swift icon - .NETNuGetN/A
    - Swift icon - KotlinGradleN/A
    - Swift icon - JavaGradleN/A
    - C++ icon - C++NoneN/A
    + + + PHP icon + + PHP + Composer + composer install + + + + Python icon + + Python + pip + pip install -r requirements.txt + + + + Ruby icon + + Ruby + Bundler + bundle install + + + + Deno icon + + Deno + deno + deno cache <ENTRYPOINT_FILE> + + + + Dart icon + + Dart + pub + pub get + + + + Swift icon + + Swift + Swift Package Manager + N/A + + + + Swift icon + + .NET + NuGet + N/A + + + + Swift icon + + Kotlin + Gradle + N/A + + + + Swift icon + + Java + Gradle + N/A + + + + C++ icon + + C++ + None + N/A + + -

    - To install your dependencies before your function is built, you should add the relevant install command to the top your function's Build setting > Commands. -

    +

    + To install your dependencies before your function is built, you should add the relevant install command to the top your function's Build setting > Commands. +

    Using Appwrite in a Function

    - Appwrite can be used in your functions by adding the relevant SDK to your function's dependencies. - Authenticating with Appwrite is done via an API key or a JWT token. + Appwrite can be used in your functions by adding the relevant SDK to your function's dependencies. + Authenticating with Appwrite is done via an API key or a JWT token. API keys must be generated and exported as an environment variable.

    From 4dc3e51bc3ee329fdd5a38d7c9e929b3be0d76b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 29 Aug 2023 14:39:31 +0000 Subject: [PATCH 2/9] Review changes --- app/views/docs/functions-develop.phtml | 29 +++++++++++++------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/app/views/docs/functions-develop.phtml b/app/views/docs/functions-develop.phtml index dc192922..abe69d73 100644 --- a/app/views/docs/functions-develop.phtml +++ b/app/views/docs/functions-develop.phtml @@ -876,7 +876,7 @@ return function ($context) { "content-type" => "text/html" ]); default: - return $context->res->send("This is a text response", 200); + return $context->res->send("This is a text response", 200); } }; @@ -917,7 +917,6 @@ return function ($context) { }) else return context.res.send("This is a text response", 200) - end end @@ -1159,7 +1158,7 @@ namespace runtime {

    export default async ({ res, log, error }) => {
         log("This is a log, use for logging information to console");
    -    log(`This function was called with ${context.req.method} method`);
    +    log(`This function was called with ${req.method} method`);
         error("This is an error, use for logging errors to console");
     
         return res.send("Check the Appwrite Console to see logs and errors!");
    @@ -1428,7 +1427,7 @@ namespace runtime {
                 

    Node.js

    export default async ({ req, res, log }) => {
    -    return res.send(process.env.MY_VAR, 200);
    +    return res.send(process.env.MY_VAR);
     }
    @@ -1438,7 +1437,7 @@ namespace runtime {
    <?php
     
     return function ($context) {
    -    return $context->res->send(getenv('MY_VAR'), 200);
    +    return $context->res->send(getenv('MY_VAR'));
     };
    @@ -1446,14 +1445,14 @@ return function ($context) {

    Python

    def main(context):
    -    return context.res.send(os.environ['MY_VAR'], 200)
    + return context.res.send(os.environ['MY_VAR'])
  • Ruby

    def main(context)
    -    return context.res.send(ENV['MY_VAR'], 200)
    +    return context.res.send(ENV['MY_VAR'])
     end
  • @@ -1461,7 +1460,7 @@ end

    Deno

    export default async ({ req, res, log }) => {
    -    return res.send(Deno.env.get('MY_VAR'), 200);
    +    return res.send(Deno.env.get('MY_VAR'));
     }
    @@ -1471,7 +1470,7 @@ end
    import 'dart:async';
     
     Future<dynamic> main(final context) async {
    -    return context.res.send(Platform.environment['MY_VAR'], 200);
    +    return context.res.send(Platform.environment['MY_VAR']);
     }
    @@ -1481,7 +1480,7 @@ Future<dynamic> main(final context) async {
    import Foundation
     
     func main(context: RuntimeContext) async throws -> RuntimeOutput {
    -    return await context.res.send(ProcessInfo.processInfo.environment["MY_VAR"], 200)
    +    return await context.res.send(ProcessInfo.processInfo.environment["MY_VAR"])
     }
    @@ -1493,7 +1492,7 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput { public class Handler { public async Task<RuntimeOutput> Main(RuntimeContext Context) { - return await Context.Res.Send(Environment.GetEnvironmentVariable("MY_VAR"), 200); + return await Context.Res.Send(Environment.GetEnvironmentVariable("MY_VAR")); } } @@ -1508,7 +1507,7 @@ import io.openruntimes.kotlin.RuntimeOutput class Main { fun main(context: RuntimeContext): RuntimeOutput { - return context.res.send(System.getenv("MY_VAR"), 200) + return context.res.send(System.getenv("MY_VAR")) } } @@ -1523,7 +1522,7 @@ import io.openruntimes.java.RuntimeOutput; public class Main { public RuntimeOutput main(RuntimeContext context) throws Exception { - return context.getRes().send(System.getenv("MY_VAR"), 200); + return context.getRes().send(System.getenv("MY_VAR")); } } @@ -1541,7 +1540,7 @@ namespace runtime { public: static RuntimeOutput main(RuntimeContext &context) { - return context.res.send(std::getenv("MY_VAR"), 200); + return context.res.send(std::getenv("MY_VAR")); }; } @@ -2348,7 +2347,7 @@ export function add(a, b) { import { add } from './utils.js'; -export default function ({res}) { +export default function ({ res }) { return res.send(add(1, 2)); } From d6d63a639440f8d59f9eb6a32c8f5553bf8cee1d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 29 Aug 2023 15:40:11 +0100 Subject: [PATCH 3/9] fix: remove duplicate python example + add missing php --- app/views/docs/functions-examples.phtml | 86 +++++++++++-------------- 1 file changed, 37 insertions(+), 49 deletions(-) diff --git a/app/views/docs/functions-examples.phtml b/app/views/docs/functions-examples.phtml index 485c4fe3..96af6e77 100644 --- a/app/views/docs/functions-examples.phtml +++ b/app/views/docs/functions-examples.phtml @@ -139,6 +139,43 @@ export default async function ({ req, res }) { +
  • +

    PHP

    +
    +

    +

    +
    <?php
    +
    +require(__DIR__ . '/../vendor/autoload.php');
    +
    +use Appwrite\Client;
    +use Appwrite\Exception;
    +use Appwrite\Services\Database;
    +use GuzzleHttp\Client as GuzzleClient;
    +
    +return function ($context) {
    +    $client = new GuzzleClient();
    +
    +    if ($context->req->path === '/eur') {
    +        $amountInEuros = floatval($context->req->query['amount']);
    +        $response = $client->get('https://api.exchangerate.host/latest?base=EUR&symbols=USD');
    +        $data = $response->json();
    +        $amountInDollars = $amountInEuros * $data['rates']['USD'];
    +        return $context->res->send(strval($amountInDollars));
    +    }
    +
    +    if ($context->req->path === '/inr') {
    +        $amountInRupees = floatval($context->req->query['amount']);
    +        $response = $client->get('https://api.exchangerate.host/latest?base=INR&symbols=USD');
    +        $data = $response->json();
    +        $amountInDollars = $amountInRupees * $data['rates']['USD'];
    +        return $context->res->send(strval($amountInDollars));
    +    }
    +
    +    return $context->res->send('Invalid path');
    +};
    +
    +
  • Python

    @@ -555,55 +592,6 @@ Future main(final context) async {
  • -
  • -

    Python

    -
    -

    -

    -
    from appwrite.client import Client
    -from appwrite.services.databases import Databases
    -from appwrite.query import Query
    -
    -import os
    -
    -def main(context):
    -    vote = {
    -        'userId': context.req.query['userId'],
    -        'topicId': context.req.query['topicId'],
    -        'vote': context.req.query['vote']
    -    }
    -
    -    if vote['vote'] != 'yes' and vote['vote'] != 'no':
    -        return context.res.json({'ok': False, 'message': 'You must vote yes or no.'}, 400)
    -
    -    client = Client()
    -    client.set_endpoint('https://cloud.appwrite.io/v1')
    -    client.set_project(os.environ['APPWRITE_FUNCTION_PROJECT_ID'])
    -    client.set_key(os.environ['APPWRITE_API_KEY'])
    -
    -    database = Databases(client)
    -
    -    existing_votes = database.list_documents('[VOTES_COLLECTION_ID]', [
    -        Query.equal('userId', vote['userId']),
    -        Query.equal('topicId', vote['topicId'])
    -    ])
    -    
    -    if existing_votes['total'] > 0:
    -        return context.res.json({
    -          'ok': False, 
    -          'message': 'You have already voted on this topic.'
    -        }, 400)
    -
    -    vote_document = database.create_document('[VOTES_COLLECTION_ID]', vote)
    -
    -    return context.res.json({
    -      'ok': True, 
    -      'message': 'Vote cast.', 
    -      'vote': vote_document
    -    })
    -
    -
    -
  • From 5e5061cb96002dcb87e9d020a7a1e1db9bf9b562 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 29 Aug 2023 15:44:45 +0100 Subject: [PATCH 4/9] chore: remove unneccessary 200 codes --- app/views/docs/functions-develop.phtml | 42 ++++++++++++------------- app/views/docs/functions-examples.phtml | 10 +++--- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/app/views/docs/functions-develop.phtml b/app/views/docs/functions-develop.phtml index abe69d73..77ff76a5 100644 --- a/app/views/docs/functions-develop.phtml +++ b/app/views/docs/functions-develop.phtml @@ -844,7 +844,7 @@ public class Main { case 'empty': return res.empty(); case 'json': - return res.json({"type": "This is a JSON response"}, 200); + return res.json({"type": "This is a JSON response"}); case 'redirect': return res.redirect("https://appwrite.io", 301); case 'html': @@ -853,7 +853,7 @@ public class Main { "content-type": "text/html" }); default: - return res.send("This is a text response", 200); + return res.send("This is a text response"); } } @@ -868,7 +868,7 @@ return function ($context) { case 'empty': return $context->res->empty(); case 'json': - return $context->res->json(["type" => "This is a JSON response"], 200); + return $context->res->json(["type" => "This is a JSON response"]); case 'redirect': return $context->res->redirect("https://appwrite.io", 301); case 'html': @@ -876,7 +876,7 @@ return function ($context) { "content-type" => "text/html" ]); default: - return $context->res->send("This is a text response", 200); + return $context->res->send("This is a text response"); } }; @@ -889,7 +889,7 @@ return function ($context) { case 'empty': return context.res.empty() case 'json': - return context.res.json({"type": "This is a JSON response"}, 200) + return context.res.json({"type": "This is a JSON response"}) case 'redirect': return context.res.redirect("https://appwrite.io", 301) case 'html': @@ -897,7 +897,7 @@ return function ($context) { "content-type": "text/html" }) default: - return context.res.send("This is a text response", 200) + return context.res.send("This is a text response")

  • @@ -908,7 +908,7 @@ return function ($context) { when 'empty' return context.res.empty() when 'json' - return context.res.json({"type": "This is a JSON response"}, 200) + return context.res.json({"type": "This is a JSON response"}) when 'redirect' return context.res.redirect("https://appwrite.io", 301) when 'html' @@ -916,7 +916,7 @@ return function ($context) { "content-type": "text/html" }) else - return context.res.send("This is a text response", 200) + return context.res.send("This is a text response") end end @@ -930,7 +930,7 @@ end case 'empty': return res.empty(); case 'json': - return res.json({type": "This is a JSON response"}, 200); + return res.json({type": "This is a JSON response"}); case 'redirect': return res.redirect("https://appwrite.io", 301); case 'html': @@ -939,7 +939,7 @@ end "content-type": "text/html" }); default: - return res.send("This is a text response", 200); + return res.send("This is a text response"); } } @@ -961,7 +961,7 @@ Future<dynamic> main(final context) async { return context.res.send('<h1>This is an HTML response</h1>', 200, {'content-type': 'text/html'}); default: - return context.res.send('This is a text response', 200); + return context.res.send('This is a text response'); } } @@ -976,7 +976,7 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput { case "empty": return try await context.res.empty() case "json": - return try await context.res.send(["type": "This is a JSON response"], 200) + return try await context.res.send(["type": "This is a JSON response"]) case "redirect": return try await context.res.redirect("https://appwrite.io", 301) case "html": @@ -984,7 +984,7 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput { "content-type": "text/html" ]) default: - return try await context.res.send("This is a text response", 200) + return try await context.res.send("This is a text response") } } @@ -1002,7 +1002,7 @@ public class Handler { case "empty": return await Context.Res.Empty(); case "json": - return await Context.Res.Send(new Dictionary<string, object>() { { "type", "This is a JSON response" } }, 200); + return await Context.Res.Send(new Dictionary<string, object>() { { "type", "This is a JSON response" } }); case "redirect": return await Context.Res.Redirect("https://appwrite.io", 301); case "html": @@ -1010,7 +1010,7 @@ public class Handler { { "content-type", "text/html" } }); default: - return await Context.Res.Send("This is a text response", 200); + return await Context.Res.Send("This is a text response"); } } } @@ -1028,10 +1028,10 @@ class Main { fun main(context: RuntimeContext): RuntimeOutput { when (context.req.query["type"]) { "empty" -> return context.res.empty() - "json" -> return context.res.send(mapOf("type" to "This is a JSON response"), 200) + "json" -> return context.res.send(mapOf("type" to "This is a JSON response")) "redirect" -> return context.res.redirect("https://appwrite.io", 301) "html" -> return context.res.send("<h1>This is an HTML response</h1>", 200, mapOf("content-type" to "text/html")) - else -> return context.res.send("This is a text response", 200) + else -> return context.res.send("This is a text response") } } } @@ -1055,13 +1055,13 @@ public class Main { case "json": HashMap<String, Object> data = new HashMap<>(); data.put("type", "This is a JSON response"); - return context.getRes().send(data, 200); + return context.getRes().send(data); case "redirect": return context.getRes().redirect("https://appwrite.io", 301); case "html": return context.getRes().send("<h1>This is an HTML response</h1>", 200, Map.of("content-type", "text/html")); default: - return context.getRes().send("This is a text response", 200); + return context.getRes().send("This is a text response"); } } } @@ -1086,7 +1086,7 @@ namespace runtime { } else if (type == "json") { Json::Value data; data["type"] = "This is a JSON response"; - return context.res.send(data, 200); + return context.res.send(data); } else if (type == "redirect") { return context.res.redirect("https://appwrite.io", 301); } else if (type == "html") { @@ -1094,7 +1094,7 @@ namespace runtime { headers["content-type"] = "text/html"; return context.res.send("<h1>This is an HTML response</h1>", 200, headers); } else { - return context.res.send("This is a text response", 200); + return context.res.send("This is a text response"); } } }; diff --git a/app/views/docs/functions-examples.phtml b/app/views/docs/functions-examples.phtml index 96af6e77..cc0fed6f 100644 --- a/app/views/docs/functions-examples.phtml +++ b/app/views/docs/functions-examples.phtml @@ -684,7 +684,7 @@ export default async function ({ req, res }) { const databases = new Databases(client); const document = await databases.createDocument('[DATABASE_ID]', '[MESSAGES_COLLECTION_ID]', ID.unique(), message); - return res.send("Message sent", 200); + return res.send("Message sent"); } return res.send('Not found', 404); @@ -743,7 +743,7 @@ def main(context): databases = Databases(client) document = databases.create_document('[DATABASE_ID]', '[MESSAGES_COLLECTION_ID]', ID.unique(), message) - return context.res.send("Message sent", 200) + return context.res.send("Message sent") return context.res.send('Not found', 404) @@ -803,7 +803,7 @@ return function ($context) { $databases = new Databases($client); $document = $databases->createDocument('[DATABASE_ID]', '[MESSAGES_COLLECTION_ID]', ID::unique(), $message); - return $context->res->send("Message sent", 200); + return $context->res->send("Message sent"); } return $context->res->send('Not found', 404); @@ -858,7 +858,7 @@ def main(context) databases = Appwrite::Database.new(client) document = databases.create_document('[DATABASE_ID]', '[MESSAGES_COLLECTION_ID]', ID.unique(), message) - return context.res.send("Message sent", 200) + return context.res.send("Message sent") end return context.res.send('Not found', 404) @@ -913,7 +913,7 @@ Future main(final context) async { final databases = Database(client); final document = await databases.createDocument('[DATABASE_ID]', '[MESSAGES_COLLECTION_ID]', ID.unique(), message); - return context.res.send("Message sent", 200); + return context.res.send("Message sent"); } return context.res.send('Not found', 404); From 5d6d411360946e31d052c4750eab508d8f51e476 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:24:22 +0100 Subject: [PATCH 5/9] fix: standardise appwrite sdk setup for python --- app/views/docs/functions-examples.phtml | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/views/docs/functions-examples.phtml b/app/views/docs/functions-examples.phtml index cc0fed6f..0fdabae8 100644 --- a/app/views/docs/functions-examples.phtml +++ b/app/views/docs/functions-examples.phtml @@ -398,10 +398,12 @@ def main(context): if vote['vote'] != 'yes' and vote['vote'] != 'no': return context.res.json({'ok': False, 'message': 'You must vote yes or no.'}, 400) - client = Client() - client.set_endpoint('https://cloud.appwrite.io/v1') - client.set_project(os.environ['APPWRITE_FUNCTION_PROJECT_ID']) - client.set_key(os.environ['APPWRITE_API_KEY']) + client = ( + Client() + .set_endpoint("https://cloud.appwrite.io/v1") + .set_project(os.environ["APPWRITE_FUNCTION_PROJECT_ID"]) + .set_key(os.environ["APPWRITE_API_KEY"]) + ) database = Databases(client) @@ -734,10 +736,10 @@ def main(context): } client = ( - Client() - .set_endpoint('https://cloud.appwrite.io/v1') - .set_project(os.environ['APPWRITE_FUNCTION_PROJECT_ID']) - .set_key(os.environ['APPWRITE_API_KEY']) + Client() + .set_endpoint("https://cloud.appwrite.io/v1") + .set_project(os.environ["APPWRITE_FUNCTION_PROJECT_ID"]) + .set_key(os.environ["APPWRITE_API_KEY"]) ) databases = Databases(client) From 1678f8e58ff00b9a3c0b6e32c84dcee734c5a226 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:44:10 +0100 Subject: [PATCH 6/9] fix: python code splitting --- app/views/docs/functions-develop.phtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/docs/functions-develop.phtml b/app/views/docs/functions-develop.phtml index 77ff76a5..63664e37 100644 --- a/app/views/docs/functions-develop.phtml +++ b/app/views/docs/functions-develop.phtml @@ -2383,10 +2383,10 @@ def add(a, b):
    // src/main.py
     
    -import utils
    +from .utils import add
     
     def main(context):
    -    return context.res.send(utils.add(1, 2))
    + return context.res.send(add(1, 2))
  • From 31598efbf8e4259a47b12c9c9f738d840ea6708a Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 29 Aug 2023 13:19:42 -0400 Subject: [PATCH 7/9] Fix try/awaits --- app/views/docs/functions-develop.phtml | 48 ++++++++++++-------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/app/views/docs/functions-develop.phtml b/app/views/docs/functions-develop.phtml index 63664e37..42b8d4f1 100644 --- a/app/views/docs/functions-develop.phtml +++ b/app/views/docs/functions-develop.phtml @@ -171,14 +171,12 @@ def main(context) end # `ctx.res.json()` is a handy helper for sending JSON - return context.res.json( - { - "motto": "Build Fast. Scale Big. All in One Place.", - "learn": "https://appwrite.io/docs", - "connect": "https://appwrite.io/discord", - "getInspired": "https://builtwith.appwrite.io", - } - ) + return context.res.json({ + "motto": "Build Fast. Scale Big. All in One Place.", + "learn": "https://appwrite.io/docs", + "connect": "https://appwrite.io/discord", + "getInspired": "https://builtwith.appwrite.io", + }) end
  • @@ -286,7 +284,7 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput { if context.req.method == "GET" { // Send a response with the res object helpers // `res.send()` dispatches a string back to the client - return try context.res.send("Hello, World!") + return context.res.send("Hello, World!") } // `context.res.json()` is a handy helper for sending JSON @@ -671,7 +669,7 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput { context.log(context.req.queryString) // Raw query params string. For example "limit=12&offset=50" context.log(NSJSONSerialization.jsonObject(with: context.req.query, options: [])!) // Parsed query params. For example, req.query.limit - return try context.res.send("All the request parameters are logged to the Appwrite Console.") + return context.res.send("All the request parameters are logged to the Appwrite Console.") } @@ -974,17 +972,17 @@ Future<dynamic> main(final context) async { func main(context: RuntimeContext) async throws -> RuntimeOutput { switch context.req.query["type"] { case "empty": - return try await context.res.empty() + return context.res.empty() case "json": - return try await context.res.send(["type": "This is a JSON response"]) + return context.res.send(["type": "This is a JSON response"]) case "redirect": - return try await context.res.redirect("https://appwrite.io", 301) + return context.res.redirect("https://appwrite.io", 301) case "html": - return try await context.res.send("<h1>This is an HTML response</h1>", 200, [ + return context.res.send("<h1>This is an HTML response</h1>", 200, [ "content-type": "text/html" ]) default: - return try await context.res.send("This is a text response") + return context.res.send("This is a text response") } } @@ -1000,17 +998,17 @@ public class Handler { switch (Context.Request.Query["type"]) { case "empty": - return await Context.Res.Empty(); + return Context.Res.Empty(); case "json": - return await Context.Res.Send(new Dictionary<string, object>() { { "type", "This is a JSON response" } }); + return Context.Res.Send(new Dictionary<string, object>() { { "type", "This is a JSON response" } }); case "redirect": - return await Context.Res.Redirect("https://appwrite.io", 301); + return Context.Res.Redirect("https://appwrite.io", 301); case "html": - return await Context.Res.Send("<h1>This is an HTML response</h1>", 200, new Dictionary<string, string>() { + return Context.Res.Send("<h1>This is an HTML response</h1>", 200, new Dictionary<string, string>() { { "content-type", "text/html" } }); default: - return await Context.Res.Send("This is a text response"); + return Context.Res.Send("This is a text response"); } } } @@ -1156,7 +1154,7 @@ namespace runtime {
  • Node.js

    -
    export default async ({ res, log, error }) => {
    +                
    export default async ({ req, res, log, error }) => {
         log("This is a log, use for logging information to console");
         log(`This function was called with ${req.method} method`);
         error("This is an error, use for logging errors to console");
    @@ -1254,7 +1252,7 @@ public class Handler {
             Context.Log($"This function was called with {Context.Req.Method} method");
             Context.Error("This is an error, use for logging errors to console");
     
    -        return await Context.Res.Send("Check the Appwrite Console to see logs and errors!");
    +        return Context.Res.Send("Check the Appwrite Console to see logs and errors!");
         }
     }
    @@ -1480,7 +1478,7 @@ Future<dynamic> main(final context) async {
    import Foundation
     
     func main(context: RuntimeContext) async throws -> RuntimeOutput {
    -    return await context.res.send(ProcessInfo.processInfo.environment["MY_VAR"])
    +    return context.res.send(ProcessInfo.processInfo.environment["MY_VAR"])
     }
  • @@ -1492,7 +1490,7 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput { public class Handler { public async Task<RuntimeOutput> Main(RuntimeContext Context) { - return await Context.Res.Send(Environment.GetEnvironmentVariable("MY_VAR")); + return Context.Res.Send(Environment.GetEnvironmentVariable("MY_VAR")); } } @@ -1862,7 +1860,7 @@ func main(context: RuntimeContext) async throws -> RuntimeOutput { try await databases.createDocument(databaseId: "[DATABASE_ID]", collectionId: "[COLLECTION_ID]", data: [:]) } catch { context.error("Failed to create document: \(error.localizedDescription)") - return try await context.res.send("Failed to create document") + return context.res.send("Failed to create document") } return context.res.send("Document created") From 66ddf617a3d278614c7d5bb9b21d3b1566ba12a0 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 29 Aug 2023 13:23:30 -0400 Subject: [PATCH 8/9] ctx -> context --- app/views/docs/functions-develop.phtml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/views/docs/functions-develop.phtml b/app/views/docs/functions-develop.phtml index 42b8d4f1..7309b25f 100644 --- a/app/views/docs/functions-develop.phtml +++ b/app/views/docs/functions-develop.phtml @@ -124,13 +124,13 @@ def main(context): # If something goes wrong, log an error context.error("Hello, Errors!") - # The `ctx.req` object contains the request data + # The `context.req` object contains the request data if context.req.method == "GET": # Send a response with the res object helpers - # `ctx.res.send()` dispatches a string back to the client + # `context.res.send()` dispatches a string back to the client return context.res.send("Hello, World!") - # `ctx.res.json()` is a handy helper for sending JSON + # `context.res.json()` is a handy helper for sending JSON return context.res.json( { "motto": "Build Fast. Scale Big. All in One Place.", @@ -163,14 +163,14 @@ def main(context) # If something goes wrong, log an error context.error("Hello, Errors!") - # The `ctx.req` object contains the request data + # The `context.req` object contains the request data if (context.req.method == "GET") # Send a response with the res object helpers - # `ctx.res.send()` dispatches a string back to the client + # `context.res.send()` dispatches a string back to the client return context.res.send("Hello, World!") end - # `ctx.res.json()` is a handy helper for sending JSON + # `context.res.json()` is a handy helper for sending JSON return context.res.json({ "motto": "Build Fast. Scale Big. All in One Place.", "learn": "https://appwrite.io/docs", @@ -765,7 +765,7 @@ public class Main {

    Headers

    Appwrite Functions will always receive a set of headers that provide meta data about the function execution. - These are provided along side any custom headers sent to the function. + These are provided alongside any custom headers sent to the function.

    From 9935d70c698adedc355b52626f17735f4866fdb5 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 29 Aug 2023 13:23:39 -0400 Subject: [PATCH 9/9] Fix mismatched tags --- app/views/docs/functions-develop.phtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/docs/functions-develop.phtml b/app/views/docs/functions-develop.phtml index 7309b25f..efef145b 100644 --- a/app/views/docs/functions-develop.phtml +++ b/app/views/docs/functions-develop.phtml @@ -762,7 +762,7 @@ public class Main { -

    Headers

    +

    Headers

    Appwrite Functions will always receive a set of headers that provide meta data about the function execution. These are provided alongside any custom headers sent to the function.