blob: 42dfbce2f92e2ee1baa274d0da8abf66d02af45e [file] [log] [blame]
Serhii Turivnyi9eccd842019-07-18 17:02:29 +03001#
2# TestRail API binding for Python 3.x (API v2, available since
3# TestRail 3.0)
4#
5# Learn more:
6#
7# http://docs.gurock.com/testrail-api2/start
8# http://docs.gurock.com/testrail-api2/accessing
9#
10# Copyright Gurock Software GmbH. See license.md for details.
11#
12
stavrovska28772bc2024-05-22 09:33:50 +020013import base64
14import json
Serhii Turivnyi9eccd842019-07-18 17:02:29 +030015import time
stavrovska28772bc2024-05-22 09:33:50 +020016import urllib.error
17import urllib.request
18
Serhii Turivnyi9eccd842019-07-18 17:02:29 +030019
20class APIClient:
stavrovska28772bc2024-05-22 09:33:50 +020021 def __init__(self, base_url):
22 self.user = ""
23 self.password = ""
24 if not base_url.endswith("/"):
25 base_url += "/"
26 self.__url = base_url + "index.php?/api/v2/"
Serhii Turivnyi9eccd842019-07-18 17:02:29 +030027
stavrovska28772bc2024-05-22 09:33:50 +020028 #
29 # Send Get
30 #
31 # Issues a GET request (read) against the API and returns the result
32 # (as Python dict).
33 #
34 # Arguments:
35 #
36 # uri The API method to call including parameters
37 # (e.g. get_case/1)
38 #
39 def send_get(self, uri):
40 try:
41 return self.__send_request("GET", uri, None)
42 except APIError:
43 print("Got an API Exception. Waiting 30 sec.")
44 time.sleep(30)
45 return self.__send_request("GET", uri, None)
Serhii Turivnyi9eccd842019-07-18 17:02:29 +030046
stavrovska28772bc2024-05-22 09:33:50 +020047 #
48 # Send POST
49 #
50 # Issues a POST request (write) against the API and returns the result
51 # (as Python dict).
52 #
53 # Arguments:
54 #
55 # uri The API method to call including parameters
56 # (e.g. add_case/1)
57 # data The data to submit as part of the request (as
58 # Python dict, strings must be UTF-8 encoded)
59 #
60 def send_post(self, uri, data):
61 return self.__send_request("POST", uri, data)
Serhii Turivnyi9eccd842019-07-18 17:02:29 +030062
stavrovska28772bc2024-05-22 09:33:50 +020063 def __send_request(self, method, uri, data):
64 url = self.__url + uri
65 request = urllib.request.Request(url)
66 if method == "POST":
67 request.data = bytes(json.dumps(data), "utf-8")
68 auth = str(
69 base64.b64encode(
70 bytes("%s:%s" % (self.user, self.password), "utf-8")
71 ),
72 "ascii",
73 ).strip()
74 request.add_header("Authorization", "Basic %s" % auth)
75 request.add_header("Content-Type", "application/json")
Serhii Turivnyi9eccd842019-07-18 17:02:29 +030076
stavrovska28772bc2024-05-22 09:33:50 +020077 e = None
78 try:
79 response = urllib.request.urlopen(request).read()
80 except urllib.error.HTTPError as ex:
81 response = ex.read()
82 e = ex
Serhii Turivnyi9eccd842019-07-18 17:02:29 +030083
stavrovska28772bc2024-05-22 09:33:50 +020084 if response:
85 result = json.loads(response.decode())
86 else:
87 result = {}
Serhii Turivnyi9eccd842019-07-18 17:02:29 +030088
stavrovska28772bc2024-05-22 09:33:50 +020089 if e is not None:
90 if result and "error" in result:
91 error = '"' + result["error"] + '"'
92 else:
93 error = "No additional error message received"
94 raise APIError(
95 "TestRail API returned HTTP %s (%s)" % (e.code, error)
96 )
Serhii Turivnyi9eccd842019-07-18 17:02:29 +030097
stavrovska28772bc2024-05-22 09:33:50 +020098 return result
99
Serhii Turivnyi9eccd842019-07-18 17:02:29 +0300100
101class APIError(Exception):
stavrovska28772bc2024-05-22 09:33:50 +0200102 pass