Skip to content

Commit 17abbb0

Browse files
daviximLaure-di
authored andcommitted
Update code-examples.mdx (scaleway#4448)
- Remove duplicate code - Standardize indentation to 4 spaces (PEP-8) - Fix a capitalization for better readability
1 parent 7b03294 commit 17abbb0

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

pages/serverless-functions/reference-content/code-examples.mdx

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,25 @@ There are several ways to return a response from a handler function:
3737

3838
```python
3939
def handle(event, context):
40-
return {
41-
"body": {
42-
"message": "hello world"
43-
},
44-
"statusCode": 200,
45-
"headers": {
46-
"Content-Type": ["application/json"],
47-
"your-header": "your-value"
48-
}
49-
}
40+
return {
41+
"body": {
42+
"message": "hello world"
43+
},
44+
"statusCode": 200,
45+
"headers": {
46+
"Content-Type": ["application/json"],
47+
"your-header": "your-value"
48+
}
49+
}
5050
```
5151

5252
**Straight response without body:**
5353

5454
```python
5555
def handle(event, context):
56-
return {"message": "hello world"}
57-
# or
58-
return "my Message"
56+
return {"message": "hello world"}
57+
# or
58+
return "my Message"
5959
```
6060

6161
**Stringified response body (AWS Lambda):**
@@ -64,13 +64,13 @@ def handle(event, context):
6464
import json
6565

6666
def handle(event, context):
67-
return {
68-
"body": json.dumps({"message": "hello world"}),
69-
"headers": {
70-
"Content-Type": ["application/json"],
71-
},
72-
"statusCode": 200,
73-
}
67+
return {
68+
"body": json.dumps({"message": "hello world"}),
69+
"headers": {
70+
"Content-Type": ["application/json"],
71+
},
72+
"statusCode": 200,
73+
}
7474
```
7575

7676
### Getting data in Python
@@ -85,8 +85,8 @@ The following snippet returns the environment variable `ENV` you specified in th
8585
import os
8686

8787
def handle(event, context):
88-
env_variable = os.getenv('ENV')
89-
return env_variable
88+
env_variable = os.getenv('ENV')
89+
return env_variable
9090
```
9191

9292
### Using event objects in Python
@@ -103,10 +103,10 @@ You can pass information through your HTTP request with event objects. They are
103103
```python
104104

105105
def handle(event, context):
106-
# the event object is a Python dict
107-
query_param = event['queryStringParameters']['parameter']
108-
query_body = event['body']
109-
# ...
106+
# the event object is a Python dict
107+
query_param = event['queryStringParameters']['parameter']
108+
query_body = event['body']
109+
# ...
110110
```
111111

112112
Example of reading URL parameters:
@@ -115,12 +115,12 @@ Example of reading URL parameters:
115115
curl https://myfunc/user/?id=1
116116
```
117117

118-
Will fill the event with the following parameters:
118+
will fill the event with the following parameters:
119119
```python
120120
event['path'] = "/user/"
121121

122122
event['queryStringParameters'] = {
123-
"id": "1"
123+
"id": "1"
124124
}
125125
```
126126

@@ -130,8 +130,8 @@ CRON jobs share the same pattern as HTTP calls. You can find the information pas
130130

131131
```python
132132
def handle(event, context):
133-
cron_body = event['body']
134-
# ...
133+
cron_body = event['body']
134+
# ...
135135
```
136136

137137
### Connecting to HTTP services in Python
@@ -150,20 +150,20 @@ auth_token=os.getenv('X-AUTH-TOKEN') # You can pass tokens through os environmen
150150
url={YOUR URL} # If you want a dynamic URL based on information sent to handle(), define the URL inside the function
151151

152152
def handle(event, context):
153-
req = request.Request(url, method='GET')
154-
req.add_header('X-Auth-Token',auth_token)
155-
try:
153+
req = request.Request(url, method='GET')
154+
req.add_header('X-Auth-Token',auth_token)
155+
try:
156156
res = request.urlopen(req).read().decode()
157-
except error.HTTPError as e:
158-
res = e.read().decode()
157+
except error.HTTPError as e:
158+
res = e.read().decode()
159159

160-
return {
161-
"body": json.loads(res),
162-
"headers": {
163-
"Content-Type": ["application/json"],
164-
},
165-
"statusCode": 200,
166-
}
160+
return {
161+
"body": json.loads(res),
162+
"headers": {
163+
"Content-Type": ["application/json"],
164+
},
165+
"statusCode": 200,
166+
}
167167
```
168168

169169
**`POST` request example**
@@ -194,18 +194,6 @@ def handle(event, context):
194194
},
195195
"statusCode": 200,
196196
}
197-
try:
198-
res = request.urlopen(req).read().decode()
199-
except error.HTTPError as e:
200-
res = e.read().decode()
201-
202-
return {
203-
"body": json.loads(res),
204-
"headers": {
205-
"Content-Type": ["application/json"],
206-
},
207-
"statusCode": 200,
208-
}
209197
```
210198
<Message type="note">
211199
To use other libraries like `request`, you need to upload your project [with advanced methods](/serverless-functions/reference-content/deploy-function/) (recommended)

0 commit comments

Comments
 (0)