Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to add body in PUT, PATCH and DELETE #60

Merged
merged 1 commit into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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