HTTPProxyHandler
Bases: BaseHandler
A handler for proxying requests to a remote server.
This handler is used to proxy requests to a remote server. It has a call method that is used to handle incoming requests. The handler can be used as a route handler in an aiohttp.web application. It executes specified before and after handlers, before and after the incoming request is proxied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
middlewares
|
List[ProxyMiddlewareDef] | None
|
You can if you want initialize the handler with a set of proxy middlewares right away |
None
|
error_handler
|
ErrorHandler
|
Callable that is called when an error occurs during the proxied request. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If connection options contain invalid keys. |
Source code in aiorp/http_handler.py
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
__call__(request)
async
Handle incoming requests.
This method is called when the handler is used as a route handler in an aiohttp.web app. It executes the middleware chain that was set by the users.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming request to proxy. |
required |
Returns:
| Type | Description |
|---|---|
Response | StreamResponse
|
The response from the external server. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If proxy context is not set. |
HTTPInternalServerError
|
If there's an error during request processing. |
Source code in aiorp/http_handler.py
__init__(*args, middlewares=None, error_handler=None, **kwargs)
Initialize the HTTP proxy handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Variable length argument list. |
()
|
error_handler
|
ErrorHandler
|
Optional callable for handling errors during proxied requests. |
None
|
**kwargs
|
Any
|
Arbitrary keyword arguments. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If connection options contain invalid keys. |
Source code in aiorp/http_handler.py
add_middleware(middleware_def)
Register a middleware with explicit ordering.
It will be registered depending on the order and relative to other defined middlewares. A lower number means sooner registration, while a higher number results in a later registration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
middleware_def
|
ProxyMiddlewareDef
|
The proxy middleware definition to add |
required |
Source code in aiorp/http_handler.py
client_edge(func)
Register an client edge middleware that can yield.
This middleware is registered as first, meaning the code before yield will act before any other one, but code after yield will execute the last.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
ProxyMiddleware
|
The middleware function which yields. |
required |
Returns:
| Type | Description |
|---|---|
ProxyMiddleware
|
The decorated middleware function. |
Source code in aiorp/http_handler.py
proxy(func)
Register a middleware with default execution order that can yield.
Executes the pre-yield code before target edge, but after client edge middleware, and the post-yield code after target edge but before client edge middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
ProxyMiddleware
|
The middleware function which yields. |
required |
Returns:
| Type | Description |
|---|---|
ProxyMiddleware
|
The decorated middleware function. |
Source code in aiorp/http_handler.py
target_edge(func)
Register a target edge middleware that can yield.
This middleware is registered the last. The code before yield will act after all other middlewares. The code after the yield runs before any other middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[ProxyContext], AsyncGenerator[None]]
|
The middleware function which yields. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[ProxyContext], AsyncGenerator[None]]
|
The decorated middleware function. |