Index: drupalgapps.module
===================================================================
--- drupalgapps.module	(revision 523)
+++ drupalgapps.module	(working copy)
@@ -74,7 +74,7 @@
     '#description' => t('domain name for which you/your organisation has/have registered Google Apps. Example : www.example.com'),
     '#size' => 30,
     '#required' => TRUE,
-    '#default_value' => variable_get('gapps_domain_name', ''),
+    '#default_value' => variable_get('drupalgapps_domain_name', ''),
   );
 
   $form['domain_info']['organisation'] = array(
@@ -82,7 +82,7 @@
     '#title'=> t('Organisation Name (Optional)'),
     '#description' => t('Organisation to which this domain belong or The organisation which is going to use  Google apps on this Domain. Which ever more specific.'),
     '#size' => 30,
-    '#default_value' => variable_get('gapps_organisation_name', ''),
+    '#default_value' => variable_get('drupalgapps_organisation_name', ''),
   );
 
   $form['admin_credentials'] = array(
@@ -100,7 +100,7 @@
     '#default_value' => 'admin@example.com',
     '#description' => t('Enter the ADMIN EMAIL (ful ) . Preferably you should create a seperate email id with admin rights (privilages), and donot use anywhere esle, i.e Specially/only used by this module. The reason being, the password for that email/account should not be changed, else you will also have to update in this modules setting immediately, else this module and all the modules using this module will be disabled'),
     '#size' => 30,
-    '#default_value' => trim(aes_decrypt(variable_get('gapps_admin_email', ''))),
+    '#default_value' => trim(aes_decrypt(variable_get('drupalgapps_admin_email', ''))),
   );
 
   $form['admin_credentials']['admin_password'] = array(
@@ -108,7 +108,7 @@
     '#title' => t('Password for the admin email'),
     '#required' => TRUE,
     '#size' => 30,
-    '#default_value' => trim(aes_decrypt(variable_get('gapps_admin_password', ''))),
+    '#default_value' => trim(aes_decrypt(variable_get('drupalgapps_admin_password', ''))),
   );
 
 
@@ -124,7 +124,7 @@
     '#type' => 'select',
     '#title' => t('Revewal rate of auth. token (in Hours)'),
     '#options' => drupal_map_assoc(range(1, 23)),
-    '#default_value' => variable_get('gapps_token_renewal_rate', 23),
+    '#default_value' => variable_get('drupalgapps_token_renewal_rate', 23),
     '#description' => t('ONLY FOR ADVANCED USERS.<br/> DO NOT change it untill you have knowledge about the Authentication token involved in provisioning API authentication.<br/><br/> Though, the authenication token used for provisioning API expires every 24 hours, but to eliminate any last minute glichtes, by DEFAULT the module runs the cron job and renew the token in every 23 hrs.  '),
   );
 
@@ -139,7 +139,7 @@
   $form['token_policy']['on_failure']['send_email'] = array(
     '#type' => 'checkbox',
     '#title' => t('Send Email to Administrator'),
-    '#default_value' => variable_get('gapps_email_send_option', 1),
+    '#default_value' => variable_get('drupalgapps_email_send_option', 1),
     '#description' => t('Enabled by default. On enabling this , Drupal will mail admin if token expires or get revoked or anyother reason due to which Provisioning API doesnot function.'),
   );
 
@@ -149,7 +149,7 @@
     '#description' => t('If left Empty, the email will be sent to the administrator account used to fetch the authentication token, i.e. Enter above.'),
     '#size' => 30,
     '#required' => FALSE,
