Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
else:
try:
message = "The request failed with code {} {}: {}".format(
req.status_code, req.reason, req.json()
)
except ValueError:
message = (
"The request failed with code {} {} but more specific "
"details were not returned in json. Check the NetBox Logs "
"or investigate this exception's error attribute.".format(
req.status_code, req.reason
)
)
super(RequestError, self).__init__(message)
self.req = req
self.request_body = req.request.body
self.base = req.url
self.error = req.text
"{}secrets/get-session-key/?preserve_key=True".format(self.base),
headers={
"accept": "application/json",
"authorization": "Token {}".format(self.token),
"Content-Type": "application/x-www-form-urlencoded",
},
data=urlencode({"private_key": self.private_key.strip("\n")}),
verify=self.ssl_verify,
)
if req.ok:
try:
return req.json()["session_key"]
except json.JSONDecodeError:
raise ContentError(req)
else:
raise RequestError(req)
:Raises: RequestError if req.ok returns false.
:Returns: Version number as a string. Empty string if version is not
present in the headers.
"""
headers = {
"Content-Type": "application/json;",
}
req = requests.get(
self.normalize_url(self.base),
headers=headers,
verify=self.ssl_verify,
)
if req.ok:
return req.headers.get("API-Version", "")
else:
raise RequestError(req)
)
if req.status_code == 204 and verb == "post":
raise AllocationError(req)
if verb == "delete":
if req.ok:
return True
else:
raise RequestError(req)
elif req.ok:
try:
return req.json()
except json.JSONDecodeError:
raise ContentError(req)
else:
raise RequestError(req)