From 0fb4b1889eea0f605dff95d8c4516d2a8b86a4d9 Mon Sep 17 00:00:00 2001 From: sgoral Date: Thu, 31 Oct 2024 10:38:13 +0100 Subject: [PATCH] test: init commit for splunk sdk instead of requests --- solnlib/splunk_rest_client.py | 114 ++++------------------------------ 1 file changed, 12 insertions(+), 102 deletions(-) diff --git a/solnlib/splunk_rest_client.py b/solnlib/splunk_rest_client.py index 419c4177..a7102f66 100644 --- a/solnlib/splunk_rest_client.py +++ b/solnlib/splunk_rest_client.py @@ -21,12 +21,9 @@ calling splunklib SDK directly in business logic code. """ -import logging + import os -import traceback -from io import BytesIO from urllib.parse import quote -from urllib3.util.retry import Retry from splunklib import binding, client @@ -34,6 +31,7 @@ from .splunkenv import get_splunkd_access_info __all__ = ["SplunkRestClient"] + MAX_REQUEST_RETRIES = 5 @@ -73,109 +71,19 @@ def _request_handler(context): :type content: dict """ - try: - import requests - except ImportError: - # FIXME proxy ? - return binding.handler( - key_file=context.get("key_file"), cert_file=context.get("cert_file") - ) - - try: - requests.urllib3.disable_warnings() - except AttributeError: - pass - - proxies = _get_proxy_info(context) verify = context.get("verify", False) + key_file = None + cert_file = None if context.get("key_file") and context.get("cert_file"): - # cert: if tuple, ('cert', 'key') pair as per requests library - cert = context["cert_file"], context["key_file"] - elif context.get("cert_file"): - cert = context["cert_file"] - elif context.get("cert"): - # as the solnlib uses requests, we need to have a check for 'cert' key as well - cert = context["cert"] - else: - cert = None - - retries = Retry( - total=MAX_REQUEST_RETRIES, - backoff_factor=0.3, - status_forcelist=[500, 502, 503, 504], - allowed_methods=["GET", "POST", "PUT", "DELETE"], - raise_on_status=False, - ) - if context.get("pool_connections", 0): - logging.info("Use HTTP connection pooling") - session = requests.Session() - adapter = requests.adapters.HTTPAdapter( - max_retries=retries, - pool_connections=context.get("pool_connections", 10), - pool_maxsize=context.get("pool_maxsize", 10), - ) - session.mount("https://", adapter) - req_func = session.request - else: - req_func = requests.request + key_file, cert_file = context["cert_file"], context["key_file"] - def request(url, message, **kwargs): - """ - :param url: URL - :type url: string - :param message: Can contain following key/values: { - 'method': 'GET' or 'DELETE', or 'PUT' or 'POST' - 'headers': [[key, value], [key, value], ...], - 'body': string - } - :type message: dict - """ - - body = message.get("body") - headers = { - "User-Agent": "curl", - "Accept": "*/*", - "Connection": "Keep-Alive", - } - - if body: - headers["Content-Length"] = str(len(body)) - - for key, value in message["headers"]: - headers[key] = value - - method = message.get("method", "GET") - - try: - resp = req_func( - method, - url, - data=body, - headers=headers, - stream=False, - verify=verify, - proxies=proxies, - cert=cert, - **kwargs, - ) - except Exception: - logging.error( - "Failed to issue http request=%s to url=%s, error=%s", - method, - url, - traceback.format_exc(), - ) - raise - - return { - "status": resp.status_code, - "reason": resp.reason, - "headers": dict(resp.headers), - "body": BytesIO(resp.content), - } + elif context.get("cert_file"): + cert_file = context["cert_file"] - return request + return binding.handler( + key_file=key_file, cert_file=cert_file, verify=verify, context=context + ) class SplunkRestClient(client.Service): @@ -231,6 +139,7 @@ def __init__( host = "::1" handler = _request_handler(context) + retries = 5 super().__init__( handler=handler, scheme=scheme, @@ -240,4 +149,5 @@ def __init__( app=app, owner=owner, autologin=True, + retries=retries, )