-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.py
111 lines (88 loc) · 3.24 KB
/
base.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright 2016: Mirantis Inc.
# All Rights Reserved.
#
# 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 functools
import random
import re
from ceagle import config
USE_FAKE_DATA = config.get_config().get("use_fake_api_data", True)
FAKE_REGIONS = [
"west-1.hooli.net",
"north-1.piedpiper.net",
"east-1.hooli.net",
"south-1.piedpiper.net",
]
def route(reg):
def decorator(method):
method._route = re.compile(reg)
return method
return decorator
class BaseFakeClient(object):
"""Base fake client.
Usage:
>>> from ceagle.api_fake_data import base
>>> class MyClient(base.FakeClient):
... @base.route(r"/api/(?P<method>)")
... def _method(self, query, method):
... return {"query": query}, 200
...
>>> c = MyClient(name="name", endpoint="endpoint")
>>> resp, code = c.get("/api/foo", foo="bar")
>>> assert code == 200
>>> assert resp == {"query": {"foo": "bar"}}
"""
def __init__(self, name, endpoint):
self._setup_routing()
def _setup_routing(self):
self._routes = []
for attr in dir(self):
method = getattr(self, attr)
route = getattr(method, "_route", None)
if route:
self._routes.append((route, method))
def _find_route(self, path):
for reg, method in self._routes:
match = reg.match(path)
if match:
return method, match
return None, None
def default(self, path, *args, **kwargs):
return ("not found", 404)
def get(self, path, **kwargs):
method, match = self._find_route(path)
if method is None:
return self.default(path, **kwargs)
return method(kwargs, **match.groupdict())
class FakeClient(BaseFakeClient):
@route("/api/(?P<api>\w+)/regions")
def _regions(self, query, api):
return FAKE_REGIONS, 200
def api_handler(fake):
"""Function that handles api_fake_data substitution functions
It is intended to be used as a decorator around fake api functions. It
turns a function it decorates into a decorator, that accepts "real" api
function (real_function_handler). The resulting decorator returns a
(choice_maker) function, that calls real or fake function, depending on the
value of USE_FAKE_DATA global variable.
"""
def real_function_handler(real):
@functools.wraps(real)
def choice_maker(*args, **kwargs):
if USE_FAKE_DATA:
return fake(*args, **kwargs)
return real(*args, **kwargs)
return choice_maker
return real_function_handler
def randnum(from_num, to_num, round_by=2):
return round(from_num + ((to_num - from_num) * random.random()), round_by)