deduplicate aiohttp session creation
This commit is contained in:
parent
6e9cae820a
commit
b1a9b37c5e
|
@ -8,22 +8,14 @@ import cursor
|
|||
import aiohttp
|
||||
import pleroma
|
||||
import argparse
|
||||
import platform
|
||||
import pendulum
|
||||
import aiosqlite
|
||||
import contextlib
|
||||
import qtoml as toml
|
||||
from utils import suppress, loading_spinner
|
||||
from pleroma import Pleroma
|
||||
from functools import partial
|
||||
from utils import suppress, loading_spinner, http_session_factory
|
||||
|
||||
USER_AGENT = (
|
||||
'mirror-bot; '
|
||||
f'{aiohttp.__version__}; '
|
||||
f'{platform.python_implementation()}/{platform.python_version()}'
|
||||
)
|
||||
|
||||
UTC = pendulum.timezone('UTC')
|
||||
JSON_CONTENT_TYPE = 'application/json'
|
||||
ACTIVITYPUB_CONTENT_TYPE = 'application/activity+json'
|
||||
|
||||
|
@ -37,9 +29,8 @@ class PostMirror:
|
|||
Pleroma(api_base_url=self.config['site'], access_token=self.config['access_token']),
|
||||
)
|
||||
self._http = await stack.enter_async_context(
|
||||
aiohttp.ClientSession(
|
||||
http_session_factory(
|
||||
headers={
|
||||
'User-Agent': USER_AGENT,
|
||||
'Accept': ', '.join([JSON_CONTENT_TYPE, ACTIVITYPUB_CONTENT_TYPE]),
|
||||
},
|
||||
trust_env=True,
|
||||
|
|
14
pleroma.py
14
pleroma.py
|
@ -5,19 +5,9 @@ import yarl
|
|||
import json
|
||||
import hashlib
|
||||
import aiohttp
|
||||
from multidict import MultiDict
|
||||
from http import HTTPStatus
|
||||
|
||||
def http_session_factory(headers={}):
|
||||
py_version = '.'.join(map(str, sys.version_info))
|
||||
user_agent = (
|
||||
'pleroma-ebooks (https://lab.freak.university/KayFaraday/pleroma-ebooks); '
|
||||
'aiohttp/{aiohttp.__version__}; '
|
||||
'python/{py_version}'
|
||||
)
|
||||
return aiohttp.ClientSession(
|
||||
headers={'User-Agent': user_agent, **headers},
|
||||
)
|
||||
from multidict import MultiDict
|
||||
from utils import http_session_factory
|
||||
|
||||
class BadRequest(Exception):
|
||||
pass
|
||||
|
|
13
utils.py
13
utils.py
|
@ -1,10 +1,23 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import anyio
|
||||
import aiohttp
|
||||
import platform
|
||||
import itertools
|
||||
import contextlib
|
||||
from functools import wraps
|
||||
|
||||
def http_session_factory(headers={}, **kwargs):
|
||||
user_agent = (
|
||||
'mirror-bot (https://lab.freak.university/KayFaraday/mirror-bot); '
|
||||
f'aiohttp/{aiohttp.__version__}; '
|
||||
f'{platform.python_implementation()}/{platform.python_version()}'
|
||||
)
|
||||
return aiohttp.ClientSession(
|
||||
headers={'User-Agent': user_agent, **headers},
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def as_corofunc(f):
|
||||
@wraps(f)
|
||||
async def wrapped(*args, **kwargs):
|
||||
|
|
Loading…
Reference in New Issue