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 * |
Scott Baker | 75e9d56 | 2019-02-22 09:34:00 -0800 | [diff] [blame] | 9 | from apihelper import XOSAPIHelperMixin |
| 10 | from decorators import translate_exceptions |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 11 | import grpc |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 12 | |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 13 | class XosService(xos_pb2_grpc.xosServicer, XOSAPIHelperMixin): |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 14 | 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 Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 23 | @translate_exceptions("{{ object.name }}", "List{{ object.name }}") |
| 24 | @track_request_time("{{ object.name }}", "List{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 25 | def List{{ object.name }}(self, request, context): |
| 26 | user=self.authenticate(context) |
| 27 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 28 | res = self.list(model, user) |
| 29 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 30 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 31 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 32 | @translate_exceptions("{{ object.name }}", "Filter{{ object.name }}") |
| 33 | @track_request_time("{{ object.name }}", "Filter{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 34 | def Filter{{ object.name }}(self, request, context): |
| 35 | user=self.authenticate(context) |
| 36 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 37 | res = self.filter(model, user, request) |
| 38 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 39 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 40 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 41 | @translate_exceptions("{{ object.name }}", "Get{{ object.name }}") |
| 42 | @track_request_time("{{ object.name }}", "Get{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 43 | def Get{{ object.name }}(self, request, context): |
| 44 | user=self.authenticate(context) |
| 45 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 46 | res = self.get(model, user, request.id) |
| 47 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 48 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 49 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 50 | @translate_exceptions("{{ object.name }}", "Create{{ object.name }}") |
| 51 | @track_request_time("{{ object.name }}", "Create{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 52 | def Create{{ object.name }}(self, request, context): |
| 53 | user=self.authenticate(context) |
| 54 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 55 | res = self.create(model, user, request) |
| 56 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 57 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 58 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 59 | @translate_exceptions("{{ object.name }}", "Delete{{ object.name }}") |
| 60 | @track_request_time("{{ object.name }}", "Delete{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 61 | def Delete{{ object.name }}(self, request, context): |
| 62 | user=self.authenticate(context) |
| 63 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 64 | res = self.delete(model, user, request.id) |
| 65 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 66 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 67 | |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 68 | @translate_exceptions("{{ object.name }}", "Update{{ object.name }}") |
| 69 | @track_request_time("{{ object.name }}", "Update{{ object.name }}") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 70 | def Update{{ object.name }}(self, request, context): |
| 71 | user=self.authenticate(context) |
| 72 | model=self.get_model("{{ object.name }}") |
Matteo Scandolo | 1d01b69 | 2018-10-03 15:53:36 -0700 | [diff] [blame] | 73 | res = self.update(model, user, request.id, request, context) |
| 74 | REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc() |
| 75 | return res |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 76 | {%- endif %} |
| 77 | {% endfor %} |
| 78 | |
| 79 | |