Skip to content

Commit 627581b

Browse files
refactor: Changing the request header to a private field
1 parent d7901f1 commit 627581b

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

Pilot/pilotTools.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,11 +709,10 @@ def sendMessage(url, pilotUUID, wnVO, method, rawMessage):
709709
caPath=caPath,
710710
certEnv=cert)
711711

712+
X509config.generateUserAgent(pilotUUID=pilotUUID)
713+
712714
# Do the request
713-
_res = X509config.executeRequest(raw_data=raw_data,
714-
headers={
715-
"User-Agent": X509config.generateUserAgent(pilotUUID=pilotUUID)
716-
})
715+
_res = X509config.executeRequest(raw_data=raw_data)
717716

718717

719718
class CommandBase(object):

Pilot/proxyTools.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ def __init__(self, url, caPath, name="unknown"):
8686
self.name = name
8787
self.url = url
8888
self.caPath = caPath
89+
self.headers = {
90+
"User-Agent": "Dirac Pilot [Unknown ID]"
91+
}
8992
# We assume we have only one context, so this variable could be shared to avoid opening n times a cert.
9093
# On the contrary, to avoid race conditions, we do avoid using "self.data" and "self.headers"
9194
self._context = None
@@ -97,19 +100,16 @@ def generateUserAgent(self, pilotUUID):
97100
98101
:param pilotUUID: Unique ID of the Pilot
99102
:type pilotUUID: str
100-
:type param_name: param_type
101-
:return: The generated user agent
102-
:rtype: str
103103
"""
104-
return "Dirac Pilot [%s]" % pilotUUID
104+
self.headers["User-Agent"] = "Dirac Pilot [%s]" % pilotUUID
105105

106106
def _prepareRequest(self):
107107
"""As previously, loads the SSL certificates of the server (to avoid "unknown issuer")"""
108108
# Load the SSL context
109109
self._context = ssl.create_default_context()
110110
self._context.load_verify_locations(capath=self.caPath)
111111

112-
def executeRequest(self, raw_data, headers={"User-Agent": "Dirac Pilot [Unknown ID]"}):
112+
def executeRequest(self, raw_data):
113113
"""Execute a HTTP request with the data, headers, and the pre-defined data (SSL + auth)
114114
115115
:param raw_data: Data to send
@@ -125,7 +125,7 @@ def executeRequest(self, raw_data, headers={"User-Agent": "Dirac Pilot [Unknown
125125
# Python2
126126
data = urlencode(raw_data)
127127

128-
request = Request(self.url, data=data, headers=headers)
128+
request = Request(self.url, data=data, headers=self.headers)
129129

130130
res = urlopen(request, context=self._context)
131131
res.close()
@@ -140,11 +140,10 @@ def __init__(self, url, caPath, jwtData):
140140
super(TokenBasedRequest, self).__init__(url, caPath, "TokenBasedConnection")
141141

142142
self.jwtData = jwtData
143-
144-
def executeRequest(self, raw_data, headers={"User-Agent": "Dirac Pilot [Unknown ID]"}):
143+
144+
def addJwtToHeader(self):
145145
# Adds the JWT in the HTTP request (in the Bearer field)
146-
headers["Authorization"] = "Bearer: %s" % self.jwtData
147-
return super(TokenBasedRequest, self).executeRequest(raw_data, headers)
146+
self.headers["Authorization"] = "Bearer: %s" % self.jwtData
148147

149148

150149
class X509BasedRequest(BaseConnectedRequest):
@@ -165,8 +164,8 @@ def __init__(self, url, caPath, certEnv):
165164
)
166165
self._hasExtraCredentials = True
167166

168-
def executeRequest(self, raw_data, headers={"User-Agent": "Dirac Pilot [Unknown ID]"}):
167+
def executeRequest(self, raw_data):
169168
# Adds a flag if the passed cert is a Directory
170169
if self._hasExtraCredentials:
171170
raw_data["extraCredentials"] = '"hosts"'
172-
return super(X509BasedRequest, self).executeRequest(raw_data, headers)
171+
return super(X509BasedRequest, self).executeRequest(raw_data)

0 commit comments

Comments
 (0)