From 3f6bc303ba0b1bc4531c93d31a5cbc28b1273916 Mon Sep 17 00:00:00 2001 From: davixim <77257145+davixim@users.noreply.github.com> Date: Thu, 20 Feb 2025 03:14:46 +0100 Subject: [PATCH] Update code-examples.mdx - Remove duplicate code - Standardize indentation to 4 spaces (PEP-8) - Fix a capitalization for better readability --- .../reference-content/code-examples.mdx | 96 ++++++++----------- 1 file changed, 42 insertions(+), 54 deletions(-) diff --git a/pages/serverless-functions/reference-content/code-examples.mdx b/pages/serverless-functions/reference-content/code-examples.mdx index 754ed3369f..754714fbbe 100644 --- a/pages/serverless-functions/reference-content/code-examples.mdx +++ b/pages/serverless-functions/reference-content/code-examples.mdx @@ -37,25 +37,25 @@ There are several ways to return a response from a handler function: ```python def handle(event, context): - return { - "body": { - "message": "hello world" - }, - "statusCode": 200, - "headers": { - "Content-Type": ["application/json"], - "your-header": "your-value" - } - } + return { + "body": { + "message": "hello world" + }, + "statusCode": 200, + "headers": { + "Content-Type": ["application/json"], + "your-header": "your-value" + } + } ``` **Straight response without body:** ```python def handle(event, context): - return {"message": "hello world"} - # or - return "my Message" + return {"message": "hello world"} + # or + return "my Message" ``` **Stringified response body (AWS Lambda):** @@ -64,13 +64,13 @@ def handle(event, context): import json def handle(event, context): - return { - "body": json.dumps({"message": "hello world"}), - "headers": { - "Content-Type": ["application/json"], - }, - "statusCode": 200, - } + return { + "body": json.dumps({"message": "hello world"}), + "headers": { + "Content-Type": ["application/json"], + }, + "statusCode": 200, + } ``` ### Getting data in Python @@ -85,8 +85,8 @@ The following snippet returns the environment variable `ENV` you specified in th import os def handle(event, context): - env_variable = os.getenv('ENV') - return env_variable + env_variable = os.getenv('ENV') + return env_variable ``` ### Using event objects in Python @@ -103,10 +103,10 @@ You can pass information through your HTTP request with event objects. They are ```python def handle(event, context): - # the event object is a Python dict - query_param = event['queryStringParameters']['parameter'] - query_body = event['body'] - # ... + # the event object is a Python dict + query_param = event['queryStringParameters']['parameter'] + query_body = event['body'] + # ... ``` Example of reading URL parameters: @@ -115,12 +115,12 @@ Example of reading URL parameters: curl https://myfunc/user/?id=1 ``` -Will fill the event with the following parameters: +will fill the event with the following parameters: ```python event['path'] = "/user/" event['queryStringParameters'] = { - "id": "1" + "id": "1" } ``` @@ -130,8 +130,8 @@ CRON jobs share the same pattern as HTTP calls. You can find the information pas ```python def handle(event, context): - cron_body = event['body'] - # ... + cron_body = event['body'] + # ... ``` ### Connecting to HTTP services in Python @@ -150,20 +150,20 @@ auth_token=os.getenv('X-AUTH-TOKEN') # You can pass tokens through os environmen url={YOUR URL} # If you want a dynamic URL based on information sent to handle(), define the URL inside the function def handle(event, context): - req = request.Request(url, method='GET') - req.add_header('X-Auth-Token',auth_token) - try: + req = request.Request(url, method='GET') + req.add_header('X-Auth-Token',auth_token) + try: res = request.urlopen(req).read().decode() - except error.HTTPError as e: - res = e.read().decode() + except error.HTTPError as e: + res = e.read().decode() - return { - "body": json.loads(res), - "headers": { - "Content-Type": ["application/json"], - }, - "statusCode": 200, - } + return { + "body": json.loads(res), + "headers": { + "Content-Type": ["application/json"], + }, + "statusCode": 200, + } ``` **`POST` request example** @@ -194,18 +194,6 @@ def handle(event, context): }, "statusCode": 200, } - try: - res = request.urlopen(req).read().decode() - except error.HTTPError as e: - res = e.read().decode() - - return { - "body": json.loads(res), - "headers": { - "Content-Type": ["application/json"], - }, - "statusCode": 200, - } ``` To use other libraries like `request`, you need to upload your project [with advanced methods](/serverless-functions/reference-content/deploy-function/) (recommended)