From f5237c1273f418b61654bdfe5b162ef20783c66d Mon Sep 17 00:00:00 2001 From: Amirouche Date: Mon, 5 Feb 2024 14:58:32 +0100 Subject: [PATCH] wip --- api/chalicelib/core/projects.py | 11 ++++++----- api/chalicelib/core/signup.py | 5 +++-- api/chalicelib/core/tenants.py | 8 ++++---- api/chalicelib/core/users.py | 10 +++++----- api/chalicelib/utils/pg_client.py | 3 ++- api/routers/subs/v1_api.py | 22 +++++++++++----------- 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/api/chalicelib/core/projects.py b/api/chalicelib/core/projects.py index 79ff185c4..4408e9c38 100644 --- a/api/chalicelib/core/projects.py +++ b/api/chalicelib/core/projects.py @@ -49,8 +49,8 @@ async def __create(tenant_id, data): RETURNING project_id;""", data) await cur.execute(query=query) - project_id = await cur.fetchone()["project_id"] - return get_project(tenant_id=tenant_id, project_id=project_id, include_gdpr=True) + project_id = (await cur.fetchone())["project_id"] + return await get_project(tenant_id=tenant_id, project_id=project_id, include_gdpr=True) async def get_projects(tenant_id: int, gdpr: bool = False, recorded: bool = False): @@ -135,13 +135,13 @@ async def get_project(tenant_id, project_id, include_last_session=False, include async def create(tenant_id, user_id, data: schemas.CreateProjectSchema, skip_authorization=False): - if __exists_by_name(name=data.name, exclude_id=None): + if await __exists_by_name(name=data.name, exclude_id=None): raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"name already exists.") if not skip_authorization: admin = await users.get(user_id=user_id, tenant_id=tenant_id) if not admin["admin"] and not admin["superAdmin"]: return {"errors": ["unauthorized"]} - return {"data": __create(tenant_id=tenant_id, data=data.model_dump())} + return {"data": await __create(tenant_id=tenant_id, data=data.model_dump())} async def edit(tenant_id, user_id, project_id, data: schemas.CreateProjectSchema): @@ -177,7 +177,8 @@ async def get_gdpr(project_id): AND s.deleted_at IS NULL;""", {"project_id": project_id}) await cur.execute(query=query) - row = await cur.fetchone()["gdpr"] + row = await cur.fetchone() + row = row["gdpr"] row["projectId"] = project_id return row diff --git a/api/chalicelib/core/signup.py b/api/chalicelib/core/signup.py index e459ea9d8..5f51d6a75 100644 --- a/api/chalicelib/core/signup.py +++ b/api/chalicelib/core/signup.py @@ -72,9 +72,10 @@ async def create_tenant(data: schemas.UserSignupSchema): VALUES (%(projectName)s, TRUE) RETURNING project_id, (SELECT api_key FROM t) AS api_key;""" - with pg_client.cursor() as cur: + async with pg_client.cursor() as cur: + print('fuuuu') await cur.execute(cur.mogrify(query, params)) - + print('barrr') await telemetry.new_client() r = await users.authenticate(email, password) r["smtp"] = smtp.has_smtp() diff --git a/api/chalicelib/core/tenants.py b/api/chalicelib/core/tenants.py index 0cf39afb8..061013c15 100644 --- a/api/chalicelib/core/tenants.py +++ b/api/chalicelib/core/tenants.py @@ -52,15 +52,15 @@ async def edit_tenant(tenant_id, changes): return helper.dict_to_camel_case(await cur.fetchone()) -async def tenants_exists_sync(use_pool=True): +def tenants_exists_sync(use_pool=True): with pg_client.PostgresClient(use_pool=use_pool) as cur: - await cur.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)") - out = await cur.fetchone()["exists"] + cur.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)") + out = cur.fetchone()["exists"] return out async def tenants_exists(use_pool=True): - async with pg_client.cusor() as cur: + async with pg_client.cursor() as cur: await cur.execute("SELECT EXISTS(SELECT 1 FROM public.tenants)") row = await cur.fetchone() return row["exists"] diff --git a/api/chalicelib/core/users.py b/api/chalicelib/core/users.py index 448aafcd7..fba686b0b 100644 --- a/api/chalicelib/core/users.py +++ b/api/chalicelib/core/users.py @@ -174,7 +174,7 @@ async def create_member(tenant_id, user_id, data: schemas.CreateMemberSchema, ba background_tasks.add_task(email_helper.send_team_invitation, **{ "recipient": data.email, "invitation_link": new_member["invitationLink"], - "client_id": await tenants.get_by_tenant_id(tenant_id)["name"], + "client_id": (await tenants.get_by_tenant_id(tenant_id))["name"], "sender_name": admin["name"] }) return {"data": new_member} @@ -261,7 +261,7 @@ async def edit_account(user_id, tenant_id, changes: schemas.EditAccountSchema): return {"errors": ["unauthorized"]} if changes.name is not None and len(changes.name) > 0: - update(tenant_id=tenant_id, user_id=user_id, changes={"name": changes.name}) + await update(tenant_id=tenant_id, user_id=user_id, changes={"name": changes.name}) _tenant_changes = {} if changes.tenantName is not None and len(changes.tenantName) > 0: @@ -272,7 +272,7 @@ async def edit_account(user_id, tenant_id, changes: schemas.EditAccountSchema): if len(_tenant_changes.keys()) > 0: tenants.edit_tenant(tenant_id=tenant_id, changes=_tenant_changes) - return {"data": __get_account_info(tenant_id=tenant_id, user_id=user_id)} + return {"data": await __get_account_info(tenant_id=tenant_id, user_id=user_id)} async def edit_member(user_id_to_update, tenant_id, changes: schemas.EditMemberSchema, editor_id): @@ -565,7 +565,7 @@ async def change_jwt_iat_jti(user_id): EXTRACT (epoch FROM jwt_refresh_iat)::BIGINT AS jwt_refresh_iat;""", {"user_id": user_id}) await cur.execute(query) - row = cur.fetchone() + row = await cur.fetchone() return row.get("jwt_iat"), row.get("jwt_refresh_jti"), row.get("jwt_refresh_iat") @@ -609,7 +609,7 @@ async def authenticate(email, password, for_change_password=False) -> dict | boo if for_change_password: return True r = helper.dict_to_camel_case(r) - jwt_iat, jwt_r_jti, jwt_r_iat = change_jwt_iat_jti(user_id=r['userId']) + jwt_iat, jwt_r_jti, jwt_r_iat = await change_jwt_iat_jti(user_id=r['userId']) return { "jwt": authorizers.generate_jwt(user_id=r['userId'], tenant_id=r['tenantId'], iat=jwt_iat, aud=f"front:{helper.get_stage_name()}"), diff --git a/api/chalicelib/utils/pg_client.py b/api/chalicelib/utils/pg_client.py index 1c58b28d6..dbb44a8bc 100644 --- a/api/chalicelib/utils/pg_client.py +++ b/api/chalicelib/utils/pg_client.py @@ -186,6 +186,7 @@ def terminate(): @contextlib.asynccontextmanager async def cursor(): from app import app + import psycopg async with app.state.postgresql.connection() as cnx: - with cnx.cursor() as cur: + async with psycopg.AsyncClientCursor(cnx) as cur: yield cur diff --git a/api/routers/subs/v1_api.py b/api/routers/subs/v1_api.py index 5ea729f40..1d08e42ef 100644 --- a/api/routers/subs/v1_api.py +++ b/api/routers/subs/v1_api.py @@ -12,7 +12,7 @@ public_app, app, app_apikey = get_routers() async def get_user_sessions(projectKey: str, userId: str, start_date: int = None, end_date: int = None, context: schemas.CurrentContext = Depends(OR_context)): return { - "data": sessions.get_user_sessions( + "data": await sessions.get_user_sessions( project_id=context.project.project_id, user_id=userId, start_date=start_date, @@ -24,7 +24,7 @@ async def get_user_sessions(projectKey: str, userId: str, start_date: int = None @app_apikey.get('/v1/{projectKey}/sessions/{sessionId}/events', tags=["api"]) async def get_session_events(projectKey: str, sessionId: int, context: schemas.CurrentContext = Depends(OR_context)): return { - "data": events.get_by_session_id( + "data": await events.get_by_session_id( project_id=context.project.project_id, session_id=sessionId ) @@ -34,7 +34,7 @@ async def get_session_events(projectKey: str, sessionId: int, context: schemas.C @app_apikey.get('/v1/{projectKey}/users/{userId}', tags=["api"]) async def get_user_details(projectKey: str, userId: str, context: schemas.CurrentContext = Depends(OR_context)): return { - "data": sessions.get_session_user( + "data": await sessions.get_session_user( project_id=context.project.project_id, user_id=userId ) @@ -44,23 +44,23 @@ async def get_user_details(projectKey: str, userId: str, context: schemas.Curren @app_apikey.delete('/v1/{projectKey}/users/{userId}', tags=["api"]) async def schedule_to_delete_user_data(projectKey: str, userId: str, _=Body(None), context: schemas.CurrentContext = Depends(OR_context)): - record = jobs.create(project_id=context.project.project_id, user_id=userId) + record = await jobs.create(project_id=context.project.project_id, user_id=userId) return {"data": record} @app_apikey.get('/v1/{projectKey}/jobs', tags=["api"]) async def get_jobs(projectKey: str, context: schemas.CurrentContext = Depends(OR_context)): - return {"data": jobs.get_all(project_id=context.project.project_id)} + return {"data": await jobs.get_all(project_id=context.project.project_id)} @app_apikey.get('/v1/{projectKey}/jobs/{jobId}', tags=["api"]) async def get_job(projectKey: str, jobId: int, context: schemas.CurrentContext = Depends(OR_context)): - return {"data": jobs.get(job_id=jobId, project_id=context.project.project_id)} + return {"data": await jobs.get(job_id=jobId, project_id=context.project.project_id)} @app_apikey.delete('/v1/{projectKey}/jobs/{jobId}', tags=["api"]) async def cancel_job(projectKey: str, jobId: int, _=Body(None), context: schemas.CurrentContext = Depends(OR_context)): - job = jobs.get(job_id=jobId, project_id=context.project.project_id) + job = await jobs.get(job_id=jobId, project_id=context.project.project_id) job_not_found = len(job.keys()) == 0 if job_not_found: @@ -69,12 +69,12 @@ async def cancel_job(projectKey: str, jobId: int, _=Body(None), context: schemas return {"errors": ["The request job has already been canceled/completed."]} job["status"] = "cancelled" - return {"data": jobs.update(job_id=jobId, job=job)} + return {"data": await jobs.update(job_id=jobId, job=job)} @app_apikey.get('/v1/projects', tags=["api"]) async def get_projects(context: schemas.CurrentContext = Depends(OR_context)): - records = projects.get_projects(tenant_id=context.tenant_id) + records = await projects.get_projects(tenant_id=context.tenant_id) for record in records: del record['projectId'] @@ -84,14 +84,14 @@ async def get_projects(context: schemas.CurrentContext = Depends(OR_context)): @app_apikey.get('/v1/projects/{projectKey}', tags=["api"]) async def get_project(projectKey: str, context: schemas.CurrentContext = Depends(OR_context)): return { - "data": projects.get_by_project_key(project_key=projectKey) + "data": await projects.get_by_project_key(project_key=projectKey) } @app_apikey.post('/v1/projects', tags=["api"]) async def create_project(data: schemas.CreateProjectSchema = Body(...), context: schemas.CurrentContext = Depends(OR_context)): - record = projects.create( + record = await projects.create( tenant_id=context.tenant_id, user_id=None, data=data,