Skip to content

Commit

Permalink
Merge pull request #2154 from devos50/subscribed_channels_endpoint
Browse files Browse the repository at this point in the history
Implemented the subscribed channels endpoint
  • Loading branch information
whirm committed May 3, 2016
2 parents b96c597 + ad98252 commit 5830961
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Tribler/Core/Modules/restapi/channels_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import json

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

from Tribler.Core.simpledefs import NTFY_CHANNELCAST


class BaseChannelsEndpoint(resource.Resource):
"""
This class contains some utility methods to work with raw channels from the database.
All endpoints that are using the database, should derive from this class.
"""

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],
"subscribed": (channel[7] == 2)}


class ChannelsEndpoint(BaseChannelsEndpoint):
"""
This endpoint is responsible for handing all requests regarding channels in Tribler.
"""

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

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


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

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))

return json.dumps({"subscribed": results_json})
5 changes: 5 additions & 0 deletions Tribler/Core/Modules/restapi/root_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from twisted.web import resource

from Tribler.Core.Modules.restapi.channels_endpoint import ChannelsEndpoint
from Tribler.Core.Modules.restapi.my_channel_endpoint import MyChannelEndpoint
from Tribler.Core.Modules.restapi.settings_endpoint import SettingsEndpoint
from Tribler.Core.Modules.restapi.variables_endpoint import VariablesEndpoint
Expand All @@ -14,6 +16,9 @@ def __init__(self, session):
resource.Resource.__init__(self)
self.session = session

self.channels_endpoint = ChannelsEndpoint(self.session)
self.putChild("channels", self.channels_endpoint)

self.my_channel_endpoint = MyChannelEndpoint(self.session)
self.putChild("mychannel", self.my_channel_endpoint)

Expand Down
51 changes: 51 additions & 0 deletions Tribler/Test/Core/Modules/RestApi/test_channels_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import time

from Tribler.Core.Utilities.twisted_thread import deferred
from Tribler.Core.simpledefs import NTFY_CHANNELCAST, NTFY_VOTECAST
from Tribler.Test.Core.Modules.RestApi.base_api_test import AbstractApiTest


class TestChannelsEndpoint(AbstractApiTest):

def setUp(self, autoload_discovery=True):
super(TestChannelsEndpoint, self).setUp(autoload_discovery)
self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)
self.votecast_db_handler = self.session.open_dbhandler(NTFY_VOTECAST)
self.channel_db_handler._get_my_dispersy_cid = lambda: "myfakedispersyid"

def insert_channel_in_db(self, dispersy_cid, peer_id, name, description):
return self.channel_db_handler.on_channel_from_dispersy(dispersy_cid, peer_id, name, description)

def vote_for_channel(self, cid, vote_time):
self.votecast_db_handler.on_votes_from_dispersy([[cid, None, 'random', 2, vote_time]])

@deferred(timeout=10)
def test_channels_unknown_endpoint(self):
"""
Testing whether the API returns an error if an unknown endpoint is queried
"""
self.should_check_equality = False
return self.do_request('channels/thisendpointdoesnotexist123', expected_code=404)

@deferred(timeout=10)
def test_get_subscribed_channels_no_subscriptions(self):
"""
Testing whether the API returns no channels when you have not subscribed to any channel
"""
expected_json = {"subscribed": []}
return self.do_request('channels/subscribed', expected_code=200, expected_json=expected_json)

@deferred(timeout=10)
def test_get_subscribed_channels_one_subscription(self):
"""
Testing whether the API returns the right channel when subscribed to one channel
"""
expected_json = {u'subscribed': [{u'description': u'This is a description', u'id': 1,
u'dispersy_cid': 'rand'.encode('hex'), u'modified': int(time.time()),
u'name': u'Test Channel', u'spam': 0,
u'subscribed': True, u'torrents': 0, u'votes': 0}]}

cid = self.insert_channel_in_db('rand', 42, expected_json[u'subscribed'][0][u'name'],
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)
26 changes: 26 additions & 0 deletions doc/Tribler REST API.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ The API has been built using [Twisted Web](http://twistedmatrix.com/trac/wiki/Tw

## Endpoints

### Channels

| Endpoint | Description |
| ---- | --------------- |
| GET /channels/subscribed | Get the channels you are subscribed to |

### My Channel

| Endpoint | Description |
Expand All @@ -28,6 +34,26 @@ 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/subscribed`

Returns all the channels you are subscribed to.

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

## `GET /mychannel/overview`

Returns an overview of the channel of the user. This includes the name, description and identifier of the channel.
Expand Down

0 comments on commit 5830961

Please sign in to comment.