[Fixed] Related to a Base error #371
-
Before error, can you guys elaborate a bit on difference in Now, about the error, though the site loads the html and stuff with the fastapi , database related stuff give Running micro...
Response:
{
"errorMessage": "'requestContext'",
"errorType": "KeyError",
"requestId": "some_id_hided_cus_idk_what_it_can_do",
"stackTrace": [
" File \"/opt/python/detalib/debugger.py\", line 154, in wrap\n raise e\n",
" File \"/opt/python/detalib/debugger.py\", line 142, in wrap\n result = func(event, context)\n",
" File \"/var/task/_entry.py\", line 14, in handler\n return handle(event, main)\n",
" File \"/opt/python/detalib/handler.py\", line 25, in handle\n return handlers.handle_bare_asgi(event, main, context)\n",
" File \"/opt/python/detalib/handlers.py\", line 21, in handle_bare_asgi\n return Asgi(main.app, enable_lifespan=False)(event, context)\n",
" File \"/opt/python/detalib/adapters/asgi/adapter.py\", line 57, in __call__\n raise exc\n",
" File \"/opt/python/detalib/adapters/asgi/adapter.py\", line 55, in __call__\n response = self.handler(event, context)\n",
" File \"/opt/python/detalib/adapters/asgi/adapter.py\", line 61, in handler\n response = self.handle_http(event, context)\n",
" File \"/opt/python/detalib/adapters/asgi/adapter.py\", line 70, in handle_http\n server, client = get_server_and_client(event)\n",
" File \"/opt/python/detalib/adapters/asgi/adapter.py\", line 17, in get_server_and_client\n client_addr = event[\"requestContext\"].get(\"identity\", {}).get(\"sourceIp\", None)\n"
]
} And this error Traceback (most recent call last):
File "/opt/python/detalib/adapters/asgi/protocols/http.py", line 47, in run
await app(self.scope, self.receive, self.send)
File "/opt/python/fastapi/applications.py", line 269, in __call__
await super().__call__(scope, receive, send)
File "/opt/python/starlette/applications.py", line 124, in __call__
await self.middleware_stack(scope, receive, send)
File "/opt/python/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/opt/python/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/opt/python/starlette/exceptions.py", line 93, in __call__
raise exc
File "/opt/python/starlette/exceptions.py", line 82, in __call__
await self.app(scope, receive, sender)
File "/opt/python/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/opt/python/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/opt/python/starlette/routing.py", line 670, in __call__
await route.handle(scope, receive, send)
File "/opt/python/starlette/routing.py", line 266, in handle
await self.app(scope, receive, send)
File "/opt/python/starlette/routing.py", line 65, in app
response = await func(request)
File "/opt/python/fastapi/routing.py", line 227, in app
raw_response = await run_endpoint_function(
File "/opt/python/fastapi/routing.py", line 160, in run_endpoint_function
return await dependant.call(**values)
File "/var/task/main.py", line 75, in login
stats = udb.get({"username": username})
File "/opt/python/deta/base.py", line 91, in get
key = quote(key, safe="")
File "/var/lang/lib/python3.9/urllib/parse.py", line 871, in quote
return quote_from_bytes(string, safe)
File "/var/lang/lib/python3.9/urllib/parse.py", line 896, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes I will post the related code after this... |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 9 replies
-
My import os
import json
from types import MethodType
from typing import Optional
import uvicorn
import nest_asyncio
import motor.motor_asyncio
import asyncio
from datetime import timedelta
from fastapi import Request, Form, Cookie
from fastapi import Response
from fastapi.responses import RedirectResponse, JSONResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi import FastAPI, HTTPException, Depends
from fastapi.security import OAuth2PasswordRequestForm
from fastapi_another_jwt_auth import AuthJWT
from fastapi_another_jwt_auth.exceptions import AuthJWTException
from pydantic import BaseModel
from src.multiuse.files import templateit
from src.work import work as work_main
from src.admin import admin as admin_main
from src.update import update as update_main
#from src.multiuse.data import get_data
from deta import Deta
deta = Deta()
app = FastAPI(docs_url="/play", redoc_url="/docs")
cur = templateit(app)
udb = deta.Base("users")
class Settings(BaseModel):
authjwt_secret_key: str = "nah"
# Configure application to store and get JWT from cookies
authjwt_token_location: set = {"cookies"}
# Disable CSRF Protection for this example. default is True
access_expires: int = timedelta(days=7)
authjwt_cookie_csrf_protect: bool = False
settings = Settings()
@AuthJWT.load_config
def get_config():
return settings
@app.exception_handler(AuthJWTException)
def authjwt_exception_handler(request: Request, exc: AuthJWTException):
return RedirectResponse(url="/", status_code=303)
@app.post('/auth/login')
async def login(
request : Request,
username: str = Form(),
password: str = Form(),
Authorize: AuthJWT = Depends()
):
stats = udb.get({"username": username})
if not stats:
return JSONResponse(status_code=401, content={"detail": "Invalid username or password"})
if password != stats["password"]:
return JSONResponse(status_code=401, content={"detail": "Invalid username or password"})
access_token = Authorize.create_access_token(subject=username)
response = RedirectResponse(url="/", status_code=303)
Authorize.set_access_cookies(access_token, response=response)
return response
# Endpoint for revoking the current users access token
@app.get('/logout')
async def access_revoke(Authorize: AuthJWT = Depends()):
Authorize.jwt_required()
response = RedirectResponse(url="/", status_code=303)
Authorize.unset_jwt_cookies(response=response)
return response
@app.get("/")
async def index(request: Request, Authorize: AuthJWT = Depends()):
Authorize.jwt_optional()
# If no jwt is sent in the request, get_jwt_subject() will return None
current_user = Authorize.get_jwt_subject() or "anonymous"
if current_user == "anonymous":
return cur.TemplateResponse("index.html", {"request": request, "current_user": current_user}, status_code=200)
#dat = await udb.find_one({"username": current_user})
dat = udb.get({"username": current_user})
utype = dat["type"]
return cur.TemplateResponse(
"index.html",
{
"request": request,
"current_user": current_user,
"utype": utype
}, status_code=200
)
@app.get("/login", response_class=HTMLResponse, include_in_schema=False)
async def home(request : Request):
return cur.TemplateResponse("login.html", {"request": request}, status_code=200)
app.include_router(work_main.router)
#app.include_router(admin_main.router)
app.include_router(update_main.router)
if __name__ == '__main__':
uvicorn.run("main:app",host='127.0.0.1', port=8000, reload=True, debug=True, workers=2) |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
hi, |
Beta Was this translation helpful? Give feedback.
-
@abdelhai Still same issue [2022-08-08T16:00:26Z] Exception in ASGI application
Traceback (most recent call last):
File "/opt/python/detalib/adapters/asgi/protocols/http.py", line 47, in run
await app(self.scope, self.receive, self.send)
File "/opt/python/fastapi/applications.py", line 269, in __call__
await super().__call__(scope, receive, send)
File "/opt/python/starlette/applications.py", line 124, in __call__
await self.middleware_stack(scope, receive, send)
File "/opt/python/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/opt/python/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/opt/python/starlette/exceptions.py", line 93, in __call__
raise exc
File "/opt/python/starlette/exceptions.py", line 82, in __call__
await self.app(scope, receive, sender)
File "/opt/python/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/opt/python/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/opt/python/starlette/routing.py", line 670, in __call__
await route.handle(scope, receive, send)
File "/opt/python/starlette/routing.py", line 266, in handle
await self.app(scope, receive, send)
File "/opt/python/starlette/routing.py", line 65, in app
response = await func(request)
File "/opt/python/fastapi/routing.py", line 227, in app
raw_response = await run_endpoint_function(
File "/opt/python/fastapi/routing.py", line 160, in run_endpoint_function
return await dependant.call(**values)
File "/var/task/main.py", line 74, in login
stats = udb.get({"username": username})
File "/opt/python/deta/base.py", line 91, in get
key = quote(key, safe="")
File "/var/lang/lib/python3.9/urllib/parse.py", line 871, in quote
return quote_from_bytes(string, safe)
File "/var/lang/lib/python3.9/urllib/parse.py", line 896, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes The issue seems to be related to the keys when its retrieving the data from the form, and then searching for that in Base |
Beta Was this translation helpful? Give feedback.
-
@abdelhai Are you planning to add more functionality to Base? Especially in delete ( for many delete or using a dict like fetch instead of str)? |
Beta Was this translation helpful? Give feedback.
-
To run your FastAPI app locally just use |
Beta Was this translation helpful? Give feedback.
@abdelhai Still same issue