Skip to content

Commit

Permalink
Merge pull request #80 from rtx3/patch-1
Browse files Browse the repository at this point in the history
Add HTTP GET method and 404 error handler
  • Loading branch information
whiteinge authored Oct 26, 2016
2 parents 3d44019 + ff0d89b commit 29b2b6a
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pepper/libpepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,51 @@ def __init__(self, api_url='https://localhost:8000', debug_http=False, ignore_ss
self._ssl_verify = not ignore_ssl_errors
self.auth = {}

def req_get(self, path):
'''
A thin wrapper from get http method of saltstack api
api = Pepper('http://ipaddress/api/')
print(api.login('salt','salt','pam'))
print(api.req_get('/keys'))
'''

import requests

headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
}
if self.auth and 'token' in self.auth and self.auth['token']:
headers.setdefault('X-Auth-Token', self.auth['token'])
else:
raise PepperException('Authentication required')
return
# Optionally toggle SSL verification
#self._ssl_verify = self.ignore_ssl_errors
params = {'url': self._construct_url(path),
'headers': headers,
'verify': self._ssl_verify == True,
}
try:
resp = requests.get(**params)

if resp.status_code == 401:
raise PepperException(str(resp.status_code) + ':Authentication denied')
return

if resp.status_code == 500:
raise PepperException(str(resp.status_code) + ':Server error.')
return

if resp.status_code == 404:
raise PepperException(str(resp.status_code) +' :This request returns nothing.')
return
except PepperException as e:
print(e)
return
return resp.json()

def req(self, path, data=None):
'''
A thin wrapper around urllib2 to send requests and return the response
Expand Down

0 comments on commit 29b2b6a

Please sign in to comment.