Consolidate Sending Service API calls in one class [MAILPOET-795]
This commit is contained in:
@@ -7,6 +7,7 @@ use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Services\Bridge;
|
||||
use MailPoet\Services\Bridge\API;
|
||||
use MailPoet\Util\Helpers;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
@@ -31,7 +32,7 @@ class Bounce {
|
||||
function initApi() {
|
||||
if(!$this->api) {
|
||||
$mailer_config = Mailer::getMailerConfig();
|
||||
$this->api = new Bounce\API($mailer_config['mailpoet_api_key']);
|
||||
$this->api = new API($mailer_config['mailpoet_api_key']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +141,7 @@ class Bounce {
|
||||
}
|
||||
|
||||
function processEmails(array $subscriber_emails) {
|
||||
$checked_emails = $this->api->check($subscriber_emails);
|
||||
$checked_emails = $this->api->checkBounces($subscriber_emails);
|
||||
$this->processApiResponse((array)$checked_emails);
|
||||
}
|
||||
|
||||
|
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
namespace MailPoet\Cron\Workers\Bounce;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class API {
|
||||
public $url = 'https://bridge.mailpoet.com/api/v0/bounces/search';
|
||||
public $api_key;
|
||||
|
||||
function __construct($api_key) {
|
||||
$this->api_key = $api_key;
|
||||
}
|
||||
|
||||
function check(array $emails) {
|
||||
$result = wp_remote_post(
|
||||
$this->url,
|
||||
$this->request($emails)
|
||||
);
|
||||
if(wp_remote_retrieve_response_code($result) === 200) {
|
||||
return json_decode(wp_remote_retrieve_body($result), true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function auth() {
|
||||
return 'Basic ' . base64_encode('api:' . $this->api_key);
|
||||
}
|
||||
|
||||
private function request($body) {
|
||||
return array(
|
||||
'timeout' => 10,
|
||||
'httpversion' => '1.0',
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Content-Type' => 'application/json',
|
||||
'Authorization' => $this->auth()
|
||||
),
|
||||
'body' => json_encode($body)
|
||||
);
|
||||
}
|
||||
}
|
@@ -4,18 +4,18 @@ namespace MailPoet\Mailer\Methods;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Config\ServicesChecker;
|
||||
use MailPoet\Services\Bridge;
|
||||
use MailPoet\Services\Bridge\API;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class MailPoet {
|
||||
public $url = 'https://bridge.mailpoet.com/api/v0/messages';
|
||||
public $api_key;
|
||||
public $api;
|
||||
public $sender;
|
||||
public $reply_to;
|
||||
public $services_checker;
|
||||
|
||||
function __construct($api_key, $sender, $reply_to) {
|
||||
$this->api_key = $api_key;
|
||||
$this->api = new API($api_key);
|
||||
$this->sender = $sender;
|
||||
$this->reply_to = $reply_to;
|
||||
$this->services_checker = new ServicesChecker(false);
|
||||
@@ -26,26 +26,23 @@ class MailPoet {
|
||||
$response = __('MailPoet API key is invalid!', 'mailpoet');
|
||||
return Mailer::formatMailerSendErrorResult($response);
|
||||
}
|
||||
|
||||
$message_body = $this->getBody($newsletter, $subscriber);
|
||||
$result = wp_remote_post(
|
||||
$this->url,
|
||||
$this->request($message_body)
|
||||
);
|
||||
if(is_wp_error($result)) {
|
||||
return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
|
||||
}
|
||||
$response_code = wp_remote_retrieve_response_code($result);
|
||||
if($response_code !== 201) {
|
||||
if($response_code === 401) {
|
||||
$result = $this->api->sendMessages($message_body);
|
||||
|
||||
switch($result['status']) {
|
||||
case API::SENDING_STATUS_CONNECTION_ERROR:
|
||||
return Mailer::formatMailerConnectionErrorResult($result['message']);
|
||||
case API::SENDING_STATUS_SEND_ERROR:
|
||||
if(!empty($result['code']) && $result['code'] === API::RESPONSE_CODE_KEY_INVALID) {
|
||||
Bridge::invalidateKey();
|
||||
}
|
||||
$response = (wp_remote_retrieve_body($result)) ?
|
||||
wp_remote_retrieve_body($result) :
|
||||
wp_remote_retrieve_response_message($result);
|
||||
return Mailer::formatMailerSendErrorResult($response);
|
||||
}
|
||||
return Mailer::formatMailerSendErrorResult($result['message']);
|
||||
case API::SENDING_STATUS_OK:
|
||||
default:
|
||||
return Mailer::formatMailerSendSuccessResult();
|
||||
}
|
||||
}
|
||||
|
||||
function processSubscriber($subscriber) {
|
||||
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriber_data);
|
||||
@@ -98,21 +95,4 @@ class MailPoet {
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
|
||||
function auth() {
|
||||
return 'Basic ' . base64_encode('api:' . $this->api_key);
|
||||
}
|
||||
|
||||
function request($body) {
|
||||
return array(
|
||||
'timeout' => 10,
|
||||
'httpversion' => '1.0',
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Content-Type' => 'application/json',
|
||||
'Authorization' => $this->auth()
|
||||
),
|
||||
'body' => json_encode($body)
|
||||
);
|
||||
}
|
||||
}
|
@@ -4,8 +4,17 @@ namespace MailPoet\Services\Bridge;
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class API {
|
||||
public $url = 'https://bridge.mailpoet.com/api/v0/me';
|
||||
public $api_key;
|
||||
const SENDING_STATUS_OK = 'ok';
|
||||
const SENDING_STATUS_CONNECTION_ERROR = 'connection_error';
|
||||
const SENDING_STATUS_SEND_ERROR = 'send_error';
|
||||
|
||||
const RESPONSE_CODE_KEY_INVALID = 401;
|
||||
|
||||
private $api_key;
|
||||
|
||||
public $url_me = 'https://bridge.mailpoet.com/api/v0/me';
|
||||
public $url_messages = 'https://bridge.mailpoet.com/api/v0/messages';
|
||||
public $url_bounces = 'https://bridge.mailpoet.com/api/v0/bounces/search';
|
||||
|
||||
function __construct($api_key) {
|
||||
$this->setKey($api_key);
|
||||
@@ -13,17 +22,10 @@ class API {
|
||||
|
||||
function checkKey() {
|
||||
$result = wp_remote_post(
|
||||
$this->url,
|
||||
$this->url_me,
|
||||
$this->request(array('site' => home_url()))
|
||||
);
|
||||
return $this->processResponse($result);
|
||||
}
|
||||
|
||||
function setKey($api_key) {
|
||||
$this->api_key = $api_key;
|
||||
}
|
||||
|
||||
private function processResponse($result) {
|
||||
$code = wp_remote_retrieve_response_code($result);
|
||||
switch($code) {
|
||||
case 200:
|
||||
@@ -41,6 +43,50 @@ class API {
|
||||
return array('code' => $code, 'data' => $body);
|
||||
}
|
||||
|
||||
function sendMessages($message_body) {
|
||||
$result = wp_remote_post(
|
||||
$this->url_messages,
|
||||
$this->request($message_body)
|
||||
);
|
||||
if(is_wp_error($result)) {
|
||||
return array(
|
||||
'status' => self::SENDING_STATUS_CONNECTION_ERROR,
|
||||
'message' => $result->get_error_message()
|
||||
);
|
||||
}
|
||||
$response_code = wp_remote_retrieve_response_code($result);
|
||||
if($response_code !== 201) {
|
||||
$response = (wp_remote_retrieve_body($result)) ?
|
||||
wp_remote_retrieve_body($result) :
|
||||
wp_remote_retrieve_response_message($result);
|
||||
return array(
|
||||
'status' => self::SENDING_STATUS_SEND_ERROR,
|
||||
'message' => $response,
|
||||
'code' => $response_code
|
||||
);
|
||||
}
|
||||
return array('status' => self::SENDING_STATUS_OK);
|
||||
}
|
||||
|
||||
function checkBounces(array $emails) {
|
||||
$result = wp_remote_post(
|
||||
$this->url,
|
||||
$this->request($emails)
|
||||
);
|
||||
if(wp_remote_retrieve_response_code($result) === 200) {
|
||||
return json_decode(wp_remote_retrieve_body($result), true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function setKey($api_key) {
|
||||
$this->api_key = $api_key;
|
||||
}
|
||||
|
||||
function getKey() {
|
||||
return $this->api_key;
|
||||
}
|
||||
|
||||
private function auth() {
|
||||
return 'Basic ' . base64_encode('api:' . $this->api_key);
|
||||
}
|
||||
|
@@ -3,7 +3,6 @@
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\Cron\CronHelper;
|
||||
use MailPoet\Cron\Workers\Bounce;
|
||||
use MailPoet\Cron\Workers\Bounce\API;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Models\Setting;
|
||||
|
@@ -4,7 +4,7 @@ namespace MailPoet\Cron\Workers\Bounce;
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class MockAPI {
|
||||
function check(array $emails) {
|
||||
function checkBounces(array $emails) {
|
||||
return array_map(
|
||||
function ($email) {
|
||||
return array(
|
||||
|
@@ -3,6 +3,7 @@
|
||||
use Codeception\Util\Stub;
|
||||
use MailPoet\Mailer\Methods\MailPoet;
|
||||
use MailPoet\Config\ServicesChecker;
|
||||
use MailPoet\Services\Bridge\API;
|
||||
|
||||
class MailPoetAPITest extends MailPoetTest {
|
||||
function _before() {
|
||||
@@ -68,17 +69,6 @@ class MailPoetAPITest extends MailPoetTest {
|
||||
expect($body[0]['text'])->equals($this->newsletter['body']['text']);
|
||||
}
|
||||
|
||||
function testItCanCreateRequest() {
|
||||
$body = $this->mailer->getBody($this->newsletter, $this->subscriber);
|
||||
$request = $this->mailer->request($body);
|
||||
expect($request['timeout'])->equals(10);
|
||||
expect($request['httpversion'])->equals('1.0');
|
||||
expect($request['method'])->equals('POST');
|
||||
expect($request['headers']['Content-Type'])->equals('application/json');
|
||||
expect($request['headers']['Authorization'])->equals($this->mailer->auth());
|
||||
expect($request['body'])->equals(json_encode($body));
|
||||
}
|
||||
|
||||
function testItCanProcessSubscriber() {
|
||||
expect($this->mailer->processSubscriber('test@test.com'))
|
||||
->equals(
|
||||
@@ -100,11 +90,6 @@ class MailPoetAPITest extends MailPoetTest {
|
||||
));
|
||||
}
|
||||
|
||||
function testItCanDoBasicAuth() {
|
||||
expect($this->mailer->auth())
|
||||
->equals('Basic ' . base64_encode('api:' . $this->settings['api_key']));
|
||||
}
|
||||
|
||||
function testItWillNotSendIfApiKeyIsMarkedInvalid() {
|
||||
if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
|
||||
$this->mailer->api_key = 'someapi';
|
||||
@@ -122,7 +107,7 @@ class MailPoetAPITest extends MailPoetTest {
|
||||
|
||||
function testItCannotSendWithoutProperApiKey() {
|
||||
if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
|
||||
$this->mailer->api_key = 'someapi';
|
||||
$this->mailer->api->setKey('someapi');
|
||||
$result = $this->mailer->send(
|
||||
$this->newsletter,
|
||||
$this->subscriber
|
||||
|
Reference in New Issue
Block a user