Skip to content

Commit 625a0a3

Browse files
committed
Reqeusts object is now optional
1 parent dfb1f3c commit 625a0a3

File tree

5 files changed

+73
-28
lines changed

5 files changed

+73
-28
lines changed

robyn/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import argparse
44
import asyncio
5+
from inspect import signature
56

67
from .robyn import Server
78
from .responses import static_file, jsonify
@@ -40,8 +41,9 @@ def add_route(self, route_type, endpoint, handler):
4041

4142
""" We will add the status code here only
4243
"""
44+
number_of_params = len(signature(handler).parameters)
4345
self.server.add_route(
44-
route_type, endpoint, handler, asyncio.iscoroutinefunction(handler)
46+
route_type, endpoint, handler, asyncio.iscoroutinefunction(handler), number_of_params
4547
)
4648

4749
def add_directory(self, route, directory_path, index_file=None, show_files_listing=False):

src/processor.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,22 @@ pub fn apply_headers(response: &mut HttpResponseBuilder, headers: &Arc<Headers>)
3535
///
3636
pub async fn handle_request(
3737
function: PyFunction,
38+
number_of_params: u8,
3839
headers: &Arc<Headers>,
3940
payload: &mut web::Payload,
4041
req: &HttpRequest,
4142
route_params: HashMap<String, String>,
4243
) -> HttpResponse {
43-
let contents = match execute_function(function, payload, headers, req, route_params).await {
44+
let contents = match execute_function(
45+
function,
46+
payload,
47+
headers,
48+
req,
49+
route_params,
50+
number_of_params,
51+
)
52+
.await
53+
{
4454
Ok(res) => res,
4555
Err(err) => {
4656
println!("Error: {:?}", err);
@@ -76,6 +86,7 @@ async fn execute_function(
7686
headers: &Headers,
7787
req: &HttpRequest,
7888
route_params: HashMap<String, String>,
89+
number_of_params: u8,
7990
) -> Result<String> {
8091
let mut data: Option<Vec<u8>> = None;
8192

@@ -119,7 +130,12 @@ async fn execute_function(
119130
};
120131

121132
// this makes the request object to be accessible across every route
122-
let coro: PyResult<&PyAny> = handler.call1((request,));
133+
let coro: PyResult<&PyAny> = match number_of_params {
134+
0 => handler.call0(),
135+
1 => handler.call1((request,)),
136+
// this is done to accomodate any future params
137+
2_u8..=u8::MAX => handler.call1((request,)),
138+
};
123139
pyo3_asyncio::tokio::into_future(coro?)
124140
})?;
125141

@@ -166,16 +182,20 @@ async fn execute_function(
166182
let handler = handler.as_ref(py);
167183
request.insert("params", route_params.into_py(py));
168184
request.insert("headers", headers_python.into_py(py));
169-
let output: PyResult<&PyAny> = match data {
185+
match data {
170186
Some(res) => {
171187
let data = res.into_py(py);
172188
request.insert("body", data);
173-
174-
handler.call1((request,))
175189
}
176-
None => handler.call1((request,)),
190+
None => {}
177191
};
178192

193+
let output: PyResult<&PyAny> = match number_of_params {
194+
0 => handler.call0(),
195+
1 => handler.call1((request,)),
196+
// this is done to accomodate any future params
197+
2_u8..=u8::MAX => handler.call1((request,)),
198+
};
179199
let output: &str = output?.extract()?;
180200
Ok(output.to_string())
181201
})

src/router.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ use matchit::Node;
1111
/// Contains the thread safe hashmaps of different routes
1212
1313
pub struct Router {
14-
get_routes: Arc<RwLock<Node<PyFunction>>>,
15-
post_routes: Arc<RwLock<Node<PyFunction>>>,
16-
put_routes: Arc<RwLock<Node<PyFunction>>>,
17-
delete_routes: Arc<RwLock<Node<PyFunction>>>,
18-
patch_routes: Arc<RwLock<Node<PyFunction>>>,
19-
head_routes: Arc<RwLock<Node<PyFunction>>>,
20-
options_routes: Arc<RwLock<Node<PyFunction>>>,
21-
connect_routes: Arc<RwLock<Node<PyFunction>>>,
22-
trace_routes: Arc<RwLock<Node<PyFunction>>>,
14+
get_routes: Arc<RwLock<Node<(PyFunction, u8)>>>,
15+
post_routes: Arc<RwLock<Node<(PyFunction, u8)>>>,
16+
put_routes: Arc<RwLock<Node<(PyFunction, u8)>>>,
17+
delete_routes: Arc<RwLock<Node<(PyFunction, u8)>>>,
18+
patch_routes: Arc<RwLock<Node<(PyFunction, u8)>>>,
19+
head_routes: Arc<RwLock<Node<(PyFunction, u8)>>>,
20+
options_routes: Arc<RwLock<Node<(PyFunction, u8)>>>,
21+
connect_routes: Arc<RwLock<Node<(PyFunction, u8)>>>,
22+
trace_routes: Arc<RwLock<Node<(PyFunction, u8)>>>,
2323
}
2424

2525
impl Router {
@@ -38,7 +38,7 @@ impl Router {
3838
}
3939

4040
#[inline]
41-
fn get_relevant_map(&self, route: Method) -> Option<&Arc<RwLock<Node<PyFunction>>>> {
41+
fn get_relevant_map(&self, route: Method) -> Option<&Arc<RwLock<Node<(PyFunction, u8)>>>> {
4242
match route {
4343
Method::GET => Some(&self.get_routes),
4444
Method::POST => Some(&self.post_routes),
@@ -54,7 +54,7 @@ impl Router {
5454
}
5555

5656
#[inline]
57-
fn get_relevant_map_str(&self, route: &str) -> Option<&Arc<RwLock<Node<PyFunction>>>> {
57+
fn get_relevant_map_str(&self, route: &str) -> Option<&Arc<RwLock<Node<(PyFunction, u8)>>>> {
5858
let method = match Method::from_bytes(route.as_bytes()) {
5959
Ok(res) => res,
6060
Err(_) => return None,
@@ -65,7 +65,14 @@ impl Router {
6565

6666
// Checks if the functions is an async function
6767
// Inserts them in the router according to their nature(CoRoutine/SyncFunction)
68-
pub fn add_route(&self, route_type: &str, route: &str, handler: Py<PyAny>, is_async: bool) {
68+
pub fn add_route(
69+
&self,
70+
route_type: &str,
71+
route: &str,
72+
handler: Py<PyAny>,
73+
is_async: bool,
74+
number_of_params: u8,
75+
) {
6976
let table = match self.get_relevant_map_str(route_type) {
7077
Some(table) => table,
7178
None => return,
@@ -80,15 +87,15 @@ impl Router {
8087
table
8188
.write()
8289
.unwrap()
83-
.insert(route.to_string(), function)
90+
.insert(route.to_string(), (function, number_of_params))
8491
.unwrap();
8592
}
8693

8794
pub fn get_route(
8895
&self,
8996
route_method: Method,
9097
route: &str,
91-
) -> Option<(PyFunction, HashMap<String, String>)> {
98+
) -> Option<((PyFunction, u8), HashMap<String, String>)> {
9299
let table = self.get_relevant_map(route_method)?;
93100
match table.read().unwrap().at(route) {
94101
Ok(res) => {

src/server.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,17 @@ impl Server {
143143

144144
/// Add a new route to the routing tables
145145
/// can be called after the server has been started
146-
pub fn add_route(&self, route_type: &str, route: &str, handler: Py<PyAny>, is_async: bool) {
146+
pub fn add_route(
147+
&self,
148+
route_type: &str,
149+
route: &str,
150+
handler: Py<PyAny>,
151+
is_async: bool,
152+
number_of_params: u8,
153+
) {
147154
println!("Route added for {} {} ", route_type, route);
148-
self.router.add_route(route_type, route, handler, is_async);
155+
self.router
156+
.add_route(route_type, route, handler, is_async, number_of_params);
149157
}
150158
}
151159

@@ -164,8 +172,16 @@ async fn index(
164172
req: HttpRequest,
165173
) -> impl Responder {
166174
match router.get_route(req.method().clone(), req.uri().path()) {
167-
Some((handler_function, route_params)) => {
168-
handle_request(handler_function, &headers, &mut payload, &req, route_params).await
175+
Some(((handler_function, number_of_params), route_params)) => {
176+
handle_request(
177+
handler_function,
178+
number_of_params,
179+
&headers,
180+
&mut payload,
181+
&req,
182+
route_params,
183+
)
184+
.await
169185
}
170186
None => {
171187
let mut response = HttpResponse::Ok();

test_python/base_routes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313

1414
@app.get("/")
15-
async def h(request):
16-
print(request)
15+
async def h(requests):
16+
print(requests)
1717
global callCount
1818
callCount += 1
1919
message = "Called " + str(callCount) + " times"
@@ -26,7 +26,7 @@ async def test(request):
2626
return static_file("./index.html")
2727

2828
@app.get("/jsonify")
29-
async def json_get(request):
29+
async def json_get():
3030
return jsonify({"hello": "world"})
3131

3232

0 commit comments

Comments
 (0)