-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathutils.py
More file actions
76 lines (55 loc) · 2.33 KB
/
utils.py
File metadata and controls
76 lines (55 loc) · 2.33 KB
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
from httpx import Client, HTTPTransport
from scanapi.errors import InvalidKeyError, MissingMandatoryKeyError
from scanapi.tree.tree_keys import MAX_RETRIES_KEY
def join_urls(first_url, second_url):
"""Function that returns one url if two aren't given else joins the two
urls and returns them.
"""
if not first_url:
return second_url
if not second_url:
return first_url
first_url = first_url.strip("/")
second_url = second_url.lstrip("/")
return "/".join([first_url, second_url])
def validate_keys(keys, available_keys, required_keys, scope):
"""Caller function that validates keys."""
_validate_allowed_keys(keys, available_keys, scope)
_validate_required_keys(keys, required_keys, scope)
def _validate_allowed_keys(keys, available_keys, scope):
"""Private function that checks if the spec keys are allowed.
Args:
keys [list of strings]: the specification keys
available_keys [tuple of string]: the available keys for that scope
scope [string]: the scope of the current node: 'root', 'endpoint',
'request' or 'test'
"""
for key in keys:
if key not in available_keys:
raise InvalidKeyError(key, scope, available_keys)
def _validate_required_keys(keys, required_keys, scope):
"""Private function that checks if there is any required key missing.
Args:
keys [list of strings]: the specification keys
required_keys [tuple of string]: the required keys for that scope
scope [string]: the scope of the current node: 'root', 'endpoint',
'request' or 'test'
"""
if not set(required_keys) <= set(keys):
missing_keys = set(required_keys) - set(keys)
raise MissingMandatoryKeyError(missing_keys, scope)
def session_with_retry(retry_configuration, verify=True):
"""Instantiate a requests session.
Args:
retry_configuration [dict]: The retry configuration
for a request. (Available for version >= 2.2.0).
verify [bool]: SSL certificates used to verify the
identity of requested hosts
Returns:
[httpx.Client]: Client
"""
retry_configuration = retry_configuration or {}
retries = retry_configuration.get(MAX_RETRIES_KEY, 0)
return Client(
transport=HTTPTransport(retries=retries), timeout=None, verify=verify
)