-
-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathutils.py
More file actions
93 lines (72 loc) · 2.67 KB
/
Copy pathutils.py
File metadata and controls
93 lines (72 loc) · 2.67 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from httpx import Client, HTTPTransport
from typing import Any, Iterable
from scanapi.errors import InvalidKeyError, MissingMandatoryKeyError
from scanapi.tree.tree_keys import MAX_RETRIES_KEY
def join_urls(first_url: str | None, second_url: str | None) -> str | None:
"""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: Iterable[str],
available_keys: tuple[str, ...],
required_keys: tuple[str, ...],
scope: str,
) -> None:
"""Caller function that validates keys."""
_validate_allowed_keys(keys, available_keys, scope)
_validate_required_keys(keys, required_keys, scope)
def _validate_allowed_keys(
keys: Iterable[str],
available_keys: tuple[str, ...],
scope: str,
) -> None:
"""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: Iterable[str],
required_keys: tuple[str, ...],
scope: str,
) -> None:
"""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: dict[str, Any] | None,
verify: bool = True,
) -> Client:
"""Instantiate a requests session.
Args:
retry_configuration (dict): 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: Configured client session.
"""
retry_configuration = retry_configuration or {}
retries = retry_configuration.get(MAX_RETRIES_KEY, 0)
return Client(
transport=HTTPTransport(retries=retries), timeout=None, verify=verify
)