gcp-pubsub-dao 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gcp_pubsub_dao-0.1.0/PKG-INFO +46 -0
- gcp_pubsub_dao-0.1.0/README.md +34 -0
- gcp_pubsub_dao-0.1.0/gcp_pubsub_dao/__init__.py +6 -0
- gcp_pubsub_dao-0.1.0/gcp_pubsub_dao/pubsub_publisher_dao.py +14 -0
- gcp_pubsub_dao-0.1.0/gcp_pubsub_dao/pubsub_subscriber_dao.py +70 -0
- gcp_pubsub_dao-0.1.0/pyproject.toml +42 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: gcp-pubsub-dao
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: DAO for GCP PubSub service.
|
|
5
|
+
Author: Raman Shakun
|
|
6
|
+
Author-email: shakunroman@gmail.com
|
|
7
|
+
Requires-Python: >=3.11,<4.0
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Requires-Dist: google-cloud-pubsub (>=2.18.2,<3.0.0)
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# Ash DAL
|
|
14
|
+
The library provides DAO classes for GCP pubsub publisher/subscriber.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
### PyPi
|
|
19
|
+
|
|
20
|
+
*To be added*
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
```python
|
|
24
|
+
from gcp_pubsub_dao import PubSubSubscriberDAO, Message
|
|
25
|
+
|
|
26
|
+
dao = PubSubSubscriberDAO(project_id="prodect-dev", subscription_id="subscription")
|
|
27
|
+
messages: Message = dao.get_messages(messages_count=2)
|
|
28
|
+
|
|
29
|
+
for message in messages:
|
|
30
|
+
print(message.data)
|
|
31
|
+
|
|
32
|
+
dao.ack_messages(ack_ids=[message[0].ack_id])
|
|
33
|
+
dao.nack_messages(ack_ids=[message[1].ack_id])
|
|
34
|
+
|
|
35
|
+
dao.close() # to clean up connections
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from gcp_pubsub_dao import PubSubPublisherDAO
|
|
40
|
+
|
|
41
|
+
dao = PubSubPublisherDAO(project_id="prodect-dev")
|
|
42
|
+
try:
|
|
43
|
+
dao.publish_message(topic_name="topic", payload=b"asdfsdf", attributes={"kitId": "AW12345678"})
|
|
44
|
+
except Exception as ex:
|
|
45
|
+
print(ex)
|
|
46
|
+
```
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Ash DAL
|
|
2
|
+
The library provides DAO classes for GCP pubsub publisher/subscriber.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
|
|
6
|
+
### PyPi
|
|
7
|
+
|
|
8
|
+
*To be added*
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
```python
|
|
12
|
+
from gcp_pubsub_dao import PubSubSubscriberDAO, Message
|
|
13
|
+
|
|
14
|
+
dao = PubSubSubscriberDAO(project_id="prodect-dev", subscription_id="subscription")
|
|
15
|
+
messages: Message = dao.get_messages(messages_count=2)
|
|
16
|
+
|
|
17
|
+
for message in messages:
|
|
18
|
+
print(message.data)
|
|
19
|
+
|
|
20
|
+
dao.ack_messages(ack_ids=[message[0].ack_id])
|
|
21
|
+
dao.nack_messages(ack_ids=[message[1].ack_id])
|
|
22
|
+
|
|
23
|
+
dao.close() # to clean up connections
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from gcp_pubsub_dao import PubSubPublisherDAO
|
|
28
|
+
|
|
29
|
+
dao = PubSubPublisherDAO(project_id="prodect-dev")
|
|
30
|
+
try:
|
|
31
|
+
dao.publish_message(topic_name="topic", payload=b"asdfsdf", attributes={"kitId": "AW12345678"})
|
|
32
|
+
except Exception as ex:
|
|
33
|
+
print(ex)
|
|
34
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from google.cloud.pubsub_v1 import PublisherClient
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class PubSubPublisherDAO:
|
|
5
|
+
def __init__(self, project_id: str):
|
|
6
|
+
self.publisher = PublisherClient()
|
|
7
|
+
self.project_id = project_id
|
|
8
|
+
|
|
9
|
+
def publish_message(self, topic_name: str, payload: bytes, attributes: dict) -> None:
|
|
10
|
+
self.publisher.publish(
|
|
11
|
+
topic=self.publisher.topic_path(project=self.project_id, topic=topic_name),
|
|
12
|
+
data=payload,
|
|
13
|
+
**attributes,
|
|
14
|
+
).result()
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from google.cloud.pubsub_v1 import SubscriberClient
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class Message:
|
|
8
|
+
data: bytes
|
|
9
|
+
attributes: dict[str, str]
|
|
10
|
+
message_id: str
|
|
11
|
+
delivery_attempt: int
|
|
12
|
+
ack_id: str
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class PubSubSubscriberDAO:
|
|
16
|
+
def __init__(self, project_id: str, subscription_id: str):
|
|
17
|
+
self.subscriber = SubscriberClient()
|
|
18
|
+
self.project_id = project_id
|
|
19
|
+
self.subscription_id = subscription_id
|
|
20
|
+
|
|
21
|
+
def get_topic_name_by_subscription_id(self, subscription_id: str) -> str:
|
|
22
|
+
subscription = self.subscriber.get_subscription(
|
|
23
|
+
request={
|
|
24
|
+
"subscription": self.subscriber.subscription_path(self.project_id, subscription_id),
|
|
25
|
+
},
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
return subscription.topic.rsplit("/", 1)[1]
|
|
29
|
+
|
|
30
|
+
def get_messages(self, messages_count: int, return_immediately: bool = True) -> list[Message]:
|
|
31
|
+
response = self.subscriber.pull(
|
|
32
|
+
request={
|
|
33
|
+
"subscription": self.subscriber.subscription_path(self.project_id, self.subscription_id),
|
|
34
|
+
"max_messages": messages_count,
|
|
35
|
+
"return_immediately": return_immediately,
|
|
36
|
+
},
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
messages = []
|
|
40
|
+
for received_message in response.received_messages:
|
|
41
|
+
message = Message(
|
|
42
|
+
data=received_message.message.data,
|
|
43
|
+
attributes=dict(received_message.message.attributes),
|
|
44
|
+
message_id=received_message.message.message_id,
|
|
45
|
+
delivery_attempt=received_message.delivery_attempt,
|
|
46
|
+
ack_id=received_message.ack_id,
|
|
47
|
+
)
|
|
48
|
+
messages.append(message)
|
|
49
|
+
|
|
50
|
+
return messages
|
|
51
|
+
|
|
52
|
+
def ack_messages(self, ack_ids: list[str]) -> None:
|
|
53
|
+
self.subscriber.acknowledge(
|
|
54
|
+
request={
|
|
55
|
+
"subscription": self.subscriber.subscription_path(self.project_id, self.subscription_id),
|
|
56
|
+
"ack_ids": ack_ids,
|
|
57
|
+
},
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
def nack_messages(self, ack_ids: list[str]) -> None:
|
|
61
|
+
self.subscriber.modify_ack_deadline(
|
|
62
|
+
request={
|
|
63
|
+
"subscription": self.subscriber.subscription_path(self.project_id, self.subscription_id),
|
|
64
|
+
"ack_ids": ack_ids,
|
|
65
|
+
"ack_deadline_seconds": 0,
|
|
66
|
+
},
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def close(self) -> None:
|
|
70
|
+
self.subscriber.close()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "gcp-pubsub-dao"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "DAO for GCP PubSub service."
|
|
5
|
+
authors = ["Raman Shakun <shakunroman@gmail.com>"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
packages = [{include = "gcp_pubsub_dao"}]
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = "^3.11"
|
|
11
|
+
google-cloud-pubsub = "^2.18.2"
|
|
12
|
+
|
|
13
|
+
[tool.poetry.group.dev.dependencies]
|
|
14
|
+
ruff = "^0.0.284"
|
|
15
|
+
pyright = "^1.1.321"
|
|
16
|
+
safety = "^2.3.5"
|
|
17
|
+
isort = "^5.12.0"
|
|
18
|
+
black = "^23.7.0"
|
|
19
|
+
pytest = "^7.4.0"
|
|
20
|
+
pytest-cov = "^4.1.0"
|
|
21
|
+
|
|
22
|
+
[build-system]
|
|
23
|
+
requires = ["poetry-core"]
|
|
24
|
+
build-backend = "poetry.core.masonry.api"
|
|
25
|
+
|
|
26
|
+
[tool.black]
|
|
27
|
+
line-length = 120
|
|
28
|
+
preview = true
|
|
29
|
+
target-version = ['py311']
|
|
30
|
+
|
|
31
|
+
[tool.pyright]
|
|
32
|
+
include = ["gcp_pubsub_dao"]
|
|
33
|
+
exclude = ["tests"]
|
|
34
|
+
pythonVersion = "3.11"
|
|
35
|
+
|
|
36
|
+
[tool.ruff]
|
|
37
|
+
src = ["gcp_pubsub_dao"]
|
|
38
|
+
line-length = 120
|
|
39
|
+
select = [
|
|
40
|
+
"F", "E", "W", "C90", "I", "N", "S", "B", "A", "ISC", "T20", "Q", "PTH",
|
|
41
|
+
"COM", "C4", "PL", "RET", "SIM", "RUF", "ARG", "ICN", "DTZ", "UP",
|
|
42
|
+
]
|