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 8, 2023
1 parent f1e55d2 commit 697e4a8
Show file tree
Hide file tree
Showing 7 changed files with 48 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 @@ -404,12 +403,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 @@ -442,12 +441,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 @@ -480,12 +479,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 @@ -518,12 +517,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 @@ -535,7 +534,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 @@ -545,7 +544,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
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.ws import WS
Expand Down Expand Up @@ -315,4 +315,4 @@ def inner(handler):
return inner


__all__ = ["Robyn", "jsonify", "serve_file", "serve_html", "Response"]
__all__ = [Robyn, Request, Response, 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
38 changes: 24 additions & 14 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 @@ -155,10 +151,22 @@ pub struct Request {
pub method: Method,
#[pyo3(get, set)]
pub params: HashMap<String, String>,
#[pyo3(get, set)]
pub body: ActixBytesWrapper,
}

#[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)
}
}

impl Request {
pub fn from_actix_request(
req: &HttpRequest,
Expand All @@ -173,7 +181,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 @@ -186,7 +194,7 @@ impl Request {

Self {
queries,
headers: request_headers,
headers,
method: req.method().clone(),
params: HashMap::new(),
body: ActixBytesWrapper { content: body },
Expand All @@ -201,7 +209,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 @@ -254,9 +261,14 @@ 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 set_file_path(&mut self, file_path: &str) -> PyResult<()> {
Expand All @@ -272,5 +284,3 @@ impl Response {
Ok(())
}
}

pub type Headers = DashMap<String, String>;

0 comments on commit 697e4a8

Please sign in to comment.