def retrieve_apikey(self, providerkey=None, token=None):
        """
        Get an API key from a registration token retrieved in retrieve/token.
        The user must have approved your request first, or you will get an
        error response.

        The parameters are :
        - providerkey (required) : your provider API key.
        - token (required): the token returned from retrieve_token.

        This returns a dictionary such as:
        {'apikey': u'16b776682332cf11102b67d6db215821f2c233a3',
         'code': u'200',
         'remaining': u'999',
         'resetdate': u'1299535575'}
        """

        h = Https(API_DOMAIN)

        data = {'apikey': self.apikey}

        if providerkey is not None:
            data['providerkey'] = providerkey
        else:
            raise Exception("Provider Key is required for retrieving API key")

        if token is not None:
            data['token'] = token
        else:
            raise Exception("Token is required for retrieving API key.\
                             Call retrieve_token to request it.")

        h.request("GET",
                  "/publicapi/retrieve/apikey?" + urlencode(data),
                  headers=self.headers)

        request = h.getresponse()
        request_status = request.status

        if request_status == 200:
            dom = minidom.parseString(request.read())
            code = dom.getElementsByTagName('prowl')[0].\
                            getElementsByTagName('success')[0].\
                            getAttribute('code')
            remaining = dom.getElementsByTagName('prowl')[0].\
                            getElementsByTagName('success')[0].\
                            getAttribute('remaining')
            resetdate = dom.getElementsByTagName('prowl')[0].\
                            getElementsByTagName('success')[0].\
                            getAttribute('resetdate')
            users_api_key = dom.getElementsByTagName('prowl')[0].\
                                getElementsByTagName('retrieve')[0].\
                                getAttribute('apikey')
            return dict(apikey=users_api_key,
                        code=code,
                        remaining=remaining,
                        resetdate=resetdate)
        else:
            self._relay_error(request_status)
    def post(self,
             application=None,
             event=None,
             description=None,
             priority=0,
             providerkey=None,
             url=None):
        """
        Post a notification..

        You must provide either event or description or both.
        The parameters are :
        - application ; The name of your application or the application
          generating the event.
        - providerkey (optional) : your provider API key.
          Only necessary if you have been whitelisted.
        - priority (optional) : default value of 0 if not provided.
          An integer value ranging [-2, 2] representing:
             -2. Very Low
             -1. Moderate
              0. Normal
              1. High
              2. Emergency (note : emergency priority messages may bypass
                            quiet hours according to the user's settings)
        - event : the name of the event or subject of the notification.
        - description : a description of the event, generally terse.
        - url (optional) : The URL which should be attached to the
          notification.
        """

        # Create the http object
        h = Https(API_DOMAIN)

        # Perform the request and get the response headers and content
        data = {
            'apikey': self.apikey,
            'application': application,
            'event': event,
            'description': description,
            'priority': priority
        }

        if providerkey is not None:
            data['providerkey'] = providerkey

        if url is not None:
            data['url'] = url[0:512]  # API limits to 512 characters

        h.request("POST",
                  "/publicapi/add",
                  headers=self.headers,
                  body=urlencode(data))
        response = h.getresponse()
        request_status = response.status

        if request_status == 200:
            return True
        else:
            self._relay_error(request_status, response.reason)
    def retrieve_token(self, providerkey=None):
        """
        Get a registration token for use in retrieve/apikey
        and the associated URL for the user to approve the request.

        The parameters are :
        - providerkey (required) : your provider API key.

        This returns a dictionary such as:
        {'code': u'0',
         'remaining': u'999',
         'resetdate': u'1299535575',
         'token': u'60fd568423e3cd337b45172be91cabe46b94c200',
         'url': u'https://www.prowlapp.com/retrieve.php?token=60fd5684'}
        """

        h = Https(API_DOMAIN)

        data = {'apikey': self.apikey}

        if providerkey is not None:
            data['providerkey'] = providerkey

        h.request("GET",
                  "/publicapi/retrieve/token?" + urlencode(data),
                  headers=self.headers)

        request = h.getresponse()
        request_status = request.status

        if request_status == 200:
            dom = minidom.parseString(request.read())
            code = dom.getElementsByTagName('prowl')[0].\
                            getElementsByTagName('success')[0].\
                            getAttribute('code')
            remaining = dom.getElementsByTagName('prowl')[0].\
                            getElementsByTagName('success')[0].\
                            getAttribute('remaining')
            resetdate = dom.getElementsByTagName('prowl')[0].\
                            getElementsByTagName('success')[0].\
                            getAttribute('resetdate')
            token = dom.getElementsByTagName('prowl')[0].\
                        getElementsByTagName('retrieve')[0].\
                        getAttribute('token')
            url = dom.getElementsByTagName('prowl')[0].\
                      getElementsByTagName('retrieve')[0].\
                      getAttribute('url')
            return dict(token=token,
                        url=url,
                        code=code,
                        remaining=remaining,
                        resetdate=resetdate)
        else:
            self._relay_error(request_status)
Beispiel #4
0
    def verify_key(self, providerkey=None):
        """
        Verify if the API key is valid.
         
        The parameters are :
        - providerkey (optional) : your provider API key.
          Only necessary if you have been whitelisted.
        """
        h = Https(API_DOMAIN)

        data = {'apikey': self.apikey}

        if providerkey is not None:
            data['providerkey'] = providerkey

        h.request("GET",
                  "/publicapi/verify?" + urlencode(data),
                  headers=self.headers)

        request_status = h.getresponse().status

        if request_status != 200:
            raise Exception("Invalid API Key %s" % self.apikey)