Serhii Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 1 | # |
| 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 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 13 | import base64 |
| 14 | import json |
Serhii Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 15 | import time |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 16 | import urllib.error |
| 17 | import urllib.request |
| 18 | |
Serhii Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 19 | |
| 20 | class APIClient: |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 21 | 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 Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 27 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 28 | # |
| 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 Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 46 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 47 | # |
| 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 Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 62 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 63 | 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 Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 76 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 77 | 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 Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 83 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 84 | if response: |
| 85 | result = json.loads(response.decode()) |
| 86 | else: |
| 87 | result = {} |
Serhii Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 88 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 89 | 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 Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 97 | |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 98 | return result |
| 99 | |
Serhii Turivnyi | 9eccd84 | 2019-07-18 17:02:29 +0300 | [diff] [blame] | 100 | |
| 101 | class APIError(Exception): |
stavrovska | 28772bc | 2024-05-22 09:33:50 +0200 | [diff] [blame^] | 102 | pass |