Compare commits

...

2 Commits

Author SHA1 Message Date
Kay Faraday b1a9b37c5e deduplicate aiohttp session creation 2022-07-02 23:09:46 +00:00
Kay Faraday 6e9cae820a gitignore __pycache__/ 2022-07-02 23:06:45 +00:00
4 changed files with 18 additions and 23 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
__pycache__/
*.toml
!*.example.toml
*.timestamp

View File

@ -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,

View File

@ -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

View File

@ -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):