74
74
import datetime
75
75
import uuid
76
76
77
- from googleapiclient import errors
78
77
from googleapiclient import _helpers as util
79
-
78
+ from googleapiclient import errors
80
79
81
80
# The unix time epoch starts at midnight 1970.
82
81
EPOCH = datetime .datetime .utcfromtimestamp (0 )
@@ -111,28 +110,28 @@ def _upper_header_keys(headers):
111
110
class Notification (object ):
112
111
"""A Notification from a Channel.
113
112
114
- Notifications are not usually constructed directly, but are returned
115
- from functions like notification_from_headers().
113
+ Notifications are not usually constructed directly, but are returned
114
+ from functions like notification_from_headers().
116
115
117
- Attributes:
118
- message_number: int, The unique id number of this notification.
119
- state: str, The state of the resource being monitored.
120
- uri: str, The address of the resource being monitored.
121
- resource_id: str, The unique identifier of the version of the resource at
122
- this event.
123
- """
116
+ Attributes:
117
+ message_number: int, The unique id number of this notification.
118
+ state: str, The state of the resource being monitored.
119
+ uri: str, The address of the resource being monitored.
120
+ resource_id: str, The unique identifier of the version of the resource at
121
+ this event.
122
+ """
124
123
125
124
@util .positional (5 )
126
125
def __init__ (self , message_number , state , resource_uri , resource_id ):
127
126
"""Notification constructor.
128
127
129
- Args:
130
- message_number: int, The unique id number of this notification.
131
- state: str, The state of the resource being monitored. Can be one
132
- of "exists", "not_exists", or "sync".
133
- resource_uri: str, The address of the resource being monitored.
134
- resource_id: str, The identifier of the watched resource.
135
- """
128
+ Args:
129
+ message_number: int, The unique id number of this notification.
130
+ state: str, The state of the resource being monitored. Can be one
131
+ of "exists", "not_exists", or "sync".
132
+ resource_uri: str, The address of the resource being monitored.
133
+ resource_id: str, The identifier of the watched resource.
134
+ """
136
135
self .message_number = message_number
137
136
self .state = state
138
137
self .resource_uri = resource_uri
@@ -142,53 +141,17 @@ def __init__(self, message_number, state, resource_uri, resource_id):
142
141
class Channel (object ):
143
142
"""A Channel for notifications.
144
143
145
- Usually not constructed directly, instead it is returned from helper
146
- functions like new_webhook_channel().
147
-
148
- Attributes:
149
- type: str, The type of delivery mechanism used by this channel. For
150
- example, 'web_hook'.
151
- id: str, A UUID for the channel.
152
- token: str, An arbitrary string associated with the channel that
153
- is delivered to the target address with each event delivered
154
- over this channel.
155
- address: str, The address of the receiving entity where events are
156
- delivered. Specific to the channel type.
157
- expiration: int, The time, in milliseconds from the epoch, when this
158
- channel will expire.
159
- params: dict, A dictionary of string to string, with additional parameters
160
- controlling delivery channel behavior.
161
- resource_id: str, An opaque id that identifies the resource that is
162
- being watched. Stable across different API versions.
163
- resource_uri: str, The canonicalized ID of the watched resource.
164
- """
165
-
166
- @util .positional (5 )
167
- def __init__ (
168
- self ,
169
- type ,
170
- id ,
171
- token ,
172
- address ,
173
- expiration = None ,
174
- params = None ,
175
- resource_id = "" ,
176
- resource_uri = "" ,
177
- ):
178
- """Create a new Channel.
179
-
180
- In user code, this Channel constructor will not typically be called
181
- manually since there are functions for creating channels for each specific
182
- type with a more customized set of arguments to pass.
144
+ Usually not constructed directly, instead it is returned from helper
145
+ functions like new_webhook_channel().
183
146
184
- Args :
147
+ Attributes :
185
148
type: str, The type of delivery mechanism used by this channel. For
186
149
example, 'web_hook'.
187
150
id: str, A UUID for the channel.
188
151
token: str, An arbitrary string associated with the channel that
189
152
is delivered to the target address with each event delivered
190
153
over this channel.
191
- address: str, The address of the receiving entity where events are
154
+ address: str, The address of the receiving entity where events are
192
155
delivered. Specific to the channel type.
193
156
expiration: int, The time, in milliseconds from the epoch, when this
194
157
channel will expire.
@@ -198,6 +161,42 @@ def __init__(
198
161
being watched. Stable across different API versions.
199
162
resource_uri: str, The canonicalized ID of the watched resource.
200
163
"""
164
+
165
+ @util .positional (5 )
166
+ def __init__ (
167
+ self ,
168
+ type ,
169
+ id ,
170
+ token ,
171
+ address ,
172
+ expiration = None ,
173
+ params = None ,
174
+ resource_id = "" ,
175
+ resource_uri = "" ,
176
+ ):
177
+ """Create a new Channel.
178
+
179
+ In user code, this Channel constructor will not typically be called
180
+ manually since there are functions for creating channels for each specific
181
+ type with a more customized set of arguments to pass.
182
+
183
+ Args:
184
+ type: str, The type of delivery mechanism used by this channel. For
185
+ example, 'web_hook'.
186
+ id: str, A UUID for the channel.
187
+ token: str, An arbitrary string associated with the channel that
188
+ is delivered to the target address with each event delivered
189
+ over this channel.
190
+ address: str, The address of the receiving entity where events are
191
+ delivered. Specific to the channel type.
192
+ expiration: int, The time, in milliseconds from the epoch, when this
193
+ channel will expire.
194
+ params: dict, A dictionary of string to string, with additional parameters
195
+ controlling delivery channel behavior.
196
+ resource_id: str, An opaque id that identifies the resource that is
197
+ being watched. Stable across different API versions.
198
+ resource_uri: str, The canonicalized ID of the watched resource.
199
+ """
201
200
self .type = type
202
201
self .id = id
203
202
self .token = token
@@ -210,12 +209,12 @@ def __init__(
210
209
def body (self ):
211
210
"""Build a body from the Channel.
212
211
213
- Constructs a dictionary that's appropriate for passing into watch()
214
- methods as the value of body argument.
212
+ Constructs a dictionary that's appropriate for passing into watch()
213
+ methods as the value of body argument.
215
214
216
- Returns:
217
- A dictionary representation of the channel.
218
- """
215
+ Returns:
216
+ A dictionary representation of the channel.
217
+ """
219
218
result = {
220
219
"id" : self .id ,
221
220
"token" : self .token ,
@@ -236,13 +235,13 @@ def body(self):
236
235
def update (self , resp ):
237
236
"""Update a channel with information from the response of watch().
238
237
239
- When a request is sent to watch() a resource, the response returned
240
- from the watch() request is a dictionary with updated channel information,
241
- such as the resource_id, which is needed when stopping a subscription.
238
+ When a request is sent to watch() a resource, the response returned
239
+ from the watch() request is a dictionary with updated channel information,
240
+ such as the resource_id, which is needed when stopping a subscription.
242
241
243
- Args:
244
- resp: dict, The response from a watch() method.
245
- """
242
+ Args:
243
+ resp: dict, The response from a watch() method.
244
+ """
246
245
for json_name , param_name in CHANNEL_PARAMS .items ():
247
246
value = resp .get (json_name )
248
247
if value is not None :
@@ -251,20 +250,20 @@ def update(self, resp):
251
250
252
251
def notification_from_headers (channel , headers ):
253
252
"""Parse a notification from the webhook request headers, validate
254
- the notification, and return a Notification object.
253
+ the notification, and return a Notification object.
255
254
256
- Args:
257
- channel: Channel, The channel that the notification is associated with.
258
- headers: dict, A dictionary like object that contains the request headers
259
- from the webhook HTTP request.
255
+ Args:
256
+ channel: Channel, The channel that the notification is associated with.
257
+ headers: dict, A dictionary like object that contains the request headers
258
+ from the webhook HTTP request.
260
259
261
- Returns:
262
- A Notification object.
260
+ Returns:
261
+ A Notification object.
263
262
264
- Raises:
265
- errors.InvalidNotificationError if the notification is invalid.
266
- ValueError if the X-GOOG-MESSAGE-NUMBER can't be converted to an int.
267
- """
263
+ Raises:
264
+ errors.InvalidNotificationError if the notification is invalid.
265
+ ValueError if the X-GOOG-MESSAGE-NUMBER can't be converted to an int.
266
+ """
268
267
headers = _upper_header_keys (headers )
269
268
channel_id = headers [X_GOOG_CHANNEL_ID ]
270
269
if channel .id != channel_id :
0 commit comments