This commit is contained in:
parent
1ea9332a98
commit
f061d8a523
|
@ -8,9 +8,13 @@ from services.db import local_session
|
|||
from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper
|
||||
import strawberry
|
||||
from strawberry.schema.config import StrawberryConfig
|
||||
import logging
|
||||
|
||||
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
|
||||
|
||||
# Инициализация логгера
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@strawberry_sqlalchemy_mapper.type(NotificationMessage)
|
||||
class Notification:
|
||||
|
@ -81,8 +85,11 @@ class Query:
|
|||
total=session.query(NotificationMessage).count(),
|
||||
)
|
||||
return nr
|
||||
except SQLAlchemyError as ex:
|
||||
print(f"[resolvers.schema] {ex}")
|
||||
except Exception as ex:
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
logger.error(f"[load_notifications] Ошибка при выполнении запроса к базе данных: {ex}")
|
||||
return NotificationsResult(notifications=[], total=0, unread=0)
|
||||
|
||||
|
||||
|
@ -100,7 +107,9 @@ class Mutation:
|
|||
session.commit()
|
||||
except SQLAlchemyError as e:
|
||||
session.rollback()
|
||||
print(f"[mark_notification_as_read] error: {str(e)}")
|
||||
logger.error(
|
||||
f"[mark_notification_as_read] Ошибка при обновлении статуса прочтения уведомления: {str(e)}"
|
||||
)
|
||||
return NotificationSeenResult(error="cant mark as read")
|
||||
return NotificationSeenResult()
|
||||
|
||||
|
@ -119,7 +128,9 @@ class Mutation:
|
|||
session.commit()
|
||||
except SQLAlchemyError as e:
|
||||
session.rollback()
|
||||
print(f"[mark_all_notifications_as_read] error: {str(e)}")
|
||||
logger.error(
|
||||
f"[mark_all_notifications_as_read] Ошибка при обновлении статуса прочтения всех уведомлений: {str(e)}"
|
||||
)
|
||||
return NotificationSeenResult(error="cant mark as read")
|
||||
return NotificationSeenResult()
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from functools import wraps
|
||||
import aiohttp
|
||||
from aiohttp.web import HTTPUnauthorized
|
||||
|
||||
from orm.author import Author
|
||||
from services.db import local_session
|
||||
from settings import AUTH_URL
|
||||
|
@ -9,9 +10,10 @@ from settings import AUTH_URL
|
|||
async def check_auth(req) -> (bool, int | None):
|
||||
token = req.headers.get("Authorization")
|
||||
if token:
|
||||
# Logging the authentication token
|
||||
print(f"[services.auth] checking auth token: {token}")
|
||||
query_name = "validate_jwt_token"
|
||||
operation = "ValidateToken"
|
||||
opeation = "ValidateToken"
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
@ -24,18 +26,21 @@ async def check_auth(req) -> (bool, int | None):
|
|||
}
|
||||
|
||||
gql = {
|
||||
"query": f"query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
||||
"query": f"query {opeation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
||||
"variables": variables,
|
||||
"operationName": operation,
|
||||
"operationName": opeation,
|
||||
}
|
||||
print(f"[services.auth] Graphql: {gql}")
|
||||
# print(f"[services.auth] Graphql: {gql}")
|
||||
try:
|
||||
# Asynchronous HTTP request to the authentication server
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
|
||||
# Logging the GraphQL response
|
||||
response_text = await response.text()
|
||||
print(f"[services.auth] GraphQL Response: {response_text}")
|
||||
|
||||
if response.status == 200:
|
||||
# Parsing JSON response
|
||||
data = await response.json()
|
||||
errors = data.get("errors")
|
||||
if errors:
|
||||
|
@ -44,14 +49,18 @@ async def check_auth(req) -> (bool, int | None):
|
|||
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
|
||||
|
||||
if user_id:
|
||||
# Logging the retrieved user ID
|
||||
print(f"[services.auth] User ID retrieved: {user_id}")
|
||||
return True, user_id
|
||||
else:
|
||||
# Logging when no user ID is found in the response
|
||||
print("[services.auth] No user ID found in the response")
|
||||
else:
|
||||
# Logging when the request to the authentication server fails
|
||||
print(f"[services.auth] Request failed with status: {response.status}")
|
||||
|
||||
except Exception as e:
|
||||
# Handling and logging exceptions during authentication check
|
||||
print(f"[services.auth] {e}")
|
||||
|
||||
return False, None
|
||||
|
|
Loading…
Reference in New Issue
Block a user