Skip to content

Commit

Permalink
Merge pull request #2171 from devos50/remove_rss_feed_endpoint
Browse files Browse the repository at this point in the history
Implemented endpoint to remove a rss feed from your channel
  • Loading branch information
whirm committed May 13, 2016
2 parents 1c8304f + ffacf4a commit 29bd44d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 44 deletions.
65 changes: 44 additions & 21 deletions Tribler/Core/Modules/restapi/my_channel_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ def __init__(self, session):
self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)

@staticmethod
def return_404(request):
def return_404(request, message="your channel has not been created"):
"""
Returns a 404 response code if your channel has not been created.
"""
request.setResponseCode(http.NOT_FOUND)
return "your channel has not been created"
return json.dumps({"error": message})

def get_my_channel_object(self):
"""
Returns the Channel object associated with you channel that is used to manage rss feeds.
"""
my_channel_id = self.channel_db_handler.getMyChannelId()
return self.session.lm.channel_manager.get_my_channel(my_channel_id)


class MyChannelEndpoint(MyChannelBaseEndpoint):
Expand Down Expand Up @@ -97,13 +104,8 @@ class MyChannelRssFeedsEndpoint(MyChannelBaseEndpoint):
This class is responsible for handling requests regarding rss feeds in your channel.
"""

def get_my_channel_object(self):
"""
Returns the Channel object associated with you channel that is used to manage rss feeds.
"""
my_channel_id = self.channel_db_handler.getMyChannelId()
channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id)
return channel_obj
def getChild(self, path, request):
return MyChannelModifyRssFeedsEndpoint(self.session, path)

def render_GET(self, request):
"""
Expand All @@ -126,24 +128,45 @@ def render_GET(self, request):

return json.dumps({"rssfeeds": feeds_list})


class MyChannelModifyRssFeedsEndpoint(MyChannelBaseEndpoint):
"""
This class is responsible for methods that modify the list of RSS feed URLs (adding/removing feeds).
"""

def __init__(self, session, feed_url):
MyChannelBaseEndpoint.__init__(self, session)
self.feed_url = feed_url

def render_PUT(self, request):
"""
Add a RSS feed to your channel.
Example request:
{
"rss_feed_url": "http://rssprovider.com/feed.xml"
}
Add a RSS feed to your channel. Returns error 409 if the supplied RSS feed already exists.
Note that the rss feed url should be URL-encoded.
"""
request.setHeader('Content-Type', 'text/json')
channel_obj = self.get_my_channel_object()
if channel_obj is None:
return MyChannelBaseEndpoint.return_404(request)

parameters = http.parse_qs(request.content.read(), 1)
if 'rss_feed_url' not in parameters or len(parameters['rss_feed_url']) == 0:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "rss_feed_url parameter missing"})
if self.feed_url in channel_obj.get_rss_feed_url_list():
request.setResponseCode(http.CONFLICT)
return json.dumps({"error": "this rss feed already exists"})

channel_obj.create_rss_feed(parameters['rss_feed_url'][0])
request.setHeader('Content-Type', 'text/json')
channel_obj.create_rss_feed(self.feed_url)
return json.dumps({"added": True})

def render_DELETE(self, request):
"""
Delete a RSS feed from your channel. Returns error 404 if the RSS feed that is being removed does not exist.
Note that the rss feed url should be URL-encoded.
"""
request.setHeader('Content-Type', 'text/json')
channel_obj = self.get_my_channel_object()
if channel_obj is None:
return MyChannelBaseEndpoint.return_404(request)

if self.feed_url not in channel_obj.get_rss_feed_url_list():
return MyChannelBaseEndpoint.return_404(request, message="this url is not added to your RSS feeds")

channel_obj.remove_rss_feed(self.feed_url)
return json.dumps({"removed": True})
5 changes: 2 additions & 3 deletions Tribler/Test/Core/Modules/RestApi/base_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ def parse_body(self, body):

def parse_response(self, response):
self.assertEqual(response.code, self.expected_response_code)
if response.code == 200:
if response.code == 200 or response.code == 400:
return readBody(response)
else:
return succeed(None)
return succeed(None)

