Skip to content

Commit

Permalink
fix: changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineRR committed Mar 19, 2023
1 parent 901fc84 commit 1ff3bb3
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 41 deletions.
27 changes: 13 additions & 14 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import os
import pathlib

from robyn import WS, Robyn, jsonify, serve_file, serve_html
from robyn.robyn import Request, Response
from robyn import WS, Robyn, Request, Response, jsonify, serve_file, serve_html
from robyn.templating import JinjaTemplate

from views import SyncView, AsyncView
Expand Down Expand Up @@ -69,7 +68,7 @@ def sync_after_request(response: Response):
new_headers = response.headers
new_headers["after"] = "sync_after_request"
response.headers = new_headers
response.set_body(response.body.as_str() + " after")
response.body = response.body + " after"
return response


Expand All @@ -93,7 +92,7 @@ async def async_after_request(response: Response):
new_headers = response.headers
new_headers["after"] = "async_after_request"
response.headers = new_headers
response.set_body(response.body.as_str() + " after")
response.body = response.body + " after"
return response


Expand Down Expand Up @@ -429,12 +428,12 @@ async def async_dict_post():

@app.post("/sync/body")
def sync_body_post(request: Request):
return request.body.as_str()
return request.body


@app.post("/async/body")
async def async_body_post(request: Request):
return request.body.as_str()
return request.body


# --- PUT ---
Expand Down Expand Up @@ -467,12 +466,12 @@ async def async_dict_put():

@app.put("/sync/body")
def sync_body_put(request: Request):
return request.body.as_str()
return request.body


@app.put("/async/body")
async def async_body_put(request: Request):
return request.body.as_str()
return request.body


# --- DELETE ---
Expand Down Expand Up @@ -505,12 +504,12 @@ async def async_dict_delete():

@app.delete("/sync/body")
def sync_body_delete(request: Request):
return request.body.as_str()
return request.body


@app.delete("/async/body")
async def async_body_delete(request: Request):
return request.body.as_str()
return request.body


# --- PATCH ---
Expand Down Expand Up @@ -543,12 +542,12 @@ async def async_dict_patch():

@app.patch("/sync/body")
def sync_body_patch(request: Request):
return request.body.as_str()
return request.body


@app.patch("/async/body")
async def async_body_patch(request: Request):
return request.body.as_str()
return request.body


# ===== Views =====
Expand All @@ -560,7 +559,7 @@ def get():
return "Hello, world!"

def post(request: Request):
body = request.body.as_str()
body = request.body
return {"status_code": 200, "body": body}


Expand All @@ -570,7 +569,7 @@ async def get():
return "Hello, world!"

async def post(request: Request):
body = request.body.as_str()
body = request.body
return {"status_code": 200, "body": body}


Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_binary_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
def test_binary_output(route: str, text: str, session):
r = get(route)
assert r.headers["Content-Type"] == "application/octet-stream"
assert r.text == "OK"
assert r.text == text
4 changes: 2 additions & 2 deletions integration_tests/views/async_view.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from robyn.robyn import Request
from robyn import Request


def AsyncView():
async def get():
return "Hello, world!"

