afscgap.http_util
Utilities for HTTP requests as part of afscgap.
(c) 2023 Regents of University of California / The Eric and Wendy Schmidt Center for Data Science and the Environment at UC Berkeley.
This file is part of afscgap released under the BSD 3-Clause License. See LICENSE.md.
1""" 2Utilities for HTTP requests as part of afscgap. 3 4(c) 2023 Regents of University of California / The Eric and Wendy Schmidt Center 5for Data Science and the Environment at UC Berkeley. 6 7This file is part of afscgap released under the BSD 3-Clause License. See 8LICENSE.md. 9""" 10import requests 11 12from afscgap.typesdef import REQUESTOR 13 14TIMEOUT = 60 * 5 # 5 minutes 15 16ACCEPTABLE_CODES = [200] 17 18 19def check_result(target: requests.Response): 20 """Assert that a result returned an acceptable status code. 21 22 Args: 23 target: The response to check. 24 25 Raises: 26 RuntimeError: Raised if the response returned indicates an issue or 27 unexpected status code. 28 """ 29 if target.status_code not in ACCEPTABLE_CODES: 30 message = 'Got non-OK response from API: %d (%s)' % ( 31 target.status_code, 32 target.text 33 ) 34 raise RuntimeError(message) 35 36 37def build_requestor() -> REQUESTOR: 38 """Build a requestor strategy that uses the requests library. 39 40 Returns: 41 Newly built strategy. 42 """ 43 return lambda x: requests.get(x, timeout=TIMEOUT)
TIMEOUT =
300
ACCEPTABLE_CODES =
[200]
def
check_result(target: requests.models.Response):
20def check_result(target: requests.Response): 21 """Assert that a result returned an acceptable status code. 22 23 Args: 24 target: The response to check. 25 26 Raises: 27 RuntimeError: Raised if the response returned indicates an issue or 28 unexpected status code. 29 """ 30 if target.status_code not in ACCEPTABLE_CODES: 31 message = 'Got non-OK response from API: %d (%s)' % ( 32 target.status_code, 33 target.text 34 ) 35 raise RuntimeError(message)
Assert that a result returned an acceptable status code.
Arguments:
- target: The response to check.
Raises:
- RuntimeError: Raised if the response returned indicates an issue or unexpected status code.
def
build_requestor() -> Callable[[str], requests.models.Response]:
38def build_requestor() -> REQUESTOR: 39 """Build a requestor strategy that uses the requests library. 40 41 Returns: 42 Newly built strategy. 43 """ 44 return lambda x: requests.get(x, timeout=TIMEOUT)
Build a requestor strategy that uses the requests library.
Returns:
Newly built strategy.