Skip to content

Commit 0a02fcc

Browse files
authored
Merge pull request #109 from BenjaminSchiborr/features/event_stream
Adds ability to request a stream from the salt rest api
2 parents 4a7ff5d + 656cc73 commit 0a02fcc

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

pepper/libpepper.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,59 @@ def __init__(self, api_url='https://localhost:8000', debug_http=False, ignore_ss
8080
self.debug_http = int(debug_http)
8181
self._ssl_verify = not ignore_ssl_errors
8282
self.auth = {}
83+
84+
def req_stream(self, path):
85+
'''
86+
A thin wrapper to get a response from saltstack api.
87+
The body of the response will not be downloaded immediately.
88+
Make sure to close the connection after use.
89+
api = Pepper('http://ipaddress/api/')
90+
print(api.login('salt','salt','pam'))
91+
response = api.req_stream('/events')
92+
93+
:param path: The path to the salt api resource
94+
95+
:return: :class:`Response <Response>` object
96+
97+
:rtype: requests.Response
98+
'''
99+
import requests
100+
101+
headers = {
102+
'Accept': 'application/json',
103+
'Content-Type': 'application/json',
104+
'X-Requested-With': 'XMLHttpRequest',
105+
}
106+
if self.auth and 'token' in self.auth and self.auth['token']:
107+
headers.setdefault('X-Auth-Token', self.auth['token'])
108+
else:
109+
raise PepperException('Authentication required')
110+
return
111+
# Optionally toggle SSL verification
112+
#self._ssl_verify = self.ignore_ssl_errors
113+
params = {'url': self._construct_url(path),
114+
'headers': headers,
115+
'verify': self._ssl_verify == True,
116+
'stream': True
117+
}
118+
try:
119+
resp = requests.get(**params)
120+
121+
if resp.status_code == 401:
122+
raise PepperException(str(resp.status_code) + ':Authentication denied')
123+
return
124+
125+
if resp.status_code == 500:
126+
raise PepperException(str(resp.status_code) + ':Server error.')
127+
return
128+
129+
if resp.status_code == 404:
130+
raise PepperException(str(resp.status_code) +' :This request returns nothing.')
131+
return
132+
except PepperException as e:
133+
print(e)
134+
return
135+
return resp
83136

84137
def req_get(self, path):
85138
'''

0 commit comments

Comments
 (0)