-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2154 from devos50/subscribed_channels_endpoint
Implemented the subscribed channels endpoint
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Tribler/Test/Core/Modules/RestApi/test_channels_endpoints.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters