Skip to content

Commit f9df3d6

Browse files
refactor: Changing the request header to a private field
1 parent 67e7a6a commit f9df3d6

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
@@ -711,11 +711,10 @@ def sendMessage(url, pilotUUID, wnVO, method, rawMessage):
711711
caPath=caPath,
712712
certEnv=cert)
713713

714+
X509config.generateUserAgent(pilotUUID=pilotUUID)
715+
714716
# Do the request
715-
_res = X509config.executeRequest(raw_data=raw_data,
716-
headers={
717-
"User-Agent": X509config.generateUserAgent(pilotUUID=pilotUUID)
718-
})
717+
_res = X509config.executeRequest(raw_data=raw_data)
719718

720719

721720
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)