diff --git a/website/common/utils.py b/website/common/utils.py index 15c3dee..484de8e 100644 --- a/website/common/utils.py +++ b/website/common/utils.py @@ -13,6 +13,8 @@ from wagtail.models import get_page_models as get_wagtail_page_models HEADER_TAGS = ["h2", "h3", "h4", "h5", "h6"] +requests_session = requests.Session() + @dataclass class TocEntry: @@ -113,6 +115,6 @@ def get_site_title() -> str: @django_cache_decorator(time=21600) def get_url_mime_type(url: str) -> Optional[str]: try: - return requests.head(url).headers.get("Content-Type") + return requests_session.head(url).headers.get("Content-Type") except requests.exceptions.RequestException: return None diff --git a/website/contrib/code_block/utils.py b/website/contrib/code_block/utils.py index 8ecd65c..9cca503 100644 --- a/website/contrib/code_block/utils.py +++ b/website/contrib/code_block/utils.py @@ -1,10 +1,11 @@ from functools import cache from importlib.metadata import version -import requests import yaml from django_cache_decorator import django_cache_decorator +from website.common.utils import requests_session + PYGMENTS_VERSION = version("pygments") PYGMENTS_VERSION_SLUG = PYGMENTS_VERSION.replace(".", "-") @@ -13,7 +14,7 @@ LINGUIST_DATA_URL = "https://raw.githubusercontent.com/github/linguist/master/li @django_cache_decorator(time=600) def _get_linguist_colours() -> dict[str, str]: - response = requests.get(LINGUIST_DATA_URL) + response = requests_session.get(LINGUIST_DATA_URL) response.raise_for_status() diff --git a/website/contrib/unsplash/utils.py b/website/contrib/unsplash/utils.py index 5c6c026..3e7e9e5 100644 --- a/website/contrib/unsplash/utils.py +++ b/website/contrib/unsplash/utils.py @@ -1,9 +1,10 @@ -import requests from django.conf import settings +from website.common.utils import requests_session + def get_unsplash_photo(image_id: str) -> dict: - response = requests.get( + response = requests_session.get( f"https://api.unsplash.com/photos/{image_id}", headers={ "Accept-Version": "v1", diff --git a/website/spotify/client.py b/website/spotify/client.py index a9f969b..ec968a9 100644 --- a/website/spotify/client.py +++ b/website/spotify/client.py @@ -1,17 +1,18 @@ -import requests from django.conf import settings +from website.common.utils import requests_session + API_LIMIT = 50 def is_valid_playlist(playlist_id: str) -> bool: - return requests.get( + return requests_session.get( f"https://{settings.SPOTIFY_PROXY_HOST}/v1/playlists/{playlist_id}" ).ok def get_playlist(playlist_id: str) -> dict: - playlist_response = requests.get( + playlist_response = requests_session.get( f"https://{settings.SPOTIFY_PROXY_HOST}/v1/playlists/{playlist_id}", params={"fields": "name,external_urls.spotify,tracks.total,description"}, ) @@ -20,7 +21,7 @@ def get_playlist(playlist_id: str) -> dict: tracks = [] for offset in range(0, playlist_data["tracks"]["total"], API_LIMIT): - tracks_response = requests.get( + tracks_response = requests_session.get( f"https://{settings.SPOTIFY_PROXY_HOST}/v1/playlists/{playlist_id}/tracks", params={ "offset": str(offset), diff --git a/website/spotify/healthchecks.py b/website/spotify/healthchecks.py index 0f72319..df3ad0c 100644 --- a/website/spotify/healthchecks.py +++ b/website/spotify/healthchecks.py @@ -1,12 +1,13 @@ -import requests from django.conf import settings from health_check.backends import BaseHealthCheckBackend +from website.common.utils import requests_session + class SpotifyHealthCheckBackend(BaseHealthCheckBackend): def check_status(self) -> None: try: - requests.get( + requests_session.get( f"https://{settings.SPOTIFY_PROXY_HOST}/.health/" ).raise_for_status() except Exception as e: