Skip to content

Commit

Permalink
Implemented endpoint to edit channel properties
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 committed Jul 15, 2016
1 parent fc593f2 commit e0c2751
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
58 changes: 51 additions & 7 deletions Tribler/Core/Modules/restapi/channels/my_channel_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import json
from twisted.web import http, resource
from Tribler.Core.simpledefs import NTFY_CHANNELCAST
from twisted.web import http
from Tribler.Core.Modules.restapi.channels.base_channels_endpoint import BaseChannelsEndpoint
from Tribler.Core.Modules.restapi.util import get_parameter


NO_CHANNEL_CREATED_RESPONSE_MSG = "your channel has not been created"


class MyChannelEndpoint(resource.Resource):
class MyChannelEndpoint(BaseChannelsEndpoint):
"""
This class is responsible for managing requests regarding your channel.
"""
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):
"""
Expand Down Expand Up @@ -51,3 +48,50 @@ def render_GET(self, request):

return json.dumps({'mychannel': {'identifier': my_channel[1].encode('hex'), 'name': my_channel[2],
'description': my_channel[3]}})

def render_POST(self, request):
"""
.. http:post:: /mychannel
Modify the name and/or the description of your channel.
This endpoint returns a 404 HTTP response if you have not created a channel (yet).
**Example request**:
.. sourcecode:: none
curl -X POST http://localhost:8085/mychannel
--data "name=My fancy playlist&description=This playlist contains some random movies"
**Example response**:
.. sourcecode:: javascript
{
"modified": True
}
:statuscode 404: if your channel has not been created (yet).
"""
my_channel_id = self.channel_db_handler.getMyChannelId()
if my_channel_id is None:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": NO_CHANNEL_CREATED_RESPONSE_MSG})

channel_community = self.get_community_for_channel_id(my_channel_id)
if channel_community is None:
return BaseChannelsEndpoint.return_404(request,
message="the community for the your channel cannot be found")

parameters = http.parse_qs(request.content.read(), 1)
my_channel = self.channel_db_handler.getChannel(my_channel_id)

changes = {}
if my_channel[2] != get_parameter(parameters, 'name'):
changes['name'] = get_parameter(parameters, 'name')
if my_channel[3] != get_parameter(parameters, 'description'):
changes['description'] = get_parameter(parameters, 'description')

channel_community.modifyChannel(changes)

return json.dumps({'modified': True})
9 changes: 9 additions & 0 deletions Tribler/Core/Modules/restapi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,12 @@ def convert_remote_torrent_to_json(torrent):
return {'id': torrent['torrent_id'], "infohash": torrent['infohash'].encode('hex'), "name": torrent_name,
'size': torrent['length'], 'category': torrent['category'], 'num_seeders': torrent['num_seeders'],
'num_leechers': torrent['num_leechers'], 'last_tracker_check': 0}


def get_parameter(parameters, name):
"""
Return a specific parameter with a name from a HTTP request (or None if that parameter is not available).
"""
if name not in parameters or len(parameters[name]) == 0:
return None
return parameters[name][0]

0 comments on commit e0c2751

Please sign in to comment.