gajus / drill

无附加功能的Mandrill API接口。

0.0.3 2014-08-26 15:50 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:45:09 UTC


README

Build Status Coverage Status Latest Stable Version License

无附加功能的 Mandrill API 接口。

Drill 客户端 实现不对底层的API架构或所需参数做出任何假设。然而,如果根据响应没有提供所需参数,它将抛出异常。因此,它不是一个会防止你的代码在Mandrill API更改时崩溃的抽象。它用于与API端点交互和处理错误。换句话说,Mandrill RESTful API实现了版本控制,这应该可以防止在API更改时破坏使用Drill实现的代码。

提供的唯一方法是 api

/**
 * @param string $key Mandrill API key.
 */
$drill = new \Gajus\Drill\Client('NlLdhX5FVKS55VBK1xPW_g');

/**
 * @see https://mandrillapp.com/api/docs/messages.JSON.html
 * @param string $path
 * @param array $parameters
 */
$response = $drill->api('messages/send', [
    'message' => [
        'text' => 'Test',
        'subject' => 'test',
        'from_email' => 'dummy@gajus.com',
        'to' => [
            ['email' => 'dummy@gajus.com']
        ],
    ]
]);

将Mandrill响应转换为关联数组

array(1) {
  [0]=>
  array(4) {
    ["email"]=>
    string(15) "dummy@gajus.com"
    ["status"]=>
    string(4) "sent"
    ["_id"]=>
    string(32) "f65f65c266f74e2884344ccfff3bb337"
    ["reject_reason"]=>
    NULL
  }
}

错误处理

可以在向API发出请求之前捕获的情况或Drill实现强制执行的规则(而不是API规范)将抛出 Gajus\Drill\Exception\InvalidArgumentException

运行时发生的所有错误都将导致 Gajus\Drill\Exception\ErrorException

请注意,Mandrill API返回的错误具有不一致的命名约定(驼峰式与下划线,例如:"UserError","Invalid_Key")。Drill将所有错误转换为驼峰式约定(例如:"Invalid_Key"变为"InvalidKeyException")。

$drill = new \Gajus\Drill\Client('fxBTBjWKxJ05K9MjkFak1A');

try {
  $response = $drill->api('messages/send', [
      'message' => [
          'text' => 'Test',
          'subject' => 'test',
          'from_email' => 'invalidemail',
          'to' => [
              ['email' => 'dummy@gajus.com']
          ],
      ]
  ]);
} catch (\Gajus\Drill\Exception\RuntimeException\ValidationErrorException $e) {
    // @see https://mandrillapp.com/api/docs/messages.html
} catch (\Gajus\Drill\Exception\RuntimeException\UserErrorException $e) {
    // @see https://mandrillapp.com/api/docs/messages.html
} catch (\Gajus\Drill\Exception\RuntimeException\UnknownSubaccountException $e) {
    // @see https://mandrillapp.com/api/docs/messages.html
} catch (\Gajus\Drill\Exception\RuntimeException\PaymentRequiredException $e) {
    // @see https://mandrillapp.com/api/docs/messages.html
} catch (\Gajus\Drill\Exception\RuntimeException\GeneralErrorException $e) {
    // @see https://mandrillapp.com/api/docs/messages.html
} catch (\Gajus\Drill\Exception\RuntimeException\ValidationErrorException $e) {
    // @see https://mandrillapp.com/api/docs/messages.html
} catch (\Gajus\Drill\Exception\RuntimeException $e) {
    // All possible API errors.
} catch (\Gajus\Drill\Exception\InvalidArgumentException $e) {
    // Invalid SDK use errors.
} catch (\Gajus\Drill\Exception\DrillException $e) {
    // Everything.
}