Skip to content

Commit

Permalink
Merge pull request #108 from python-astrodynamics/feature/timeout
Browse files Browse the repository at this point in the history
Increase default timeouts and add per-request timeout parameter
  • Loading branch information
RazerM authored Feb 24, 2025
2 parents 71786ee + da6396d commit 33f4b52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/spacetrack/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import outcome
import sniffio
from filelock import AsyncFileLock
from httpx import USE_CLIENT_DEFAULT

from .base import (
BASE_URL,
Expand Down Expand Up @@ -51,7 +52,7 @@ def __init__(
cache_path=None,
):
if httpx_client is None:
httpx_client = httpx.AsyncClient()
httpx_client = httpx.AsyncClient(timeout=30)
elif not isinstance(httpx_client, httpx.AsyncClient):
raise TypeError("httpx_client must be an httpx.AsyncClient instance")
super().__init__(
Expand Down Expand Up @@ -162,6 +163,7 @@ async def generic_request(
iter_content=False,
controller=None,
parse_types=False,
timeout=USE_CLIENT_DEFAULT,
**kwargs,
):
r"""Generic Space-Track query.
Expand Down Expand Up @@ -230,6 +232,7 @@ async def generic_request(
iter_content=iter_content,
controller=controller,
parse_types=parse_types,
timeout=timeout,
**kwargs,
)
)
Expand Down
23 changes: 17 additions & 6 deletions src/spacetrack/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import httpx
import outcome
from filelock import FileLock
from httpx import USE_CLIENT_DEFAULT
from logbook import Logger
from platformdirs import user_cache_path
from represent import ReprHelper, ReprHelperMixin
Expand Down Expand Up @@ -323,7 +324,7 @@ def __init__(
cache_path=None,
):
if httpx_client is None:
httpx_client = httpx.Client()
httpx_client = httpx.Client(timeout=30)
self.client = httpx_client
self.identity = identity
self.password = password
Expand Down Expand Up @@ -495,10 +496,11 @@ def logout(self):
def _generic_request_generator(
self,
class_,
iter_lines=False,
iter_content=False,
controller=None,
parse_types=False,
iter_lines,
iter_content,
controller,
parse_types,
timeout,
**kwargs,
):
if iter_lines and iter_content:
Expand Down Expand Up @@ -586,11 +588,14 @@ def _generic_request_generator(
url,
files={"file": kwargs["file"]},
params=params,
timeout=timeout,
)
logger.debug(request.url)
resp = yield from self._ratelimited_send_generator(request)
else:
request = self.client.build_request("GET", url, params=params)
request = self.client.build_request(
"GET", url, params=params, timeout=timeout
)
logger.debug(request.url)
resp = yield from self._ratelimited_send_generator(
request, stream=iter_lines or iter_content
Expand Down Expand Up @@ -637,6 +642,7 @@ def generic_request(
iter_content=False,
controller=None,
parse_types=False,
timeout=USE_CLIENT_DEFAULT,
**kwargs,
):
r"""Generic Space-Track query.
Expand Down Expand Up @@ -670,6 +676,8 @@ def generic_request(
parse_types: Parse string values in response according to type given
in predicate information, e.g. ``'2017-01-01'`` ->
``datetime.date(2017, 1, 1)``.
timeout: Override default client timeout for this request. See
`HTTPX Timeouts`_.
**kwargs: These keywords must match the predicate fields on
Space-Track. You may check valid keywords with the following
snippet:
Expand Down Expand Up @@ -697,6 +705,8 @@ def generic_request(
Passing ``format='json'`` will return the JSON **unparsed**. Do
not set ``format`` if you want the parsed JSON object returned!
.. _`HTTPX Timeouts`: https://www.python-httpx.org/advanced/timeouts/
"""
return self._run_event_generator(
self._generic_request_generator(
Expand All @@ -705,6 +715,7 @@ def generic_request(
iter_content=iter_content,
controller=controller,
parse_types=parse_types,
timeout=timeout,
**kwargs,
)
)
Expand Down

0 comments on commit 33f4b52

Please sign in to comment.