You are here

protected function DrupalMandrill::request in Mandrill 7

Make a request to Mandrill's API.

Every API call uses this function to actually make the request to Mandrill's servers.

@link https://mandrillapp.com/api/docs/

Parameters

string $method: API method name

array $args: query arguments

string $http: GET or POST request type

string $output: API response format (json,php,xml,yaml). json and xml are decoded into arrays automatically.

Return value

array Array on success.

Throws

MandrillException.

30 calls to DrupalMandrill::request()
DrupalMandrill::messages_search in lib/mandrill.inc
@link https://mandrillapp.com/api/docs/messages.html#method=search
DrupalMandrill::messages_send in lib/mandrill.inc
@link https://mandrillapp.com/api/docs/messages.html#method=send
DrupalMandrill::messages_send_template in lib/mandrill.inc
@link https://mandrillapp.com/api/docs/messages.html#method=send-template
DrupalMandrill::senders_domains in lib/mandrill.inc
@link https://mandrillapp.com/api/docs/senders.html#method=domains
DrupalMandrill::senders_info in lib/mandrill.inc
@link https://mandrillapp.com/api/docs/senders.html#method=info

... See full list

File

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

Class

DrupalMandrill
Class DrupalMandrill.

Code

protected function request($method, $args = array(), $http = 'POST', $output = 'json') {
  if (!isset($args['key'])) {
    $args['key'] = $this->api;
  }
  $api_version = self::API_VERSION;
  $dot_output = 'json' == $output ? '' : ".{$output}";
  $url = self::END_POINT . "{$api_version}/{$method}{$dot_output}";
  $params = drupal_json_encode($args);
  switch ($http) {
    case 'GET':
      $url .= '?' . $params;
      $response = drupal_http_request($url, array(
        'method' => 'GET',
        'timeout' => $this->timeout,
      ));
      break;
    case 'POST':
      $response = drupal_http_request($url, array(
        'method' => 'POST',
        'data' => $params,
        'timeout' => $this->timeout,
      ));
      break;
    default:
      throw new MandrillException('Unknown request type');
  }
  $response_code = $response->code;
  if (0 == $response_code) {
    return $response->error;
  }
  $body = $response->data;
  switch ($output) {
    case 'json':
      $body = json_decode($body, TRUE);
      break;
    case 'php':
      $body = unserialize($body);
      break;
  }
  if (200 == $response_code) {
    return $body;
  }
  else {
    $message = isset($body['message']) ? $body['message'] : $body;
    if (is_array($message)) {
      $message = "Unspecified Error";
    }
    throw new MandrillException($message, $response_code);
  }
}