Set request headers in async search get request

Gotta ask for json
This commit is contained in:
Mouse Reeve 2022-05-30 11:19:16 -07:00
parent 9a9cef7766
commit 5e81ec75fb

View file

@ -12,7 +12,7 @@ from django.db.models import signals
from requests import HTTPError from requests import HTTPError
from bookwyrm import book_search, models from bookwyrm import book_search, models
from bookwyrm.settings import SEARCH_TIMEOUT from bookwyrm.settings import SEARCH_TIMEOUT, USER_AGENT
from bookwyrm.tasks import app from bookwyrm.tasks import app
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -25,12 +25,19 @@ class ConnectorException(HTTPError):
async def async_connector_search(query, items, params): async def async_connector_search(query, items, params):
"""Try a number of requests simultaneously""" """Try a number of requests simultaneously"""
timeout = aiohttp.ClientTimeout(total=SEARCH_TIMEOUT) timeout = aiohttp.ClientTimeout(total=SEARCH_TIMEOUT)
# pylint: disable=line-too-long
headers = {
"Accept": (
'application/json, application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"; charset=utf-8'
),
"User-Agent": USER_AGENT,
}
async with aiohttp.ClientSession(timeout=timeout) as session: async with aiohttp.ClientSession(timeout=timeout) as session:
for url, connector in items: for url, connector in items:
url = connector.get_search_url(query) url = connector.get_search_url(query)
# raise_not_valid_url(url) # raise_not_valid_url(url)
async with session.get(url, params=params) as response: async with session.get(url, headers=headers, params=params) as response:
print("Status:", response.status) print("Status:", response.status)
print(response.ok) print(response.ok)
print("Content-type:", response.headers["content-type"]) print("Content-type:", response.headers["content-type"])