File size: 6,780 Bytes
065fee7 |
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 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 |
.. _aiohttp-abc:
Abstract Base Classes
=====================
.. module:: aiohttp.abc
Abstract routing
----------------
aiohttp has abstract classes for managing web interfaces.
The most part of :mod:`aiohttp.web` is not intended to be inherited
but few of them are.
aiohttp.web is built on top of few concepts: *application*, *router*,
*request* and *response*.
*router* is a *pluggable* part: a library user may build a *router*
from scratch, all other parts should work with new router seamlessly.
:class:`aiohttp.abc.AbstractRouter` has the only mandatory method:
:meth:`aiohttp.abc.AbstractRouter.resolve` coroutine. It must return an
:class:`aiohttp.abc.AbstractMatchInfo` instance.
If the requested URL handler is found
:meth:`aiohttp.abc.AbstractMatchInfo.handler` is a :term:`web-handler` for
requested URL and :attr:`aiohttp.abc.AbstractMatchInfo.http_exception` is ``None``.
Otherwise :attr:`aiohttp.abc.AbstractMatchInfo.http_exception` is an instance of
:exc:`~aiohttp.web.HTTPException` like *404: NotFound* or *405: Method
Not Allowed*. :meth:`aiohttp.abc.AbstractMatchInfo.handler` raises
:attr:`~aiohttp.abc.AbstractMatchInfo.http_exception` on call.
.. class:: AbstractRouter
Abstract router, :class:`aiohttp.web.Application` accepts it as
*router* parameter and returns as
:attr:`aiohttp.web.Application.router`.
.. method:: resolve(request)
:async:
Performs URL resolving. It's an abstract method, should be
overridden in *router* implementation.
:param request: :class:`aiohttp.web.Request` instance for
resolving, the request has
:attr:`aiohttp.web.Request.match_info` equals to
``None`` at resolving stage.
:return: :class:`aiohttp.abc.AbstractMatchInfo` instance.
.. class:: AbstractMatchInfo
Abstract *match info*, returned by :meth:`aiohttp.abc.AbstractRouter.resolve` call.
.. attribute:: http_exception
:exc:`aiohttp.web.HTTPException` if no match was found, ``None``
otherwise.
.. method:: handler(request)
:async:
Abstract method performing :term:`web-handler` processing.
:param request: :class:`aiohttp.web.Request` instance for
resolving, the request has
:attr:`aiohttp.web.Request.match_info` equals to
``None`` at resolving stage.
:return: :class:`aiohttp.web.StreamResponse` or descendants.
:raise: :class:`aiohttp.web.HTTPException` on error
.. method:: expect_handler(request)
:async:
Abstract method for handling *100-continue* processing.
Abstract Class Based Views
--------------------------
For *class based view* support aiohttp has abstract
:class:`AbstractView` class which is *awaitable* (may be uses like
``await Cls()`` or ``yield from Cls()`` and has a *request* as an
attribute.
.. class:: AbstractView
An abstract class, base for all *class based views* implementations.
Methods ``__iter__`` and ``__await__`` should be overridden.
.. attribute:: request
:class:`aiohttp.web.Request` instance for performing the request.
Abstract Cookie Jar
-------------------
.. class:: AbstractCookieJar
The cookie jar instance is available as :attr:`aiohttp.ClientSession.cookie_jar`.
The jar contains :class:`~http.cookies.Morsel` items for storing
internal cookie data.
API provides a count of saved cookies::
len(session.cookie_jar)
These cookies may be iterated over::
for cookie in session.cookie_jar:
print(cookie.key)
print(cookie["domain"])
An abstract class for cookie storage. Implements
:class:`collections.abc.Iterable` and
:class:`collections.abc.Sized`.
.. method:: update_cookies(cookies, response_url=None)
Update cookies returned by server in ``Set-Cookie`` header.
:param cookies: a :class:`collections.abc.Mapping`
(e.g. :class:`dict`, :class:`~http.cookies.SimpleCookie`) or
*iterable* of *pairs* with cookies returned by server's
response.
:param str response_url: URL of response, ``None`` for *shared
cookies*. Regular cookies are coupled with server's URL and
are sent only to this server, shared ones are sent in every
client request.
.. method:: filter_cookies(request_url)
Return jar's cookies acceptable for URL and available in
``Cookie`` header for sending client requests for given URL.
:param str response_url: request's URL for which cookies are asked.
:return: :class:`http.cookies.SimpleCookie` with filtered
cookies for given URL.
.. method:: clear(predicate=None)
Removes all cookies from the jar if the predicate is ``None``. Otherwise remove only those :class:`~http.cookies.Morsel` that ``predicate(morsel)`` returns ``True``.
:param predicate: callable that gets :class:`~http.cookies.Morsel` as a parameter and returns ``True`` if this :class:`~http.cookies.Morsel` must be deleted from the jar.
.. versionadded:: 3.8
.. method:: clear_domain(domain)
Remove all cookies from the jar that belongs to the specified domain or its subdomains.
:param str domain: domain for which cookies must be deleted from the jar.
.. versionadded:: 3.8
Abstract Access Logger
-------------------------------
.. class:: AbstractAccessLogger
An abstract class, base for all :class:`aiohttp.web.RequestHandler`
``access_logger`` implementations
Method ``log`` should be overridden.
.. method:: log(request, response, time)
:param request: :class:`aiohttp.web.Request` object.
:param response: :class:`aiohttp.web.Response` object.
:param float time: Time taken to serve the request.
Abstract Resolver
-------------------------------
.. class:: AbstractResolver
An abstract class, base for all resolver implementations.
Method ``resolve`` should be overridden.
.. method:: resolve(host, port, family)
Resolve host name to IP address.
:param str host: host name to resolve.
:param int port: port number.
:param int family: socket family.
:return: list of :class:`aiohttp.abc.ResolveResult` instances.
.. method:: close()
Release resolver.
.. class:: ResolveResult
Result of host name resolution.
.. attribute:: hostname
The host name that was provided.
.. attribute:: host
The IP address that was resolved.
.. attribute:: port
The port that was resolved.
.. attribute:: family
The address family that was resolved.
.. attribute:: proto
The protocol that was resolved.
.. attribute:: flags
The flags that were resolved.
|