Ved Gupta commited on
Commit
f1cd69e
·
1 Parent(s): 9fd370e

getToken added

Browse files
app/api/endpoints/transcribe.py CHANGED
@@ -2,7 +2,6 @@ from typing import Annotated, List, Union
2
 
3
  from fastapi import APIRouter, File, UploadFile, Request, Header, HTTPException
4
  from fastapi.background import BackgroundTasks
5
- from pydantic import BaseModel
6
 
7
  from app.core.database import SessionLocal
8
 
@@ -13,16 +12,12 @@ from app.utils.utils import (
13
  get_model_name,
14
  )
15
  from app.core.models import AuthTokenController, TranscribeController
 
16
 
17
  router = APIRouter()
18
  database = SessionLocal()
19
 
20
 
21
- class Transcription(BaseModel):
22
- text: str
23
- filename: str
24
-
25
-
26
  @router.post("/", response_model=Transcription)
27
  async def post_audio(
28
  background_tasks: BackgroundTasks,
 
2
 
3
  from fastapi import APIRouter, File, UploadFile, Request, Header, HTTPException
4
  from fastapi.background import BackgroundTasks
 
5
 
6
  from app.core.database import SessionLocal
7
 
 
12
  get_model_name,
13
  )
14
  from app.core.models import AuthTokenController, TranscribeController
15
+ from app.api.models import Transcription
16
 
17
  router = APIRouter()
18
  database = SessionLocal()
19
 
20
 
 
 
 
 
 
21
  @router.post("/", response_model=Transcription)
22
  async def post_audio(
23
  background_tasks: BackgroundTasks,
app/api/endpoints/users.py CHANGED
@@ -3,7 +3,14 @@ from uuid import UUID
3
  from fastapi import APIRouter, status, Depends, HTTPException
4
  from sqlalchemy.orm import Session
5
 
6
- from app.api.models.user import User, UserResponse, PasswordUpdate, UserDeletedResponse
 
 
 
 
 
 
 
7
  from app.core.database import get_db
8
  from app.core.models.User import UserController
9
 
@@ -33,6 +40,24 @@ async def create_user(user: User, db: Session = Depends(get_db)):
33
  ) from exc
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  @router.get("/{user_id}/", status_code=status.HTTP_200_OK, response_model=UserResponse)
37
  async def read_user(user_id: UUID, db: Session = Depends(get_db)):
38
  try:
 
3
  from fastapi import APIRouter, status, Depends, HTTPException
4
  from sqlalchemy.orm import Session
5
 
6
+ from app.api.models.user import (
7
+ User,
8
+ UserResponse,
9
+ PasswordUpdate,
10
+ UserDeletedResponse,
11
+ User_GET_TOKEN,
12
+ Response_Token,
13
+ )
14
  from app.core.database import get_db
15
  from app.core.models.User import UserController
16
 
 
40
  ) from exc
41
 
42
 
43
+ @router.post(
44
+ "/get_token", status_code=status.HTTP_200_OK, response_model=Response_Token
45
+ )
46
+ async def get_user_token(user: User_GET_TOKEN, db: Session = Depends(get_db)):
47
+ try:
48
+ USER = UserController(db)
49
+ token = USER.read_token(user.email, user.password)
50
+ return {"token": token}
51
+ except HTTPException as exc:
52
+ logger.error(exc)
53
+ raise exc
54
+ except Exception as exc:
55
+ logger.error(exc)
56
+ raise HTTPException(
57
+ status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)
58
+ ) from exc
59
+
60
+
61
  @router.get("/{user_id}/", status_code=status.HTTP_200_OK, response_model=UserResponse)
62
  async def read_user(user_id: UUID, db: Session = Depends(get_db)):
63
  try:
app/api/models/__init__.py CHANGED
@@ -1,2 +1,2 @@
1
  from .user import User
2
- from .transcribe import Transcribe
 
1
  from .user import User
2
+ from .transcribe import Transcription
app/api/models/ping.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class PingResponse(BaseModel):
5
+ ping: str
app/api/models/transcribe.py CHANGED
@@ -11,5 +11,6 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
11
  from app.core.database import Base
12
 
13
 
14
- class Transcribe(BaseModel):
15
- ...
 
 
11
  from app.core.database import Base
12
 
13
 
14
+ class Transcription(BaseModel):
15
+ text: str
16
+ filename: str
app/api/models/user.py CHANGED
@@ -31,6 +31,15 @@ class PasswordUpdate(BaseModel):
31
  new_password: str
32
 
33
 
 
 
 
 
 
 
 
 
 
34
  class User(UserBase):
