diff --git a/stockcli/utils.py b/stockcli/utils.py --- a/stockcli/utils.py +++ b/stockcli/utils.py @@ -11,9 +11,15 @@ from .console import error_console UNALLOWED_CHARACTERS = str.maketrans(dict((c, None) for c in string.whitespace)) -def make_request(method: str, url_path: str, data: Optional[Any] = None) -> Any: +def make_request( + method: str, url_path: str, data: Optional[Any] = None, *, cached: bool = False +) -> Any: obj = click.get_current_context().obj - session = obj["request_session"] + + if cached: + session = obj["cached_session"] + else: + session = obj["request_session"] base_url = obj["base_url"] requested_url = base_url + url_path @@ -49,19 +55,22 @@ def make_request(method: str, url_path: error_console.print("Too many redirects!") raise else: - return response.json() - - -def get_request(url_path: str) -> Any: - return make_request("get", url_path) + try: + return response.json() + except requests.JSONDecodeError: + return response -def post_request(url_path: str, data: Dict[str, Any]) -> Any: - return make_request("post", url_path, data) +def get_request(url_path: str, *, cached: bool = False) -> Any: + return make_request("get", url_path, cached=cached) -def put_request(url_path: str, data: Dict[str, Any]) -> Any: - return make_request("put", url_path, data) +def post_request(url_path: str, data: Dict[str, Any], *, cached: bool = False) -> Any: + return make_request("post", url_path, data, cached=cached) + + +def put_request(url_path: str, data: Dict[str, Any], *, cached: bool = False) -> Any: + return make_request("put", url_path, data, cached=cached) def prepare_barcode(barcode: str) -> str: