Skip to content

Commit

Permalink
feat: allow dictionary being returned directly (sparckles#768)
Browse files Browse the repository at this point in the history
* feat: allow dictionary being returned directly

* fix: integration tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* handle header types

* handle file types

* docs: update robyn docs

* get rid of unused functions

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
sansyrox and pre-commit-ci[bot] authored Mar 24, 2024
1 parent 7ba794a commit 2180e15
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 145 deletions.
4 changes: 2 additions & 2 deletions docs_src/src/pages/documentation/api_reference/exceptions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ Batman scaled his application across multiple cores for better performance. He u
```python {{ title: 'untyped' }}
@app.exception
def handle_exception(error):
return {"status_code": 500, "body": f"error msg: {error}"}
return Response({status_code=500, body=f"error msg: {error}", headers={}})
```

```python {{ title: 'typed' }}
@app.exception
def handle_exception(error: Exception):
return {"status_code": 500, "body": f"error msg: {error}"}
return Response({status_code=500, body=f"error msg: {error}", headers={}})
```
</CodeGroup>
</Col>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Batman scaled his application across multiple cores for better performance. He u
with open('test.txt', 'wb') as f:
f.write(body)

return jsonify({'message': 'success'})
return {'message': 'success'}
```
```python {{ title: 'typed' }}
from robyn.robyn import File
Expand All @@ -42,7 +42,7 @@ Batman scaled his application across multiple cores for better performance. He u
with open('test.txt', 'wb') as f:
f.write(file)

return jsonify({'message': 'success'})
return {'message': 'success'}
```

</CodeGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Batman was curios about the ability to return JSON responses from the applicatio

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

```

Expand All @@ -208,7 +208,7 @@ Batman was curios about the ability to return JSON responses from the applicatio

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

```
</CodeGroup>
Expand Down Expand Up @@ -239,7 +239,7 @@ Batman was curious about how to access path parameters and query parameters from
@app.post("/jsonify/:id")
async def json(request):
print(request["path_params"]["id"])
return jsonify({"hello": "world"})
return {"hello": "world"}


```
Expand All @@ -250,7 +250,7 @@ Batman was curious about how to access path parameters and query parameters from
@app.post("/jsonify/:id")
async def json(request: Request):
print(request["path_params"]["id"])
return jsonify({"hello": "world"})
return {"hello": "world"}

```

Expand Down
8 changes: 4 additions & 4 deletions docs_src/src/pages/documentation/api_reference/views.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A view, simply is a function with a collection of other closures. e.g.

def post(request):
body = request.body
return {"status_code": 200, "description": body}
return Response({"status_code": 200, "description": body, "headers": {}})

```

Expand All @@ -38,7 +38,7 @@ A view, simply is a function with a collection of other closures. e.g.

def post(request: Request):
body = request.body
return {"status_code": 200, "description": body}
return Response({"status_code": 200, "description": body, "headers": {}})
```
</CodeGroup>
</Col>
Expand All @@ -62,7 +62,7 @@ You can serve views in two ways:

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

```
```python {{ title: 'typed' }}
Expand All @@ -75,7 +75,7 @@ You can serve views in two ways:

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

```
</CodeGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async def login_user(request):
raise HTTPException(status_code=401, detail="Invalid credentials")


return jsonify({"access_token": token})
return {"access_token": token}

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The code would look like this:

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


Expand All @@ -87,7 +87,7 @@ The code would look like this:

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

app.add_view("/sync/view/decorator", sync_decorator_view)
```
Expand Down
154 changes: 71 additions & 83 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,42 +244,38 @@ async def async_str_const_get():

@app.get("/sync/dict")
def sync_dict_get():
return {
"status_code": 200,
"description": "sync dict get",
"type": "text",
"headers": {"sync": "dict"},
}
return Response(
status_code=200,
description="sync dict get",
headers={"sync": "dict"},
)


@app.get("/async/dict")
async def async_dict_get():
return {
"status_code": 200,
"description": "async dict get",
"type": "text",
"headers": {"async": "dict"},
}
return Response(
status_code=200,
description="async dict get",
headers={"async": "dict"},
)


@app.get("/sync/dict/const", const=True)
def sync_dict_const_get():
return {
"status_code": 200,
"description": "sync dict const get",
"type": "text",
"headers": {"sync_const": "dict"},
}
return Response(
status_code=200,
description="sync dict const get",
headers={"sync_const": "dict"},
)


@app.get("/async/dict/const", const=True)
async def async_dict_const_get():
return {
"status_code": 200,
"description": "async dict const get",
"type": "text",
"headers": {"async_const": "dict"},
}
return Response(
status_code=200,
description="async dict const get",
headers={"async_const": "dict"},
)


# Response
Expand Down Expand Up @@ -484,22 +480,21 @@ async def async_query(request: Request):

@app.get("/404")
def return_404():
return {"status_code": 404, "description": "not found", "type": "text"}
return Response(status_code=404, description="not found", headers={"Content-Type": "text"})


@app.get("/202")
def return_202():
return {"status_code": 202, "description": "hello", "type": "text"}
return Response(status_code=202, description="hello", headers={"Content-Type": "text"})


@app.get("/307")
async def redirect():
return {
"status_code": 307,
"description": "",
"type": "text",
"headers": {"Location": "redirect_route"},
}
return Response(
status_code=307,
description="",
headers={"Location": "redirect_route"},
)


@app.get("/redirect_route")
Expand All @@ -524,22 +519,20 @@ async def async_raise():

@app.post("/sync/dict")
def sync_dict_post():
return {
"status_code": 200,
"description": "sync dict post",
"type": "text",
"headers": {"sync": "dict"},
}
return Response(
status_code=200,
description="sync dict post",
headers={"sync": "dict"},
)


@app.post("/async/dict")
async def async_dict_post():
return {
"status_code": 200,
"description": "async dict post",
"type": "text",
"headers": {"async": "dict"},
}
return Response(
status_code=200,
description="async dict post",
headers={"async": "dict"},
)


# Body
Expand Down Expand Up @@ -581,22 +574,20 @@ async def async_json_post(request: Request):

@app.put("/sync/dict")
def sync_dict_put():
return {
"status_code": 200,
"description": "sync dict put",
"type": "text",
"headers": {"sync": "dict"},
}
return Response(
status_code=200,
description="sync dict put",
headers={"sync": "dict"},
)


@app.put("/async/dict")
async def async_dict_put():
return {
"status_code": 200,
"description": "async dict put",
"type": "text",
"headers": {"async": "dict"},
}
return Response(
status_code=200,
description="async dict put",
headers={"async": "dict"},
)


# Body
Expand All @@ -619,22 +610,20 @@ async def async_body_put(request: Request):

@app.delete("/sync/dict")
def sync_dict_delete():
return {
"status_code": 200,
"description": "sync dict delete",
"type": "text",
"headers": {"sync": "dict"},
}
return Response(
status_code=200,
description="sync dict delete",
headers={"sync": "dict"},
)


@app.delete("/async/dict")
async def async_dict_delete():
return {
"status_code": 200,
"description": "async dict delete",
"type": "text",
"headers": {"async": "dict"},
}
return Response(
status_code=200,
description="async dict delete",
headers={"async": "dict"},
)


# Body
Expand All @@ -657,22 +646,21 @@ async def async_body_delete(request: Request):

@app.patch("/sync/dict")
def sync_dict_patch():
return {
"status_code": 200,
"description": "sync dict patch",
"type": "text",
"headers": {"sync": "dict"},
}
return Response(
status_code=200,
description="sync dict patch",
headers={"sync": "dict"},
)


@app.patch("/async/dict")
async def async_dict_patch():
return {
"status_code": 200,
"description": "async dict patch",
"type": "text",
"headers": {"async": "dict"},
}
return Response(
status_code=200,
description="async dict patch",
# need to fix this
headers={"async": "dict"},
)


# Body
Expand All @@ -698,7 +686,7 @@ def get():

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


@app.view("/async/view/decorator")
Expand All @@ -708,15 +696,15 @@ async def get():

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


# ==== Exception Handling ====


@app.exception
def handle_exception(error):
return {"status_code": 500, "description": f"error msg: {error}"}
return Response(status_code=500, description=f"error msg: {error}", headers={})


@app.get("/sync/exception/get")
Expand Down
Loading

0 comments on commit 2180e15

Please sign in to comment.