Skip to content

Commit 857f894

Browse files
committed
fix: changes after review
1 parent f1e55d2 commit 857f894

File tree

7 files changed

+56
-41
lines changed

7 files changed

+56
-41
lines changed

integration_tests/base_routes.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import os
22
import pathlib
33

4-
from robyn import WS, Robyn, jsonify, serve_file, serve_html
5-
from robyn.robyn import Request, Response
4+
from robyn import WS, Robyn, Request, Response, jsonify, serve_file, serve_html
65
from robyn.templating import JinjaTemplate
76

87
from views import SyncView, AsyncView
@@ -69,7 +68,7 @@ def sync_after_request(response: Response):
6968
new_headers = response.headers
7069
new_headers["after"] = "sync_after_request"
7170
response.headers = new_headers
72-
response.set_body(response.body.as_str() + " after")
71+
response.body = response.body + " after"
7372
return response
7473

7574

@@ -93,7 +92,7 @@ async def async_after_request(response: Response):
9392
new_headers = response.headers
9493
new_headers["after"] = "async_after_request"
9594
response.headers = new_headers
96-
response.set_body(response.body.as_str() + " after")
95+
response.body = response.body + " after"
9796
return response
9897

9998

@@ -404,12 +403,12 @@ async def async_dict_post():
404403

405404
@app.post("/sync/body")
406405
def sync_body_post(request: Request):
407-
return request.body.as_str()
406+
return request.body
408407

409408

410409
@app.post("/async/body")
411410
async def async_body_post(request: Request):
412-
return request.body.as_str()
411+
return request.body
413412

414413

415414
# --- PUT ---
@@ -442,12 +441,12 @@ async def async_dict_put():
442441

443442
@app.put("/sync/body")
444443
def sync_body_put(request: Request):
445-
return request.body.as_str()
444+
return request.body
446445

447446

448447
@app.put("/async/body")
449448
async def async_body_put(request: Request):
450-
return request.body.as_str()
449+
return request.body
451450

452451

453452
# --- DELETE ---
@@ -480,12 +479,12 @@ async def async_dict_delete():
480479

481480
@app.delete("/sync/body")
482481
def sync_body_delete(request: Request):
483-
return request.body.as_str()
482+
return request.body
484483

485484

486485
@app.delete("/async/body")
487486
async def async_body_delete(request: Request):
488-
return request.body.as_str()
487+
return request.body
489488

490489

491490
# --- PATCH ---
@@ -518,12 +517,12 @@ async def async_dict_patch():
518517

519518
@app.patch("/sync/body")
520519
def sync_body_patch(request: Request):
521-
return request.body.as_str()
520+
return request.body
522521

523522

524523
@app.patch("/async/body")
525524
async def async_body_patch(request: Request):
526-
return request.body.as_str()
525+
return request.body
527526

528527

529528
# ===== Views =====
@@ -535,7 +534,7 @@ def get():
535534
return "Hello, world!"
536535

537536
def post(request: Request):
538-
body = request.body.as_str()
537+
body = request.body
539538
return {"status_code": 200, "body": body}
540539

541540

@@ -545,7 +544,7 @@ async def get():
545544
return "Hello, world!"
546545

547546
async def post(request: Request):
548-
body = request.body.as_str()
547+
body = request.body
549548
return {"status_code": 200, "body": body}
550549

551550

integration_tests/views/async_view.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from robyn.robyn import Request
1+
from robyn import Request
22

33

44
def AsyncView():
55
async def get():
66
return "Hello, world!"
77

