Commits

Jeffrey Goettsch committed 8f4ed21

nma.Client now parses the XML responses it receives. nma.Client.verify and nma.Client.notify now return True or False depending on if the API call was successful or not.

Comments (0)

Files changed (1)

pushnotify/nma.py

 
 import urllib
 import urllib2
+try:
+    from xml.etree import cElementTree
+    ElementTree = cElementTree
+except ImportError:
+    from xml.etree import ElementTree
 
 
 PUBLIC_API_URL = u'https://www.notifymyandroid.com/publicapi'
         """
 
         self._browser = urllib2.build_opener(urllib2.HTTPSHandler())
+        self._last_type = None
+        self._last_code = None
+        self._last_message = None
+        self._last_remaining = None
+        self._last_resettimer = None
 
         self.apikeys = [] if apikeys is None else apikeys
         self.developerkey = developerkey
 
         return response
 
+    def _parse_response(self, xmlresp):
+
+        root = ElementTree.fromstring(xmlresp)
+
+        self._last_type = root[0].tag.lower()
+        self._last_code = root[0].attrib['code']
+
+        if self._last_type == 'success':
+            self._last_message = None
+            self._last_remaining = root[0].attrib['remaining']
+            self._last_resettimer = root[0].attrib['resettimer']
+        elif self._last_type == 'error':
+            self._last_message = root[0].text
+            self._last_remaining = None
+            self._last_resettimer = None
+        else:
+            pass
+            # TODO: throw an UnrecognizedResponse exception or something
+
+        return root
+
     def _post(self, url, data):
 
         request = urllib2.Request(url, data)
                     used while displaying the notification.
                 (default: None)
 
+        Returns:
+            A boolean containing True of the notifications were sent
+            successfully, and False if they were not. For multiple API
+            keys, only return False if they all failed.
+
         """
 
         data = {'apikey': ','.join(self.apikeys),
 
         data = urllib.urlencode(data)
 
-        return self._post(NOTIFY_URL, data)
+        response = self._post(NOTIFY_URL, data)
+        self._parse_response(response)
+
+        return self._last_code == '200'
 
     def verify(self, apikey):
         """Verify an API key.
             urllib2.URLError
 
         Returns:
-            A string containing the XML from the verify call.
+            A boolean containing True if the API key is valid, and False
+            if it is not.
 
         """
 
         querystring = urllib.urlencode({'apikey': apikey})
         url = '?'.join([VERIFY_URL, querystring])
 
-        return self._get(url)
+        response = self._get(url)
+        self._parse_response(response)
+
+        return self._last_code == '200'
 
 if __name__ == '__main__':
     pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.