Skip to content

Commit

Permalink
Merge pull request #109 from thinkshout/oauth-with-api-key
Browse files Browse the repository at this point in the history
Refactors classes to allow api_key and oauth access_tokens
  • Loading branch information
aprice42 authored Feb 10, 2023
2 parents 8f9737c + 47fedcf commit 31ba958
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 303 deletions.
105 changes: 15 additions & 90 deletions src/Mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Mailchimp\http\MailchimpCurlHttpClient;
use Mailchimp\http\MailchimpGuzzleHttpClient;
use Mailchimp\http\MailchimpHttpClientInterface;
use Mailchimp\MailchimpApiInterface;

/**
* Mailchimp library.
*
* @package Mailchimp
*/
class Mailchimp {
class Mailchimp implements MailchimpApiInterface {

const VERSION = '2.0.0';
const DEFAULT_DATA_CENTER = 'us1';
Expand Down Expand Up @@ -46,7 +47,7 @@ class Mailchimp {
*
* @var MailchimpHttpClientInterface $client
*/
protected $client;
public $client;

/**
* The REST API endpoint.
Expand Down Expand Up @@ -79,6 +80,13 @@ class Mailchimp {
*/
private $debug_error_code;

/**
* Authentication settings.
*
* @var array $authentication_settings
*/
protected $authentication_settings;

/**
* Array of pending batch operations.
*
Expand All @@ -91,18 +99,16 @@ class Mailchimp {
/**
* Mailchimp constructor.
*
* @param string $api_key
* The Mailchimp API key.
* @param string $api_user
* The Mailchimp API username.
* @param array $authentication_settings
* Authentication settings.
* @param array $http_options
* HTTP client options.
* @param MailchimpHttpClientInterface $client
* Optional custom HTTP client. $http_options are ignored if this is set.
*/
public function __construct($api_key, $api_user = 'apikey', $http_options = [], MailchimpHttpClientInterface $client = NULL) {
$this->api_key = $api_key;
$this->api_user = $api_user;
public function __construct($authentication_settings, $http_options = [], MailchimpHttpClientInterface $client = NULL) {
$this->api_key = $authentication_settings['api_key'];
$this->api_user = $authentication_settings['api_user'];

$dc = $this->getDataCenter($this->api_key);

Expand All @@ -116,87 +122,6 @@ public function __construct($api_key, $api_user = 'apikey', $http_options = [],
}
}

/**
* Sets a custom HTTP client to be used for all API requests.
*
* @param \Mailchimp\http\MailchimpHttpClientInterface $client
* The HTTP client.
*/
public function setClient(MailchimpHttpClientInterface $client) {
$this->client = $client;
}

/**
* Sets a Mailchimp error code to be returned by all requests.
*
* Used to test and debug error handling.
*
* @param string $error_code
* The Mailchimp error code.
*/
public function setDebugErrorCode($error_code) {
$this->debug_error_code = $error_code;
}

/**
* Gets Mailchimp account information for the authenticated account.
*
* @param array $parameters
* Associative array of optional request parameters.
*
* @return object
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/root/#read-get_root
*/
public function getAccount($parameters = []) {
return $this->request('GET', '/', NULL, $parameters);
}

/**
* Processes all pending batch operations.
*
* @throws MailchimpAPIException
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/batches/#create-post_batches
*/
public function processBatchOperations() {
$parameters = [
'operations' => $this->batch_operations,
];

try {
$response = $this->request('POST', '/batches', NULL, $parameters);

// Reset batch operations.
$this->batch_operations = [];

return $response;

}
catch (MailchimpAPIException $e) {
$message = 'Failed to process batch operations: ' . $e->getMessage();
throw new MailchimpAPIException($message, $e->getCode(), $e);
}
}

/**
* Gets the status of a batch request.
*
* @param string $batch_id
* The ID of the batch operation.
*
* @return object
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/batches/#read-get_batches_batch_id
*/
public function getBatchOperation($batch_id) {
$tokens = [
'batch_id' => $batch_id,
];

return $this->request('GET', '/batches/{batch_id}', $tokens);
}

/**
* Adds a pending batch operation.
*
Expand Down
113 changes: 18 additions & 95 deletions src/Mailchimp2.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Mailchimp\http\MailchimpCurlHttpClient;
use Mailchimp\http\MailchimpGuzzleHttpClient;
use Mailchimp\http\MailchimpHttpClientInterface;
use Mailchimp\MailchimpApiInterface;

/**
* Mailchimp library with access token authentication.
* Mailchimp library with OAuth support.
*
* @package Mailchimp
*/
class Mailchimp2 {
class Mailchimp2 implements MailchimpApiInterface {

const VERSION = '2.0.0';
const DEFAULT_DATA_CENTER = 'us1';
Expand Down Expand Up @@ -46,7 +47,7 @@ class Mailchimp2 {
*
* @var MailchimpHttpClientInterface $client
*/
protected $client;
public $client;

/**
* The REST API endpoint.
Expand Down Expand Up @@ -88,24 +89,27 @@ class Mailchimp2 {
*/
private $batch_operations;

/**
* Authentication settings.
*
* @var array $authentication_settings
*/
protected $authentication_settings;

/**
* Mailchimp constructor.
*
* @param string $access_token
* The Mailchimp Access token.
* @param string $data_center
* The Mailchimp data center associated with the account.
* @param string $api_user
* The Mailchimp API username.
* @param array $authentication_settings
* Authentication settings.
* @param array $http_options
* HTTP client options.
* @param MailchimpHttpClientInterface $client
* Optional custom HTTP client. $http_options are ignored if this is set.
*/
public function __construct($access_token, $data_center, $api_user = 'OAuth', $http_options = [], MailchimpHttpClientInterface $client = NULL) {
$this->access_token = $access_token;
$this->api_user = $api_user;
$this->endpoint = str_replace(Mailchimp::DEFAULT_DATA_CENTER, $data_center, $this->endpoint);
public function __construct($authentication_settings, $http_options = [], MailchimpHttpClientInterface $client = NULL) {
$this->access_token = $authentication_settings['access_token'];
$this->api_user = $authentication_settings['api_user'];
$this->endpoint = str_replace(Mailchimp::DEFAULT_DATA_CENTER, $authentication_settings['data_center'], $this->endpoint);

if (!empty($client)) {
$this->client = $client;
Expand All @@ -115,87 +119,6 @@ public function __construct($access_token, $data_center, $api_user = 'OAuth', $h
}
}

/**
* Sets a custom HTTP client to be used for all API requests.
*
* @param \Mailchimp\http\MailchimpHttpClientInterface $client
* The HTTP client.
*/
public function setClient(MailchimpHttpClientInterface $client) {
$this->client = $client;
}

/**
* Sets a Mailchimp error code to be returned by all requests.
*
* Used to test and debug error handling.
*
* @param string $error_code
* The Mailchimp error code.
*/
public function setDebugErrorCode($error_code) {
$this->debug_error_code = $error_code;
}

/**
* Gets Mailchimp account information for the authenticated account.
*
* @param array $parameters
* Associative array of optional request parameters.
*
* @return object
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/root/#read-get_root
*/
public function getAccount($parameters = []) {
return $this->request('GET', '/', NULL, $parameters);
}

/**
* Processes all pending batch operations.
*
* @throws MailchimpAPIException
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/batches/#create-post_batches
*/
public function processBatchOperations() {
$parameters = [
'operations' => $this->batch_operations,
];

try {
$response = $this->request('POST', '/batches', NULL, $parameters);

// Reset batch operations.
$this->batch_operations = [];

return $response;

}
catch (MailchimpAPIException $e) {
$message = 'Failed to process batch operations: ' . $e->getMessage();
throw new MailchimpAPIException($message, $e->getCode(), $e);
}
}

/**
* Gets the status of a batch request.
*
* @param string $batch_id
* The ID of the batch operation.
*
* @return object
*
* @see http://developer.mailchimp.com/documentation/mailchimp/reference/batches/#read-get_batches_batch_id
*/
public function getBatchOperation($batch_id) {
$tokens = [
'batch_id' => $batch_id,
];

return $this->request('GET', '/batches/{batch_id}', $tokens);
}

/**
* Adds a pending batch operation.
*
Expand Down Expand Up @@ -242,7 +165,7 @@ protected function addBatchOperation($method, $path, $parameters = []) {
}

/**
* Makes a request to the Mailchimp API.
* Makes a request to the Mailchimp API using OAuth.
*
* @param string $method
* The REST method to use when making the request.
Expand Down
38 changes: 38 additions & 0 deletions src/MailchimpApiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Mailchimp;

use Mailchimp\http\MailchimpHttpClientInterface;

/**
* Defines an interface for a Mailchimp API object.
*
* @internal
*/
interface MailchimpApiInterface {

/**
* Makes a request to the Mailchimp API.
*
* @param string $method
* The REST method to use when making the request.
* @param string $path
* The API path to request.
* @param array $tokens
* Associative array of tokens and values to replace in the path.
* @param array $parameters
* Associative array of parameters to send in the request body.
* @param bool $batch
* TRUE if this request should be added to pending batch operations.
* @param bool $returnAssoc
* TRUE to return Mailchimp API response as an associative array.
*
* @return mixed
* Object or Array if $returnAssoc is TRUE.
*
* @throws MailchimpAPIException
*/
public function request($method, $path, $tokens = NULL, $parameters = [], $batch = FALSE, $returnAssoc = FALSE);


}
Loading

0 comments on commit 31ba958

Please sign in to comment.