Skip to content

Commit

Permalink
Merge pull request #2159 from devos50/all_channels_endpoint
Browse files Browse the repository at this point in the history
Implemented endpoint to fetch all channels in Tribler
  • Loading branch information
whirm committed May 4, 2016
2 parents 402a4db + 5cf02e3 commit 47c9187
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 13 deletions.
47 changes: 34 additions & 13 deletions Tribler/Core/Modules/restapi/channels_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json

from twisted.web import resource
from twisted.web.resource import NoResource

from Tribler.Core.simpledefs import NTFY_CHANNELCAST

Expand All @@ -12,6 +11,11 @@ class BaseChannelsEndpoint(resource.Resource):
All endpoints that are using the database, should derive from this class.
"""

def __init__(self, session):
resource.Resource.__init__(self)
self.session = session
self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)

def convert_db_channel_to_json(self, channel):
return {"id": channel[0], "dispersy_cid": channel[1].encode('hex'), "name": channel[2], "description": channel[3],
"votes": channel[5], "torrents": channel[4], "spam": channel[6], "modified": channel[8],
Expand All @@ -24,10 +28,9 @@ class ChannelsEndpoint(BaseChannelsEndpoint):
"""

def __init__(self, session):
resource.Resource.__init__(self)
self.session = session
BaseChannelsEndpoint.__init__(self, session)

child_handler_dict = {"subscribed": ChannelsSubscribedEndpoint}
child_handler_dict = {"subscribed": ChannelsSubscribedEndpoint, "discovered": ChannelsDiscoveredEndpoint}
for path, child_cls in child_handler_dict.iteritems():
self.putChild(path, child_cls(self.session))

Expand All @@ -52,15 +55,33 @@ class ChannelsSubscribedEndpoint(BaseChannelsEndpoint):
}
"""

def __init__(self, session):
resource.Resource.__init__(self)
self.session = session
self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)

def render_GET(self, request):
subscribed_channels_db = self.channel_db_handler.getMySubscribedChannels(includeDispsersy=True)
results_json = []
for channel in subscribed_channels_db:
results_json.append(self.convert_db_channel_to_json(channel))

results_json = [self.convert_db_channel_to_json(channel) for channel in subscribed_channels_db]
return json.dumps({"subscribed": results_json})


class ChannelsDiscoveredEndpoint(BaseChannelsEndpoint):
"""
A GET request to this endpoint returns all channels discovered in Tribler.
Example GET response:
{
"channels": [{
"id": 3,
"dispersy_cid": "da69aaad39ccf468aba2ab9177d5f8d8160135e6",
"name": "My fancy channel",
"description": "A description of this fancy channel",
"subscribed": False,
"votes": 23,
"torrents": 3,
"spam": 5,
"modified": 14598395,
}, ...]
}
"""

def render_GET(self, request):
all_channels_db = self.channel_db_handler.getAllChannels()
results_json = [self.convert_db_channel_to_json(channel) for channel in all_channels_db]
return json.dumps({"channels": results_json})
28 changes: 28 additions & 0 deletions Tribler/Test/Core/Modules/RestApi/test_channels_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import time

from Tribler.Core.Utilities.twisted_thread import deferred
Expand Down Expand Up @@ -49,3 +50,30 @@ def test_get_subscribed_channels_one_subscription(self):
expected_json[u'subscribed'][0][u'description'])
self.vote_for_channel(cid, expected_json[u'subscribed'][0][u'modified'])
return self.do_request('channels/subscribed', expected_code=200, expected_json=expected_json)

@deferred(timeout=10)
def test_get_discovered_channels_no_channels(self):
"""
Testing whether the API returns no channels when fetching discovered channels
and there are no channels in the database
"""
expected_json = {u'channels': []}
return self.do_request('channels/discovered', expected_code=200, expected_json=expected_json)

def verify_channels(self, channels):
channels_json = json.loads(channels)
self.assertEqual(len(channels_json['channels']), 10)
for i in range(len(channels_json['channels'])):
self.assertEqual(channels_json['channels'][i]['name'], 'Test channel %d' % i)
self.assertEqual(channels_json['channels'][i]['description'], 'Test description %d' % i)

@deferred(timeout=10)
def test_get_discovered_channels(self):
"""
Testing whether the API returns inserted channels when fetching discovered channels
"""
self.should_check_equality = False
for i in range(0, 10):
self.insert_channel_in_db('rand%d' % i, 42 + i, 'Test channel %d' % i, 'Test description %d' % i)

return self.do_request('channels/discovered', expected_code=200).addCallback(self.verify_channels)
25 changes: 25 additions & 0 deletions doc/Tribler REST API.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The API has been built using [Twisted Web](http://twistedmatrix.com/trac/wiki/Tw

| Endpoint | Description |
| ---- | --------------- |
| GET /channels/discovered | Get all discovered channels in Tribler |
| GET /channels/subscribed | Get the channels you are subscribed to |

### My Channel
Expand All @@ -34,10 +35,34 @@ The API has been built using [Twisted Web](http://twistedmatrix.com/trac/wiki/Tw
| ---- | --------------- |
| GET /variables | Returns runtime-defined variables used by the current Tribler session |

## `GET /channels/discovered`

Returns all discovered channels in Tribler.

### Example response

```json
{
"channels": [{
"id": 3,
"dispersy_cid": "da69aaad39ccf468aba2ab9177d5f8d8160135e6",
"name": "My fancy channel",
"description": "A description of this fancy channel",
"subscribed": False,
"votes": 23,
"torrents": 3,
"spam": 5,
"modified": 14598395,
}, ...]
}
```

## `GET /channels/subscribed`

Returns all the channels you are subscribed to.

### Example response

```json
{
"subscribed": [{
Expand Down

0 comments on commit 47c9187

Please sign in to comment.