Skip to content

Commit

Permalink
feat: split query & mutation for correct graphql schema gen and add l…
Browse files Browse the repository at this point in the history
…ogging
  • Loading branch information
dartt0n committed Jul 20, 2024
1 parent 7b1ad4a commit e9a4470
Show file tree
Hide file tree
Showing 17 changed files with 896 additions and 581 deletions.
1 change: 1 addition & 0 deletions dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ WORKDIR /app
RUN --mount=type=cache,target=/root/.cache \
poetry install --without=dev

ENV STRAWBERRY_DISABLE_RICH_ERRORS=1
CMD ["poetry", "run", "gunicorn", "main:app", "--bind", "0.0.0.0:8080", "--worker-class", "aiohttp.GunicornWebWorker"]
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test path="./src/tests" n_workers="8":
docker stop randorm-api-tests-mongo

run-server n_workers="1":
poetry run gunicorn main:app --bind 0.0.0.0:8080 --workers {{ n_workers }} --worker-class aiohttp.GunicornWebWorker
poetry run gunicorn main:app --bind localhost:8080 --workers {{ n_workers }} --worker-class aiohttp.GunicornWebWorker

docker-build:
docker buildx build . -t randorm-api:latest
Expand Down
478 changes: 275 additions & 203 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ loguru = "^0.7.2"
redis = { extras = ["hiredis"], version = "^5.0.7" }
strawberry-graphql = { extras = ["debug-server"], version = "^0.236.0" }
gunicorn = "^22.0.0"
ujson = "^5.10.0"

[tool.poetry.group.dev.dependencies]
black = "^24.4.2" # formatter
Expand Down
37 changes: 37 additions & 0 deletions src/adapter/external/auth/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from src.protocol.external.auth.oauth import OAuthContainer, OAuthDTO, OauthProtocol
from src.protocol.internal.database.user import CreateUser, ReadUser
from src.service.user import UserService
from src.utils.logger.logger import Logger

log = Logger("telegram-auth-adapter")


class TgUserProfileMixin(Profile): ...
Expand Down Expand Up @@ -94,30 +97,43 @@ def __check_hash(
data.to_data_string().encode(),
hashlib.sha256,
)

if signing.hexdigest() != data.hash:
log.error("callback data validation faiked: hash mismatch")
raise auth_exception.InvalidCredentialsException(
"data is malformed since the hash does not match"
)

log.debug("callback data validation passed")

async def register(self, data: Any) -> TgOauthContainer:
try:
log.debug("building callback data from custom data")
request: TgOauthRegisterCallback = TgOauthRegisterCallback.model_validate(
data, from_attributes=True
)

log.debug("checking callback data hash")
self.__check_hash(request)

log.debug(f"creating new user with telegram_id={request.id}")
user = await self.__service.create(
CreateUser(telegram_id=request.id, profile=request.profile)
)
except (ValidationError, database_exception.ReflectUserException) as e:
log.error(
f"failed to build callback data or reflect user data to create user with exception: {e}"
)
raise auth_exception.InvalidCredentialsException(
"failed to reflect user data to create user"
) from e
except service_exception.CreateUserException as e:
log.error(f"creating new user failed with service exception: {e}")
raise auth_exception.UserAlreadyExistsException(
"creating new user failed"
) from e
except auth_exception.AuthException as e:
log.error(f"authentiocation failed with auth exception: {e}")
raise e

return TgOauthContainer.construct(
Expand All @@ -126,17 +142,25 @@ async def register(self, data: Any) -> TgOauthContainer:

async def login(self, data: Any) -> TgOauthContainer:
try:
log.debug("building callback data from custom data")
request = TgOauthLoginCallback.model_validate(data, from_attributes=True)

log.debug("checking callback data hash")
self.__check_hash(request)

user = await self.__service.find_by_telegram_id(request.id)
except (ValidationError, database_exception.ReflectUserException) as e:
log.error(
"failed to build callback data or reflect user data to read user with exception: {e}"
)
raise auth_exception.InvalidCredentialsException(
"failed to reflect user data to read user"
) from e
except service_exception.ReadUserException as e:
log.error(f"reading user failed with service exception: {e}")
raise auth_exception.UserNotFoundException("reading user failed") from e
except auth_exception.AuthException as e:
log.error(f"authentiocation failed with auth exception: {e}")
raise e

return TgOauthContainer.construct(
Expand All @@ -146,19 +170,32 @@ async def login(self, data: Any) -> TgOauthContainer:
async def retrieve_user(self, data: TgOauthContainer) -> User:
try:
if not isinstance(data, TgOauthContainer):
log.error(
"invalid data type was passed to retrieve user function. "
f"expected TgOauthContainer, got {type(data)}"
)
raise auth_exception.InvalidCredentialsException("invalid data type")

try:
log.debug("building dto from container")
dto = data.to_dto(self.__jwt_secret)
except Exception as e:
log.error(f"failed to build dto from container with exception: {e}")
raise auth_exception.InvalidCredentialsException(
"invalid data type"
) from e

user = await self.__service.read(ReadUser(_id=dto.id))
except (ValidationError, AttributeError) as e:
log.error(
f"failed to construct dto or reflect to read user with exception: {e}"
)
raise auth_exception.InvalidCredentialsException("invalid data type") from e
except service_exception.ReadUserException as e:
log.error(f"reading user failed with service exception: {e}")
raise auth_exception.UserNotFoundException("user not found") from e
else:
log.debug(
f"user id={user.id}, telegram_id={user.telegram_id} was retrieved"
)
return user
2 changes: 1 addition & 1 deletion src/adapter/external/graphql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from src.adapter.external.graphql import (
operation,
query,
scalar,
schema,
tool,
type,
update,
Expand Down
Loading

0 comments on commit e9a4470

Please sign in to comment.