-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from PromptSail/features/raw-reansaction-and-d…
…etail-separation Old transaction model splitted into details (Transaction) and request/response (RawTransaction)
- Loading branch information
Showing
25 changed files
with
1,503 additions
and
1,494 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from enum import Enum | ||
from typing import Any | ||
|
||
from pydantic import BaseModel, Field | ||
from transactions.models import generate_uuid | ||
|
||
|
||
class TransactionTypeEnum(str, Enum): | ||
request = "request" | ||
response = "response" | ||
|
||
|
||
class RawTransaction(BaseModel): | ||
id: str = Field(default_factory=generate_uuid) | ||
transaction_id: str | ||
type: TransactionTypeEnum | ||
data: dict[str, Any] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from raw_transactions.models import RawTransaction, TransactionTypeEnum | ||
from seedwork.exceptions import NotFoundException | ||
from seedwork.repositories import MongoRepository | ||
|
||
|
||
class RawTransactionNotFoundException(NotFoundException): | ||
""" | ||
Exception raised when a raw transaction is not found. | ||
""" | ||
|
||
... | ||
|
||
|
||
class RawTransactionRepository(MongoRepository): | ||
""" | ||
Repository for managing and accessing raw transaction data. | ||
Inherits from MongoRepository and is specific to the RawTransaction model. | ||
""" | ||
|
||
model_class = RawTransaction | ||
|
||
def add(self, doc): | ||
""" | ||
Add a document to the repository. | ||
:param doc: The document to be added to the repository. | ||
:return: The result of the add operation. | ||
""" | ||
result = super().add(doc) | ||
return result | ||
|
||
def get_for_transaction(self, transaction_id: str) -> list[RawTransaction]: | ||
""" | ||
Retrieve a list of transactions associated with a specific project. | ||
:param transaction_id: The identifier of the transaction for which raw transactions are retrieved. | ||
:return: A list of RawTransaction objects associated with the specified transaction. | ||
""" | ||
return self.find({"transaction_id": transaction_id}) | ||
|
||
def get_request_by_transaction_id(self, transaction_id: str) -> RawTransaction: | ||
""" | ||
Retrieve a specific transaction by its unique identifier. | ||
:param transaction_id: The unique identifier of the transaction to retrieve. | ||
:return: The RawTransaction object (type - request) corresponding to the specified transaction_id. | ||
""" | ||
return self.find_one( | ||
{"transaction_id": transaction_id, "type": TransactionTypeEnum.request} | ||
) | ||
|
||
def get_response_by_transaction_id(self, transaction_id: str) -> RawTransaction: | ||
""" | ||
Retrieve a specific transaction by its unique identifier. | ||
:param transaction_id: The unique identifier of the transaction to retrieve. | ||
:return: The RawTransaction object (type - request) corresponding to the specified transaction_id. | ||
""" | ||
return self.find_one( | ||
{"transaction_id": transaction_id, "type": TransactionTypeEnum.response} | ||
) | ||
|
||
def delete_cascade(self, transaction_id: str): | ||
""" | ||
Delete multiple raw transactions and related data for a specific project using cascading deletion. | ||
:param transaction_id: The Project ID for which transactions and related data will be deleted. | ||
:return: The result of the cascading deletion operation. | ||
""" | ||
return self.delete_many(filter_by={"transaction_id": transaction_id}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import Any | ||
|
||
from pydantic import BaseModel | ||
|
||
from .models import RawTransaction, TransactionTypeEnum | ||
|
||
|
||
class RawTransactionSchema(RawTransaction): | ||
... | ||
|
||
|
||
class CreateRawTransactionSchema(BaseModel): | ||
transaction_id: str | ||
type: TransactionTypeEnum | ||
data: dict[str, Any] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from .models import RawTransaction, TransactionTypeEnum | ||
from .repositories import RawTransactionRepository | ||
from .schemas import CreateRawTransactionSchema | ||
|
||
|
||
def get_raw_transaction_for_transaction( | ||
transaction_id: str, raw_transaction_repository: RawTransactionRepository | ||
) -> list[RawTransaction]: | ||
""" | ||
Retrieve a list of transactions associated with a specific project. | ||
:param transaction_id: The identifier of the transaction for which raw transactions are retrieved. | ||
:param raw_transaction_repository: An instance of RawTransactionRepository used for accessing transaction data. | ||
:return: A list of Transaction objects associated with the specified project. | ||
""" | ||
raw_transactions = raw_transaction_repository.get_for_transaction(transaction_id) | ||
return raw_transactions | ||
|
||
|
||
def get_request_for_transaction( | ||
transaction_id: str, raw_transaction_repository: RawTransactionRepository | ||
) -> RawTransaction: | ||
""" | ||
Retrieve a specific raw transaction (type - request) by transaction unique identifier. | ||
:param transaction_id: The unique identifier of the transaction to retrieve. | ||
:param raw_transaction_repository: An instance of TransactionRepository used for accessing transaction data. | ||
:return: The Transaction object corresponding to the specified transaction_id. | ||
""" | ||
request = raw_transaction_repository.get_request_by_transaction_id(transaction_id) | ||
return request | ||
|
||
|
||
def get_response_for_transaction( | ||
transaction_id: str, raw_transaction_repository: RawTransactionRepository | ||
) -> RawTransaction: | ||
""" | ||
Retrieve a specific raw transaction (type - response) by transaction unique identifier. | ||
:param transaction_id: The unique identifier of the transaction to retrieve. | ||
:param raw_transaction_repository: An instance of TransactionRepository used for accessing transaction data. | ||
:return: The Transaction object corresponding to the specified transaction_id. | ||
""" | ||
response = raw_transaction_repository.get_response_by_transaction_id(transaction_id) | ||
return response | ||
|
||
|
||
def delete_raw_transactions( | ||
transaction_id: str, raw_transaction_repository: RawTransactionRepository | ||
) -> None: | ||
""" | ||
Delete multiple raw transactions (request and response) for a specific transaction. | ||
:param transaction_id: The Transaction ID for which raw transactions will be deleted. | ||
:param raw_transaction_repository: An instance of RawTransactionRepository used for accessing transaction data. | ||
:return: None | ||
""" | ||
raw_transaction_repository.delete_cascade(transaction_id=transaction_id) | ||
|
||
|
||
def add_raw_transaction( | ||
data: CreateRawTransactionSchema, | ||
raw_transaction_repository: RawTransactionRepository, | ||
) -> RawTransaction: | ||
raw_transaction = RawTransaction(**data.model_dump()) | ||
raw_transaction_repository.add(raw_transaction) | ||
return raw_transaction | ||
|
||
|
||
def store_raw_transactions( | ||
request, | ||
request_content, | ||
response, | ||
response_content, | ||
transaction_id: str, | ||
raw_transaction_repository: RawTransactionRepository, | ||
): | ||
request_data = RawTransaction( | ||
transaction_id=transaction_id, | ||
type=TransactionTypeEnum.request, | ||
data=dict( | ||
method=request.method, | ||
url=str(request.url), | ||
host=request.headers.get("host", ""), | ||
headers=dict(request.headers), | ||
extensions=dict(request.extensions), | ||
content=request_content, | ||
), | ||
) | ||
response_data = RawTransaction( | ||
transaction_id=transaction_id, | ||
type=TransactionTypeEnum.response, | ||
data=dict( | ||
status_code=response.status_code, | ||
headers=dict(response.headers), | ||
next_requset=response.next_request, | ||
is_error=response.is_error, | ||
is_success=response.is_success, | ||
content=response_content, | ||
elapsed=response.elapsed.total_seconds(), | ||
encoding=response.encoding, | ||
), | ||
) | ||
raw_transaction_repository.add(request_data) | ||
raw_transaction_repository.add(response_data) |
Oops, something went wrong.