Skip to content

Commit

Permalink
fix: add ignore_duplicated parameter to insert_session_item
Browse files Browse the repository at this point in the history
  • Loading branch information
MissterHao committed Feb 3, 2023
1 parent f9b5fb0 commit 2b0e705
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions dysession/aws/dynamodb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
import logging
from datetime import datetime
from typing import Any, Callable, Dict, Literal, Optional, Union

import boto3
Expand Down Expand Up @@ -118,6 +118,7 @@ def insert_session_item(
data: SessionDataModel,
table_name: Optional[str] = None,
return_consumed_capacity: Literal["INDEXES", "TOTAL", "NONE"] = "TOTAL",
ignore_duplicated: bool = True,
) -> bool:
"""Insert a session key"""

Expand All @@ -126,7 +127,7 @@ def insert_session_item(
if table_name is None:
table_name = get_config()["DYNAMODB_TABLENAME"]

if key_exists(data.session_key):
if not ignore_duplicated and key_exists(data.session_key):
raise SessionKeyDuplicated

resource = boto3.resource("dynamodb", region_name=get_config()["DYNAMODB_REGION"])
Expand Down Expand Up @@ -188,13 +189,20 @@ def set(
ignore_duplicated: bool = True,
) -> None:
try:
insert_session_item(data, table_name, return_consumed_capacity)
insert_session_item(
data,
table_name,
return_consumed_capacity,
ignore_duplicated=ignore_duplicated,
)
except SessionKeyDuplicated:
if not ignore_duplicated:
raise SessionKeyDuplicated

def exists(self, session_key: str) -> bool:
if type(session_key) is not str:
raise TypeError(f"session_key should be type of str instead of {type(session_key)}.")
raise TypeError(
f"session_key should be type of str instead of {type(session_key)}."
)

return key_exists(session_key=session_key)

0 comments on commit 2b0e705

Please sign in to comment.