Skip to content

Commit

Permalink
Implemented endpoint to get autocompletion terms when searching
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 committed Jun 24, 2016
1 parent e0b9d4f commit 925e8b9
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Tribler/Core/Modules/restapi/search_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def __init__(self, session):
self.torrent_db_handler = self.session.open_dbhandler(NTFY_TORRENTS)
self._logger = logging.getLogger(self.__class__.__name__)

self.putChild("completions", SearchCompletionsEndpoint(session))

def render_GET(self, request):
"""
.. http:get:: /search?q=(string:query)
Expand Down Expand Up @@ -83,3 +85,44 @@ def render_GET(self, request):
self._logger.error(exc)

return json.dumps({"queried": True})


class SearchCompletionsEndpoint(resource.Resource):
"""
This class is responsible for managing requests regarding the search completions terms of a query.
"""

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

def render_GET(self, request):
"""
.. http:get:: /search/completions?q=(string:query)
A GET request to this endpoint will return autocompletion suggestions for the given query. For instance,
when searching for "pioneer", this endpoint might return "pioneer one" if that torrent is present in the
local database. This endpoint can be used to suggest terms to users while they type their search query.
**Example request**:
.. sourcecode:: none
curl -X GET http://localhost:8085/search/completions?q=pioneer
**Example response**:
.. sourcecode:: javascript
{
"completions": ["pioneer one", "pioneer movie"]
}
"""
if 'q' not in request.args:
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": "query parameter missing"})

keywords = unicode(request.args['q'][0]).lower()
results = self.torrent_db_handler.getAutoCompleteTerms(keywords, max_terms=5)
return json.dumps({"completions": results})

0 comments on commit 925e8b9

Please sign in to comment.