Skip to content

Commit 347d9ed

Browse files
author
Shakeel Mohamed
committed
Update docstrings and minor style changes
1 parent f306710 commit 347d9ed

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

splunklib/binding.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,8 @@ def f():
224224
"""
225225
@wraps(request_fun)
226226
def wrapper(self, *args, **kwargs):
227-
# TODO: verify this logic is correct with tests
228227
if self.token is _NoAuthenticationToken and \
229-
self.cookie is _NoAuthenticationToken:
228+
self.cookie is _NoAuthenticationToken:
230229
# Not yet logged in.
231230
if self.autologin and self.username and self.password:
232231
# This will throw an uncaught
@@ -392,6 +391,10 @@ class Context(object):
392391
:param app: The app context of the namespace (optional, the default is "None").
393392
:type app: ``string``
394393
:param token: A session token. When provided, you don't need to call :meth:`login`.
394+
:type token: ``string``
395+
:param cookie: A session cookie. When provided, you don't need to call :meth:`login`.
396+
This parameter is only supported for Splunk 6.2+.
397+
:type cookie: ``string``
395398
:param username: The Splunk account username, which is used to
396399
authenticate the Splunk instance.
397400
:type username: ``string``
@@ -407,8 +410,10 @@ class Context(object):
407410
c.login()
408411
# Or equivalently
409412
c = binding.connect(username="boris", password="natasha")
410-
# Of if you already have a session token
413+
# Or if you already have a session token
411414
c = binding.Context(token="atg232342aa34324a")
415+
# Or if you already have a valid cookie
416+
c = binding.Context(cookie="splunkd_8089=...")
412417
"""
413418
def __init__(self, handler=None, **kwargs):
414419
self.http = HttpLib(handler)
@@ -423,8 +428,6 @@ def __init__(self, handler=None, **kwargs):
423428
self.username = kwargs.get("username", "")
424429
self.password = kwargs.get("password", "")
425430
self.autologin = kwargs.get("autologin", False)
426-
427-
# FIXME: update the docstrings for this
428431
self.cookie = kwargs.get("cookie", _NoAuthenticationToken)
429432
if self.cookie is None: # In case someone explicitly passes cookie=None
430433
self.cookie = _NoAuthenticationToken
@@ -434,14 +437,13 @@ def __init__(self, handler=None, **kwargs):
434437
def _auth_headers(self):
435438
"""Headers required to authenticate a request.
436439
437-
Assumes your ``Context`` already has a authentication token,
438-
either provided explicitly or obtained by logging into the
439-
Splunk instance.
440+
Assumes your ``Context`` already has a authentication token or
441+
cookie, either provided explicitly or obtained by logging
442+
into the Splunk instance.
440443
441444
:returns: A list of 2-tuples containing key and value
442445
"""
443446
if self.cookie is not _NoAuthenticationToken:
444-
#TODO: update docs!
445447
return [("cookie", self.cookie)]
446448
elif self.token is _NoAuthenticationToken:
447449
return []
@@ -1137,8 +1139,12 @@ def request(self, url, message, **kwargs):
11371139
response = record(response)
11381140
if 400 <= response.status:
11391141
raise HTTPError(response)
1140-
if response.has_key("Set-Cookie"):
1141-
self.cookie = response["Set-Cookie"]
1142+
1143+
# Update the cookie with any HTTP request
1144+
for key, value in response.headers:
1145+
if key.lower() == "set-cookie":
1146+
self.cookie = value
1147+
break
11421148
return response
11431149

11441150

@@ -1250,6 +1256,7 @@ def request(url, message, **kwargs):
12501256
"Host": host,
12511257
"User-Agent": "splunk-sdk-python/0.1",
12521258
"Accept": "*/*",
1259+
"Cookie": "1"
12531260
} # defaults
12541261
for key, value in message["headers"]:
12551262
head[key] = value

0 commit comments

Comments
 (0)