From ff0d89b4419c4cc9eac5d34937c0179c60127b0e Mon Sep 17 00:00:00 2001 From: rtx3 Date: Thu, 18 Aug 2016 16:00:44 +0800 Subject: [PATCH] Add HTTP GET method --- pepper/libpepper.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/pepper/libpepper.py b/pepper/libpepper.py index 7e9219e..b542dd2 100644 --- a/pepper/libpepper.py +++ b/pepper/libpepper.py @@ -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