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
|
1 |
|
import json |
10
|
1 |
|
import logging |
11
|
1 |
|
from tabpy.tabpy_server.common.util import format_exception |
12
|
1 |
|
from tabpy.tabpy_server.handlers import ManagementHandler |
13
|
1 |
|
from tabpy.tabpy_server.handlers.util import AuthErrorStates |
14
|
1 |
|
from tornado import gen |
15
|
|
|
|
16
|
|
|
|
17
|
1 |
|
class EndpointsHandler(ManagementHandler): |
18
|
1 |
|
def initialize(self, app): |
19
|
1 |
|
super(EndpointsHandler, self).initialize(app) |
20
|
|
|
|
21
|
1 |
|
def get(self): |
22
|
1 |
|
if self.should_fail_with_auth_error() != AuthErrorStates.NONE: |
23
|
1 |
|
self.fail_with_auth_error() |
24
|
1 |
|
return |
25
|
|
|
|
26
|
1 |
|
self._add_CORS_header() |
27
|
1 |
|
self.write(json.dumps(self.tabpy_state.get_endpoints())) |
28
|
|
|
|
29
|
1 |
|
@gen.coroutine |
30
|
1 |
|
def post(self): |
31
|
|
|
if self.should_fail_with_auth_error() != AuthErrorStates.NONE: |
32
|
|
|
self.fail_with_auth_error() |
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(self.request.body.decode("utf-8")) |
43
|
|
|
except Exception as ex: |
44
|
|
|
self.error_out(400, "Failed to decode input body", str(ex)) |
45
|
|
|
self.finish() |
46
|
|
|
return |
47
|
|
|
|
48
|
|
|
if "name" not in request_data: |
49
|
|
|
self.error_out(400, "name is required to add an endpoint.") |
50
|
|
|
self.finish() |
51
|
|
|
return |
52
|
|
|
|
53
|
|
|
name = request_data["name"] |
54
|
|
|
|
55
|
|
|
# check if endpoint already exist |
56
|
|
|
if name in self.tabpy_state.get_endpoints(): |
57
|
|
|
self.error_out(400, f"endpoint {name} already exists.") |
58
|
|
|
self.finish() |
59
|
|
|
return |
60
|
|
|
|
61
|
|
|
self.logger.log(logging.DEBUG, f'Adding endpoint "{name}"') |
62
|
|
|
err_msg = yield self._add_or_update_endpoint("add", name, 1, request_data) |
63
|
|
|
if err_msg: |
64
|
|
|
self.error_out(400, err_msg) |
65
|
|
|
else: |
66
|
|
|
self.logger.log(logging.DEBUG, f"Endpoint {name} successfully added") |
67
|
|
|
self.set_status(201) |
68
|
|
|
self.write(self.tabpy_state.get_endpoints(name)) |
69
|
|
|
self.finish() |
70
|
|
|
return |
71
|
|
|
|
72
|
|
|
except Exception as e: |
73
|
|
|
err_msg = format_exception(e, "/add_endpoint") |
74
|
|
|
self.error_out(500, "error adding endpoint", err_msg) |
75
|
|
|
self.finish() |
76
|
|
|
return |
77
|
|
|
|