sparckles / Robyn

Robyn is a Super Fast Async Python Web Framework with a Rust runtime.
https://robyn.tech/
BSD 2-Clause "Simplified" License
3.89k stars 195 forks source link

Error in Crime Data Endpoints Documentation Code #835

Closed Amaljyothi44 closed 2 weeks ago

Amaljyothi44 commented 4 weeks ago

Bug Description

There is an issue with the get_crimes endpoint in the crime data documentation. The code provided is throwing a TypeError when attempting to retrieve query parameters.

@app.get("/crimes")
async def get_crimes(request):
    with SessionLocal() as db:
        skip = request.query_params.get("skip", 0)
        limit = request.query_params.get("limit", 100)
        crimes = crud.get_crimes(db, skip=skip, limit=limit)
    return crimes

**Error:**
skip = request.query_params.get("skip", 0)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument 'default': 'int' object cannot be converted to 'PyString'

Steps to Reproduce

No response

Your operating system

None

Your Python version (python --version)

Python 3.11.3

Your Robyn version

robyn==0.54.5

Additional Info

Corrected Code:

@app.get("/crimes")
async def get_crimes(request):
    with SessionLocal() as db:  # Use SessionLocal here
        skip = request.query_params.get("skip", "0") 
        limit = request.query_params.get("limit", "100")
        skip = int(skip)
        limit = int(limit)
        crimes = crud.get_crimes(db, skip=skip, limit=limit)
    return crimes

@sansyrox please CMIIW

sansyrox commented 4 weeks ago

Hey @Amaljyothi44 πŸ‘‹

Thanks for raising the issue. Could you confirm your robyn version?

Amaljyothi44 commented 4 weeks ago

@sansyrox I have updated the version of Robyn I am using. The current version is robyn==0.54.5.

sansyrox commented 3 weeks ago

Hey @Amaljyothi44 πŸ‘‹

One suggestion - please format code snippets using backticks(`) . But apart from that yes, this is definitely an issue. We could fix it like your suggestion, but maybe we can add a cast_to in the request.query_params, which takes a python function callable and tries to cast it to data type. ( https://github.com/sparckles/Robyn/issues/836 )

e.g.

@app.get("/crimes")
async def get_crimes(request):
    with SessionLocal() as db:  # Use SessionLocal here
        skip = request.query_params.get("skip",cast_to=int, default=0) 
        limit = request.query_params.get("limit", cast_to=int, default=100)
        skip = int(skip)
        limit = int(limit)
        crimes = crud.get_crimes(db, skip=skip, limit=limit)
    return crimes

What do you think of this approach?

sansyrox commented 3 weeks ago

Hey @Amaljyothi44 ,

The suggestion is to implement it πŸ˜„

Amaljyothi44 commented 3 weeks ago

@sansyrox ohhh, sorry πŸ˜…,

I agree that using cast_to in request.query_params is a more efficient and cleaner approach.

sansyrox commented 3 weeks ago

@Amaljyothi44 , would you like to take that up?

Amaljyothi44 commented 3 weeks ago

@sansyrox Sure, I'm working on it.