1
|
|
|
''' |
2
|
|
|
HTTP handeler to serve general endpoints request, specifically |
3
|
|
|
http://myserver:9004/endpoints |
4
|
|
|
|
5
|
|
|
For how individual endpoint requests are served look |
6
|
|
|
at endpoint_handler.py |
7
|
|
|
''' |
8
|
|
|
|
9
|
|
|
import json |
10
|
|
|
import logging |
11
|
|
|
from tabpy.tabpy_server.common.util import format_exception |
12
|
|
|
from tabpy.tabpy_server.handlers import ManagementHandler |
13
|
|
|
from tornado import gen |
14
|
|
|
import tornado.web |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class EndpointsHandler(ManagementHandler): |
18
|
|
|
def initialize(self, app): |
19
|
|
|
super(EndpointsHandler, self).initialize(app) |
20
|
|
|
|
21
|
|
|
def get(self): |
22
|
|
|
if self.should_fail_with_not_authorized(): |
23
|
|
|
self.fail_with_not_authorized() |
24
|
|
|
return |
25
|
|
|
|
26
|
|
|
self._add_CORS_header() |
27
|
|
|
self.write(json.dumps(self.tabpy_state.get_endpoints())) |
28
|
|
|
|
29
|
|
|
@gen.coroutine |
30
|
|
|
def post(self): |
31
|
|
|
if self.should_fail_with_not_authorized(): |
32
|
|
|
self.fail_with_not_authorized() |
33
|
|
|
return |
34
|
|
|
|
35
|
|
|
try: |
36
|
|
|
if not self.request.body: |
37
|
|
|
self.error_out(400, "Input body cannot be empty") |
38
|
|
|
self.finish() |
39
|
|
|
return |
40
|
|
|
|
41
|
|
|
try: |
42
|
|
|
request_data = json.loads( |
43
|
|
|
self.request.body.decode('utf-8')) |
44
|
|
|
except Exception as ex: |
45
|
|
|
self.error_out( |
46
|
|
|
400, |
47
|
|
|
"Failed to decode input body", |
48
|
|
|
str(ex)) |
49
|
|
|
self.finish() |
50
|
|
|
return |
51
|
|
|
|
52
|
|
|
if 'name' not in request_data: |
53
|
|
|
self.error_out(400, |
54
|
|
|
"name is required to add an endpoint.") |
55
|
|
|
self.finish() |
56
|
|
|
return |
57
|
|
|
|
58
|
|
|
name = request_data['name'] |
59
|
|
|
|
60
|
|
|
# check if endpoint already exist |
61
|
|
|
if name in self.tabpy_state.get_endpoints(): |
62
|
|
|
self.error_out(400, f'endpoint {name} already exists.') |
63
|
|
|
self.finish() |
64
|
|
|
return |
65
|
|
|
|
66
|
|
|
self.logger.log( |
67
|
|
|
logging.DEBUG, |
68
|
|
|
f'Adding endpoint "{name}"') |
69
|
|
|
err_msg = yield self._add_or_update_endpoint('add', name, 1, |
70
|
|
|
request_data) |
71
|
|
|
if err_msg: |
72
|
|
|
self.error_out(400, err_msg) |
73
|
|
|
else: |
74
|
|
|
self.logger.log( |
75
|
|
|
logging.DEBUG, |
76
|
|
|
f'Endpoint {name} successfully added') |
77
|
|
|
self.set_status(201) |
78
|
|
|
self.write(self.tabpy_state.get_endpoints(name)) |
79
|
|
|
self.finish() |
80
|
|
|
return |
81
|
|
|
|
82
|
|
|
except Exception as e: |
83
|
|
|
err_msg = format_exception(e, '/add_endpoint') |
84
|
|
|
self.error_out(500, "error adding endpoint", err_msg) |
85
|
|
|
self.finish() |
86
|
|
|
return |
87
|
|
|
|