|
62 | 62 | import datetime
|
63 | 63 | import json
|
64 | 64 | import logging
|
| 65 | +import re |
65 | 66 | import socket
|
66 | 67 | from datetime import datetime, timedelta
|
67 | 68 | from time import sleep
|
@@ -731,6 +732,25 @@ def __init__(self, service, path):
|
731 | 732 | self.service = service
|
732 | 733 | self.path = path
|
733 | 734 |
|
| 735 | + def get_api_version(self, path): |
| 736 | + """Return the API version of the service used in the provided path. |
| 737 | +
|
| 738 | + Args: |
| 739 | + path (str): A fully-qualified endpoint path (for example, "/services/search/jobs"). |
| 740 | +
|
| 741 | + Returns: |
| 742 | + int: Version of the API (for example, 1) |
| 743 | + """ |
| 744 | + # Default to v1 if undefined in the path |
| 745 | + # For example, "/services/search/jobs" is using API v1 |
| 746 | + api_version = 1 |
| 747 | + |
| 748 | + versionSearch = re.search('(?:servicesNS\/[^/]+\/[^/]+|services)\/[^/]+\/v(\d+)\/', path) |
| 749 | + if versionSearch: |
| 750 | + api_version = int(versionSearch.group(1)) |
| 751 | + |
| 752 | + return api_version |
| 753 | + |
734 | 754 | def get(self, path_segment="", owner=None, app=None, sharing=None, **query):
|
735 | 755 | """Performs a GET operation on the path segment relative to this endpoint.
|
736 | 756 |
|
@@ -794,12 +814,14 @@ def get(self, path_segment="", owner=None, app=None, sharing=None, **query):
|
794 | 814 | # ^-- This was "%s%s" % (self.path, path_segment).
|
795 | 815 | # That doesn't work, because self.path may be UrlEncoded.
|
796 | 816 |
|
797 |
| - # Search API v2 fallback to v1: |
798 |
| - # - In v2, /results_preview, /events and /results do not support search params. |
799 |
| - # - Fallback from v2 to v1 if Splunk Version is < 9. |
800 |
| - if (PATH_JOBS_V2 in path and 'search' in query and path.endswith(tuple(["results_preview", "events", "results"]))) or self.service.splunk_version < (9,): |
801 |
| - path = path.replace(PATH_JOBS_V2, PATH_JOBS) |
| 817 | + # Get the API version from the path |
| 818 | + api_version = self.get_api_version(path) |
802 | 819 |
|
| 820 | + # Search API v2+ fallback to v1: |
| 821 | + # - In v2+, /results_preview, /events and /results do not support search params. |
| 822 | + # - Fallback from v2+ to v1 if Splunk Version is < 9. |
| 823 | + if api_version >= 2 and ('search' in query and path.endswith(tuple(["results_preview", "events", "results"])) or self.service.splunk_version < (9,)): |
| 824 | + path = path.replace(PATH_JOBS_V2, PATH_JOBS) |
803 | 825 | return self.service.get(path,
|
804 | 826 | owner=owner, app=app, sharing=sharing,
|
805 | 827 | **query)
|
@@ -860,12 +882,14 @@ def post(self, path_segment="", owner=None, app=None, sharing=None, **query):
|
860 | 882 | self.path = self.path + '/'
|
861 | 883 | path = self.service._abspath(self.path + path_segment, owner=owner, app=app, sharing=sharing)
|
862 | 884 |
|
863 |
| - # Search API v2 fallback to v1: |
864 |
| - # - In v2, /results_preview, /events and /results do not support search params. |
865 |
| - # - Fallback from v2 to v1 if Splunk Version is < 9. |
866 |
| - if (PATH_JOBS_V2 in path and 'search' in query and path.endswith(tuple(["results_preview", "events", "results"]))) or self.service.splunk_version < (9,): |
| 885 | + # Get the API version from the path |
| 886 | + api_version = self.get_api_version(path) |
| 887 | + |
| 888 | + # Search API v2+ fallback to v1: |
| 889 | + # - In v2+, /results_preview, /events and /results do not support search params. |
| 890 | + # - Fallback from v2+ to v1 if Splunk Version is < 9. |
| 891 | + if api_version >= 2 and ('search' in query and path.endswith(tuple(["results_preview", "events", "results"])) or self.service.splunk_version < (9,)): |
867 | 892 | path = path.replace(PATH_JOBS_V2, PATH_JOBS)
|
868 |
| - |
869 | 893 | return self.service.post(path, owner=owner, app=app, sharing=sharing, **query)
|
870 | 894 |
|
871 | 895 |
|
|
0 commit comments