Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 1 | import base64 |
| 2 | import time |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 3 | from protos import xos_pb2, xos_pb2_grpc |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 4 | from google.protobuf.empty_pb2 import Empty |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 5 | from apistats import track_request_time, REQUEST_COUNT |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 6 | |
| 7 | from django.contrib.auth import authenticate as django_authenticate |
| 8 | from xos.exceptions import * |
| 9 | from apihelper import XOSAPIHelperMixin, translate_exceptions |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 10 | import grpc |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 11 | |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 12 | class XosService(xos_pb2_grpc.xosServicer, XOSAPIHelperMixin): |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 13 | def __init__(self, thread_pool): |
| 14 | self.thread_pool = thread_pool |
| 15 | XOSAPIHelperMixin.__init__(self) |
| 16 | |
| 17 | def stop(self): |
| 18 | pass |
| 19 | |
| 20 | {% for object in proto.messages | sort(attribute='name') %} |
| 21 | {%- if object.name!='XOSBase' %} |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 22 | @translate_exceptions("{{ object.name }}", "List{{ object.name }}") |
| 23 | @track_request_time("{{ object.name }}", "List{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 24 | def List{{ object.name }}(self, request, context): |
| 25 | user=self.authenticate(context) |
| 26 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 27 | res = self.list(model, user) |
| 28 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 29 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 30 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 31 | @translate_exceptions("{{ object.name }}", "Filter{{ object.name }}") |
| 32 | @track_request_time("{{ object.name }}", "Filter{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 33 | def Filter{{ object.name }}(self, request, context): |
| 34 | user=self.authenticate(context) |
| 35 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 36 | res = self.filter(model, user, request) |
| 37 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 38 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 39 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 40 | @translate_exceptions("{{ object.name }}", "Get{{ object.name }}") |
| 41 | @track_request_time("{{ object.name }}", "Get{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 42 | def Get{{ object.name }}(self, request, context): |
| 43 | user=self.authenticate(context) |
| 44 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 45 | res = self.get(model, user, request.id) |
| 46 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 47 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 48 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 49 | @translate_exceptions("{{ object.name }}", "Create{{ object.name }}") |
| 50 | @track_request_time("{{ object.name }}", "Create{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 51 | def Create{{ object.name }}(self, request, context): |
| 52 | user=self.authenticate(context) |
| 53 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 54 | res = self.create(model, user, request) |
| 55 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 56 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 57 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 58 | @translate_exceptions("{{ object.name }}", "Delete{{ object.name }}") |
| 59 | @track_request_time("{{ object.name }}", "Delete{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 60 | def Delete{{ object.name }}(self, request, context): |
| 61 | user=self.authenticate(context) |
| 62 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 63 | res = self.delete(model, user, request.id) |
| 64 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 65 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 66 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 67 | @translate_exceptions("{{ object.name }}", "Update{{ object.name }}") |
| 68 | @track_request_time("{{ object.name }}", "Update{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 69 | def Update{{ object.name }}(self, request, context): |
| 70 | user=self.authenticate(context) |
| 71 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame^] | 72 | res = self.update(model, user, request.id, request, context) |
| 73 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 74 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 75 | {%- endif %} |
| 76 | {% endfor %} |
| 77 | |
| 78 | |