2021-03-20 20:16:51 +03:00
|
|
|
"""Services module."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import sqlite3
|
|
|
|
from typing import Dict
|
|
|
|
|
|
|
|
from mypy_boto3_s3 import S3Client
|
|
|
|
|
|
|
|
|
|
|
|
class BaseService:
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.logger = logging.getLogger(
|
2021-10-01 03:09:42 +03:00
|
|
|
f"{__name__}.{self.__class__.__name__}",
|
2021-03-20 20:16:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class UserService(BaseService):
|
|
|
|
|
|
|
|
def __init__(self, db: sqlite3.Connection) -> None:
|
|
|
|
self.db = db
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def get_user(self, email: str) -> Dict[str, str]:
|
2021-10-01 03:09:42 +03:00
|
|
|
self.logger.debug("User %s has been found in database", email)
|
|
|
|
return {"email": email, "password_hash": "..."}
|
2021-03-20 20:16:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
class AuthService(BaseService):
|
|
|
|
|
|
|
|
def __init__(self, db: sqlite3.Connection, token_ttl: int) -> None:
|
|
|
|
self.db = db
|
|
|
|
self.token_ttl = token_ttl
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def authenticate(self, user: Dict[str, str], password: str) -> None:
|
|
|
|
assert password is not None
|
|
|
|
self.logger.debug(
|
2021-10-01 03:09:42 +03:00
|
|
|
"User %s has been successfully authenticated",
|
|
|
|
user["email"],
|
2021-03-20 20:16:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class PhotoService(BaseService):
|
|
|
|
|
|
|
|
def __init__(self, db: sqlite3.Connection, s3: S3Client) -> None:
|
|
|
|
self.db = db
|
|
|
|
self.s3 = s3
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def upload_photo(self, user: Dict[str, str], photo_path: str) -> None:
|
|
|
|
self.logger.debug(
|
2021-10-01 03:09:42 +03:00
|
|
|
"Photo %s has been successfully uploaded by user %s",
|
2021-03-20 20:16:51 +03:00
|
|
|
photo_path,
|
2021-10-01 03:09:42 +03:00
|
|
|
user["email"],
|
2021-03-20 20:16:51 +03:00
|
|
|
)
|