App¶
TaipanStack's Application entry point.
App¶
Secure System¶
Secure System Module.
This module demonstrates a secure implementation of a user management service following strict typing and security guidelines.
UserNotFoundError
¶
UserNotFoundError(user_id: UUID)
Bases: Exception
Exception raised when a user is not found.
Initialize the exception with the user ID.
UserAlreadyExistsError
¶
UserAlreadyExistsError(message: str)
Bases: Exception
Exception raised when a user already exists.
Initialize the exception with a message.
UserCreationError
¶
UserCreationError(message: str = 'Failed to create user')
Bases: Exception
Exception class for user creation errors.
Initialize the exception with a message.
UserCreate
¶
Bases: SecureBaseModel
Model for creating a new user.
| ATTRIBUTE | DESCRIPTION |
|---|---|
username |
The username of the user.
TYPE:
|
email |
The email address of the user.
TYPE:
|
password |
The password of the user (will be treated as a secret).
TYPE:
|
ip_address |
The IP address from which the user is registering.
TYPE:
|
User
¶
Bases: SecureBaseModel
Model representing a registered user.
| ATTRIBUTE | DESCRIPTION |
|---|---|
id |
Unique identifier for the user.
TYPE:
|
username |
The username of the user.
TYPE:
|
email |
The email address of the user.
TYPE:
|
is_active |
Whether the user account is active.
TYPE:
|
UserInDB
¶
Bases: User
Model representing a registered user in the database.
| ATTRIBUTE | DESCRIPTION |
|---|---|
password_hash |
The hashed password of the user.
TYPE:
|
UserRepository
¶
Bases: ABC
Abstract base class for user data access.
save
abstractmethod
¶
save(
user: UserInDB,
) -> Result[None, UserAlreadyExistsError]
Save a user to the repository.
| PARAMETER | DESCRIPTION |
|---|---|
user
|
The user to save.
TYPE:
|
InMemoryUserRepository
¶
InMemoryUserRepository()
Bases: UserRepository
In-memory implementation of UserRepository.
Initialize the in-memory repository.
save
¶
save(
user: UserInDB,
) -> Result[None, UserAlreadyExistsError]
Save a user to the in-memory storage.
| PARAMETER | DESCRIPTION |
|---|---|
user
|
The user to save.
TYPE:
|
UserService
¶
UserService(user_repository: UserRepository)
Service for managing users securely.
Initialize the UserService with a repository.
| PARAMETER | DESCRIPTION |
|---|---|
user_repository
|
The repository to use for data access.
TYPE:
|
create_user
¶
create_user(
user_create: UserCreate,
) -> Result[User, UserCreationError]
Create a new user.
| PARAMETER | DESCRIPTION |
|---|---|
user_create
|
The user creation data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[User, UserCreationError]
|
Ok(User) on success, Err(UserCreationError) on failure. |
get_user
¶
get_user(user_id: UUID) -> Result[User, UserNotFoundError]
Retrieve a user by ID using Result pattern.
| PARAMETER | DESCRIPTION |
|---|---|
user_id
|
The UUID of the user.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[User, UserNotFoundError]
|
Ok(User) if found, Err(UserNotFoundError) if not found. |
Example
result = service.get_user(some_id) if isinstance(result, Ok): ... print(f"Found: {result.unwrap().username}") ... else: ... print(f"Not found: {result.unwrap_err().user_id}")