-
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 #2150 from devos50/variables_endpoint
Implemented variables endpoint
- Loading branch information
Showing
4 changed files
with
75 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
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,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
13
Tribler/Test/Core/Modules/RestApi/test_variables_endpoint.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,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) |
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