-
Notifications
You must be signed in to change notification settings - Fork 27
/
utils.py
103 lines (84 loc) · 3.79 KB
/
utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import json
import requests
from conan.errors import ConanException
def load_json(json_file):
try:
with open(json_file, 'r') as f:
data = json.load(f)
return data
except FileNotFoundError:
raise ConanException(f"Error: The file {json_file} was not found.")
except json.JSONDecodeError:
raise ConanException(f"Error: The file {json_file} is not a valid JSON file.")
except Exception as e:
raise ConanException(f"An unexpected error occurred: {e}")
def response_to_str(response):
content = response.content
try:
# A bytes message, decode it as str
if isinstance(content, bytes):
content = content.decode('utf-8')
content_type = response.headers.get("content-type")
if content_type == "application/json":
# Errors from Artifactory looks like:
# {"errors" : [ {"status" : 400, "message" : "Bla bla bla"}]}
try:
data = json.loads(content)["errors"][0]
content = "{}: {}".format(data["status"], data["message"])
except Exception:
pass
elif "text/html" in content_type:
content = "{}: {}".format(response.status_code, response.reason)
return content
except Exception:
return response.content
class UnauthorizedException(ConanException):
"""Exception raised for unauthorized request (HTTP 401)."""
pass
class ForbiddenException(ConanException):
"""Exception raised for forbidden request (HTTP 403)."""
pass
class NotFoundException(ConanException):
"""Exception raised for request to non-existent resources (HTTP 404)."""
pass
class BadRequestException(ConanException):
"""Exception raised for bad request (HTTP 400)."""
pass
class UnexpectedResponseException(ConanException):
"""Exception raised for unexpected response status codes."""
pass
def api_request(method, request_url, user=None, password=None, json_data=None,
sign_key_name=None):
headers = {}
if json_data:
headers.update({"Content-Type": "application/json"})
if sign_key_name:
headers.update({"X-JFrog-Crypto-Key-Name": sign_key_name})
requests_method = getattr(requests, method)
if user and password:
response = requests_method(request_url, auth=(
user, password), data=json_data, headers=headers)
else:
response = requests_method(request_url)
if response.status_code == 400:
raise BadRequestException(response_to_str(response))
elif response.status_code == 401:
raise UnauthorizedException(response_to_str(response))
elif response.status_code == 403:
raise ForbiddenException(response_to_str(response))
elif response.status_code == 404:
raise NotFoundException(response_to_str(response))
elif response.status_code not in [200, 204]:
raise UnexpectedResponseException(response_to_str(response))
return response_to_str(response)
def assert_server_or_url_user_password(args):
if args.server and args.url:
raise ConanException("--server and --url (with --user & --password/--token)) flags cannot be used together.")
if not args.server and not args.url:
raise ConanException("Specify --server or --url (with --user & --password/--token) flags to contact Artifactory.")
if args.url:
if not (args.user and (args.password or args.token)):
raise ConanException("Specify --user and --password/--token to use with the --url flag to contact Artifactory.")
if args.password and args.token:
raise ConanException("--password and --token arguments cannot be used at the same time. Please specify either --password OR --token.")
assert args.server or (args.url and args.user and (args.password or args.token))