-    '#default_value' => variable_get('gapps_failure_email', ''),
+    '#default_value' => variable_get('drupalgapps_failure_email', ''),
   );
 
   $form['proxy_config'] = array(
@@ -165,7 +165,7 @@
     '#description' => t('If your webserver is under any procy server, through which it is accessing Internet, Please enter Proxy server IP address and port Details in form <b>[ip address]:[port]</b> , eg : 172.16.68.6:3128'),
     '#size' => 30,
     '#required' => FALSE,
-    '#default_value' => variable_get('gapps_proxy', ''),
+    '#default_value' => variable_get('drupalgapps_proxy', ''),
   );
 
   $form['submit'] = array(
@@ -190,45 +190,43 @@
   }
   else {
     $domain_name = $form_state['values']['domain_name'];
-    variable_set('gapps_domain_name', $domain_name);
+    variable_set('drupalgapps_domain_name', $domain_name);
 
     $organisation_name = $form_state['values']['organisation'];
-    variable_set('gapps_organisation_name', $organisation_name);
+    variable_set('drupalgapps_organisation_name', $organisation_name);
 
     $admin_email_address = $form_state['values']['admin_email'];
     $encrypted_admin_email = aes_encrypt($admin_email_address);
-    variable_set('gapps_admin_email', $encrypted_admin_email);
+    variable_set('drupalgapps_admin_email', $encrypted_admin_email);
 
     $admin_email_password = $form_state['values']['admin_password'];
     $encrypted_admin_password = aes_encrypt($admin_email_password);
-    variable_set('gapps_admin_password', $encrypted_admin_password);
+    variable_set('drupalgapps_admin_password', $encrypted_admin_password);
 
     $renewal_rate = $form_state['values']['renewal_rate'];
-    variable_set('gapps_token_renewal_rate', $renewal_rate);
+    variable_set('drupalgapps_token_renewal_rate', $renewal_rate);
 
     $email_send_option = $form_state['values']['send_email'];
-    variable_set('gapps_email_send_option', $email_send_option);
+    variable_set('drupalgapps_email_send_option', $email_send_option);
 
     $failure_email_address = $form_state['values']['failure_email'];
-    variable_set('gapps_failure_email', $failure_email_address);
+    variable_set('drupalgapps_failure_email', $failure_email_address);
 
     $proxy_address = $form_state['values']['proxy'];
     if ($proxy_address) {
-      variable_set('gapps_proxy', $proxy_address);
+      variable_set('drupalgapps_proxy', $proxy_address);
     }
     else {
-      variable_set('gapps_proxy', NULL);
+      variable_set('drupalgapps_proxy', NULL);
     }
 
     drupalgapps_get_authtoken();
   }
-
 }
 
-/*
+/**
  * The function to fetch the Authentication token
  */
-
 function drupalgapps_get_authtoken() {
 
   /*
@@ -244,9 +242,9 @@
   */
 
 
-  $admin_api_email = trim(aes_decrypt(variable_get('gapps_admin_email', '')));
-  $admin_api_password = trim(aes_decrypt(variable_get('gapps_admin_password', '')));
-  $proxy_add = variable_get('gapps_proxy', NULL);
+  $admin_api_email = trim(aes_decrypt(variable_get('drupalgapps_admin_email', '')));
+  $admin_api_password = trim(aes_decrypt(variable_get('drupalgapps_admin_password', '')));
+  $proxy_add = variable_get('drupalgapps_proxy', NULL);
   if ($proxy_add) {
     $proxy_exist = 1;
   }
@@ -364,7 +362,7 @@
         'drupalgapps_cron' => 'Google apps authentication token renewal tasks',
       );
     case 'rule':
-      return '* */' . variable_get('gapps_token_renewal_rate', 23) . ' * * *';
+      return '* */' . variable_get('drupalgapps_token_renewal_rate', 23) . ' * * *';
   }
 }
 
@@ -405,8 +403,8 @@
   ));
 
 
-  if (variable_get('gapps_failure_email', NULL) != NULL) {
-    drupal_mail('drupalgapps', 'warning', variable_get('gapps_failure_email', variable_get('site_mail', NULL)), user_preferred_language($account), $params, NULL, TRUE);
+  if (variable_get('drupalgapps_failure_email', NULL) != NULL) {
+    drupal_mail('drupalgapps', 'warning', variable_get('drupalgapps_failure_email', variable_get('site_mail', NULL)), user_preferred_language($account), $params, NULL, TRUE);
   }
   else {
     drupal_mail('drupalgapps', 'warning', $admin_api_email, user_preferred_language($account), $params, NULL, TRUE);
@@ -431,52 +429,50 @@
   }
 }
 