35
  password: str
36
 
 
31
  new_password: str
32
 
33
 
34
+ class User_GET_TOKEN(BaseModel):
35
+ email: str
36
+ password: str
37
+
38
+
39
+ class Response_Token(BaseModel):
40
+ token: str
41
+
42
+
43
  class User(UserBase):
44
  password: str
45
 
app/core/models/AuthToken.py CHANGED
@@ -39,6 +39,12 @@ class AuthTokenController:
39
  raise Exception("Invalid Token!")
40
  return user.user_id
41
 
 
 
 
 
 
 
42
  def create(self, user_id: UUID):
43
  self.user_id = user_id
44
  self.token = self.create_token()
 
39
  raise Exception("Invalid Token!")
40
  return user.user_id
41
 
42
+ def get_token_from_user_id(self, user_id: UUID) -> str:
43
+ token = self.db.query(AuthToken).filter(AuthToken.user_id == user_id).first()
44
+ if not token:
45
+ raise Exception("Invalid Token!")
46
+ self.auth_token = token
47
+
48
  def create(self, user_id: UUID):
49
  self.user_id = user_id
50
  self.token = self.create_token()
app/core/models/User.py CHANGED
@@ -86,6 +86,14 @@ class UserController:
86
  return
87
  AuthTokenController(self.db).create(self.db_user.id)
88
 
 
 
 
 
 
 
 
 
89
  def CheckUserIsExistsByEmailAndUsername(self, email: str, username: str):
90
  db_user = (
91
  self.db.query(UserInDB)
@@ -96,6 +104,14 @@ class UserController:
96
  return True
97
  return False
98
 
 
 
 
 
 
 
 
 
99
  def read(self, user_id: uuid.UUID):
100
  self.db_user = self.db.query(UserInDB).filter(UserInDB.id == user_id).first()
101
  if not self.db_user:
 
86
  return
87
  AuthTokenController(self.db).create(self.db_user.id)
88
 
89
+ def read_token(self, email: str, password: str):
90
+ self.read_by_email(email)
91
+ if not verify_password(password, self.db_user.hashed_password):
92
+ raise HTTPException(status_code=400, detail="Incorrect password")
93
+ TOKEN = AuthTokenController(self.db)
94
+ TOKEN.get_token_from_user_id(self.db_user.id)
95
+ return TOKEN.get_token()
96
+
97
  def CheckUserIsExistsByEmailAndUsername(self, email: str, username: str):
98
  db_user = (
99
  self.db.query(UserInDB)
 
104
  return True
105
  return False
106
 
107
+ def read_by_email(self, email: str):
108
+ self.db_user = self.db.query(UserInDB).filter(UserInDB.email == email).first()
109
+ if not self.db_user:
110
+ raise HTTPException(
111
+ status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
112
+ )
113
+ self.user = self.db_user.data()
114
+
115
  def read(self, user_id: uuid.UUID):
116
  self.db_user = self.db.query(UserInDB).filter(UserInDB.id == user_id).first()
117
  if not self.db_user:
app/main.py CHANGED
@@ -4,6 +4,7 @@ from fastapi.middleware.cors import CORSMiddleware
4
  from app.api import api_router
5
  from app.core.config import settings
6
  from app.core.errors import error_handler
 
7
 
8
  from app.utils import print_routes
9
  from app.utils.checks import run_checks
@@ -27,7 +28,7 @@ if settings.BACKEND_CORS_ORIGINS:
27
  )
28
 
29
 
30
- @app.get("/ping")
31
  async def ping():
32
  return {"ping": "pong"}
33
 
@@ -36,7 +37,6 @@ async def ping():
36
  app.include_router(api_router, prefix=settings.API_V1_STR)
37
 
38
  # # Error handlers
39
- # app.add_exception_handler(422, error_handler)
40
  app.add_exception_handler(500, error_handler)
41
 
42
  # Print all routes
 
4
  from app.api import api_router
5
  from app.core.config import settings
6
  from app.core.errors import error_handler
7
+ from app.api.models.ping import PingResponse
8
 
9
  from app.utils import print_routes
10
  from app.utils.checks import run_checks
 
28
  )
29
 
30
 
31
+ @app.get("/ping", tags=["ping"], response_model=PingResponse)
32
  async def ping():
33
  return {"ping": "pong"}
34
 
 
37
  app.include_router(api_router, prefix=settings.API_V1_STR)
38
 
39
  # # Error handlers
 
40
  app.add_exception_handler(500, error_handler)
41
 
42
  # Print all routes