Permalink
Browse files

Implement feeds client

  • Loading branch information...
1 parent a17e22f commit 1ddae5653d6b4abb0a61bcb6718d01b079ec7d7c @paulj committed Apr 23, 2012
View
@@ -108,6 +108,41 @@
success_result
end
+get '/v1/feeds/list' do
+ require_auth
+
+ headers 'Content-Type' => 'text/xml'
+ <<-EOX
+<?xml version="1.0" encoding="UTF-8"?>
+<opml version="2.0">
+ <head>
+ <title>Feeds for example.com</title>
+ </head>
+ <body>
+ <outline text="http://example.com/feed/1" type="atom" xmlUrl="http://example.com/feed/1" refreshRate="300"/>
+ <outline text="http://example.com/feed/2" type="atom" xmlUrl="http://example.com/feed/2" refreshRate="50"/>
+ </body>
+</opml>
+EOX
+end
+
+get '/v1/feeds/register' do
+ require_parameter :url, :interval
+ require_auth
+
+ success_result
+end
+
+get '/v1/feeds/unregister' do
+ require_parameter :url
+ require_auth
+
+ generate_error(400, 'incorrect_url') if params[:url] == 'foo'
+
+ success_result
+end
+
+
error(KeyNotFoundException) { generate_error(404, 'not_found') }
error(UnknownUserException) { generate_error(404, 'not_found') }
error UnknownSessionException do
@@ -90,6 +90,8 @@ def assert_status(self, response):
raise InvalidQueryException(res['errorMessage'])
elif res['errorCode'] == "timeout":
raise EchoTimeoutException()
+ elif res['errorCode'] == "incorrect_url":
+ raise IncorrectURLException()
else:
# TODO: More decoding!
raise EchoException(res['errorCode'])
@@ -30,3 +30,6 @@ class EchoTimeoutException(EchoException):
class InvalidQueryException(EchoException):
"""The provided echo query was not valid"""
+
+class IncorrectURLException(EchoException):
+ """The URL provided was incorrect."""
@@ -1,15 +1,32 @@
from echo_client import EchoClient
+from xml.dom.minidom import parseString
class FeedsClient(EchoClient):
"""Client for managing the feeds polled by Echo"""
def list(self):
"""Lists the feeds registered for this account."""
- return self.execute_query('feeds/list', {})
+ opml_str = self.execute_query('feeds/list', {})
+ opml = parseString(opml_str)
+
+ def process_outline_element(el):
+ return (el.getAttribute('text'), el.getAttribute('refreshRate'))
+
+ feeds = map(process_outline_element, self._find_xml_elements(opml, ["opml", "body", "outline"]))
+ return feeds
def register(self, url, interval):
"""Registers the given url for polling with the specified interval (in seconds)"""
- pass
+ return self.execute_query('feeds/register', {'url':url, 'interval':interval})
def unregister(self, url):
"""Unregisters the given url from polling"""
- pass
+ return self.execute_query('feeds/unregister', {'url':url})
+
+ def _find_xml_elements(self, el, tags):
+ for child in el.childNodes:
+ if child.localName==tags[0]:
+ if len(tags) == 1:
+ yield child
+ else:
+ for child_child in self._find_xml_elements(child, tags[1:]):
+ yield child_child
@@ -1,5 +1,30 @@
import unittest
+from streamserver import *
+import configuration_factory as cf
+
class FeedsClientTestSuite(unittest.TestCase):
- def test_foo(self):
- pass
+ def setUp(self):
+ self.noauth_client = FeedsClient(auth = cf.no_auth, host = cf.test_host)
+ self.basic_client = FeedsClient(auth = cf.basic_auth, host = cf.test_host)
+ self.oauth_client = FeedsClient(auth = cf.oauth_auth, host = cf.test_host)
+
+ def test_list_basic(self):
+ feeds = self.basic_client.list()
+ self.assertEquals([(u'http://example.com/feed/1', u'300'), (u'http://example.com/feed/2', u'50')], feeds)
+
+ def test_register_basic(self):
+ self.basic_client.register('http://www.example.com/feed.xml', 5)
+
+ def test_register_oauth(self):
+ self.oauth_client.register('http://www.example.com/feed.xml', 15)
+
+ def test_unregister_basic(self):
+ self.basic_client.unregister('http://www.example.com/feed.xml')
+
+ def test_unregister_bad_url(self):
+ with self.assertRaises(IncorrectURLException):
+ self.basic_client.unregister('foo')
+
+ def test_unregister_oauth(self):
+ self.oauth_client.unregister('http://www.example.com/feed.xml')

0 comments on commit 1ddae56

Please sign in to comment.