Defines the HTTP interface specific API
| 77 | |
| 78 | |
| 79 | class HTTPInterfaceAPI(InterfaceAPI): |
| 80 | """Defines the HTTP interface specific API""" |
| 81 | |
| 82 | __slots__ = ( |
| 83 | "routes", |
| 84 | "versions", |
| 85 | "base_url", |
| 86 | "falcon", |
| 87 | "_output_format", |
| 88 | "_input_format", |
| 89 | "versioned", |
| 90 | "_middleware", |
| 91 | "_not_found_handlers", |
| 92 | "sinks", |
| 93 | "_not_found", |
| 94 | "_exception_handlers", |
| 95 | ) |
| 96 | |
| 97 | def __init__(self, api, base_url=""): |
| 98 | super().__init__(api) |
| 99 | self.versions = set() |
| 100 | self.routes = OrderedDict() |
| 101 | self.sinks = OrderedDict() |
| 102 | self.versioned = OrderedDict() |
| 103 | self.base_url = base_url |
| 104 | |
| 105 | @property |
| 106 | def output_format(self): |
| 107 | return getattr(self, "_output_format", hug.defaults.output_format) |
| 108 | |
| 109 | @output_format.setter |
| 110 | def output_format(self, formatter): |
| 111 | self._output_format = formatter |
| 112 | |
| 113 | @property |
| 114 | def not_found(self): |
| 115 | """Returns the active not found handler""" |
| 116 | return getattr(self, "_not_found", self.base_404) |
| 117 | |
| 118 | def urls(self): |
| 119 | """Returns a generator of all URLs attached to this API""" |
| 120 | for base_url, mapping in self.routes.items(): |
| 121 | for url, _ in mapping.items(): |
| 122 | yield base_url + url |
| 123 | |
| 124 | def handlers(self): |
| 125 | """Returns all registered handlers attached to this API""" |
| 126 | used = [] |
| 127 | for _base_url, mapping in self.routes.items(): |
| 128 | for _url, methods in mapping.items(): |
| 129 | for _method, versions in methods.items(): |
| 130 | for _version, handler in versions.items(): |
| 131 | if not handler in used: |
| 132 | used.append(handler) |
| 133 | yield handler |
| 134 | |
| 135 | def input_format(self, content_type): |
| 136 | """Returns the set input_format handler for the given content_type""" |