1414
1515"""Define API Subscriptions."""
1616
17+ import datetime
18+
1719from google .cloud .exceptions import NotFound
1820from google .cloud .pubsub ._helpers import topic_name_from_path
1921from google .cloud .pubsub .iam import Policy
@@ -43,6 +45,19 @@ class Subscription(object):
4345 (Optional) URL to which messages will be pushed by the back-end. If
4446 not set, the application must pull messages.
4547
48+ :type retain_acked_messages: bool
49+ :param retain_acked_messages:
50+ (Optional) Whether to retain acked messages. If set, acked messages
51+ are retained in the subscription's backlog for a duration indicated
52+ by `message_retention_duration`.
53+
54+ :type message_retention_duration: :class:`datetime.timedelta`
55+ :param message_retention_duration:
56+ (Optional) Whether to retain acked messages. If set, acked messages
57+ are retained in the subscription's backlog for a duration indicated
58+ by `message_retention_duration`. If unset, defaults to 7 days.
59+
60+
4661 :type client: :class:`~google.cloud.pubsub.client.Client`
4762 :param client:
4863 (Optional) The client to use. If not passed, falls back to the
@@ -57,6 +72,7 @@ class Subscription(object):
5772 """
5873
5974 def __init__ (self , name , topic = None , ack_deadline = None , push_endpoint = None ,
75+ retain_acked_messages = None , message_retention_duration = None ,
6076 client = None ):
6177
6278 if client is None and topic is None :
@@ -71,6 +87,8 @@ def __init__(self, name, topic=None, ack_deadline=None, push_endpoint=None,
7187 self ._project = self ._client .project
7288 self .ack_deadline = ack_deadline
7389 self .push_endpoint = push_endpoint
90+ self .retain_acked_messages = retain_acked_messages
91+ self .message_retention_duration = message_retention_duration
7492
7593 @classmethod
7694 def from_api_repr (cls , resource , client , topics = None ):
@@ -107,10 +125,21 @@ def from_api_repr(cls, resource, client, topics=None):
107125 ack_deadline = resource .get ('ackDeadlineSeconds' )
108126 push_config = resource .get ('pushConfig' , {})
109127 push_endpoint = push_config .get ('pushEndpoint' )
128+ retain_acked_messages = resource .get ('retainAckedMessages' )
129+ resource_duration = resource .get ('duration' , {})
130+ message_retention_duration = datetime .timedelta (
131+ seconds = resource_duration .get ('seconds' , 0 ),
132+ microseconds = resource_duration .get ('nanos' , 0 ) / 1000 )
110133 if topic is None :
111134 return cls (name , ack_deadline = ack_deadline ,
112- push_endpoint = push_endpoint , client = client )
113- return cls (name , topic , ack_deadline , push_endpoint )
135+ push_endpoint = push_endpoint ,
136+ retain_acked_messages = retain_acked_messages ,
137+ message_retention_duration = message_retention_duration ,
138+ client = client )
139+ return cls (name , topic = topic , ack_deadline = ack_deadline ,
140+ push_endpoint = push_endpoint ,
141+ retain_acked_messages = retain_acked_messages ,
142+ message_retention_duration = message_retention_duration )
114143
115144 @property
116145 def project (self ):
@@ -182,8 +211,10 @@ def create(self, client=None):
182211 client = self ._require_client (client )
183212 api = client .subscriber_api
184213 api .subscription_create (
185- self .full_name , self .topic .full_name , self .ack_deadline ,
186- self .push_endpoint )
214+ self .full_name , self .topic .full_name ,
215+ ack_deadline = self .ack_deadline , push_endpoint = self .push_endpoint ,
216+ retain_acked_messages = self .retain_acked_messages ,
217+ message_retention_duration = self .message_retention_duration )
187218
188219 def exists (self , client = None ):
189220 """API call: test existence of the subscription via a GET request
0 commit comments