@@ -8,7 +8,7 @@ use anyhow::{bail, Result};
8
8
use crate :: types:: { Headers , PyFunction } ;
9
9
use futures_util:: stream:: StreamExt ;
10
10
use pyo3:: prelude:: * ;
11
- use pyo3:: types:: { PyDict , PyTuple } ;
11
+ use pyo3:: types:: PyDict ;
12
12
13
13
use std:: fs:: File ;
14
14
use std:: io:: Read ;
@@ -17,9 +17,9 @@ use std::io::Read;
17
17
const MAX_SIZE : usize = 10_000 ;
18
18
19
19
#[ inline]
20
- pub fn apply_headers ( response : & mut HttpResponseBuilder , headers : & Arc < Headers > ) {
21
- for a in headers. iter ( ) {
22
- response. insert_header ( ( a . key ( ) . clone ( ) , a . value ( ) . clone ( ) ) ) ;
20
+ pub fn apply_headers ( response : & mut HttpResponseBuilder , headers : HashMap < String , String > ) {
21
+ for ( key , val ) in ( headers) . iter ( ) {
22
+ response. insert_header ( ( key. clone ( ) , val . clone ( ) ) ) ;
23
23
}
24
24
}
25
25
@@ -37,7 +37,7 @@ pub fn apply_headers(response: &mut HttpResponseBuilder, headers: &Arc<Headers>)
37
37
pub async fn handle_request (
38
38
function : PyFunction ,
39
39
number_of_params : u8 ,
40
- headers : & Arc < Headers > ,
40
+ headers : HashMap < String , String > ,
41
41
payload : & mut web:: Payload ,
42
42
req : & HttpRequest ,
43
43
route_params : HashMap < String , String > ,
@@ -46,7 +46,7 @@ pub async fn handle_request(
46
46
let contents = match execute_http_function (
47
47
function,
48
48
payload,
49
- headers,
49
+ headers. clone ( ) ,
50
50
req,
51
51
route_params,
52
52
queries,
@@ -58,17 +58,30 @@ pub async fn handle_request(
58
58
Err ( err) => {
59
59
println ! ( "Error: {:?}" , err) ;
60
60
let mut response = HttpResponse :: InternalServerError ( ) ;
61
- apply_headers ( & mut response, headers) ;
61
+ apply_headers ( & mut response, headers. clone ( ) ) ;
62
62
return response. finish ( ) ;
63
63
}
64
64
} ;
65
65
66
- let mut response = HttpResponse :: Ok ( ) ;
66
+ let body = contents . get ( "body" ) . unwrap ( ) . to_owned ( ) ;
67
67
let status_code =
68
68
actix_http:: StatusCode :: from_str ( contents. get ( "status_code" ) . unwrap ( ) ) . unwrap ( ) ;
69
- apply_headers ( & mut response, headers) ;
70
- response. status ( status_code) ;
71
- response. body ( contents. get ( "body" ) . unwrap ( ) . to_owned ( ) )
69
+
70
+ let mut response = HttpResponse :: build ( status_code) ;
71
+ apply_headers ( & mut response, headers. clone ( ) ) ;
72
+ let final_response = if body != "" {
73
+ response. body ( body)
74
+ } else {
75
+ response. finish ( )
76
+ } ;
77
+
78
+ println ! (
79
+ "The status code is {} and the headers are {:?}" ,
80
+ final_response. status( ) ,
81
+ final_response. headers( )
82
+ ) ;
83
+ // response.body(contents.get("body").unwrap().to_owned())
84
+ final_response
72
85
}
73
86
74
87
pub async fn handle_middleware_request (
@@ -79,7 +92,7 @@ pub async fn handle_middleware_request(
79
92
req : & HttpRequest ,
80
93
route_params : HashMap < String , String > ,
81
94
queries : HashMap < String , String > ,
82
- ) -> Py < PyTuple > {
95
+ ) -> HashMap < String , HashMap < String , String > > {
83
96
let contents = match execute_middleware_function (
84
97
function,
85
98
payload,
@@ -92,12 +105,10 @@ pub async fn handle_middleware_request(
92
105
. await
93
106
{
94
107
Ok ( res) => res,
95
- Err ( err) => Python :: with_gil ( |py| {
96
- println ! ( "{:?}" , err) ;
97
- PyTuple :: empty ( py) . into_py ( py)
98
- } ) ,
108
+ Err ( _err) => HashMap :: new ( ) ,
99
109
} ;
100
110
111
+ println ! ( "These are the middleware response {:?}" , contents) ;
101
112
contents
102
113
}
103
114
@@ -123,12 +134,12 @@ async fn execute_middleware_function<'a>(
123
134
route_params : HashMap < String , String > ,
124
135
queries : HashMap < String , String > ,
125
136
number_of_params : u8 ,
126
- ) -> Result < Py < PyTuple > > {
137
+ ) -> Result < HashMap < String , HashMap < String , String > > > {
127
138
// TODO:
128
139
// try executing the first version of middleware(s) here
129
140
// with just headers as params
130
141
131
- let mut data: Option < Vec < u8 > > = None ;
142
+ let mut data: Vec < u8 > = Vec :: new ( ) ;
132
143
133
144
if req. method ( ) == Method :: POST
134
145
|| req. method ( ) == Method :: PUT
@@ -145,13 +156,13 @@ async fn execute_middleware_function<'a>(
145
156
body. extend_from_slice ( & chunk) ;
146
157
}
147
158
148
- data = Some ( body. to_vec ( ) )
159
+ data = body. to_vec ( )
149
160
}
150
161
151
162
// request object accessible while creating routes
152
163
let mut request = HashMap :: new ( ) ;
153
164
let mut headers_python = HashMap :: new ( ) ;
154
- for elem in headers. into_iter ( ) {
165
+ for elem in ( * headers) . iter ( ) {
155
166
headers_python. insert ( elem. key ( ) . clone ( ) , elem. value ( ) . clone ( ) ) ;
156
167
}
157
168
@@ -162,7 +173,7 @@ async fn execute_middleware_function<'a>(
162
173
request. insert ( "params" , route_params. into_py ( py) ) ;
163
174
request. insert ( "queries" , queries. into_py ( py) ) ;
164
175
request. insert ( "headers" , headers_python. into_py ( py) ) ;
165
- request. insert ( "body" , data. into_py ( py) ) ;
176
+ // request.insert("body", data.into_py(py));
166
177
167
178
// this makes the request object to be accessible across every route
168
179
let coro: PyResult < & PyAny > = match number_of_params {
@@ -176,10 +187,13 @@ async fn execute_middleware_function<'a>(
176
187
177
188
let output = output. await ?;
178
189
179
- let res = Python :: with_gil ( |py| -> PyResult < Py < PyTuple > > {
180
- let output: Py < PyTuple > = output. extract ( py) . unwrap ( ) ;
181
- Ok ( output)
182
- } ) ?;
190
+ let res =
191
+ Python :: with_gil ( |py| -> PyResult < HashMap < String , HashMap < String , String > > > {
192
+ let output: Vec < HashMap < String , HashMap < String , String > > > =
193
+ output. extract ( py) . unwrap ( ) ;
194
+ let responses = output[ 0 ] . clone ( ) ;
195
+ Ok ( responses)
196
+ } ) ?;
183
197
184
198
Ok ( res)
185
199
}
@@ -200,9 +214,10 @@ async fn execute_middleware_function<'a>(
200
214
2_u8 ..=u8:: MAX => handler. call1 ( ( request, ) ) ,
201
215
} ;
202
216
203
- let output: Py < PyTuple > = output?. extract ( ) . unwrap ( ) ;
217
+ let output: Vec < HashMap < String , HashMap < String , String > > > =
218
+ output?. extract ( ) . unwrap ( ) ;
204
219
205
- Ok ( output)
220
+ Ok ( output[ 0 ] . clone ( ) )
206
221
} )
207
222
} )
208
223
. await ?
@@ -215,15 +230,15 @@ async fn execute_middleware_function<'a>(
215
230
async fn execute_http_function (
216
231
function : PyFunction ,
217
232
payload : & mut web:: Payload ,
218
- headers : & Headers ,
233
+ headers : HashMap < String , String > ,
219
234
req : & HttpRequest ,
220
235
route_params : HashMap < String , String > ,
221
236
queries : HashMap < String , String > ,
222
237
number_of_params : u8 ,
223
238
// need to change this to return a response struct
224
239
// create a custom struct for this
225
240
) -> Result < HashMap < String , String > > {
226
- let mut data: Option < Vec < u8 > > = None ;
241
+ let mut data: Vec < u8 > = Vec :: new ( ) ;
227
242
228
243
if req. method ( ) == Method :: POST
229
244
|| req. method ( ) == Method :: PUT
@@ -240,28 +255,21 @@ async fn execute_http_function(
240
255
body. extend_from_slice ( & chunk) ;
241
256
}
242
257
243
- data = Some ( body. to_vec ( ) )
258
+ data = body. to_vec ( )
244
259
}
245
260
246
261
// request object accessible while creating routes
247
262
let mut request = HashMap :: new ( ) ;
248
- let mut headers_python = HashMap :: new ( ) ;
249
- for elem in headers. into_iter ( ) {
250
- headers_python. insert ( elem. key ( ) . clone ( ) , elem. value ( ) . clone ( ) ) ;
251
- }
252
263
253
264
match function {
254
265
PyFunction :: CoRoutine ( handler) => {
255
266
let output = Python :: with_gil ( |py| {
256
267
let handler = handler. as_ref ( py) ;
257
268
request. insert ( "params" , route_params. into_py ( py) ) ;
258
269
request. insert ( "queries" , queries. into_py ( py) ) ;
259
- request. insert ( "headers" , headers_python. into_py ( py) ) ;
260
-
261
- if let Some ( res) = data {
262
- let data = res. into_py ( py) ;
263
- request. insert ( "body" , data) ;
264
- } ;
270
+ request. insert ( "headers" , headers. into_py ( py) ) ;
271
+ let data = data. into_py ( py) ;
272
+ request. insert ( "body" , data) ;
265
273
266
274
// this makes the request object to be accessible across every route
267
275
let coro: PyResult < & PyAny > = match number_of_params {
@@ -298,11 +306,9 @@ async fn execute_http_function(
298
306
Python :: with_gil ( |py| {
299
307
let handler = handler. as_ref ( py) ;
300
308
request. insert ( "params" , route_params. into_py ( py) ) ;
301
- request. insert ( "headers" , headers_python. into_py ( py) ) ;
302
- if let Some ( res) = data {
303
- let data = res. into_py ( py) ;
304
- request. insert ( "body" , data) ;
305
- } ;
309
+ request. insert ( "headers" , headers. into_py ( py) ) ;
310
+ let data = data. into_py ( py) ;
311
+ request. insert ( "body" , data) ;
306
312
307
313
let output: PyResult < & PyAny > = match number_of_params {
308
314
0 => handler. call0 ( ) ,
@@ -325,22 +331,24 @@ pub async fn execute_event_handler(
325
331
event_handler : Option < Arc < PyFunction > > ,
326
332
event_loop : Arc < Py < PyAny > > ,
327
333
) {
328
- if let Some ( handler) = event_handler { match & ( * handler) {
329
- PyFunction :: SyncFunction ( function) => {
330
- println ! ( "Startup event handler" ) ;
331
- Python :: with_gil ( |py| {
332
- function. call0 ( py) . unwrap ( ) ;
333
- } ) ;
334
- }
335
- PyFunction :: CoRoutine ( function) => {
336
- let future = Python :: with_gil ( |py| {
337
- println ! ( "Startup event handler async" ) ;
338
-
339
- let coroutine = function. as_ref ( py) . call0 ( ) . unwrap ( ) ;
340
- pyo3_asyncio:: into_future_with_loop ( ( * event_loop) . as_ref ( py) , coroutine)
341
- . unwrap ( )
342
- } ) ;
343
- future. await . unwrap ( ) ;
334
+ if let Some ( handler) = event_handler {
335
+ match & ( * handler) {
336
+ PyFunction :: SyncFunction ( function) => {
337
+ println ! ( "Startup event handler" ) ;
338
+ Python :: with_gil ( |py| {
339
+ function. call0 ( py) . unwrap ( ) ;
340
+ } ) ;
341
+ }
342
+ PyFunction :: CoRoutine ( function) => {
343
+ let future = Python :: with_gil ( |py| {
344
+ println ! ( "Startup event handler async" ) ;
345
+
346
+ let coroutine = function. as_ref ( py) . call0 ( ) . unwrap ( ) ;
347
+ pyo3_asyncio:: into_future_with_loop ( ( * event_loop) . as_ref ( py) , coroutine)
348
+ . unwrap ( )
349
+ } ) ;
350
+ future. await . unwrap ( ) ;
351
+ }
344
352
}
345
- } }
353
+ }
346
354
}
0 commit comments