# Abstract Base Classes

## Abstract routing

aiohttp has abstract classes for managing web interfaces.

The most part of `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.

`aiohttp.abc.AbstractRouter` has the only mandatory method: `aiohttp.abc.AbstractRouter.resolve()` coroutine. It must return an `aiohttp.abc.AbstractMatchInfo` instance.

If the requested URL handler is found `aiohttp.abc.AbstractMatchInfo.handler()` is a web-handler for requested URL and `aiohttp.abc.AbstractMatchInfo.http_exception` is `None`.

Otherwise `aiohttp.abc.AbstractMatchInfo.http_exception` is an instance of `HTTPException` like 404: NotFound or 405: Method Not Allowed. `aiohttp.abc.AbstractMatchInfo.handler()` raises `http_exception` on call.

class aiohttp.abc.AbstractRouter

  Abstract router, `aiohttp.web.Application` accepts it as router parameter and returns as `aiohttp.web.Application.router`.

  async resolve(request)

    Performs URL resolving. It’s an abstract method, should be overridden in router implementation.

    Parameters:

      request – `aiohttp.web.Request` instance for resolving, the request has `aiohttp.web.Request.match_info` equals to `None` at resolving stage.

    Returns:

      `aiohttp.abc.AbstractMatchInfo` instance.

class aiohttp.abc.AbstractMatchInfo

  Abstract match info, returned by `aiohttp.abc.AbstractRouter.resolve()` call.

  http_exception

    `aiohttp.web.HTTPException` if no match was found, `None` otherwise.

  async handler(request)

    Abstract method performing web-handler processing.

    Parameters:

      request – `aiohttp.web.Request` instance for resolving, the request has `aiohttp.web.Request.match_info` equals to `None` at resolving stage.

    Returns:

      `aiohttp.web.StreamResponse` or descendants.

    Raise:

      `aiohttp.web.HTTPException` on error

  async expect_handler(request)

    Abstract method for handling 100-continue processing.

## Abstract Class Based Views

For class based view support aiohttp has abstract `AbstractView` class which is awaitable (may be uses like `await Cls()` or `yield from Cls()` and has a request as an attribute.

class aiohttp.abc.AbstractView

  An abstract class, base for all class based views implementations.

  Methods `__iter__` and `__await__` should be overridden.

  request

    `aiohttp.web.Request` instance for performing the request.

## Abstract Cookie Jar

class aiohttp.abc.AbstractCookieJar

  The cookie jar instance is available as `aiohttp.ClientSession.cookie_jar`.

  The jar contains `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 `collections.abc.Iterable` and `collections.abc.Sized`.

  update_cookies(cookies, response_url=None)

    Update cookies returned by server in `Set-Cookie` header.

    Parameters:

      - cookies – a `collections.abc.Mapping` (e.g. `dict`, `SimpleCookie`) or iterable of pairs with cookies returned by server’s response.

      - response_url (str) – 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.

  filter_cookies(request_url)

    Return jar’s cookies acceptable for URL and available in `Cookie` header for sending client requests for given URL.

    Parameters:

      response_url (str) – request’s URL for which cookies are asked.

    Returns:

      `http.cookies.SimpleCookie` with filtered cookies for given URL.

  clear(predicate=None)

    Removes all cookies from the jar if the predicate is `None`. Otherwise remove only those `Morsel` that `predicate(morsel)` returns `True`.

    Parameters:

      predicate –

      callable that gets `Morsel` as a parameter and returns `True` if this `Morsel` must be deleted from the jar.

      Added in version 3.8.

  clear_domain(domain)

    Remove all cookies from the jar that belongs to the specified domain or its subdomains.

    Parameters:

      domain (str) – domain for which cookies must be deleted from the jar.

    Added in version 3.8.

## Abstract Access Logger

class aiohttp.abc.AbstractAccessLogger

  An abstract class, base for all `aiohttp.web.RequestHandler` `access_logger` implementations

  Method `log` should be overridden.

  log(request, response, time)
    Parameters:

      - request – `aiohttp.web.Request` object.

      - response – `aiohttp.web.Response` object.

      - time (float) – Time taken to serve the request.

## Abstract Resolver

class aiohttp.abc.AbstractResolver

  An abstract class, base for all resolver implementations.

  Method `resolve` should be overridden.

  resolve(host, port, family)

    Resolve host name to IP address.

    Parameters:

      - host (str) – host name to resolve.

      - port (int) – port number.

      - family (int) – socket family.

    Returns:

      list of `aiohttp.abc.ResolveResult` instances.

  close()

    Release resolver.

class aiohttp.abc.ResolveResult

  Result of host name resolution.

  hostname

    The host name that was provided.

  host

    The IP address that was resolved.

  port

    The port that was resolved.

  family

    The address family that was resolved.

  proto

    The protocol that was resolved.

  flags

    The flags that were resolved.