def do_request(self, endpoint, expected_code=200, expected_json=None, request_type='GET', post_data=''):
self.expected_response_code = expected_code
Expand Down
61 changes: 51 additions & 10 deletions Tribler/Test/Core/Modules/RestApi/test_my_channel_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,21 @@ def test_add_rss_feed_no_my_channel(self):
Testing whether the API returns a 404 if no channel has been created when adding a rss feed
"""
self.session.lm.channel_manager = ChannelManager(self.session)
return self.do_request('mychannel/rssfeeds', expected_code=404, request_type='PUT')
return self.do_request('mychannel/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml',
expected_code=404, request_type='PUT')

@deferred(timeout=10)
def test_add_rss_feed_no_parameter(self):
def test_add_rss_feed_conflict(self):
"""
Testing whether the API returns a 400 and error if the url parameter is not passed
Testing whether the API returns error 409 if a channel the rss feed already exists
"""
expected_json = {"error": "rss_feed_url parameter missing"}
self.create_fake_channel("my channel", "this is a short description")
return self.do_request('mychannel/rssfeeds', expected_code=400, expected_json=expected_json, request_type='PUT')
expected_json = {"error": "this rss feed already exists"}
my_channel_id = self.create_fake_channel("my channel", "this is a short description")
channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id)
channel_obj.create_rss_feed("http://rssfeed.com/rss.xml")

return self.do_request('mychannel/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml', expected_code=409,
expected_json=expected_json, request_type='PUT')

@deferred(timeout=10)
def test_add_rss_feed_with_channel(self):
Expand All @@ -148,10 +153,46 @@ def test_add_rss_feed_with_channel(self):
"""
def verify_rss_added(_):
channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id)
self.assertEqual(channel_obj.get_rss_feed_url_list(), ["http://fakerssprovider.com/feed.rss"])
self.assertEqual(channel_obj.get_rss_feed_url_list(), ["http://rssfeed.com/rss.xml"])

expected_json = {"added": True}
my_channel_id = self.create_fake_channel("my channel", "this is a short description")
post_data = {"rss_feed_url": "http://fakerssprovider.com/feed.rss"}
return self.do_request('mychannel/rssfeeds', expected_code=200, expected_json=expected_json,
request_type='PUT', post_data=post_data).addCallback(verify_rss_added)
return self.do_request('mychannel/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml', expected_code=200,
expected_json=expected_json, request_type='PUT')\
.addCallback(verify_rss_added)

@deferred(timeout=10)
def test_remove_rss_feed_no_channel(self):
"""
Testing whether the API returns a 404 if no channel has been created when adding a rss feed
"""
self.session.lm.channel_manager = ChannelManager(self.session)
return self.do_request('mychannel/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml',
expected_code=404, request_type='DELETE')

@deferred(timeout=10)
def test_remove_rss_feed_invalid_url(self):
"""
Testing whether the API returns a 404 and error if the url parameter does not exist in the existing feeds
"""
expected_json = {"error": "this url is not added to your RSS feeds"}
self.create_fake_channel("my channel", "this is a short description")
return self.do_request('mychannel/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml', expected_code=404,
expected_json=expected_json, request_type='DELETE')

@deferred(timeout=10)
def test_remove_rss_feed_with_channel(self):
"""
Testing whether the API returns a 200 if a channel has been created and when removing a rss feed
"""
def verify_rss_removed(_):
channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id)
self.assertEqual(channel_obj.get_rss_feed_url_list(), [])

expected_json = {"removed": True}
my_channel_id = self.create_fake_channel("my channel", "this is a short description")
channel_obj = self.session.lm.channel_manager.get_my_channel(my_channel_id)
channel_obj.create_rss_feed("http://rssfeed.com/rss.xml")

return self.do_request('mychannel/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml', expected_code=200,
expected_json=expected_json, request_type='DELETE').addCallback(verify_rss_removed)
17 changes: 7 additions & 10 deletions doc/Tribler REST API.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The API has been built using [Twisted Web](http://twistedmatrix.com/trac/wiki/Tw
Some requests require one or more parameters. These parameters are passed using the JSON format. An example of performing a request with parameters using the curl command line tool can be found below:

```
curl -X PUT -d "rss_feed_url=http://fakerssprovider.com/feed.rss" http://localhost:8085/mychannel/rssfeed
curl -X PUT http://localhost:8085/mychannel/rssfeeds/http%3A%2F%2Frssfeed.com%2Frss.xml
```

## Endpoints
Expand All @@ -30,7 +30,8 @@ curl -X PUT -d "rss_feed_url=http://fakerssprovider.com/feed.rss" http://localho
| GET /mychannel/overview | Get the name, description and identifier of your channel |
| GET /mychannel/torrents | Get a list of torrents in your channel |
| GET /mychannel/rssfeeds | Get a list of rss feeds used by your channel |
| PUT /mychannel/rssfeeds | Add a rss feed to your channel |
| PUT /mychannel/rssfeeds/{feedurl} | Add a rss feed to your channel |
| DELETE /mychannel/rssfeeds/{feedurl} | Remove a rss feed from your channel |

### Settings

Expand Down Expand Up @@ -158,17 +159,13 @@ Returns a list of rss feeds in your channel. Each rss feed items contains the UR
}
```

## `PUT /mychannel/rssfeed`
## `PUT /mychannel/rssfeed/{feedurl}`

Add a RSS feed to your channel.
Add a RSS feed to your channel. Returns error 409 (Conflict) if the supplied RSS feed already exists. Note that the rss feed url should be URL-encoded.

### Example request:
## `DELETE /mychannel/rssfeed/{feedurl}`

```json
{
"rss_feed_url": "http://rssprovider.com/feed.xml"
}
```
Delete a RSS feed from your channel. Returns error 404 if the RSS feed that is being removed does not exist. Note that the rss feed url should be URL-encoded.

## `GET /settings`

Expand Down

0 comments on commit 29bd44d

Please sign in to comment.