88
async def post(request: Request):
9-
body = request.body.as_str()
9+
body = request.body
1010
return {
1111
"status": 200,
1212
"body": body,

integration_tests/views/sync_view.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from robyn.robyn import Request
1+
from robyn import Request
22

33

44
def SyncView():
55
def get():
66
return "Hello, world!"
77

88
def post(request: Request):
9-
body = request.body.as_str()
9+
body = request.body
1010
return {
1111
"status": 200,
1212
"body": body,

robyn/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from robyn.logger import Colors, logger
1616
from robyn.processpool import run_processes
1717
from robyn.responses import jsonify, serve_file, serve_html
18-
from robyn.robyn import FunctionInfo, Response
18+
from robyn.robyn import FunctionInfo, Request, Response
1919
from robyn.router import MiddlewareRouter, Router, WebSocketRouter
2020
from robyn.types import Directory, Header
2121
from robyn.ws import WS
@@ -315,4 +315,4 @@ def inner(handler):
315315
return inner
316316

317317

318-
__all__ = ["Robyn", "jsonify", "serve_file", "serve_html", "Response"]
318+
__all__ = [Robyn, Request, Response, jsonify, serve_file, serve_html]

robyn/robyn.pyi

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class FunctionInfo:
1919
class Body:
2020
content: Union[str, bytes]
2121

22-
def as_str(self) -> str:
23-
pass
2422
def as_bytes(self) -> bytes:
2523
pass
2624
def set(self, content: Union[str, bytes]):

src/server.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::routers::http_router::HttpRouter;
88
use crate::routers::types::MiddlewareRoute;
99
use crate::routers::{middleware_router::MiddlewareRouter, web_socket_router::WebSocketRouter};
1010
use crate::shared_socket::SocketHeld;
11-
use crate::types::{FunctionInfo, Headers};
11+
use crate::types::FunctionInfo;
1212
use crate::web_socket_connection::start_web_socket;
1313

1414
use std::convert::TryInto;
@@ -336,8 +336,8 @@ async fn index(
336336
router: web::Data<Arc<HttpRouter>>,
337337
const_router: web::Data<Arc<ConstRouter>>,
338338
middleware_router: web::Data<Arc<MiddlewareRouter>>,
339-
global_request_headers: web::Data<Arc<Headers>>,
340-
global_response_headers: web::Data<Arc<Headers>>,
339+
global_request_headers: web::Data<Arc<DashMap<String, String>>>,
340+
global_response_headers: web::Data<Arc<DashMap<String, String>>>,
341341
body: Bytes,
342342
req: HttpRequest,
343343
) -> impl Responder {
@@ -358,8 +358,8 @@ async fn index(
358358
};
359359

360360
// Route execution
361-
let mut response = if let Some(r) = const_router.get_route(req.method(), req.uri().path()) {
362-
r
361+
let mut response = if let Some(res) = const_router.get_route(req.method(), req.uri().path()) {
362+
res
363363
} else if let Some((function, route_params)) = router.get_route(req.method(), req.uri().path())
364364
{
365365
request.params = route_params;

src/types.rs

+32-14
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ pub struct ActixBytesWrapper {
3333

3434
#[pymethods]
3535
impl ActixBytesWrapper {
36-
pub fn as_str(&self) -> PyResult<String> {
37-
Ok(String::from_utf8(self.content.to_vec())?)
38-
}
39-
4036
pub fn as_bytes(&self) -> PyResult<Vec<u8>> {
4137
Ok(self.content.to_vec())
4238
}
@@ -48,7 +44,7 @@ impl ActixBytesWrapper {
4844
v.as_bytes().to_vec()
4945
} else {
5046
return Err(PyValueError::new_err(format!(
51-
"Could not convert {} specified body to bytes",
47+
"Could not convert body of type {} to bytes",
5248
type_of(content)
5349
)));
5450
};
@@ -155,10 +151,26 @@ pub struct Request {
155151
pub method: Method,
156152
#[pyo3(get, set)]
157153
pub params: HashMap<String, String>,
158-
#[pyo3(get, set)]
159154
pub body: ActixBytesWrapper,
160155
}
161156

157+
#[pymethods]
158+
impl Request {
159+
#[getter]
160+
pub fn get_body(&self) -> PyResult<String> {
161+
Ok(String::from_utf8(self.body.to_vec())?)
162+
}
163+
164+
#[setter]
165+
pub fn set_body(&mut self, content: &PyAny) -> PyResult<()> {
166+
self.body.set(content)
167+
}
168+
169+
pub fn get_body_as_bytes(&self) -> PyResult<Vec<u8>> {
170+
self.body.as_bytes()
171+
}
172+
}
173+
162174
impl Request {
163175
pub fn from_actix_request(
164176
req: &HttpRequest,
@@ -173,7 +185,7 @@ impl Request {
173185
queries.insert(params.0.to_string(), params.1.to_string());
174186
}
175187
}
176-
let request_headers = req
188+
let headers = req
177189
.headers()
178190
.iter()
179191
.map(|(k, v)| (k.to_string(), v.to_str().unwrap().to_string()))
@@ -186,7 +198,7 @@ impl Request {
186198

187199
Self {
188200
queries,
189-
headers: request_headers,
201+
headers,
190202
method: req.method().clone(),
191203
params: HashMap::new(),
192204
body: ActixBytesWrapper { content: body },
@@ -201,7 +213,6 @@ pub struct Response {
201213
pub response_type: String,
202214
#[pyo3(get, set)]
203215
pub headers: HashMap<String, String>,
204-
#[pyo3(get)]
205216
pub body: ActixBytesWrapper,
206217
pub file_path: Option<String>,
207218
}
@@ -254,9 +265,18 @@ impl Response {
254265
})
255266
}
256267

257-
pub fn set_body(&mut self, body: &PyAny) -> PyResult<()> {
258-
self.body = ActixBytesWrapper::new(body)?;
259-
Ok(())
268+
#[getter]
269+
pub fn get_body(&self) -> PyResult<String> {
270+
Ok(String::from_utf8(self.body.to_vec())?)
271+
}
272+
273+
#[setter]
274+
pub fn set_body(&mut self, content: &PyAny) -> PyResult<()> {
275+
self.body.set(content)
276+
}
277+
278+
pub fn get_body_as_bytes(&self) -> PyResult<Vec<u8>> {
279+
self.body.as_bytes()
260280
}
261281

262282
pub fn set_file_path(&mut self, file_path: &str) -> PyResult<()> {
@@ -272,5 +292,3 @@ impl Response {
272292
Ok(())
273293
}
274294
}
275-
276-
pub type Headers = DashMap<String, String>;

0 commit comments

Comments
 (0)