@@ -197,19 +197,30 @@ CREATE OR REPLACE
197
197
FUNCTION @extschema@._CDB_Group_API_Auth(username text , password text )
198
198
RETURNS TEXT AS
199
199
$$
200
+ import sys
200
201
import base64
201
- return base64 .encodestring (' %s:%s' % (username, password)).replace(' \n ' , ' ' )
202
+
203
+ data_to_encode = ' %s:%s' % (username, password)
204
+ if sys .version_info [0 ] < 3 :
205
+ data_encoded = base64 .encodestring (data_to_encode)
206
+ else:
207
+ data_encoded = base64 .b64encode (data_to_encode .encode ()).decode()
208
+
209
+ data_encoded = data_encoded .replace (' \n ' , ' ' )
210
+ return data_encoded
202
211
$$ LANGUAGE ' @@plpythonu@@' VOLATILE PARALLEL UNSAFE;
203
212
204
213
-- url must contain a '%s' placeholder that will be replaced by current_database, for security reasons.
205
214
CREATE OR REPLACE
206
215
FUNCTION @extschema@._CDB_Group_API_Request(method text , url text , body text , valid_return_codes int [])
207
216
RETURNS int AS
208
217
$$
218
+ python_v2 = True
209
219
try:
210
- import httplib as client
220
+ import httplib as client
211
221
except:
212
- from http import client
222
+ from http import client
223
+ python_v2 = False
213
224
214
225
params = plpy .execute (" select c.host, c.port, c.timeout, c.auth from @extschema@._CDB_Group_API_Conf() c;" )[0 ]
215
226
if params[' host' ] is None:
222
233
last_err = None
223
234
while retry > 0 :
224
235
try:
225
- conn = SD[' groups_api_client' ] = client .HTTPConnection (params[' host' ], params[' port' ], False, params[' timeout' ])
236
+ if python_v2:
237
+ conn = SD[' groups_api_client' ] = client .HTTPConnection (params[' host' ], params[' port' ], False, params[' timeout' ])
238
+ else:
239
+ conn = SD[' groups_api_client' ] = client .HTTPConnection (params[' host' ], port= params[' port' ], timeout= params[' timeout' ])
226
240
database_name = plpy .execute (" select current_database();" )[0 ][' current_database' ]
227
241
conn .request (method, url .format (database_name), body, headers)
228
242
response = conn .getresponse ()
0 commit comments