def _request(method, data=None): """Make the raw request to the Prowl API.""" # Catch the errors and treat them just like the normal response. try: res = urlopen(API_URL_BASE + method, urlencode(data) if data else None) except HTTPError as res: pass xml = XML(res.read()) if xml.tag != 'prowl': raise Error('malformed response: unexpected tag %r' % xml.tag) children = xml.getchildren() if len(children) != 1: raise Error('malformed response: too many children') node = children[0] status, data, text = node.tag, node.attrib, node.text if status not in ('success', 'error'): raise Error('malformed response: unknown status %r' % node.tag) if 'code' not in node.attrib: raise Error('malformed response: no response code') if status == 'error' and not text: raise Error('malformed response: no error message with code %d' % data['code']) data = dict((k, int(v)) for k, v in data.items()) _last_meta_data.update(data) return status, data, text
def getNotifications(self, rurl): """ Get a list of L{Notification} objects for the specified notification collection. @param rurl: a user's notification collection URL @type rurl: L{URL} """ assert(isinstance(rurl, URL)) # List all children of the notification collection results = self.getPropertiesOnHierarchy(rurl, (davxml.getcontenttype,)) items = results.keys() items.sort() notifications = [] for path in items: path = urllib.unquote(path) nurl = URL(url=path) if rurl == nurl: continue props = results[path] if props.get(davxml.getcontenttype, "none").split(";")[0] in ("text/xml", "application/xml"): data, _ignore_etag = self.readData(URL(url=path)) node = XML(data) if node.tag == str(csxml.notification): for child in node.getchildren(): if child.tag == str(csxml.invite_notification): notifications.append(InviteNotification().parseFromNotification(nurl, child)) elif child.tag == str(csxml.invite_reply): notifications.append(InviteReply().parseFromNotification(nurl, child)) return notifications
def sendNotification(key, message, priority=None, url=None, app=None, event=None, providerkey=None): API_URL_BASE = 'https://api.prowlapp.com/publicapi/' DEFAULT_PRIORITY = 0 DEFAULT_APP = 'SSHMonitor' DEFAULT_EVENT = 'default' data = { 'apikey': key if isinstance(key, basestring) else ','.join(key), 'priority': priority or DEFAULT_PRIORITY, 'application': app or DEFAULT_APP, 'event': event or DEFAULT_EVENT, 'description': message } if url is not None: data['url'] = url if providerkey is not None: data['providerkey'] = providerkey method = 'add' """Make the raw request to the Prowl API.""" # Catch the errors and treat them just like the normal response. try: res = urlopen(API_URL_BASE + method, urlencode(data) if data else None) except HTTPError as res: pass xml = XML(res.read()) if xml.tag != 'prowl': raise Error('malformed response: unexpected tag %r' % xml.tag) children = xml.getchildren() if len(children) != 1: raise Error('malformed response: too many children') node = children[0] status, data, text = node.tag, node.attrib, node.text # Unknown status if status not in ('success', 'error'): raise Error('malformed response: unknown status %r' % node.tag) # No response code if 'code' not in node.attrib: raise Error('malformed response: no response code') # No error message with code if status == 'error' and not text: raise Error('malformed response: no error message with code %d' % data['code']) data = dict((k, int(v)) for k, v in data.items()) _last_meta_data.update(data)
def getRequestFromXml(self,body): tree = XML(body) if 'request' != tree.tag: raise InputValidationException() r = record() for tag in tree.getchildren(): if tag.tag in ['id','mothername']: setattr(r,tag.tag,tag.text) if None is getattr(r,'id',None): raise InputValidationException() if None is getattr(r,'mothername',None): raise InputValidationException() return r
def layers(self): '''get a dict of layer->href''' url = self.url + 'layers.xml' headers, response = self.http.request(url, 'GET') if headers.status != 200: raise Exception('listing failed - %s, %s' % (headers,response)) # try to resolve layer if already configured dom = XML(response) layers = [] for layer in dom.getchildren(): els = layer.getchildren() layers.append(self.layer(els[0].text)) return layers
def layers(self): '''get a dict of layer->href''' url = self.url + 'layers.xml' headers, response = self.http.request(url, 'GET') if headers.status != 200: raise Exception('listing failed - %s, %s' % (headers, response)) # try to resolve layer if already configured dom = XML(response) layers = [] for layer in dom.getchildren(): els = list(layer) #.getchildren() layers.append(self.layer(els[0].text)) return layers
def to_dict(xml): try: tree = XML(xml) tables = tree.getchildren() L = [] for table in tables: D = {} for i in table.keys(): D[i] = table.get(i) D['text'] = table.text # for i in table.getchildren(): # D.update({i.tag.lower():i.text}) L.append(D) return L except: return []
def _list_gwc_layers(client=None): '''get a dict of layer->href''' if not client: client = _gwc_client() url = settings.INTERNAL_GEOSERVER_BASE_URL + 'gwc/rest/layers/' headers, response = client.request(url, 'GET') if headers.status != 200: raise Exception('listing failed - %s, %s' % (headers,response)) # try to resolve layer if already configured dom = XML(response) layers = {} for layer in dom.getchildren(): els = layer.getchildren() layers[els[0].text] = els[1].get('href') return layers
def _list_gwc_layers(client=None): '''get a dict of layer->href''' if not client: client = _gwc_client() url = settings.INTERNAL_GEOSERVER_BASE_URL + 'gwc/rest/layers/' headers, response = client.request(url, 'GET') if headers.status != 200: raise Exception('listing failed - %s, %s' % (headers, response)) # try to resolve layer if already configured dom = XML(response) layers = {} for layer in dom.getchildren(): els = layer.getchildren() layers[els[0].text] = els[1].get('href') return layers
def getNotifications(self, rurl): """ Get a list of L{Notification} objects for the specified notification collection. @param rurl: a user's notification collection URL @type rurl: L{URL} """ assert (isinstance(rurl, URL)) # List all children of the notification collection results = self.getPropertiesOnHierarchy(rurl, (davxml.getcontenttype, )) items = results.keys() items.sort() notifications = [] for path in items: path = urllib.unquote(path) nurl = URL(url=path) if rurl == nurl: continue props = results[path] if props.get(davxml.getcontenttype, "none").split(";")[0] in ("text/xml", "application/xml"): data, _ignore_etag = self.readData(URL(url=path)) node = XML(data) if node.tag == str(csxml.notification): for child in node.getchildren(): if child.tag == str(csxml.invite_notification): notifications.append( InviteNotification().parseFromNotification( nurl, child)) elif child.tag == str(csxml.invite_reply): notifications.append( InviteReply().parseFromNotification( nurl, child)) return notifications