Repository: rotki/rotki

Issue:
Implement a ping endpoint in the back-end API.
## Abstract

Implement a `ping` endpoint in the back-end API. Use a valid response as the trigger to allow a user to log in.

## Motivation

Currently there is no ideal "minimum path" to know that the backend is up and fully functioning during application initiation. In the front-end we use the `/version` endpoint but that endpoint looks up releases on Github in the background which could either be slow or fail so it's not ideal as a way to establish that the backend API is up and running.

## Specification

A very simple & fast API endpoint with little room for error will be useful in cases where a UI client will want to do any of the following:
* Establish when the API is up and running (i.e. when a client loads, from which point on the user can try to log in)
* Allow for easy way to monitor back-end API status

TODO: Details of specification (files to be edited etc.)



Discussion:
@isidorosp Thanks a lot! 

> which could either be slow or fail

Even if it fails the endpoint will still return, but indeed it can be slow since it depends on an external RPC call over a network.

> TODO: Details of specification (files to be edited etc.)

I can help with that. It's everything that's required to create a new endpoint. It should just always return true. You can either wait for me to spec it out or try to see from older PRs or from the code what's required.

If you want to impement it that is. It should be a nice and easy issue to learn new parts of the code.
> I can help with that. It's everything that's required to create a new endpoint. It should just always return true. You can either wait for me to spec it out or try to see from older PRs or from the code what's required.

Is https://github.com/rotki/rotki/commit/e8e53a398d3ffd4e3b9368cea067e2f65845efb6 a good place to start from in terms of an example on this?
@isidorosp yes I would say so!

Resolving patch:
diff --git a/rotkehlchen/api/rest.py b/rotkehlchen/api/rest.py
--- a/rotkehlchen/api/rest.py
+++ b/rotkehlchen/api/rest.py
@@ -240,6 +240,7 @@ def stop(self) -> None:
         self.stop_event.set()
 
     # - Public functions exposed via the rest api
+
     @require_loggedin_user()
     def set_settings(self, settings: ModifiableDBSettings) -> Response:
         success, message = self.rotkehlchen.set_settings(settings)
@@ -1317,6 +1318,10 @@ def version_check() -> Response:
         result = _wrap_in_ok_result(check_if_version_up_to_date())
         return api_response(process_result(result), status_code=HTTPStatus.OK)
 
+    @staticmethod
+    def ping() -> Response:
+        return api_response(_wrap_in_ok_result(True), status_code=HTTPStatus.OK)
+
     @require_loggedin_user()
     def import_data(
             self,
diff --git a/rotkehlchen/api/server.py b/rotkehlchen/api/server.py
--- a/rotkehlchen/api/server.py
+++ b/rotkehlchen/api/server.py
@@ -46,6 +46,7 @@
     UsersByNameResource,
     UsersResource,
     VersionResource,
+    PingResource,
     create_blueprint,
 )
 from rotkehlchen.logging import RotkehlchenLogsAdapter
@@ -102,6 +103,7 @@
     ('/assets/all', AllAssetsResource),
     ('/assets/ignored', IgnoredAssetsResource),
     ('/version', VersionResource),
+    ('/ping', PingResource),
     ('/import', DataImportResource),
 ]
 
diff --git a/rotkehlchen/api/v1/resources.py b/rotkehlchen/api/v1/resources.py
--- a/rotkehlchen/api/v1/resources.py
+++ b/rotkehlchen/api/v1/resources.py
@@ -714,6 +714,12 @@ def get(self) -> Response:
         return self.rest_api.version_check()
 
 
+class PingResource(BaseResource):
+
+    def get(self) -> Response:
+        return self.rest_api.ping()
+
+
 class DataImportResource(BaseResource):
 
     put_schema = DataImportSchema()


Test patch:
diff --git a/rotkehlchen/tests/api/test_misc.py b/rotkehlchen/tests/api/test_misc.py
--- a/rotkehlchen/tests/api/test_misc.py
+++ b/rotkehlchen/tests/api/test_misc.py
@@ -42,6 +42,19 @@ def patched_get_latest_release(_klass):
     assert data['result']['download_url'] is None
 
 
+def test_query_ping(rotkehlchen_api_server):
+    """Test that the ping endpoint works"""
+    expected_result = True
+    expected_message = ''
+
+    response = requests.get(api_url_for(rotkehlchen_api_server, "pingresource"))
+    assert_proper_response(response)
+    response_json = response.json()
+    assert len(response_json) == 2
+    assert response_json['result'] == expected_result
+    assert response_json['message'] == expected_message
+
+
 def test_query_version_when_update_required(rotkehlchen_api_server):
     """Test that endpoint to query version works when a new version is available"""
     def patched_get_latest_release(_klass):