File: api.py

package info (click to toggle)
python-falcon 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,172 kB
  • sloc: python: 33,608; javascript: 92; sh: 50; makefile: 50
file content (73 lines) | stat: -rw-r--r-- 2,600 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright (c) 2013 Rackspace, Inc.
#
# 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.

import falcon
from falcon.bench.queues import claims
from falcon.bench.queues import messages
from falcon.bench.queues import queues
from falcon.bench.queues import stats


class RequestIDComponent:
    def process_request(self, req, resp):
        req.context.request_id = '<generate ID>'

    def process_response(self, req, resp, resource, req_succeeded):
        resp.set_header('X-Request-ID', req.context.request_id)


class CannedResponseComponent:
    def __init__(self, body, headers):
        self._body = body
        self._headers = headers

    def process_response(self, req, resp, resource, req_succeeded):
        user_agent = req.user_agent  # NOQA
        limit = req.get_param('limit') or '10'  # NOQA

        resp.status = falcon.HTTP_200
        resp.data = self._body
        resp.set_headers(self._headers)
        resp.vary = ('X-Auth-Token', 'Accept-Encoding')
        resp.content_range = (0, len(self._body), len(self._body) + 100)


def create(body, headers):
    queue_collection = queues.CollectionResource()
    queue_item = queues.ItemResource()

    stats_endpoint = stats.Resource()

    msg_collection = messages.CollectionResource()
    msg_item = messages.ItemResource()

    claim_collection = claims.CollectionResource()
    claim_item = claims.ItemResource()

    middleware = [
        RequestIDComponent(),
        CannedResponseComponent(body, headers),
    ]

    api = falcon.App(middleware=middleware)
    api.add_route('/v1/{tenant_id}/queues', queue_collection)
    api.add_route('/v1/{tenant_id}/queues/{queue_name}', queue_item)
    api.add_route('/v1/{tenant_id}/queues/{queue_name}/stats', stats_endpoint)
    api.add_route('/v1/{tenant_id}/queues/{queue_name}/messages', msg_collection)
    api.add_route('/v1/{tenant_id}/queues/{queue_name}/messages/{message_id}', msg_item)
    api.add_route('/v1/{tenant_id}/queues/{queue_name}/claims', claim_collection)
    api.add_route('/v1/{tenant_id}/queues/{queue_name}/claims/{claim_id}', claim_item)

    return api