o ckF[@sPddlZddlZddlmZddlmZddlmZddlZGdddeZ dS)N)AWSQueryConnection) RegionInfo)jsoncspeZdZdZejdddZejdddZejdddZ dGfd d Z ddZ ddZ dHddZ ddZddZddZddZddZdd Z dId!d"Zd#d$Zd%d&Z 'dJd(d)Zd*d+ZdHd,d-ZdHd.d/Z dKd0d1Z dLd2d3Z dHd4d5ZdHd6d7Z dLd8d9ZdHd:d;Z dMdd?Z"dLd@dAZ#dHdBdCZ$dNdEdFZ%Z&S)O SNSConnectiona Amazon Simple Notification Service Amazon Simple Notification Service (Amazon SNS) is a web service that enables you to build distributed web-enabled applications. Applications can use Amazon SNS to easily push real-time notification messages to interested subscribers over multiple delivery protocols. For more information about this product see `http://aws.amazon.com/sns`_. For detailed information about Amazon SNS features and their associated API calls, see the `Amazon SNS Developer Guide`_. We also provide SDKs that enable you to access Amazon SNS from your preferred programming language. The SDKs contain functionality that automatically takes care of tasks such as: cryptographically signing your service requests, retrying requests, and handling error responses. For a list of available SDKs, go to `Tools for Amazon Web Services`_. Botosns_region_namez us-east-1sns_region_endpointzsns.us-east-1.amazonaws.com sns_versionz 2010-03-31NTr/csT| s t||j|jtd} | |_tt|j|||||||||jj| | | | ||ddS)N)connection_cls)security_tokenvalidate_certs profile_name)rDefaultRegionNameDefaultRegionEndpointrregionsuper__init__endpoint)selfaws_access_key_idaws_secret_access_key is_secureportproxy proxy_port proxy_user proxy_passdebughttps_connection_factoryrpathr r r __class__5/usr/lib/python3/dist-packages/boto/sns/connection.pyr9s   zSNSConnection.__init__c Csjt|ddd}t|ttdt|dD]\}}|\}}d||f} ||d| <||d| <qdS) aA Serialize a parameter 'name' which value is a 'dictionary' into a list of parameters. See: http://docs.aws.amazon.com/sns/latest/api/API_SetPlatformApplicationAttributes.html For example:: dictionary = {'PlatformPrincipal': 'foo', 'PlatformCredential': 'bar'} name = 'Attributes' would result in params dict being populated with: Attributes.entry.1.key = PlatformPrincipal Attributes.entry.1.value = foo Attributes.entry.2.key = PlatformCredential Attributes.entry.2.value = bar :param params: the resulting parameters will be added to this dict :param dictionary: dict - value of the serialized parameter :param name: name of the serialized parameter cSs|dS)Nrr#)xr#r#r$bsz:SNSConnection._build_dict_as_list_params..)keyz %s.entry.%sz%s.keyz%s.valueN)sorteditemsziplistrangelen) rparams dictionarynamer*kvindexr'valueprefixr#r#r$_build_dict_as_list_paramsNs$  z(SNSConnection._build_dict_as_list_paramscCsdgS)Nzhmac-v4r#)rr#r#r$_required_auth_capabilityisz'SNSConnection._required_auth_capabilitycCi}|r||d<|d|S)z :type next_token: string :param next_token: Token returned by the previous call to this method. NextToken ListTopics _make_requestr next_tokenr/r#r#r$get_all_topicsls zSNSConnection.get_all_topicscCd|i}|d|S)zu Get attributes of a Topic :type topic: string :param topic: The ARN of the topic. TopicArnGetTopicAttributesr;rtopicr/r#r#r$get_topic_attributesx z"SNSConnection.get_topic_attributescC|||d}|d|S)a Get attributes of a Topic :type topic: string :param topic: The ARN of the topic. :type attr_name: string :param attr_name: The name of the attribute you want to set. Only a subset of the topic's attributes are mutable. Valid values: Policy | DisplayName :type attr_value: string :param attr_value: The new value for the attribute. )rA AttributeNameAttributeValueSetTopicAttributesr;)rrD attr_name attr_valuer/r#r#r$set_topic_attributess  z"SNSConnection.set_topic_attributescCs2||d}|||d|||d|d|S)a Adds a statement to a topic's access control policy, granting access for the specified AWS accounts to the specified actions. :type topic: string :param topic: The ARN of the topic. :type label: string :param label: A unique identifier for the new policy statement. :type account_ids: list of strings :param account_ids: The AWS account ids of the users who will be give access to the specified actions. :type actions: list of strings :param actions: The actions you want to allow for each of the specified principal(s). rALabelzAWSAccountId.memberzActionName.member AddPermission)build_list_paramsr<)rrDlabel account_idsactionsr/r#r#r$add_permissions  zSNSConnection.add_permissioncCs||d}|d|S)a Removes a statement from a topic's access control policy. :type topic: string :param topic: The ARN of the topic. :type label: string :param label: A unique identifier for the policy statement to be removed. rNRemovePermissionr;)rrDrRr/r#r#r$remove_permissions  zSNSConnection.remove_permissioncCr@)zt Create a new Topic. :type topic: string :param topic: The name of the new topic. Name CreateTopicr;rCr#r#r$ create_topicrFzSNSConnection.create_topiccCsd|i}|d|ddS)zs Delete an existing topic :type topic: string :param topic: The ARN of the topic rA DeleteTopicr GETr;rCr#r#r$ delete_topicszSNSConnection.delete_topicc Cs|durtdd|i}|dur||d<|dur||d<|dur$||d<|dur,||d<|durst|}t|dd D]6\} } || } | |d | <d | vrX| d |d | <d | vre| d |d| <d| vrr| d|d| <q<|d|ddS)a{ Sends a message to all of a topic's subscribed endpoints :type topic: string :param topic: The topic you want to publish to. :type message: string :param message: The message you want to send to the topic. Messages must be UTF-8 encoded strings and be at most 4KB in size. :type message_structure: string :param message_structure: Optional parameter. If left as ``None``, plain text will be sent. If set to ``json``, your message should be a JSON string that matches the structure described at http://docs.aws.amazon.com/sns/latest/dg/PublishTopic.html#sns-message-formatting-by-protocol :type message_attributes: dict :param message_attributes: Message attributes to set. Should be of the form: .. code-block:: python { "name1": { "data_type": "Number", "string_value": "42" }, "name2": { "data_type": "String", "string_value": "Bob" } } :type subject: string :param subject: Optional parameter to be used as the "Subject" line of the email notifications. :type target_arn: string :param target_arn: Optional parameter for either TopicArn or EndpointArn, but not both. Nz!'message' is a required parameterMessageSubjectrA TargetArnMessageStructurer()startz MessageAttributes.entry.{0}.Name data_typez*MessageAttributes.entry.{0}.Value.DataType string_valuez-MessageAttributes.entry.{0}.Value.StringValue binary_valuez-MessageAttributes.entry.{0}.Value.BinaryValuePublishr POST) TypeErrorr)keys enumerateformatr<) rrDmessagesubject target_arnmessage_structuremessage_attributesr/riir1 attributer#r#r$publishs6.    zSNSConnection.publishcCrG)aU Subscribe to a Topic. :type topic: string :param topic: The ARN of the new topic. :type protocol: string :param protocol: The protocol used to communicate with the subscriber. Current choices are: email|email-json|http|https|sqs|sms|application :type endpoint: string :param endpoint: The location of the endpoint for the subscriber. * For email, this would be a valid email address * For email-json, this would be a valid email address * For http, this would be a URL beginning with http * For https, this would be a URL beginning with https * For sqs, this would be the ARN of an SQS Queue * For sms, this would be a phone number of an SMS-enabled device * For application, the endpoint is the EndpointArn of a mobile app and device. )rAProtocolEndpoint Subscriber;)rrDprotocolrr/r#r#r$ subscribe$s  zSNSConnection.subscribec Cs|jd}|j}t||d}d}||d|}|d}d|vr/t |d} ni} d| vr9d| d<d| vrAg| d<| dD] } | d |krOd }qE|shd d d di||dd|iid} | d | | dt | |S)a Subscribe an SQS queue to a topic. This is convenience method that handles most of the complexity involved in using an SQS queue as an endpoint for an SNS topic. To achieve this the following operations are performed: * The correct ARN is constructed for the SQS queue and that ARN is then subscribed to the topic. * A JSON policy document is contructed that grants permission to the SNS topic to send messages to the SQS queue. * This JSON policy is then associated with the SQS queue using the queue's set_attribute method. If the queue already has a policy associated with it, this process will add a Statement to that policy. If no policy exists, a new policy will be created. :type topic: string :param topic: The ARN of the new topic. :type queue: A boto Queue object :param queue: The queue you wish to subscribe to the SNS Topic. r utf-8FsqsPolicyVersionz 2008-10-17 StatementSidTzSQS:SendMessageAllowAWS* StringLikez aws:SourceArn)ActionEffect PrincipalResourcer~ Condition)idsplitarnhashlibmd5encode hexdigestrxget_attributesrloadsappend set_attributedumps) rrDqueuetq_arnsid sid_existsrespattrpolicys statementr#r#r$subscribe_sqs_queueBs8     z!SNSConnection.subscribe_sqs_queueFcCs"||d}|r d|d<|d|S)aU Get properties of a Topic :type topic: string :param topic: The ARN of the new topic. :type token: string :param token: Short-lived token sent to and endpoint during the Subscribe operation. :type authenticate_on_unsubscribe: bool :param authenticate_on_unsubscribe: Optional parameter indicating that you wish to disable unauthenticated unsubscription of the subscription. )rATokentrueAuthenticateOnUnsubscribeConfirmSubscriptionr;)rrDtokenauthenticate_on_unsubscriber/r#r#r$confirm_subscriptionvs  z"SNSConnection.confirm_subscriptioncCr@)z Allows endpoint owner to delete subscription. Confirmation message will be delivered. :type subscription: string :param subscription: The ARN of the subscription to be deleted. SubscriptionArn Unsubscriber;)r subscriptionr/r#r#r$ unsubscribes zSNSConnection.unsubscribecCr8)z Get list of all subscriptions. :type next_token: string :param next_token: Token returned by the previous call to this method. r9ListSubscriptionsr;r=r#r#r$get_all_subscriptionss  z#SNSConnection.get_all_subscriptionscCs d|i}|r ||d<|d|S)aY Get list of all subscriptions to a specific topic. :type topic: string :param topic: The ARN of the topic for which you wish to find subscriptions. :type next_token: string :param next_token: Token returned by the previous call to this method. rAr9ListSubscriptionsByTopicr;)rrDr>r/r#r#r$get_all_subscriptions_by_topics  z,SNSConnection.get_all_subscriptions_by_topiccCsHi}|dur ||d<|dur||d<|dur|||d|jd|dS)a The `CreatePlatformApplication` action creates a platform application object for one of the supported push notification services, such as APNS and GCM, to which devices and mobile apps may register. You must specify PlatformPrincipal and PlatformCredential attributes when using the `CreatePlatformApplication` action. The PlatformPrincipal is received from the notification service. For APNS/APNS_SANDBOX, PlatformPrincipal is "SSL certificate". For GCM, PlatformPrincipal is not applicable. For ADM, PlatformPrincipal is "client id". The PlatformCredential is also received from the notification service. For APNS/APNS_SANDBOX, PlatformCredential is "private key". For GCM, PlatformCredential is "API key". For ADM, PlatformCredential is "client secret". The PlatformApplicationArn that is returned when using `CreatePlatformApplication` is then used as an attribute for the `CreatePlatformEndpoint` action. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type name: string :param name: Application names must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, hyphens, and periods, and must be between 1 and 256 characters long. :type platform: string :param platform: The following platforms are supported: ADM (Amazon Device Messaging), APNS (Apple Push Notification Service), APNS_SANDBOX, and GCM (Google Cloud Messaging). :type attributes: map :param attributes: For a list of attributes, see `SetPlatformApplicationAttributes`_ NrXPlatform AttributesCreatePlatformApplicationactionr/r6r<)rr1platform attributesr/r#r#r$create_platform_applications%z)SNSConnection.create_platform_applicationcC8i}|dur ||d<|dur|||d|jd|dS)a The `SetPlatformApplicationAttributes` action sets the attributes of the platform application object for the supported push notification services, such as APNS and GCM. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type platform_application_arn: string :param platform_application_arn: PlatformApplicationArn for SetPlatformApplicationAttributes action. :type attributes: map :param attributes: A map of the platform application attributes. Attributes in this map include the following: + `PlatformCredential` -- The credential received from the notification service. For APNS/APNS_SANDBOX, PlatformCredential is "private key". For GCM, PlatformCredential is "API key". For ADM, PlatformCredential is "client secret". + `PlatformPrincipal` -- The principal received from the notification service. For APNS/APNS_SANDBOX, PlatformPrincipal is "SSL certificate". For GCM, PlatformPrincipal is not applicable. For ADM, PlatformPrincipal is "client id". + `EventEndpointCreated` -- Topic ARN to which EndpointCreated event notifications should be sent. + `EventEndpointDeleted` -- Topic ARN to which EndpointDeleted event notifications should be sent. + `EventEndpointUpdated` -- Topic ARN to which EndpointUpdate event notifications should be sent. + `EventDeliveryFailure` -- Topic ARN to which DeliveryFailure event notifications should be sent upon Direct Publish delivery failure (permanent) to one of the application's endpoints. NPlatformApplicationArnr SetPlatformApplicationAttributesrr)rplatform_application_arnrr/r#r#r$#set_platform_application_attributess'z1SNSConnection.set_platform_application_attributescC"i}|dur ||d<|jd|dS)a The `GetPlatformApplicationAttributes` action retrieves the attributes of the platform application object for the supported push notification services, such as APNS and GCM. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type platform_application_arn: string :param platform_application_arn: PlatformApplicationArn for GetPlatformApplicationAttributesInput. Nr GetPlatformApplicationAttributesrr;rrr/r#r#r$#get_platform_application_attributess z1SNSConnection.get_platform_application_attributescCr)a The `ListPlatformApplications` action lists the platform application objects for the supported push notification services, such as APNS and GCM. The results for `ListPlatformApplications` are paginated and return a limited list of applications, up to 100. If additional records are available after the first page results, then a NextToken string will be returned. To receive the next page, you call `ListPlatformApplications` using the NextToken string received from the previous call. When there are no more records to return, NextToken will be null. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type next_token: string :param next_token: NextToken string is used when calling ListPlatformApplications action to retrieve additional records that are available after the first page results. Nr9ListPlatformApplicationsrr;r=r#r#r$list_platform_applications,s z(SNSConnection.list_platform_applicationscCs2i}|dur ||d<|dur||d<|jd|dS)a The `ListEndpointsByPlatformApplication` action lists the endpoints and endpoint attributes for devices in a supported push notification service, such as GCM and APNS. The results for `ListEndpointsByPlatformApplication` are paginated and return a limited list of endpoints, up to 100. If additional records are available after the first page results, then a NextToken string will be returned. To receive the next page, you call `ListEndpointsByPlatformApplication` again using the NextToken string received from the previous call. When there are no more records to return, NextToken will be null. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type platform_application_arn: string :param platform_application_arn: PlatformApplicationArn for ListEndpointsByPlatformApplicationInput action. :type next_token: string :param next_token: NextToken string is used when calling ListEndpointsByPlatformApplication action to retrieve additional records that are available after the first page results. Nrr9"ListEndpointsByPlatformApplicationrr;)rrr>r/r#r#r$&list_endpoints_by_platform_applicationFsz4SNSConnection.list_endpoints_by_platform_applicationcCr)a The `DeletePlatformApplication` action deletes a platform application object for one of the supported push notification services, such as APNS and GCM. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type platform_application_arn: string :param platform_application_arn: PlatformApplicationArn of platform application object to delete. NrDeletePlatformApplicationrr;rr#r#r$delete_platform_applicationis  z)SNSConnection.delete_platform_applicationcCsXi}|dur ||d<|dur||d<|dur||d<|dur%|||d|jd|dS)am The `CreatePlatformEndpoint` creates an endpoint for a device and mobile app on one of the supported push notification services, such as GCM and APNS. `CreatePlatformEndpoint` requires the PlatformApplicationArn that is returned from `CreatePlatformApplication`. The EndpointArn that is returned when using `CreatePlatformEndpoint` can then be used by the `Publish` action to send a message to a mobile app or by the `Subscribe` action for subscription to a topic. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type platform_application_arn: string :param platform_application_arn: PlatformApplicationArn returned from CreatePlatformApplication is used to create a an endpoint. :type token: string :param token: Unique identifier created by the notification service for an app on a device. The specific name for Token will vary, depending on which notification service is being used. For example, when using APNS as the notification service, you need the device token. Alternatively, when using GCM or ADM, the device token equivalent is called the registration ID. :type custom_user_data: string :param custom_user_data: Arbitrary user data to associate with the endpoint. SNS does not use this data. The data must be in UTF-8 format and less than 2KB. :type attributes: map :param attributes: For a list of attributes, see `SetEndpointAttributes`_. NrrCustomUserDatarCreatePlatformEndpointrr)rrrcustom_user_datarr/r#r#r$create_platform_endpoint{s%z&SNSConnection.create_platform_endpointcCr)a  The `DeleteEndpoint` action, which is idempotent, deletes the endpoint from SNS. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type endpoint_arn: string :param endpoint_arn: EndpointArn of endpoint to delete. N EndpointArnDeleteEndpointrr;r endpoint_arnr/r#r#r$delete_endpoints zSNSConnection.delete_endpointcCr)a! The `SetEndpointAttributes` action sets the attributes for an endpoint for a device on one of the supported push notification services, such as GCM and APNS. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type endpoint_arn: string :param endpoint_arn: EndpointArn used for SetEndpointAttributes action. :type attributes: map :param attributes: A map of the endpoint attributes. Attributes in this map include the following: + `CustomUserData` -- arbitrary user data to associate with the endpoint. SNS does not use this data. The data must be in UTF-8 format and less than 2KB. + `Enabled` -- flag that enables/disables delivery to the endpoint. Message Processor will set this to false when a notification service indicates to SNS that the endpoint is invalid. Users can set it back to true, typically after updating Token. + `Token` -- device token, also referred to as a registration id, for an app and mobile device. This is returned from the notification service when an app and mobile device are registered with the notification service. NrrSetEndpointAttributesrr)rrrr/r#r#r$set_endpoint_attributessz%SNSConnection.set_endpoint_attributescCr)av The `GetEndpointAttributes` retrieves the endpoint attributes for a device on one of the supported push notification services, such as GCM and APNS. For more information, see `Using Amazon SNS Mobile Push Notifications`_. :type endpoint_arn: string :param endpoint_arn: EndpointArn for GetEndpointAttributes input. NrGetEndpointAttributesrr;rr#r#r$get_endpoint_attributess  z%SNSConnection.get_endpoint_attributesr\cCsd|d<|j||||d}|d}tj||jdkr$t|Stj d|j|j ftj d|| |j|j |)NJSON ContentType)rverbr r/ryz%s %sz%s) make_requestreaddecodebotologrstatusrrerrorreason ResponseError)rrr/r rresponsebodyr#r#r$r<s   zSNSConnection._make_request)NNTNNNNNrNNr NTN)N)NNNNNN)F)NNN)NN)NNNN)r r\)'__name__ __module__ __qualname____doc__rconfiggetrr APIVersionrr6r7r?rErMrUrWrZr]rsrxrrrrrrrrrrrrrrrr< __classcell__r#r#r!r$r sl       L5   0 0  # 1  &r) uuidrboto.connectionrboto.regioninfor boto.compatrrrr#r#r#r$s