Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: init commit for splunk sdk instead of requests #404

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 12 additions & 102 deletions solnlib/splunk_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
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

from .net_utils import validate_scheme_host_port
from .splunkenv import get_splunkd_access_info

__all__ = ["SplunkRestClient"]

MAX_REQUEST_RETRIES = 5


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -231,6 +139,7 @@ def __init__(
host = "::1"

handler = _request_handler(context)
retries = 5
super().__init__(
handler=handler,
scheme=scheme,
Expand All @@ -240,4 +149,5 @@ def __init__(
app=app,
owner=owner,
autologin=True,
retries=retries,
)
Loading