Skip to content

Commit

Permalink
Merge pull request #2150 from devos50/variables_endpoint
Browse files Browse the repository at this point in the history
Implemented variables endpoint
  • Loading branch information
whirm committed Apr 29, 2016
2 parents ea4d2cd + 7256c14 commit a06eb85
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Tribler/Core/Modules/restapi/root_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from twisted.web import resource
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


class RootEndpoint(resource.Resource):
Expand All @@ -18,3 +19,6 @@ def __init__(self, session):

self.settings_endpoint = SettingsEndpoint(self.session)
self.putChild("settings", self.settings_endpoint)

self.variables_endpoint = VariablesEndpoint(self.session)
self.putChild("variables", self.variables_endpoint)
33 changes: 33 additions & 0 deletions Tribler/Core/Modules/restapi/variables_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json

from twisted.web import resource


class VariablesEndpoint(resource.Resource):
"""
This endpoint is responsible for handing all requests regarding runtime-defined variables in Tribler such as ports.
A GET request to this endpoint returns all the runtime-defined variables in Tribler.
Example GET response:
{
"variables": {
"ports": {
"video~port": 1234,
"tunnel_community~socks5_listen_ports~1": 1235,
...
},
...
}
}
"""

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

def render_GET(self, request):
"""
Returns the runtime-defined variables in Tribler in a JSON dictionary.
"""
return json.dumps({"variables": {"ports": self.session.selected_ports}})
13 changes: 13 additions & 0 deletions Tribler/Test/Core/Modules/RestApi/test_variables_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from Tribler.Core.Utilities.twisted_thread import deferred
from Tribler.Test.Core.Modules.RestApi.base_api_test import AbstractApiTest


class TestVariablesEndpoint(AbstractApiTest):

@deferred(timeout=10)
def test_get_variables(self):
"""
Testing whether the API returns a correct variables dictionary when the variables are requested
"""
expected_json = {"variables": {"ports": self.session.selected_ports}}
return self.do_request('variables', expected_code=200, expected_json=expected_json)
25 changes: 25 additions & 0 deletions doc/Tribler REST API.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ The API has been built using [Twisted Web](http://twistedmatrix.com/trac/wiki/Tw
| ---- | --------------- |
| GET /settings | Get settings used by the current Tribler session |

### Variables

| Endpoint | Description |
| ---- | --------------- |
| GET /variables | Returns runtime-defined variables used by the current Tribler session |

## `GET /mychannel/overview`

Returns an overview of the channel of the user. This includes the name, description and identifier of the channel.
Expand Down Expand Up @@ -73,3 +79,22 @@ Returns a dictionary with the settings that the current Tribler session is using
}
}
```

## `GET /variables`

Returns a dictionary with the runtime-defined variables that the current Tribler session is using.

### Example response

```
{
"variables": {
"ports": {
"video~port": 1234,
"tunnel_community~socks5_listen_ports~1": 1235,
...
},
...
}
}
```

0 comments on commit a06eb85

Please sign in to comment.