
    (ph.+              	           S r SSKJr  SSKrSSKrSSKJr  SSKJr  \R                  " SSS5      r	SS	S
SSSSSS.r
SrSrSrSrSrS r " S S\5      r " S S\5      rS r\R*                  " S5      SS j5       rg)a  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()
    )absolute_importN)_helpers)errorsi     addressid
expirationparamsresource_idresource_uritypetoken)r   r   r	   r
   
resourceIdresourceUrir   r   zX-GOOG-CHANNEL-IDzX-GOOG-MESSAGE-NUMBERzX-GOOG-RESOURCE-STATEzX-GOOG-RESOURCE-URIzX-GOOG-RESOURCE-IDc                 `    0 nU R                  5        H  u  p#X1UR                  5       '   M     U$ )N)itemsupper)headersnew_headerskvs       J/var/www/html/venv/lib/python3.13/site-packages/googleapiclient/channel.py_upper_header_keysr   g   s-    K!"AGGI      c                   H    \ rS rSrSr\R                  " S5      S 5       rSrg)Notificationn   a  A Notification from a Channel.

Notifications are not usually constructed directly, but are returned
from functions like notification_from_headers().

Attributes:
  message_number: int, The unique id number of this notification.
  state: str, The state of the resource being monitored.
  uri: str, The address of the resource being monitored.
  resource_id: str, The unique identifier of the version of the resource at
    this event.
   c                 4    Xl         X l        X0l        X@l        g)aO  Notification constructor.

Args:
  message_number: int, The unique id number of this notification.
  state: str, The state of the resource being monitored. Can be one
    of "exists", "not_exists", or "sync".
  resource_uri: str, The address of the resource being monitored.
  resource_id: str, The identifier of the watched resource.
N)message_numberstater   r   )selfr    r!   r   r   s        r   __init__Notification.__init__|   s     -
(&r   )r    r   r   r!   N)	__name__
__module____qualname____firstlineno____doc__util
positionalr#   __static_attributes__ r   r   r   r   n   s$     
__Q' 'r   r   c                   `    \ rS rSrSr\R                  " S5          S	S j5       rS rS r	Sr
g)
Channel   a  A Channel for notifications.

Usually not constructed directly, instead it is returned from helper
functions like new_webhook_channel().

Attributes:
  type: str, The type of delivery mechanism used by this channel. For
    example, 'web_hook'.
  id: str, A UUID for the channel.
  token: str, An arbitrary string associated with the channel that
    is delivered to the target address with each event delivered
    over this channel.
  address: str, The address of the receiving entity where events are
    delivered. Specific to the channel type.
  expiration: int, The time, in milliseconds from the epoch, when this
    channel will expire.
  params: dict, A dictionary of string to string, with additional parameters
    controlling delivery channel behavior.
  resource_id: str, An opaque id that identifies the resource that is
    being watched. Stable across different API versions.
  resource_uri: str, The canonicalized ID of the watched resource.
r   Nc	                 d    Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        g)a  Create a new Channel.

In user code, this Channel constructor will not typically be called
manually since there are functions for creating channels for each specific
type with a more customized set of arguments to pass.

Args:
  type: str, The type of delivery mechanism used by this channel. For
    example, 'web_hook'.
  id: str, A UUID for the channel.
  token: str, An arbitrary string associated with the channel that
    is delivered to the target address with each event delivered
    over this channel.
  address: str,  The address of the receiving entity where events are
    delivered. Specific to the channel type.
  expiration: int, The time, in milliseconds from the epoch, when this
    channel will expire.
  params: dict, A dictionary of string to string, with additional parameters
    controlling delivery channel behavior.
  resource_id: str, An opaque id that identifies the resource that is
    being watched. Stable across different API versions.
  resource_uri: str, The canonicalized ID of the watched resource.
N)r   r   r   r   r	   r
   r   r   )	r"   r   r   r   r   r	   r
   r   r   s	            r   r#   Channel.__init__   s/    F 	
$&(r   c                 d   U R                   U R                  U R                  U R                  S.nU R                  (       a  U R                  US'   U R
                  (       a  U R
                  US'   U R                  (       a  U R                  US'   U R                  (       a  U R                  US'   U$ )zBuild a body from the Channel.

Constructs a dictionary that's appropriate for passing into watch()
methods as the value of body argument.

Returns:
  A dictionary representation of the channel.
)r   r   r   r   r
   r   r   r	   )r   r   r   r   r
   r   r   r	   )r"   results     r   bodyChannel.body   s     ''ZZII||	
 ;;#{{F8#'#3#3F< $($5$5F=!??#'??F< r   c                     [         R                  5        H'  u  p#UR                  U5      nUc  M  [        XU5        M)     g)aP  Update a channel with information from the response of watch().

When a request is sent to watch() a resource, the response returned
from the watch() request is a dictionary with updated channel information,
such as the resource_id, which is needed when stopping a subscription.

Args:
  resp: dict, The response from a watch() method.
N)CHANNEL_PARAMSr   getsetattr)r"   resp	json_name
param_namevalues        r   updateChannel.update   s8     &4%9%9%;!IHHY'E %0 &<r   )r   r	   r   r
   r   r   r   r   )NN rA   )r%   r&   r'   r(   r)   r*   r+   r#   r5   r?   r,   r-   r   r   r/   r/      s=    . 
__Q )) ))V41r   r/   c                    [        U5      nU[           nU R                  U:w  a(  [        R                  " SU R                  < SU< 35      e[        U[           5      nU[           nU[           nU[           n[        X4XV5      $ )a  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.
zChannel id mismatch: z != )r   X_GOOG_CHANNEL_IDr   r   InvalidNotificationErrorintX_GOOG_MESSAGE_NUMBERX_GOOG_RESOURCE_STATEX_GOOG_RESOURCE_URIX_GOOG_RESOURCE_IDr   )channelr   
channel_idr    r!   r   r   s          r   notification_from_headersrL      s      !)G*+JzzZ--/6zz:F
 	
 W%:;<-.2301N<MMr      c           	          SnU(       aC  U[         -
  nUR                  S-  UR                  UR                  S-  S-  -   S-  -   nUS:  a  Sn[	        S[        [        R                  " 5       5      UU UUS9$ )a  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.
r   i     i  web_hook)r	   r
   )EPOCHmicrosecondssecondsdaysr/   struuiduuid4)urlr   r	   r
   expiration_msdeltas         r   new_webhook_channelr[     s    $ MU"%b49O)OSW(WW 	 1MDJJL  r   )NNN)r)   
__future__r   datetimerV   googleapiclientr   r*   r   rQ   r8   rC   rF   rG   rH   rI   r   objectr   r/   rL   r+   r[   r-   r   r   <module>r`      s   8r '   , " 	$1%
 
!	 ( / / + ) '6 '>k1f k1\N< ! !r   