Package googleapiclient :: Module channel
[hide private]
[frames] | no frames]

Module channel

source code

Channel notifications support.

Classes and functions to support channel subscriptions and notifications
on those channels.

  - This code is based on experimental APIs and is subject to change.
  - Notification does not do deduplication of notification ids, that's up to
    the receiver.
  - Storing the Channel between calls is up to the caller.

Example setting up a channel:

  # Create a new channel that gets notifications via webhook.
  channel = new_webhook_channel("")

  # Store the channel, keyed by ''. Store it before calling the
  # watch method because notifications may start arriving before the watch
  # method returns.

  resp = service.objects().watchAll(
    bucket="some_bucket_id", body=channel.body()).execute()

  # Store the channel, keyed by ''. Store it after being updated
  # since the resource_id value will now be correct, and that's needed to
  # stop a subscription.

An example Webhook implementation using webapp2. Note that webapp2 puts
headers in a case insensitive dictionary, as headers aren't guaranteed to
always be upper case.

  id = self.request.headers[X_GOOG_CHANNEL_ID]

  # Retrieve the channel by id.
  channel = ...

  # Parse notification from the headers, including validating the id.
  n = notification_from_headers(channel, self.request.headers)

  # Do app specific stuff with the notification here.
  if n.resource_state == 'sync':
    # Code to handle sync state.
  elif n.resource_state == 'exists':
    # Code to handle the exists state.
  elif n.resource_state == 'not_exists':
    # Code to handle the not exists state.

Example of unsubscribing.


Classes [hide private]
A Notification from a Channel.
A Channel for notifications.
Functions [hide private]
_upper_header_keys(headers) source code
notification_from_headers(channel, headers)
Parse a notification from the webhook request headers, validate the notification, and return a Notification object.
source code
new_webhook_channel(*args, **kwargs)
Create a new webhook Channel.
source code
Variables [hide private]
  EPOCH = datetime.datetime(1970, 1, 1, 0, 0)
  CHANNEL_PARAMS = {'address': 'address', 'expiration': 'expirat...
  __package__ = None
Function Details [hide private]

notification_from_headers(channel, headers)

source code 
Parse a notification from the webhook request headers, validate
  the notification, and return a Notification object.

  channel: Channel, The channel that the notification is associated with.
  headers: dict, A dictionary like object that contains the request headers
    from the webhook HTTP request.

  A Notification object.

  errors.InvalidNotificationError if the notification is invalid.
  ValueError if the X-GOOG-MESSAGE-NUMBER can't be converted to an int.

new_webhook_channel(*args, **kwargs)

source code 
Create a new webhook Channel.

  url: str, URL to post notifications to.
  token: str, An arbitrary string associated with the channel that
    is delivered to the target address with each notification delivered
    over this channel.
  expiration: datetime.datetime, A time in the future when the channel
    should expire. Can also be None if the subscription should use the
    default expiration. Note that different services may have different
    limits on how long a subscription lasts. Check the response from the
    watch() method to see the value the service has set for an expiration
  params: dict, Extra parameters to pass on channel creation. Currently
    not used for webhook channels.

  • @util.positional(2)

Variables Details [hide private]


{'address': 'address',
 'expiration': 'expiration',
 'id': 'id',
 'params': 'params',
 'resourceId': 'resource_id',
 'resourceUri': 'resource_uri',
 'token': 'token',
 'type': 'type'}