service.py 1.79 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright (c) 2016, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16
17
"""The Service API endpoint."""

18
from concurrent.futures import ThreadPoolExecutor
19
import logging
20

21
from tornado.web import RequestHandler
22

23
from zoe_api.rest_api.utils import catch_exceptions, get_auth, manage_cors_headers
24
from zoe_api.api_endpoint import APIEndpoint  # pylint: disable=unused-import
25
26
27

log = logging.getLogger(__name__)

Daniele Venzano's avatar
Daniele Venzano committed
28
THREAD_POOL = ThreadPoolExecutor(20)
29

30

31
class ServiceAPI(RequestHandler):
32
    """The Service API endpoint."""
33
34
35
36

    def initialize(self, **kwargs):
        """Initializes the request handler."""
        self.api_endpoint = kwargs['api_endpoint']  # type: APIEndpoint
37

38
39
40
41
42
    def set_default_headers(self):
        """Set up the headers for enabling CORS."""
        manage_cors_headers(self)

    @catch_exceptions
hxquangnhat's avatar
hxquangnhat committed
43
    def options(self, service_id): # pylint: disable=unused-argument
44
45
46
47
        """Needed for CORS."""
        self.set_status(204)
        self.finish()

Daniele Venzano's avatar
Daniele Venzano committed
48
    @catch_exceptions
Daniele Venzano's avatar
Daniele Venzano committed
49
    def get(self, service_id):
50
        """HTTP GET method."""
51
        uid, role = get_auth(self)
52

53
        service = self.api_endpoint.service_by_id(uid, role, service_id)
54

55
        self.write(service.serialize())
56

57
58
59
    def data_received(self, chunk):
        """Not implemented as we do not use stream uploads"""
        pass