-
 /**
- * Google Apps API : wrapper of provisioning API.
+ * Client login
  */
-function drupalgapps_make_gaccount($gaccount, $firstname, $lastname, $password, $quota = '', $changePassword = TRUE, $suspended = FALSE, $admin = FALSE) {
-  $auth = variable_get('gapps_prov_api_auth_token', NULL);
-  $proxy_add = variable_get('gapps_proxy', NULL);
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
+function drupalgapps_clientlogin_api_request($url, $method = 'POST', $data = NULL) {
+  // Set up some variables.
+  $auth = variable_get('drupalgapps_prov_api_auth_token', NULL);
+  $proxy_add = variable_get('drupalgapps_proxy', NULL);
+  $proxy_exist = $proxy_add ? 1 : 0;
 
-  drupal_set_message($proxy_add);
+  // Initialize curl.
+  $ch = curl_init();
 
-
-  $data = <<<XML
-<?xml version="1.0" encoding="UTF-8"?>
-<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
-xmlns:apps="http://schemas.google.com/apps/2006">
-<atom:category scheme="http://schemas.google.com/g/2005#kind"
-term="http://schemas.google.com/apps/2006#user"/>
-<apps:login userName="$gaccount"
-password="$password" suspended="$suspended" changePasswordAtNextLogin="$changePassword"/>
-<apps:quota limit="$quota"/>
-<apps:name familyName="$lastname" givenName="$firstname"/>
-</atom:entry>
-XML;
-
- // $header="Authorization: GoogleLogin auth=DQAAAH4AAACdyei2Q271TJ-66IpnQijWT9MYhiGsmg3L2wGDVY6piy1Ca4WXd0jm_XGbbgAQcpzwbRohR-UecAbN1Dog2O3KOTNDgstBxR-eSAz5-ze4oQtS4h5JAMXpw_-T2P5R29LpWafbJaPkyAoqdg12F5nqRpapo_bUEiIV9tFxbzJhxA ";
-  //drupal_set_message($auth);
+  // Configure the header.  This will be different depending on whether we're
+  // posting or getting data.
   $header = 'Authorization: GoogleLogin auth=' . $auth . ' ';
-  drupal_set_message($header);
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
+  if ($data) {
+    $header = $header . "\nContent-type: application/atom+xml";
+    $header = $header . "\nContent-Length :" . strlen($data);
+    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+  }
 
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, 'https://apps-apis.google.com/a/feeds/jiitu.org/user/2.0');
+  // Set the method, defaults to POST.
+  switch ($method) {
+    case 'PUT':
+      curl_setopt($ch, CURLOPT_PUT, TRUE);
+      break;
+    case 'DELETE':
+      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
+      break;
+    case 'GET':
+      curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
+      break;
+  }
+
+  // Set up remaining curl parameters.
+  curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
   curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
   curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
+
+  // Get the response.
   $result = curl_exec($ch);
   $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 
@@ -485,8 +481,9 @@
       case 200:
       case 201:
       case 202:
-        drupal_set_message('Account Created');
-        break;
+        // Return good result.
+        $result = urlencode($result);
+        return $result;
       case 400:
         drupal_set_message('Error 400');
         break;
@@ -501,28 +498,53 @@
     }
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
+    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell (unknown error).'), WATCHDOG_WARNING);
     drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
     return FALSE;
   }
 
