blob: 2d8a03240e3dc1e4ee4f2bc98c602ed5748ab4a7 [file] [log] [blame]
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +02001import base64
2import time
Scott Bakerb96ba432018-02-26 09:53:48 -08003from protos import xos_pb2, xos_pb2_grpc
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +02004from google.protobuf.empty_pb2 import Empty
Matteo Scandolo1d01b692018-10-03 15:53:36 -07005from apistats import track_request_time, REQUEST_COUNT
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +02006
7from django.contrib.auth import authenticate as django_authenticate
8from xos.exceptions import *
Scott Baker75e9d562019-02-22 09:34:00 -08009from apihelper import XOSAPIHelperMixin
Scott Baker5a7b6362019-06-12 16:56:54 -070010from decorators import translate_exceptions, check_db_connection
Matteo Scandolo1d01b692018-10-03 15:53:36 -070011import grpc
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020012
Scott Bakerb96ba432018-02-26 09:53:48 -080013class XosService(xos_pb2_grpc.xosServicer, XOSAPIHelperMixin):
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020014 def __init__(self, thread_pool):
15 self.thread_pool = thread_pool
16 XOSAPIHelperMixin.__init__(self)
17
18 def stop(self):
19 pass
20
21{% for object in proto.messages | sort(attribute='name') %}
22{%- if object.name!='XOSBase' %}
Matteo Scandolo1d01b692018-10-03 15:53:36 -070023 @translate_exceptions("{{ object.name }}", "List{{ object.name }}")
24 @track_request_time("{{ object.name }}", "List{{ object.name }}")
Scott Baker5a7b6362019-06-12 16:56:54 -070025 @check_db_connection
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020026 def List{{ object.name }}(self, request, context):
27 user=self.authenticate(context)
28 model=self.get_model("{{ object.name }}")
Matteo Scandolo1d01b692018-10-03 15:53:36 -070029 res = self.list(model, user)
30 REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
31 return res
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020032
Matteo Scandolo1d01b692018-10-03 15:53:36 -070033 @translate_exceptions("{{ object.name }}", "Filter{{ object.name }}")
34 @track_request_time("{{ object.name }}", "Filter{{ object.name }}")
Scott Baker5a7b6362019-06-12 16:56:54 -070035 @check_db_connection
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020036 def Filter{{ object.name }}(self, request, context):
37 user=self.authenticate(context)
38 model=self.get_model("{{ object.name }}")
Matteo Scandolo1d01b692018-10-03 15:53:36 -070039 res = self.filter(model, user, request)
40 REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
41 return res
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020042
Matteo Scandolo1d01b692018-10-03 15:53:36 -070043 @translate_exceptions("{{ object.name }}", "Get{{ object.name }}")
44 @track_request_time("{{ object.name }}", "Get{{ object.name }}")
Scott Baker5a7b6362019-06-12 16:56:54 -070045 @check_db_connection
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020046 def Get{{ object.name }}(self, request, context):
47 user=self.authenticate(context)
48 model=self.get_model("{{ object.name }}")
Matteo Scandolo1d01b692018-10-03 15:53:36 -070049 res = self.get(model, user, request.id)
50 REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
51 return res
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020052
Matteo Scandolo1d01b692018-10-03 15:53:36 -070053 @translate_exceptions("{{ object.name }}", "Create{{ object.name }}")
54 @track_request_time("{{ object.name }}", "Create{{ object.name }}")
Scott Baker5a7b6362019-06-12 16:56:54 -070055 @check_db_connection
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020056 def Create{{ object.name }}(self, request, context):
57 user=self.authenticate(context)
58 model=self.get_model("{{ object.name }}")
Matteo Scandolo1d01b692018-10-03 15:53:36 -070059 res = self.create(model, user, request)
60 REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
61 return res
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020062
Matteo Scandolo1d01b692018-10-03 15:53:36 -070063 @translate_exceptions("{{ object.name }}", "Delete{{ object.name }}")
64 @track_request_time("{{ object.name }}", "Delete{{ object.name }}")
Scott Baker5a7b6362019-06-12 16:56:54 -070065 @check_db_connection
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020066 def Delete{{ object.name }}(self, request, context):
67 user=self.authenticate(context)
68 model=self.get_model("{{ object.name }}")
Matteo Scandolo1d01b692018-10-03 15:53:36 -070069 res = self.delete(model, user, request.id)
70 REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
71 return res
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020072
Matteo Scandolo1d01b692018-10-03 15:53:36 -070073 @translate_exceptions("{{ object.name }}", "Update{{ object.name }}")
74 @track_request_time("{{ object.name }}", "Update{{ object.name }}")
Scott Baker5a7b6362019-06-12 16:56:54 -070075 @check_db_connection
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020076 def Update{{ object.name }}(self, request, context):
77 user=self.authenticate(context)
78 model=self.get_model("{{ object.name }}")
Matteo Scandolo1d01b692018-10-03 15:53:36 -070079 res = self.update(model, user, request.id, request, context)
80 REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
81 return res
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020082{%- endif %}
83{% endfor %}
84
85