@@ -83,6 +83,51 @@ def __init__(self, api_url='https://localhost:8000', debug_http=False, ignore_ss
8383 self ._ssl_verify = not ignore_ssl_errors
8484 self .auth = {}
8585
86+ def req_get (self , path ):
87+ '''
88+ A thin wrapper from get http method of saltstack api
89+ api = Pepper('http://ipaddress/api/')
90+ print(api.login('salt','salt','pam'))
91+ print(api.req_get('/keys'))
92+ '''
93+
94+ import requests
95+
96+ headers = {
97+ 'Accept' : 'application/json' ,
98+ 'Content-Type' : 'application/json' ,
99+ 'X-Requested-With' : 'XMLHttpRequest' ,
100+ }
101+ if self .auth and 'token' in self .auth and self .auth ['token' ]:
102+ headers .setdefault ('X-Auth-Token' , self .auth ['token' ])
103+ else :
104+ raise PepperException ('Authentication required' )
105+ return
106+ # Optionally toggle SSL verification
107+ #self._ssl_verify = self.ignore_ssl_errors
108+ params = {'url' : self ._construct_url (path ),
109+ 'headers' : headers ,
110+ 'verify' : self ._ssl_verify == True ,
111+ }
112+ try :
113+ resp = requests .get (** params )
114+
115+ if resp .status_code == 401 :
116+ raise PepperException (str (resp .status_code ) + ':Authentication denied' )
117+ return
118+
119+ if resp .status_code == 500 :
120+ raise PepperException (str (resp .status_code ) + ':Server error.' )
121+ return
122+
123+ if resp .status_code == 404 :
124+ raise PepperException (str (resp .status_code ) + ' :This request returns nothing.' )
125+ return
126+ except PepperException as e :
127+ print (e )
128+ return
129+ return resp .json ()
130+
86131 def req (self , path , data = None ):
87132 '''
88133 A thin wrapper around urllib2 to send requests and return the response
0 commit comments