This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtools.py
73 lines (61 loc) · 2.22 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
@date 30.08.2019
@author [email protected]
@details :copyright: 2003–2019 Acronis International GmbH,
Rheinweg 9, 8200 Schaffhausen, Switzerland. All rights reserved.
"""
import argparse
import json
from enum import Enum, auto
import requests
class GrantType(Enum):
password = auto()
client_credentials = auto()
def parse_arguments(*args):
parser = argparse.ArgumentParser()
for arg in args:
(arg_name, required) = arg if type(arg) is tuple else (arg, True)
parser.add_argument(f'--{arg_name}', required=required)
return parser.parse_args()
def handle_error_response(response: requests.Response):
"""Extends exception handling of the response.
:param response: Response object of the "Requests" library
:raises requests.HTTPError: Raises error code exception
"""
try:
response.raise_for_status()
except requests.HTTPError:
# TODO: This can be logged
# TODO: Better handling of different errors and status codes
if response.status_code in (400, 404, 405, 409):
print('An HTTP error has occurred.')
print('See details below:')
try:
error = response.json()['error']
print(f'Error code: {error["code"]}\n'
f'Message: {error["message"]}\n'
f'Details: {error["details"]}')
except json.decoder.JSONDecodeError:
print(response.text)
except KeyError:
print(response.json())
else:
try:
print(response.json())
except json.decoder.JSONDecodeError:
print(response.text)
print('=' * len('Traceback (most recent call last):'))
raise
def clean_arguments(args: dict) -> dict:
"""Cleans up a dictionary from keys containing value `None`.
:param args: A dictionary of argument/value pairs generated with ArgumentParser
:return: A dictionary with argument/value pairs without `None` values
:rtype: dict
"""
rv = {}
for (key, val) in args.items():
if isinstance(val, dict):
val = clean_arguments(val) or None
if val is not None:
rv[key] = val
return rv