Skip to content

Commit

Permalink
Merge pull request #78 from mailjet/v3.1_smart
Browse files Browse the repository at this point in the history
V3.1 smart
  • Loading branch information
Cedric "Aridjar" Paumard authored Feb 6, 2017
2 parents 8fa484f + 444c075 commit 73b2b89
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 35 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,48 @@ $response = $mj->post(Resources::$Parseroute, ['body' => $body]);

```


## New !! Specificities of the version 3.1 !

This version adds a new way to construct the Client or your call. We add the possibility to add an array with for parameters on both Client creation or API call (please, note that each of these parameters are preset and are not mandatory in the creation or the call) :

- url (api.mailjet.com)
- version (v3)
- call (true)
- secured (true (true give 'https' when false give 'http'))

### A basic example :

``` php
<?php
...

// We initialise the client with variables. We only keep the secured from the main configuration.
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'),
getenv('MJ_APIKEY_PRIVATE'),
['url' => "www.mailjet.com", 'version' => 'v3.1', 'call' => false]
);

// We only change the version for this call
$response = $mj->get(Resources::$Contact, [], ['version' => 'v3']);

```

###Here is the priority list :

Funtion call > Client creation > resource (only with version) > wrapper configuration

Note : the settings pasted in the call will only be used for this call.

TODO :
- add a functionality changing the Client configuration
- add anoter functionality setting the client to the main configuration

Here is the list of the functionalities working with the version 3.1 :

- RESSOURCES::$Email


## Send a pull request

- Fork the project.
Expand Down
136 changes: 121 additions & 15 deletions src/Mailjet/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,25 @@ class Client
private $apikey;
private $apisecret;

private $version = 'v3/';
private $secure = true;
private $version = Config::MAIN_VERSION;
private $url = Config::MAIN_URL;
private $secure = Config::SECURED;
private $call = true;
private $settings = [];
private $changed = false;

/**
* Client constructor requires:
* @param string $key Mailjet API Key
* @param string $secret Mailjet API Secret
* @param boolean $call performs the call or not
*/
public function __construct($key, $secret, $call = true)
{
$this->call = $call;
public function __construct($key, $secret, $call = true, array $settings = [])
{
$this->apikey = $key;
$this->apisecret = $secret;
$this->initSettings($call, $settings);
$this->setSettings();
}

/**
Expand Down Expand Up @@ -87,8 +91,8 @@ private function _call($method, $resource, $action, $args)
*/
private function getApiUrl()
{
$h = $this->secure ? 'https' : 'http';
return $h . '://api.mailjet.com/' . $this->version;
$h = $this->secure === true ? 'https' : 'http';
return $h."://".$this->url.'/'.$this->version.'/';
}

/**
Expand All @@ -97,9 +101,17 @@ private function getApiUrl()
* @param array $args Request arguments
* @return Response
*/
public function post($resource, $args = [])
public function post($resource, array $args = [], array $options = [])
{
return $this->_call('POST', $resource[0], $resource[1], $args);
if (!empty($options)) {
$this->setOptions($options, $resource);
}
$result = $this->_call('POST', $resource[0], $resource[1], $args);
if (!empty($this->changed)) {
$this->setSettings();
}

return $result;
}

/**
Expand All @@ -108,9 +120,16 @@ public function post($resource, $args = [])
* @param array $args Request arguments
* @return Response
*/
public function get($resource, $args = [])
public function get($resource, array $args = [], array $options = [])
{
return $this->_call('GET', $resource[0], $resource[1], $args);
if (!empty($options)) {
$this->setOptions($options, $resource);
}
$result = $this->_call('GET', $resource[0], $resource[1], $args);
if (!empty($this->changed)) {
$this->setSettings();
}
return $result;
}

/**
Expand All @@ -119,9 +138,16 @@ public function get($resource, $args = [])
* @param array $args Request arguments
* @return Response
*/
public function put($resource, $args = [])
public function put($resource, array $args = [], array $options = [])
{
return $this->_call('PUT', $resource[0], $resource[1], $args);
if (!empty($options)) {
$this->setOptions($options, $resource);
}
$result = $this->_call('PUT', $resource[0], $resource[1], $args);
if (!empty($this->changed)) {
$this->setSettings();
}
return $result;
}

/**
Expand All @@ -130,9 +156,16 @@ public function put($resource, $args = [])
* @param array $args Request arguments
* @return Response
*/
public function delete($resource, $args = [])
public function delete($resource, array $args = [], array $options = [])
{
return $this->_call('DELETE', $resource[0], $resource[1], $args);
if (!empty($options)) {
$this->setOptions($options, $resource);
}
$result = $this->_call('DELETE', $resource[0], $resource[1], $args);
if (!empty($this->changed)) {
$this->setSettings();
}
return $result;
}

/**
Expand Down Expand Up @@ -183,4 +216,77 @@ public function setSecureProtocol($bIsSecured)

return false;
}

// TODO : make the next code more readable

/**
* Temporary set the variables generating the url
* @param array $options contain temporary modifications for the client
* @param array $resource may contain the version linked to the ressource
*/
private function setOptions($options, $resource)
{
if (!empty($options['version']) && is_string($options['version'])) {
$this->version = $options['version'];
} elseif (!empty($resource[2])) {
$this->version = $resource[2];
} if (!empty($options['url']) && is_string($options['url'])) {
$this->url = $options['url'];
} if (isset($options['secured']) && is_bool($options['secured'])) {
$this->secure = $options['secured'];
} if (isset($options['call']) && is_bool($options['call'])) {
$this->call = $options['call'];
}
$this->changed = true;
}

/**
* set back the variables generating the url
*/
private function setSettings()
{
if (!empty($this->settings['url']) && is_string($this->settings['url'])) {
$this->url = $this->settings['url'];
} if (!empty($this->settings['version']) && is_string($this->settings['version'])) {
$this->version = $this->settings['version'];
} if (isset($this->settings['call']) && is_bool($this->settings['call'])) {
$this->call = $this->settings['call'];
} if (isset($this->settings['secured']) && is_bool($this->settings['secured'])) {
$this->secure = $this->settings['secured'];
}
$this->changed = false;
}

/**
* Set a backup if the variables generating the url are change during a call.
*/
private function initSettings($call, $settings = [])
{
if (!empty($settings['url']) && is_string($settings['url'])) {
$this->settings['url'] = $settings['url'];
} else {
$this->settings['url'] = $this->url;
}

if (!empty($settings['version']) && is_string($settings['version'])) {
$this->settings['version'] = $settings['version'];
} else {
$this->settings['version'] = $this->version;
}

$settings['call'] = $call;
if (isset($settings['call']) && is_bool($settings['call'])) {
$this->settings['call'] = $settings['call'];
} else {
$this->settings['call'] = $this->call;
}

if (isset($settings['secured']) && is_bool($settings['secured'])) {
$this->settings['secured'] = $settings['secured'];
} else {
$this->settings['secured'] = $this->secure;
}

$this->changed = false;
}
}
3 changes: 3 additions & 0 deletions src/Mailjet/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ class Config
{
const WRAPPER_VERSION = 'v1.1.8';
const USER_AGENT = 'mailjet-apiv3-php/';
const MAIN_VERSION = 'v3';
const MAIN_URL = "api.mailjet.com";
const SECURED = true;
}
5 changes: 2 additions & 3 deletions src/Mailjet/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ public function call($call)
'auth' => $this->auth,
($this->type === 'application/json' ? 'json' : 'body') => $this->body,
];

$response = null;
if ($call) {
try {
$response = call_user_func_array(
array($this, strtolower($this->method)), [
$this->url, $payload]
[$this, strtolower($this->method)],
[$this->url, $payload]
);
}
catch (ClientException $e) {
Expand Down
14 changes: 1 addition & 13 deletions src/Mailjet/Resources.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
<?php
/**
* PHP version 5
*
* This is the Mailjet Resources file
*
* @category Mailjet_API
* @package Mailjet-apiv3
* @author Guillaume Badi <[email protected]>
* @license MIT https://opensource.org/licenses/MIT
* @link dev.mailjet.com
*/

namespace Mailjet;

/**
Expand All @@ -26,7 +14,7 @@
*/
class Resources
{
public static $Email = ['send', ''];
public static $Email = ['send', ''/*, 'v3.1'*/];
public static $Aggregategraphstatistics = ['aggregategraphstatistics', ''];
public static $Apikey = ['apikey', ''];
public static $Apikeyaccess = ['apikeyaccess', ''];
Expand Down
25 changes: 21 additions & 4 deletions test/Mailjet/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class MailjetTest extends \PHPUnit_Framework_TestCase
{
private function assertUrl($url, $response)
private function assertUrl($url, $response, $version = 'v3')
{
$this->assertEquals('https://api.mailjet.com/v3'.$url, $response->request->getUrl());
$this->assertEquals('https://api.mailjet.com/'.$version.$url, $response->request->getUrl());
}

public function assertPayload($payload, $response)
Expand All @@ -24,7 +24,7 @@ public function assertFilters($shouldBe, $response)

public function testGet()
{
$client = new Client('', '', false);
$client = new Client('', '', ['call' => false]);

$this->assertUrl('/REST/contact', $client->get(Resources::$Contact));

Expand All @@ -51,7 +51,7 @@ public function testGet()

public function testPost()
{
$client = new Client('', '', false);
$client = new Client('', '', ['call' => false]);

$email = [
'FromName' => 'Mailjet PHP test',
Expand All @@ -67,4 +67,21 @@ public function testPost()
$this->assertUrl('/send', $ret);
$this->assertPayload($email, $ret);
}

public function testPostV3_1()
{
$client = new Client('', '', ['call' => false]);

$email = [
'Messages' => [[
'From' => ['Email' => "[email protected]", 'Name' => "Mailjet PHP test"],
'TextPart' => "Simple Email test",
'To' => [['Email' => "[email protected]", 'Name' => 'Test']]
]]
];

$ret = $client->post(Resources::$Email, ['body' => $email], ['version' => 'v3.1']);
$this->assertUrl('/send', $ret, 'v3.1');
$this->assertPayload($email, $ret);
}
}

0 comments on commit 73b2b89

Please sign in to comment.