You are here

public function DrupalMandrill::call in Mandrill 7.2

Override call method to user Drupal's HTTP handling.

File

lib/mandrill.inc, line 56
Wrapper class around the Mandrill API.

Class

DrupalMandrill
Class DrupalMandrill.

Code

public function call($url, $params) {
  $params['key'] = $this->apikey;
  $params = drupal_json_encode($params);
  $response = drupal_http_request($this->root . $url . '.json', array(
    'method' => 'POST',
    'data' => $params,
    'headers' => array(
      'Content-Type' => 'application/json',
      'Accept-Language' => language_default()->language,
      'User-Agent' => $this->userAgent,
    ),
    'timeout' => $this->timeout,
  ));
  if (!empty($response->error)) {
    if ($response->error == "Internal Server Error") {
      throw new Mandrill_HttpError('Mandrill API call to ' . $url . ' failed: ' . $response->error . ' ' . $response->data);
    }
    else {
      throw new Mandrill_HttpError('Mandrill API call to ' . $url . ' failed: ' . $response->error);
    }
  }
  $result = drupal_json_decode($response->data);
  if ($result === NULL) {
    throw new Mandrill_Error('We were unable to decode the JSON response from the Mandrill API: ' . $response->data);
  }
  if (floor($response->code / 100) >= 4) {
    throw $this
      ->castError($result);
  }
  return $result;
}