@@ -86,6 +86,9 @@ def __init__(self, url, caPath, name="unknown"):
86
86
self .name = name
87
87
self .url = url
88
88
self .caPath = caPath
89
+ self .headers = {
90
+ "User-Agent" : "Dirac Pilot [Unknown ID]"
91
+ }
89
92
# We assume we have only one context, so this variable could be shared to avoid opening n times a cert.
90
93
# On the contrary, to avoid race conditions, we do avoid using "self.data" and "self.headers"
91
94
self ._context = None
@@ -97,19 +100,16 @@ def generateUserAgent(self, pilotUUID):
97
100
98
101
:param pilotUUID: Unique ID of the Pilot
99
102
:type pilotUUID: str
100
- :type param_name: param_type
101
- :return: The generated user agent
102
- :rtype: str
103
103
"""
104
- return "Dirac Pilot [%s]" % pilotUUID
104
+ self . headers [ "User-Agent" ] = "Dirac Pilot [%s]" % pilotUUID
105
105
106
106
def _prepareRequest (self ):
107
107
"""As previously, loads the SSL certificates of the server (to avoid "unknown issuer")"""
108
108
# Load the SSL context
109
109
self ._context = ssl .create_default_context ()
110
110
self ._context .load_verify_locations (capath = self .caPath )
111
111
112
- def executeRequest (self , raw_data , headers = { "User-Agent" : "Dirac Pilot [Unknown ID]" } ):
112
+ def executeRequest (self , raw_data ):
113
113
"""Execute a HTTP request with the data, headers, and the pre-defined data (SSL + auth)
114
114
115
115
:param raw_data: Data to send
@@ -125,7 +125,7 @@ def executeRequest(self, raw_data, headers={"User-Agent": "Dirac Pilot [Unknown
125
125
# Python2
126
126
data = urlencode (raw_data )
127
127
128
- request = Request (self .url , data = data , headers = headers )
128
+ request = Request (self .url , data = data , headers = self . headers )
129
129
130
130
res = urlopen (request , context = self ._context )
131
131
res .close ()
@@ -140,11 +140,10 @@ def __init__(self, url, caPath, jwtData):
140
140
super (TokenBasedRequest , self ).__init__ (url , caPath , "TokenBasedConnection" )
141
141
142
142
self .jwtData = jwtData
143
-
144
- def executeRequest (self , raw_data , headers = { "User-Agent" : "Dirac Pilot [Unknown ID]" } ):
143
+
144
+ def addJwtToHeader (self ):
145
145
# 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
148
147
149
148
150
149
class X509BasedRequest (BaseConnectedRequest ):
@@ -165,8 +164,8 @@ def __init__(self, url, caPath, certEnv):
165
164
)
166
165
self ._hasExtraCredentials = True
167
166
168
- def executeRequest (self , raw_data , headers = { "User-Agent" : "Dirac Pilot [Unknown ID]" } ):
167
+ def executeRequest (self , raw_data ):
169
168
# Adds a flag if the passed cert is a Directory
170
169
if self ._hasExtraCredentials :
171
170
raw_data ["extraCredentials" ] = '"hosts"'
172
- return super (X509BasedRequest , self ).executeRequest (raw_data , headers )
171
+ return super (X509BasedRequest , self ).executeRequest (raw_data )
0 commit comments