Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement feeds client

  • Loading branch information...
commit 1ddae5653d6b4abb0a61bcb6718d01b079ec7d7c 1 parent a17e22f
Paul Jones authored
35 simulator/streamserver.rb
@@ -108,6 +108,41 @@
108 108 success_result
109 109 end
110 110
  111 +get '/v1/feeds/list' do
  112 + require_auth
  113 +
  114 + headers 'Content-Type' => 'text/xml'
  115 + <<-EOX
  116 +<?xml version="1.0" encoding="UTF-8"?>
  117 +<opml version="2.0">
  118 + <head>
  119 + <title>Feeds for example.com</title>
  120 + </head>
  121 + <body>
  122 + <outline text="http://example.com/feed/1" type="atom" xmlUrl="http://example.com/feed/1" refreshRate="300"/>
  123 + <outline text="http://example.com/feed/2" type="atom" xmlUrl="http://example.com/feed/2" refreshRate="50"/>
  124 + </body>
  125 +</opml>
  126 +EOX
  127 +end
  128 +
  129 +get '/v1/feeds/register' do
  130 + require_parameter :url, :interval
  131 + require_auth
  132 +
  133 + success_result
  134 +end
  135 +
  136 +get '/v1/feeds/unregister' do
  137 + require_parameter :url
  138 + require_auth
  139 +
  140 + generate_error(400, 'incorrect_url') if params[:url] == 'foo'
  141 +
  142 + success_result
  143 +end
  144 +
  145 +
111 146 error(KeyNotFoundException) { generate_error(404, 'not_found') }
112 147 error(UnknownUserException) { generate_error(404, 'not_found') }
113 148 error UnknownSessionException do
2  streamserver/echo_client.py
@@ -90,6 +90,8 @@ def assert_status(self, response):
90 90 raise InvalidQueryException(res['errorMessage'])
91 91 elif res['errorCode'] == "timeout":
92 92 raise EchoTimeoutException()
  93 + elif res['errorCode'] == "incorrect_url":
  94 + raise IncorrectURLException()
93 95 else:
94 96 # TODO: More decoding!
95 97 raise EchoException(res['errorCode'])
3  streamserver/exceptions.py
@@ -30,3 +30,6 @@ class EchoTimeoutException(EchoException):
30 30
31 31 class InvalidQueryException(EchoException):
32 32 """The provided echo query was not valid"""
  33 +
  34 +class IncorrectURLException(EchoException):
  35 + """The URL provided was incorrect."""
23 streamserver/feeds_client.py
... ... @@ -1,15 +1,32 @@
1 1 from echo_client import EchoClient
  2 +from xml.dom.minidom import parseString
2 3
3 4 class FeedsClient(EchoClient):
4 5 """Client for managing the feeds polled by Echo"""
5 6 def list(self):
6 7 """Lists the feeds registered for this account."""
7   - return self.execute_query('feeds/list', {})
  8 + opml_str = self.execute_query('feeds/list', {})
  9 + opml = parseString(opml_str)
  10 +
  11 + def process_outline_element(el):
  12 + return (el.getAttribute('text'), el.getAttribute('refreshRate'))
  13 +
  14 + feeds = map(process_outline_element, self._find_xml_elements(opml, ["opml", "body", "outline"]))
  15 + return feeds
8 16
9 17 def register(self, url, interval):
10 18 """Registers the given url for polling with the specified interval (in seconds)"""
11   - pass
  19 + return self.execute_query('feeds/register', {'url':url, 'interval':interval})
12 20
13 21 def unregister(self, url):
14 22 """Unregisters the given url from polling"""
15   - pass
  23 + return self.execute_query('feeds/unregister', {'url':url})
  24 +
  25 + def _find_xml_elements(self, el, tags):
  26 + for child in el.childNodes:
  27 + if child.localName==tags[0]:
  28 + if len(tags) == 1:
  29 + yield child
  30 + else:
  31 + for child_child in self._find_xml_elements(child, tags[1:]):
  32 + yield child_child
29 tests/feeds_client_tests.py
... ... @@ -1,5 +1,30 @@
1 1 import unittest
2 2
  3 +from streamserver import *
  4 +import configuration_factory as cf
  5 +
3 6 class FeedsClientTestSuite(unittest.TestCase):
4   - def test_foo(self):
5   - pass
  7 + def setUp(self):
  8 + self.noauth_client = FeedsClient(auth = cf.no_auth, host = cf.test_host)
  9 + self.basic_client = FeedsClient(auth = cf.basic_auth, host = cf.test_host)
  10 + self.oauth_client = FeedsClient(auth = cf.oauth_auth, host = cf.test_host)
  11 +
  12 + def test_list_basic(self):
  13 + feeds = self.basic_client.list()
  14 + self.assertEquals([(u'http://example.com/feed/1', u'300'), (u'http://example.com/feed/2', u'50')], feeds)
  15 +
  16 + def test_register_basic(self):
  17 + self.basic_client.register('http://www.example.com/feed.xml', 5)
  18 +
  19 + def test_register_oauth(self):
  20 + self.oauth_client.register('http://www.example.com/feed.xml', 15)
  21 +
  22 + def test_unregister_basic(self):
  23 + self.basic_client.unregister('http://www.example.com/feed.xml')
  24 +
  25 + def test_unregister_bad_url(self):
  26 + with self.assertRaises(IncorrectURLException):
  27 + self.basic_client.unregister('foo')
  28 +
  29 + def test_unregister_oauth(self):
  30 + self.oauth_client.unregister('http://www.example.com/feed.xml')

0 comments on commit 1ddae56

Please sign in to comment.
Something went wrong with that request. Please try again.