Skip to content

Commit

Permalink
Add the ability to add body in PUT, PATCH and DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox committed Aug 3, 2021
1 parent 87c60fb commit 51cc6aa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
80 changes: 40 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import argparse
import asyncio
import inspect

from .robyn import Server
from .responses import static_file, jsonify
Expand Down Expand Up @@ -91,11 +90,6 @@ def post(self, endpoint):
:param endpoint [str]: [endpoint to server the route]
"""
def inner(handler):
sig = inspect.signature(handler)
params = len(sig.parameters)
if params != 1:
print("We need one argument on post.")
return
self.add_route("POST", endpoint, handler)

return inner
Expand Down Expand Up @@ -130,5 +124,5 @@ def patch(self, endpoint):
"""
def inner(handler):
self.add_route("PATCH", endpoint, handler)

return inner
8 changes: 6 additions & 2 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,17 @@ async fn execute_function(
) -> Result<String> {
let mut data: Option<Vec<u8>> = None;

if req.method() == Method::POST {
if req.method() == Method::POST
|| req.method() == Method::PUT
|| req.method() == Method::PATCH
|| req.method() == Method::DELETE
{
let mut body = web::BytesMut::new();
while let Some(chunk) = payload.next().await {
let chunk = chunk?;
// limit max size of in-memory payload
if (body.len() + chunk.len()) > MAX_SIZE {
bail!("Overflow");
bail!("Body content Overflow");
}
body.extend_from_slice(&chunk);
}
Expand Down
17 changes: 14 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@ async def test():
path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "index.html"))
return static_file(path)


@app.post("/jsonify")
async def json():
async def json(body):
return jsonify({"hello": "world"})

@app.post("/post")
async def postreq(body):
return bytearray(body).decode("utf-8")

@app.put("/put")
async def putreq(body):
return bytearray(body).decode("utf-8")

@app.delete("/delete")
async def deletereq(body):
return bytearray(body).decode("utf-8")


@app.patch("/patch")
async def patchreq(body):
return bytearray(body).decode("utf-8")


@app.get("/sleep")
async def sleeper():
Expand All @@ -38,7 +50,6 @@ async def sleeper():
@app.get("/blocker")
def blocker():
import time

time.sleep(10)
return "blocker function"

Expand Down

0 comments on commit 51cc6aa

Please sign in to comment.