|
11 | 11 | logger = logging.getLogger(__name__)
|
12 | 12 |
|
13 | 13 |
|
14 |
| -class RouteAlreadyRegisteredError(Exception): |
| 14 | +class StackInABoxServiceErrors(Exception): |
| 15 | + pass |
| 16 | + |
| 17 | + |
| 18 | +class RouteAlreadyRegisteredError(StackInABoxServiceErrors): |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +class InvalidRouteRegexError(StackInABoxServiceErrors): |
15 | 23 | pass
|
16 | 24 |
|
17 | 25 |
|
@@ -43,15 +51,40 @@ def __init__(self, name):
|
43 | 51 | .format(self.__id, self.name))
|
44 | 52 |
|
45 | 53 | @staticmethod
|
46 |
| - def __get_service_regex(base_url, service_url): |
47 |
| - regex = '^{0}{1}$'.format('', service_url) |
48 |
| - logger.debug('StackInABoxService: {0} + {1} -> {2}' |
49 |
| - .format(base_url, service_url, regex)) |
50 |
| - return re.compile(regex) |
| 54 | + def __is_regex(uri): |
| 55 | + regex_type = type(re.compile('')) |
| 56 | + return isinstance(uri, regex_type) |
51 | 57 |
|
52 | 58 | @staticmethod
|
53 |
| - def __get_service_url(url, base_url): |
54 |
| - return url[len(base_url):] |
| 59 | + def validate_regex(regex): |
| 60 | + # The regex generated by stackinabox starts with ^ |
| 61 | + # and ends with $. Enforce that the provided regex does the same. |
| 62 | + |
| 63 | + if regex.pattern.startswith('^') is False: |
| 64 | + logger.debug('StackInABoxService: Pattern must start with ^') |
| 65 | + raise InvalidRouteRegexError('Pattern must start with ^') |
| 66 | + |
| 67 | + if regex.pattern.endswith('$') is False: |
| 68 | + logger.debug('StackInABoxService: Pattern must end with $') |
| 69 | + raise InvalidRouteRegexError('Pattern must end with $') |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def __get_service_regex(base_url, service_url): |
| 73 | + # if the specified service_url is already a regex |
| 74 | + # then just use. Otherwise create what we need |
| 75 | + if StackInABoxService.__is_regex(service_url): |
| 76 | + logger.debug('StackInABoxService: Received regex {0} for use...' |
| 77 | + .format(service_url.pattern)) |
| 78 | + |
| 79 | + # Validate the regex against StackInABoxService requirement |
| 80 | + StackInABoxService.validate_regex(service_url) |
| 81 | + |
| 82 | + return service_url |
| 83 | + else: |
| 84 | + regex = '^{0}{1}$'.format('', service_url) |
| 85 | + logger.debug('StackInABoxService: {0} + {1} -> {2}' |
| 86 | + .format(base_url, service_url, regex)) |
| 87 | + return re.compile(regex) |
55 | 88 |
|
56 | 89 | @property
|
57 | 90 | def base_url(self):
|
@@ -136,6 +169,6 @@ def register(self, method, uri, call_back):
|
136 | 169 | .format(self.name, method))
|
137 | 170 | self.routes[uri]['handlers'][method] = call_back
|
138 | 171 | else:
|
139 |
| - RouteAlreadyRegisteredError( |
| 172 | + raise RouteAlreadyRegisteredError( |
140 | 173 | 'Service ({0}): Route {1} already registered'
|
141 | 174 | .format(self.name, uri))
|
0 commit comments