Skip to content

Commit

Permalink
Add get_user_info to user service
Browse files Browse the repository at this point in the history
  • Loading branch information
Only-bottle committed Dec 6, 2024
1 parent aaad0c8 commit 2936092
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
19 changes: 4 additions & 15 deletions app/api/v1/endpoints/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from app.api.v1.schemas.user import ApiKeyCreate, ApiKeyResponse, CreditInfo, DetailData, UserPayload, UserResponse
from app.api.deps import api_key_header
from app.api.v1.schemas.user import ApiKeyCreate, ApiKeyResponse, UserResponse
from app.services.user import user_service
from netspresso.utils.db.session import get_db

Expand All @@ -16,19 +17,7 @@ def generate_api_key(*, db: Session = Depends(get_db), request_body: ApiKeyCreat


@router.get("/me", response_model=UserResponse)
def get_user() -> UserResponse:
user = UserPayload(
user_id="e8e8df79-2a62-4562-8e4d-06f51dd795b2",
email="[email protected]",
detail_data=DetailData(
first_name="Byeongman",
last_name="Lee",
company="Nota AI",
),
credit_info=CreditInfo(
free=1000,
total=1000,
),
)
def get_user(*, db: Session = Depends(get_db), api_key: str = Depends(api_key_header)) -> UserResponse:
user = user_service.get_user_info(db=db, api_key=api_key)

return UserResponse(data=user)
27 changes: 26 additions & 1 deletion app/services/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from sqlalchemy.orm import Session

from app.api.v1.schemas.user import ApiKeyPayload
from app.api.v1.schemas.user import ApiKeyPayload, CreditInfo, DetailData, UserPayload
from app.utils import generate_id
from netspresso.netspresso import NetsPresso
from netspresso.utils.db.models.user import User
from netspresso.utils.db.repositories.user import user_repository

Expand Down Expand Up @@ -41,5 +42,29 @@ def generate_api_key(self, db: Session, email: str, password: str) -> ApiKeyPayl

return api_key

def get_user_info(self, db: Session, api_key: str) -> UserPayload:
user = user_repository.get_by_api_key(db=db, api_key=api_key)

netspresso = NetsPresso(email=user.email, password=user.password)

user = UserPayload(
user_id=netspresso.user_info.user_id,
email=netspresso.user_info.email,
detail_data=DetailData(
first_name=netspresso.user_info.detail_data.first_name,
last_name=netspresso.user_info.detail_data.last_name,
company=netspresso.user_info.detail_data.company,
),
credit_info=CreditInfo(
free=netspresso.user_info.credit_info.free,
reward=netspresso.user_info.credit_info.reward,
contract=netspresso.user_info.credit_info.contract,
paid=netspresso.user_info.credit_info.paid,
total=netspresso.user_info.credit_info.total,
),
)

return user


user_service = UserService()

0 comments on commit 2936092

Please sign in to comment.