2929class Connector :
3030 # Default headers, as required by Redfish spec
3131 # https://redfish.dmtf.org/schemas/DSP0266_1.5.0.html#request-headers
32- DEFAULT_HEADERS = {
33- "Accept" : "application/json" ,
34- "OData-Version" : "4.0"
35- }
32+ DEFAULT_HEADERS = {"Accept" : "application/json" , "OData-Version" : "4.0" }
3633 DEFAULT_TIMEOUT = 1 # In seconds
3734
3835 def __init__ (self , base_url , username , password , verify = True , timeout = DEFAULT_TIMEOUT ):
@@ -55,33 +52,37 @@ def _url(self, path):
5552
5653 def _log_request (self , method , path , payload , headers ):
5754 try :
58- logger .debug (json .dumps (dict (
59- request = dict (
60- method = method ,
61- base_url = self ._base_url ,
62- path = path ,
63- payload = payload ,
64- headers = headers ,
55+ logger .debug (
56+ json .dumps (
57+ dict (
58+ request = dict (
59+ method = method ,
60+ base_url = self ._base_url ,
61+ path = path ,
62+ payload = payload ,
63+ headers = headers ,
64+ )
65+ )
6566 )
66- )))
67+ )
6768 except Exception as e :
6869 logger .error (e )
6970
7071 def _log_response (self , method , path , response , json_data ):
7172 try :
72- logger .debug (json . dumps ( dict (
73- request_data = dict (
74- method = method ,
75- base_url = self ._base_url ,
76- path = path
77- ) ,
78- response = dict (
79- status_code = response .status_code ,
80- headers = dict ( response . headers . lower_items ()) ,
81- content = str ( response . content ),
82- json_data = json_data
73+ logger .debug (
74+ json . dumps (
75+ dict (
76+ request_data = dict ( method = method , base_url = self ._base_url , path = path ) ,
77+ response = dict (
78+ status_code = response . status_code ,
79+ headers = dict (response . headers . lower_items ()),
80+ content = str ( response .content ) ,
81+ json_data = json_data ,
82+ ),
83+ )
8384 )
84- )))
85+ )
8586 except Exception as e :
8687 logger .error (e )
8788
@@ -94,25 +95,16 @@ def _request(self, method, path, payload=None, headers=None, data=None, files=No
9495 args ["files" ] = files
9596 try :
9697 resp = self ._client .request (
97- method ,
98- self ._url (path ),
99- ** args ,
100- headers = headers ,
101- timeout = self ._timeout
98+ method , self ._url (path ), ** args , headers = headers , timeout = self ._timeout
10299 )
103100 except requests .exceptions .ConnectionError :
104- raise InaccessibleException (
105- "Endpoint at {} is not accessible" .format (self ._base_url ))
101+ raise InaccessibleException ("Endpoint at {} is not accessible" .format (self ._base_url ))
106102
107103 if resp .status_code == 401 :
108104 self ._unset_header ("x-auth-token" )
109105 self .login ()
110106 resp = self ._client .request (
111- method ,
112- self ._url (path ),
113- ** args ,
114- headers = headers ,
115- timeout = self ._timeout
107+ method , self ._url (path ), ** args , headers = headers , timeout = self ._timeout
116108 )
117109
118110 try :
@@ -159,9 +151,14 @@ def _has_session_support(self):
159151 return bool (self ._session_path )
160152
161153 def _session_login (self ):
162- resp = self ._client .post (self ._url (self ._session_path ), json = dict (
163- UserName = self ._username , Password = self ._password ,
164- ), timeout = self ._timeout )
154+ resp = self ._client .post (
155+ self ._url (self ._session_path ),
156+ json = dict (
157+ UserName = self ._username ,
158+ Password = self ._password ,
159+ ),
160+ timeout = self ._timeout ,
161+ )
165162 if resp .status_code not in [201 , 204 ]:
166163 raise AuthException ("Cannot create session: {}" .format (resp .text ))
167164
@@ -189,12 +186,13 @@ def _session_logout(self):
189186 self ._unset_header ("x-auth-token" )
190187
191188 def _basic_login (self ):
192- secret = "Basic {}" .format (base64 .b64encode (
193- "{}:{}" .format (self ._username , self ._password ).encode ("ascii" ),
194- ).decode ("ascii" ))
189+ secret = "Basic {}" .format (
190+ base64 .b64encode (
191+ "{}:{}" .format (self ._username , self ._password ).encode ("ascii" ),
192+ ).decode ("ascii" )
193+ )
195194 resp = self ._client .get (
196- self ._url (self ._basic_path ), headers = dict (authorization = secret ),
197- timeout = self ._timeout
195+ self ._url (self ._basic_path ), headers = dict (authorization = secret ), timeout = self ._timeout
198196 )
199197 if resp .status_code != 200 :
200198 raise AuthException ("Invalid credentials" )
0 commit comments