-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyamlsettings_requests.py
62 lines (52 loc) · 1.78 KB
/
yamlsettings_requests.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
import requests
from six.moves import urllib
from yamlsettings.extensions.base import YamlSettingsExtension
class RequestsExtension(YamlSettingsExtension):
"""Open URLs with the request library
"""
protocols = ['http', 'https']
@classmethod
def conform_query(cls, query):
"""Don't conform the query, must pass as received"""
return query
@staticmethod
def rebuild_url(scheme, path, fragment, username,
password, hostname, port, query):
"""Rebuild the netloc value"""
netloc = "@".join(filter(None, [
":".join(
filter(None, [
username,
password,
])
),
":".join(
filter(None, [
hostname,
str(port or ''),
])
)
]))
return urllib.parse.urlunsplit([
scheme,
netloc,
path,
query,
fragment,
])
@classmethod
def load_target(cls, scheme, path, fragment, username,
password, hostname, port, query,
load_method, **kwargs):
"""Load Target via requests"""
url = cls.rebuild_url(scheme, path, fragment, username,
password, hostname, port, query)
expected_status_code = kwargs.pop('expected_status_code', 200)
raise_on_status = kwargs.pop('raise_on_status', True)
resp = requests.get(url, **kwargs)
if resp.status_code != expected_status_code:
if raise_on_status and resp.status_code != 404:
raise RuntimeError(resp.status_code)
else:
raise IOError
return load_method(resp.content)