in modules/testrail.py [0:0]
def __send_request(self, method, uri, data):
url = self.__url + uri
auth = str(
base64.b64encode(bytes("%s:%s" % (self.user, self.password), "utf-8")),
"ascii",
).strip()
headers = {"Authorization": "Basic " + auth}
if method == "POST":
if uri[:14] == "add_attachment": # add_attachment API method
files = {"attachment": (open(data, "rb"))}
response = requests.post(url, headers=headers, files=files)
files["attachment"].close()
else:
headers["Content-Type"] = "application/json"
response = requests.post(url, headers=headers, json=data)
else:
headers["Content-Type"] = "application/json"
response = requests.get(url, headers=headers)
if response.status_code > 201:
try:
error = response.json()
except (
requests.exceptions.HTTPError
): # response.content not formatted as JSON
error = str(response.content)
raise APIError(
"TestRail API returned HTTP %s (%s)" % (response.status_code, error)
)
else:
if uri[:15] == "get_attachment/": # Expecting file, not JSON
try:
open(data, "wb").write(response.content)
return data
except FileNotFoundError:
return "Error saving attachment."
else:
try:
return response.json()
except requests.exceptions.HTTPError:
return {}