-  watchdog('Google Apps', 'response code : '.$code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
+  watchdog('Google Apps', 'Error response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
+  return FALSE;
 }
 
 
+/**
+ * Google Apps API : wrapper of provisioning API.
+ */
+function drupalgapps_make_gaccount($gaccount, $firstname, $lastname, $password, $quota = '', $changePassword = TRUE, $suspended = FALSE, $admin = FALSE) {
 
-// @TODO : Refactoring : rather then having so much arguments , just pass $gaccount as an array. with fname, lastname, password and key and value
+  $data = <<<XML
+<?xml version="1.0" encoding="UTF-8"?>
+<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
+xmlns:apps="http://schemas.google.com/apps/2006">
+<atom:category scheme="http://schemas.google.com/g/2005#kind"
+term="http://schemas.google.com/apps/2006#user"/>
+<apps:login userName="$gaccount"
+password="$password" suspended="$suspended" changePasswordAtNextLogin="$changePassword"/>
+<apps:quota limit="$quota"/>
+<apps:name familyName="$lastname" givenName="$firstname"/>
+</atom:entry>
+XML;
+  $url = 'https://apps-apis.google.com/a/feeds/jiitu.org/user/2.0';
+  $success = drupalgapps_clientlogin_api_request($url, 'POST', $data);
+  if ($success) {
+    drupal_set_message(t('Google account created'));
+  }
+  else {
+    drupal_set_message(t('Failed to create Google account'), 'error');
+  }
+}
 
-// @TODO : Refactoring : Modularize the CURL calls by implimenting a function
 
+// @TODO : Refactoring : rather then having so much arguments , just pass $gaccount as an array. with fname, lastname, password and key and value
 // @TODO : Refactoring : picking up XML files should be modularised
 
 
 /*
 
 function drupalgapps_task_gaccount($parameters) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
+  $auth = variable_get('drupalgapps_prov_api_auth_token', '');
+  $proxy_add = variable_get('drupalgapps_proxy', '');
 
   $post_data = '
     <atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
@@ -586,69 +608,16 @@
  * Retrieve a user account.
  */
 function drupalgapps_retrieve_gaccount($username) {
-  drupal_set_message($username);
-
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-  drupal_set_message($proxy_add);
   $url = 'https://apps-apis.google.com/a/feeds/jiitu.org/user/2.0/' . $username;
-  drupal_set_message($auth);
-  $header = "Authorization: GoogleLogin auth=".$auth." ";
-  drupal_set_message($header);
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  drupal_set_message($result);
-  $result = urlencode($result);
-  drupal_set_message($result);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Account Retrieved');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'GET');
+  if ($result) {
+    drupal_set_message(t('Google account retrieved'));
+    // @TODO: do something with the result.
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to retrieve Google account'), 'error');
   }
 
-  watchdog('Google Apps', 'response code : '.$code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
-
   return;
 }
 
@@ -657,21 +626,10 @@
  * Update a user account.
  */
 function drupalgapps_update_gaccount($gaccount, $firstname = NULL, $lastname = NULL, $password = NULL, $changePassword = NULL, $quota = NULL, $suspended = NULL, $admin = NULL) {
-
-  $auth = variable_get('gapps_prov_api_auth_token', '');    $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
   $data = '<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
           xmlns:apps="http://schemas.google.com/apps/2006">
             <atom:category scheme="http://schemas.google.com/g/2005#kind"
-                term="http://schemas.google.com/apps/2006#user"/>
-      ';
+                term="http://schemas.google.com/apps/2006#user"/>';
   if (($password) || ($suspended !== NULL) || ($changePassword !== NULL)) {
     $post_data .= '<apps:login' .
       ($password ? ' password="' . $password . '" ' : '') .
@@ -690,71 +648,20 @@
   }
   $data .= '</atom:entry>';
 
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
   $url = 'https://apps-apis.google.com/a/feeds/jiitu.org/user/2.0/' . $username;
-
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_PUT, TRUE);
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Account Created');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'PUT', $data);
+  if ($result) {
+    drupal_set_message(t('Google account updated'));
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to update Google account'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 /**
  * Rename a Google user account.
  */
 function drupalgapps_rename_gaccount($oldname, $newname) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $data =<<<XML
         <?xml version="1.0" encoding="UTF-8"?>
     <atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
@@ -773,55 +680,14 @@
     </atom:entry>
 XML;
 
-
-
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
   $url = 'https://apps-apis.google.com/a/feeds/jiitu.org/user/2.0/' . $username;
-
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_PUT, TRUE);
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('User renaming successful');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-      default;
-        watchdog('Google Apps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'PUT', $data);
+  if ($result) {
+    drupal_set_message(t('Google account renamed'));
   }
   else {
-    watchdog('Google Apps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to rename Google account'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 
@@ -829,75 +695,20 @@
  * Delete a Google user account.
  */
 function drupalgapps_delete_gaccount($username) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
   $url = 'https://apps-apis.google.com/a/feeds/jiitu.org/user/2.0/' . $username;
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('User Deleted successful');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-      default;
-        watchdog('Google Apps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'DELETE');
+  if ($result) {
+    drupal_set_message(t('Google account deleted'));
   }
   else {
-    watchdog('Google Apps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to delete Google account'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
-
 }
 
 /**
  * Create a Google nickname.
  */
 function drupalgapps_create_nickname($gaccount, $nickname) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');    $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $data=<<<XML
         <atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
           xmlns:apps="http://schemas.google.com/apps/2006">
@@ -909,168 +720,43 @@
 XML;
 
 
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, 'https://apps-apis.google.com/a/feeds/domain/nickname/2.0');
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Nickname Created');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $url = 'https://apps-apis.google.com/a/feeds/domain/nickname/2.0';
+  $result = drupalgapps_clientlogin_api_request($url, 'POST', $data);
+  if ($result) {
+    drupal_set_message(t('Google nickname created'));
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to create Google nickname'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 /**
  * Retrieve a Google nickname.
  */
 function drupalgapps_retrieve_nickname($username) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $url = 'https://apps-apis.google.com/a/feeds/jiitu.org/user/2.0/' . $username;
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Nickname Retrieved');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'GET');
+  if ($result) {
+    drupal_set_message(t('Google nickname retrieved'));
+    // @TODO: do something with the result.
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to retrieve Google nickname'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
-  return ;
 }
 
 /**
  * Delete a Google nickname.
  */
 function drupalgapps_delete_nickname($username) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $url = 'https://apps-apis.google.com/a/feeds/domain/nickname/2.0/' . $username;
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Nickname Deleted successful');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('Google Apps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'DELETE');
+  if ($result) {
+    drupal_set_message(t('Google nickname deleted'));
   }
   else {
-    watchdog('Google Apps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to delete Google nickname'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 
@@ -1078,16 +764,6 @@
  * Create a Google user group.
  */
 function drupalgapps_create_group($group_id, $group_name, $description, $email_permission) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
   $data=<<<XML
         <atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006" xmlns:gd="http://schemas.google.com/g/2005">
   <apps:property name="groupId" value="'.$group_id.'"></apps:property>
@@ -1097,50 +773,14 @@
 </atom:entry>
 XML;
 
-
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, 'https://apps-apis.google.com/a/feeds/domain/nickname/2.0');
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Group  Created');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/';
+  $result = drupalgapps_clientlogin_api_request($url, 'POST');
+  if ($result) {
+    drupal_set_message(t('Google group created'));
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to create Google group'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : '.$code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 
@@ -1149,17 +789,6 @@
  * Update a Google user group.
  */
 function drupalgapps_update_group($group_id, $group_name, $description, $email_permission) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $data = '
         <atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006" xmlns:gd="http://schemas.google.com/g/2005">
         <apps:property name="groupId" value="'.$group_id.'"></apps:property>
@@ -1175,57 +804,14 @@
   }
   $data .= '</atom:entry>';
 
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id;
-
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_PUT, TRUE);
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Group updated');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'PUT', $data);
+  if ($result) {
+    drupal_set_message(t('Google group updated'));
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to update Google group'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
-
 }
 
 
@@ -1233,60 +819,15 @@
  * Retrieve a Google user group.
  */
 function drupalgapps_retrieve_groups($member_id) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/?member=' . $member_id . '[&[directOnly=]]';
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Groups Retrieved');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'GET');
+  if ($result) {
+    drupal_set_message(t('Google group retrieved'));
+    // @TODO: do something with the result.
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to retrieve Google group'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
-  return;
 }
 
 
@@ -1295,59 +836,14 @@
  * Delete a Google user group.
  */
 function drupalgapps_delete_group($group_id) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id;
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Group Deleted successful');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('Google Apps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'DELETE');
+  if ($result) {
+    drupal_set_message(t('Google group deleted'));
   }
   else {
-    watchdog('Google Apps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to delete Google group'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 
@@ -1356,17 +852,6 @@
  * Add a member to a Google group.
  */
 function drupalgapps_add_member($group_id, $member_id) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $data=<<<XML
        <?xml version="1.0" encoding="UTF-8"?>
 <atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
@@ -1376,51 +861,14 @@
 </atom:entry>
 XML;
 
-
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id . '/member';
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('member Added');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'POST', $data);
+  if ($result) {
+    drupal_set_message(t('Google group member added'));
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to add member to Google group'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 
@@ -1431,17 +879,6 @@
  * Add a Google group to a group.
  */
 function drupalgapps_add_group($group_id) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $data=<<<XML
        <?xml version="1.0" encoding="UTF-8"?>
 <atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
@@ -1452,49 +889,13 @@
 XML;
 
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id . '/member';
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('member Added');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'POST', $data);
+  if ($result) {
+    drupal_set_message(t('Google group added to group'));
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to add group to Google group'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 
@@ -1502,180 +903,44 @@
  * Retrieve all members of a Google group.
  */
 function drupalgapps_retrieve_group_members($group_id) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id . '/member[?[start=]]';
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Groups members Retrieved');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'GET');
+  if ($result) {
+    drupal_set_message(t('Google group members retrieved'));
+    // @TODO: do something with $result.
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to retrieve Google group members'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
-  return;
 }
 
 /**
  * Retrieve a member from a Google group.
  */
 function drupalgapps_retrieve_group_member($group_id, $member_id) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id . '/member/' . $member_id;
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Groups Retrieved');
-        break;
-      case 400:
-          drupal_set_message('Error 400');
-          break;
-      case 415:
-          drupal_set_message('Error 415');
-          break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'GET');
+  if ($result) {
+    drupal_set_message(t('Google group member retrieved'));
+    // @TODO: do something with $result.
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to retrieve Google group member'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
-  return;
 }
 
 /**
  * Remove a Google group member.
  */
 function drupalgapps_delete_member($group_id, $member_id) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id . '/member/' . $member_id;
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Group Deleted successful');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('Google Apps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'DELETE');
+  if ($result) {
+    drupal_set_message(t('Google group member deleted'));
   }
   else {
-    watchdog('Google Apps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to delete Google group member'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 
@@ -1683,17 +948,6 @@
  * Add an owner member to a Google group.
  */
 function drupalgapps_add_owner($group_id, $email) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $data=<<<XML
       <?xml version="1.0" encoding="UTF-8"?>
 <atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
@@ -1704,49 +958,13 @@
 XML;
 
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id . '/owner';
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('member Added');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'POST', $data);
+  if ($result) {
+    drupal_set_message(t('Owner member added to Google group'));
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to add owner member to Google group'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 
@@ -1754,17 +972,6 @@
  * Add an owner group to a Google group.
  */
 function drupalgapps_add_ownergroup($group_id, $group_email) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $data=<<<XML
       <?xml version="1.0" encoding="UTF-8"?>
 <atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
@@ -1775,49 +982,13 @@
 XML;
 
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id . '/owner';
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-  $header = $header . "\nContent-type: application/atom+xml";
-  $header = $header . "\nContent-Length :" . strlen($data);
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('group owner  Added');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'POST', $data);
+  if ($result) {
+    drupal_set_message(t('Owner group added to Google group'));
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to add owner group to Google group'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
 
@@ -1825,118 +996,28 @@
  * Retrieve an owner of a Google group.
  */
 function drupalgapps_retrieve_group_owner($group_id) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id . '/owner';
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Groups Retrieved');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'GET');
+  if ($result) {
+    drupal_set_message(t('Google group owner retrieved'));
+    // @TODO: do something with $result.
   }
   else {
-    watchdog('drupalgapps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to retrieve Google group owner'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
-  return;
 }
 
 /**
  * Remove an owner from a Google group.
  */
 function drupalgapps_delete_owner($group_id, $owner_email) {
-  $auth = variable_get('gapps_prov_api_auth_token', '');
-  $proxy_add = variable_get('gapps_proxy', '');
-
-  if ($proxy_add) {
-    $proxy_exist = 1;
-  }
-  else {
-    $proxy_exist = 0;
-  }
-
-
   $url = 'https://apps-apis.google.com/a/feeds/group/2.0/domain/' . $group_id . '/owner/' . $owner_email;
-  $header = "Authorization: GoogleLogin auth=" . $auth;
-
-  $ch = curl_init();
-  curl_setopt($ch, CURLOPT_URL, $url);
-  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-  curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
-  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
-  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_exist);
-  curl_setopt($ch, CURLOPT_PROXY, $proxy_add);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
-  $result = curl_exec($ch);
-  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-  if ($code) {
-    switch ($code) {
-      case 200:
-      case 201:
-      case 202:
-        drupal_set_message('Group owner Deleted successful');
-        break;
-      case 400:
-        drupal_set_message('Error 400');
-        break;
-      case 415:
-        drupal_set_message('Error 415');
-        break;
-
-      default;
-        watchdog('Google Apps', t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), WATCHDOG_WARNING);
-        drupal_set_message(t('The HTTPS request to Google seems to be broken (HTTP %code).', array('%code' => $httpresult->code)), 'error');
-        return FALSE;
-    }
+  $result = drupalgapps_clientlogin_api_request($url, 'DELETE');
+  if ($result) {
+    drupal_set_message(t('Google group owner deleted'));
   }
   else {
-    watchdog('Google Apps', t('The HTTPS request to Google seems to be broken like hell  (unknown error).'), WATCHDOG_WARNING);
-    drupal_set_message(t('The HTTPS request to Google seems to be broken like hell (unknown error).'), 'error');
-    return FALSE;
+    drupal_set_message(t('Failed to delete Google group owner'), 'error');
   }
-
-  watchdog('Google Apps', 'response code : ' . $code, array(), WATCHDOG_CRITICAL, l(t('DrupalGapps Settings'), 'admin/settings/drupalgapps'));
 }
 
Index: drupalgapps.install
===================================================================
--- drupalgapps.install	(revision 519)
+++ drupalgapps.install	(working copy)
@@ -0,0 +1,29 @@
+<?php
+
+/**
+ * @file
+ * Installation file for drupalgapps module.
+ */
+
+/**
+ * Implementation of hook_install().
+ */
+function drupalgapps_install() {
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function drupalgapps_uninstall() {
+  // Remove drupalgapps variables.
+  variable_del('drupalgapps_domain_name');
+  variable_del('drupalgapps_organisation_name');
+  variable_del('drupalgapps_admin_email');
+  variable_del('drupalgapps_admin_password');
+  variable_del('drupalgapps_token_renewal_rate');
+  variable_del('drupalgapps_email_send_option');
+  variable_del('drupalgapps_failure_email');
+  variable_del('drupalgapps_proxy');
+  variable_del('drupalgapps_prov_api_auth_token');
+}
+
