File: aiohttp_client.py

package info (click to toggle)
python-crownstone-cloud 1.4.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 240 kB
  • sloc: python: 1,126; makefile: 4
file content (33 lines) | stat: -rw-r--r-- 844 bytes parent folder | download
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
"""Functions to get or create an aiohttp clientsession."""
import ssl
from typing import Any

import aiohttp
import certifi


def create_clientsession(**kwargs: Any) -> aiohttp.ClientSession:
    """Create a new aiohttp clientsession."""
    connector = get_connector()

    clientsession = aiohttp.ClientSession(
        connector=connector,
        **kwargs,
    )

    return clientsession


def get_connector() -> aiohttp.BaseConnector:
    """Return the connector for aiohttp."""

    def client_context() -> ssl.SSLContext:
        """Return an SSL context for making requests."""
        context = ssl.create_default_context(
            purpose=ssl.Purpose.SERVER_AUTH, cafile=certifi.where()
        )
        return context

    connector = aiohttp.TCPConnector(enable_cleanup_closed=True, ssl=client_context())

    return connector