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.

Notes:
  - 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("https://example.com/my_web_hook")

  # Store the channel, keyed by 'channel.id'. 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()
  channel.update(resp)

  # Store the channel, keyed by 'channel.id'. 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.

  service.channels().stop(channel.body()).execute()

Classes [hide private]
  Notification
A Notification from a Channel.
  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(url, token=None, expiration=None, params=None)
Create a new webhook Channel.
source code
Variables [hide private]
  EPOCH = datetime.datetime.utcfromtimestamp(0)
  CHANNEL_PARAMS = {"address": "address", "id": "id", "expiratio...
  X_GOOG_CHANNEL_ID = "X-GOOG-CHANNEL-ID"
  X_GOOG_MESSAGE_NUMBER = "X-GOOG-MESSAGE-NUMBER"
  X_GOOG_RESOURCE_STATE = "X-GOOG-RESOURCE-STATE"
  X_GOOG_RESOURCE_URI = "X-GOOG-RESOURCE-URI"
  X_GOOG_RESOURCE_ID = "X-GOOG-RESOURCE-ID"
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.

Args:
  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.

Returns:
  A Notification object.

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

new_webhook_channel(url, token=None, expiration=None, params=None)

source code 
Create a new webhook Channel.

Args:
  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
    time.
  params: dict, Extra parameters to pass on channel creation. Currently
    not used for webhook channels.

Decorators:
  • @util.positional(2)

Variables Details [hide private]

CHANNEL_PARAMS

Value:
{"address": "address", "id": "id", "expiration": "expiration", "params\
": "params", "resourceId": "resource_id", "resourceUri": "resource_uri\
", "type": "type", "token": "token",}