async def post(request: Request):
body = request.body.as_str()
body = request.body
return {
"status": 200,
"body": body,
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/views/sync_view.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from robyn.robyn import Request
from robyn import Request


def SyncView():
def get():
return "Hello, world!"

def post(request: Request):
body = request.body.as_str()
body = request.body
return {
"status": 200,
"body": body,
Expand Down
4 changes: 2 additions & 2 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from robyn.logger import Colors, logger
from robyn.processpool import run_processes
from robyn.responses import jsonify, serve_file, serve_html
from robyn.robyn import FunctionInfo, Response
from robyn.robyn import FunctionInfo, Request, Response
from robyn.router import MiddlewareRouter, Router, WebSocketRouter
from robyn.types import Directory, Header
from robyn.status_codes import StatusCodes
Expand Down Expand Up @@ -316,4 +316,4 @@ def inner(handler):
return inner


__all__ = ["Robyn", "jsonify", "serve_file", "serve_html", "Response", "StatusCodes"]
__all__ = [Robyn, Request, Response, StatusCodes, jsonify, serve_file, serve_html]
2 changes: 0 additions & 2 deletions robyn/robyn.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class FunctionInfo:
class Body:
content: Union[str, bytes]

def as_str(self) -> str:
pass
def as_bytes(self) -> bytes:
pass
def set(self, content: Union[str, bytes]):
Expand Down
10 changes: 5 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::routers::http_router::HttpRouter;
use crate::routers::types::MiddlewareRoute;
use crate::routers::{middleware_router::MiddlewareRouter, web_socket_router::WebSocketRouter};
use crate::shared_socket::SocketHeld;
use crate::types::{FunctionInfo, Headers};
use crate::types::FunctionInfo;
use crate::web_socket_connection::start_web_socket;

use std::convert::TryInto;
Expand Down Expand Up @@ -336,8 +336,8 @@ async fn index(
router: web::Data<Arc<HttpRouter>>,
const_router: web::Data<Arc<ConstRouter>>,
middleware_router: web::Data<Arc<MiddlewareRouter>>,
global_request_headers: web::Data<Arc<Headers>>,
global_response_headers: web::Data<Arc<Headers>>,
global_request_headers: web::Data<Arc<DashMap<String, String>>>,
global_response_headers: web::Data<Arc<DashMap<String, String>>>,
body: Bytes,
req: HttpRequest,
) -> impl Responder {
Expand All @@ -358,8 +358,8 @@ async fn index(
};

// Route execution
let mut response = if let Some(r) = const_router.get_route(req.method(), req.uri().path()) {
r
let mut response = if let Some(res) = const_router.get_route(req.method(), req.uri().path()) {
res
} else if let Some((function, route_params)) = router.get_route(req.method(), req.uri().path())
{
request.params = route_params;
Expand Down
45 changes: 32 additions & 13 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ pub struct ActixBytesWrapper {

#[pymethods]
impl ActixBytesWrapper {
pub fn as_str(&self) -> PyResult<String> {
Ok(String::from_utf8(self.content.to_vec())?)
}

pub fn as_bytes(&self) -> PyResult<Vec<u8>> {
Ok(self.content.to_vec())
}
Expand All @@ -48,7 +44,7 @@ impl ActixBytesWrapper {
v.as_bytes().to_vec()
} else {
return Err(PyValueError::new_err(format!(
"Could not convert {} specified body to bytes",
"Could not convert body of type {} to bytes",
type_of(content)
)));
};
Expand Down Expand Up @@ -184,6 +180,23 @@ pub struct Request {
pub url: Url,
}

#[pymethods]
impl Request {
#[getter]
pub fn get_body(&self) -> PyResult<String> {
Ok(String::from_utf8(self.body.to_vec())?)
}

#[setter]
pub fn set_body(&mut self, content: &PyAny) -> PyResult<()> {
self.body.set(content)
}

pub fn get_body_as_bytes(&self) -> PyResult<Vec<u8>> {
self.body.as_bytes()
}
}

impl Request {
pub fn from_actix_request(
req: &HttpRequest,
Expand All @@ -198,7 +211,7 @@ impl Request {
queries.insert(params.0.to_string(), params.1.to_string());
}
}
let request_headers = req
let headers = req
.headers()
.iter()
.map(|(k, v)| (k.to_string(), v.to_str().unwrap().to_string()))
Expand All @@ -211,7 +224,7 @@ impl Request {

Self {
queries,
headers: request_headers,
headers,
method: req.method().clone(),
params: HashMap::new(),
body,
Expand All @@ -231,7 +244,6 @@ pub struct Response {
pub response_type: String,
#[pyo3(get, set)]
pub headers: HashMap<String, String>,
#[pyo3(get)]
pub body: ActixBytesWrapper,
pub file_path: Option<String>,
}
Expand Down Expand Up @@ -284,9 +296,18 @@ impl Response {
})
}

pub fn set_body(&mut self, body: &PyAny) -> PyResult<()> {
self.body = ActixBytesWrapper::new(body)?;
Ok(())
#[getter]
pub fn get_body(&self) -> PyResult<String> {
Ok(String::from_utf8(self.body.to_vec())?)
}

#[setter]
pub fn set_body(&mut self, content: &PyAny) -> PyResult<()> {
self.body.set(content)
}

pub fn get_body_as_bytes(&self) -> PyResult<Vec<u8>> {
self.body.as_bytes()
}

pub fn set_file_path(&mut self, file_path: &str) -> PyResult<()> {
Expand All @@ -302,5 +323,3 @@ impl Response {
Ok(())
}
}

pub type Headers = DashMap<String, String>;

0 comments on commit 1ff3bb3

Please sign in to comment.