forked from noverde/faster-sam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add sqs event builder * refactor: update sqs class to use a generic event * refactor: update code
- Loading branch information
1 parent
6056d36
commit ce98acc
Showing
4 changed files
with
137 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from typing import Protocol, runtime_checkable | ||
|
||
from faster_sam.schemas import SQSInfo | ||
|
||
|
||
@runtime_checkable | ||
class IntoSQSInfo(Protocol): | ||
def into(self) -> SQSInfo: ... |
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,41 @@ | ||
from datetime import datetime | ||
from typing import Dict, Optional | ||
from pydantic import BaseModel, Base64UrlStr, Field | ||
|
||
|
||
class SQSInfo(BaseModel): | ||
id: str | ||
body: str | ||
receive_count: int | ||
sent_timestamp: int | ||
source_arn: str | ||
message_attributes: Optional[Dict[str, str]] = Field(default=None) | ||
|
||
|
||
class PubSubMessage(BaseModel): | ||
data: Base64UrlStr | ||
messageId: str | ||
publishTime: datetime | ||
attributes: Optional[Dict[str, str]] = Field(default=None) | ||
|
||
|
||
class PubSubEnvelope(BaseModel): | ||
message: PubSubMessage | ||
subscription: str | ||
deliveryAttempt: int | ||
|
||
def into(self) -> SQSInfo: | ||
milliseconds = 1000 | ||
|
||
publish_time = int(self.message.publishTime.timestamp() * milliseconds) | ||
|
||
topic_name = self.subscription.rsplit("/", maxsplit=1)[-1] | ||
source_arn = f"arn:aws:sqs:::{topic_name}" | ||
return SQSInfo( | ||
id=self.message.messageId, | ||
body=self.message.data, | ||
receive_count=self.deliveryAttempt, | ||
sent_timestamp=publish_time, | ||
message_attributes=self.message.attributes, | ||
source_arn=source_arn, | ||
) |
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