Skip to content

Commit

Permalink
Add timeout for all requests (closes #1)
Browse files Browse the repository at this point in the history
Fix bug where requests.get would hang
  • Loading branch information
fabiosangregorio committed May 8, 2021
1 parent 8ca04e6 commit 762bffa
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyreddit/config/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Project-wide configuration variables."""
MAX_POST_LENGTH = 500
MAX_TITLE_LENGTH = 200
REQUESTS_TIMEOUT = (2, 4)
1 change: 1 addition & 0 deletions pyreddit/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def get_urls_from_text(text: str) -> List[str]:
word,
headers={"User-agent": os.getenv("REDDIT_USER_AGENT")}, # type: ignore
allow_redirects=False,
timeout=config.REQUESTS_TIMEOUT,
)
start = resp.text.find("https://")
url = resp.text[start : resp.text.find('"', start)]
Expand Down
2 changes: 2 additions & 0 deletions pyreddit/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import requests
from dotenv import load_dotenv
from .config import config

from . import helpers
from .exceptions import (
Expand Down Expand Up @@ -58,6 +59,7 @@ def _get_json(post_url: str) -> Any:
response = requests.get(
f"{post_url}.json",
headers={"User-agent": os.getenv("REDDIT_USER_AGENT")},
timeout=config.REQUESTS_TIMEOUT,
)
json = response.json()
# some subreddits have the json data wrapped in brackets, some do not
Expand Down
5 changes: 4 additions & 1 deletion pyreddit/services/gfycat_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..models.content_type import ContentType
from ..models.media import Media
from .service import Service
from ..config import config


class Gfycat(Service):
Expand Down Expand Up @@ -46,7 +47,9 @@ def get(cls, url: str) -> Response:
Makes a call to the provider's API.
"""
return requests.get(
url, headers={"Authorization": f"Bearer {cls.access_token}"}
url,
headers={"Authorization": f"Bearer {cls.access_token}"},
timeout=config.REQUESTS_TIMEOUT,
)

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions pyreddit/services/service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Abstract Base static Class for every service."""
from abc import abstractmethod
from typing import Any, Optional, Union

from ..config import config
import requests
from requests import Response

Expand Down Expand Up @@ -107,7 +107,7 @@ def get(cls, url: str) -> Union[Response, str]:
Response from the service provider API.
"""
return requests.get(url, stream=True)
return requests.get(url, stream=True, timeout=config.REQUESTS_TIMEOUT)

@classmethod
@abstractmethod
Expand Down

0 comments on commit 762bffa

Please sign in to comment.