Changeset - 1ee5e95a6cc2
[Not reviewed]
default
0 1 0
Dennis Fink - 3 years ago 2022-02-28 19:01:12
dennis.fink@c3l.lu
Make it python3.7 compatible
1 file changed with 3 insertions and 3 deletions:
0 comments (0 inline, 0 general)
stockcli/utils.py
Show inline comments
 
import logging
 
import string
 
from operator import attrgetter
 
from typing import Any, Optional
 
from typing import Any, Dict, Optional
 

	
 
import click
 
import requests
 

	
 
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:
 
    obj = click.get_current_context().obj
 
    session = obj["request_session"]
 
    base_url = obj["base_url"]
 
    requested_url = base_url + url_path
 

	
 
    method_function = attrgetter(method)
 

	
 
    try:
 
        if data is not None:
 
            response = method_function(session)(requested_url, json=data)
 
        else:
 
            response = method_function(session)(requested_url)
 
        response.raise_for_status()
 
    except requests.Timeout:
 
@@ -31,34 +31,34 @@ def make_request(method: str, url_path: 
 
        raise
 
    except requests.ConnectionError:
 
        logging.error(f"Couldn't establish a connection to {requested_url}!")
 
        error_console.print("Couldn't establish a connection!")
 
        raise
 
    except requests.HTTPError:
 
        logging.error(f"{requested_url} sent back an HTTPError")
 
        error_console.print("Got the following error:")
 
        error_message = response.json()["error_message"]
 
        logging.error(error_message)
 
        error_console.print(error_message)
 
        raise
 
    except requests.TooManyRedirects:
 
        logging.error(f"{requested_url} had too many redirects!")
 
        error_console.print("Too many redirects!")
 
        raise
 
    else:
 
        return response.json()
 

	
 

	
 
def get_request(url_path: str) -> Any:
 
    return make_request("get", url_path)
 

	
 

	
 
def post_request(url_path: str, data: dict[str, Any]) -> Any:
 
def post_request(url_path: str, data: Dict[str, Any]) -> Any:
 
    return make_request("post", url_path, data)
 

	
 

	
 
def put_request(url_path: str, data: dict[str, Any]) -> Any:
 
def put_request(url_path: str, data: Dict[str, Any]) -> Any:
 
    return make_request("put", url_path, data)
 

	
 

	
 
def prepare_barcode(barcode: str) -> str:
 
    return barcode.translate(UNALLOWED_CHARACTERS)
0 comments (0 inline, 0 general)