 FUNCTION @extschema@._CDB_Group_API_Request(method text, url text, body text, valid_return_codes int[])
     RETURNS int AS
 $$
-    try:
-        import httplib as client
-    except:
-        from http import client
+    python_v2 = True
+    try:
+      import httplib as client
+    except:
+      from http import client
+      python_v3 = False
 
     params = plpy.execute("select c.host, c.port, c.timeout, c.auth from @extschema@._CDB_Group_API_Conf() c;")[0]
     if params['host'] is None:

FUNCTION @extschema@._CDB_Group_API_Auth(username text, password text)
    RETURNS TEXT AS
$$
    import base64
    return base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
$$ LANGUAGE '@@plpythonu@@' VOLATILE PARALLEL UNSAFE;

-- url must contain a '%s' placeholder that will be replaced by current_database, for security reasons.

    last_err = None
    while retry > 0:
      try:
        if python_v2:
          conn = SD['groups_api_client'] = client.HTTPConnection(params['host'], params['port'], False, params['timeout'])
        else:
          conn = SD['groups_api_client'] = client.HTTPConnection(params['host'], port=params['port'], timeout=params['timeout'])
        database_name = plpy.execute("select current_database();")[0]['current_database']
        conn.request(method, url.format(database_name), body, headers)
        response = conn.getresponse()
