Skip to content

Commit

Permalink
Merge pull request mautic#69 from escopecz/api-improvements
Browse files Browse the repository at this point in the history
API updates for 3060
  • Loading branch information
alanhartless authored Dec 12, 2016
2 parents 99d5c34 + 19c2468 commit 7d3e676
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 13 deletions.
37 changes: 37 additions & 0 deletions lib/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,43 @@ public function makeRequest($endpoint, array $parameters = array(), $method = 'G
return $response;
}

/**
* Returns HTTP response info
*
* @return array
*/
public function getResponseInfo()
{
return $this->auth->getResponseInfo();
}

/**
* Returns HTTP response headers
*
* @return array
*/
public function getResponseHeaders()
{
return $this->auth->getResponseHeaders();
}

/**
* Returns Mautic version from the HTTP response headers
* (the header exists since Mautic 2.4.0)
*
* @return string|null if not known
*/
public function getMauticVersion()
{
$headers = $this->auth->getResponseHeaders();

if (isset($headers['Mautic-Version'])) {
return $headers['Mautic-Version'];
}

return null;
}

/**
* Get a single item
*
Expand Down
43 changes: 41 additions & 2 deletions lib/Api/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,34 @@ public function getContactNotes($id, $search = '', $start = 0, $limit = 0, $orde
}

/**
* Get a segment of smart segments the contact is in
* Get a list of a contact's devices
*
* @param int $id Contact ID
* @param string $search
* @param int $start
* @param int $limit
* @param string $orderBy
* @param string $orderByDir
*
* @return array|mixed
*/
public function getContactDevices($id, $search = '', $start = 0, $limit = 0, $orderBy = '', $orderByDir = 'ASC')
{
$parameters = array();

$args = array('search', 'start', 'limit', 'orderBy', 'orderByDir');

foreach ($args as $arg) {
if (!empty($$arg)) {
$parameters[$arg] = $$arg;
}
}

return $this->makeRequest($this->endpoint.'/'.$id.'/devices', $parameters);
}

/**
* Get a list of smart segments the contact is in
*
* @param $id
*
Expand All @@ -139,7 +166,19 @@ public function getContactSegments($id)
}

/**
* Get a segment of campaigns the contact is in
* Get a list of companies the contact is in
*
* @param $id
*
* @return array|mixed
*/
public function getContactCompanies($id)
{
return $this->makeRequest($this->endpoint.'/'.$id.'/companies');
}

/**
* Get a list of campaigns the contact is in
*
* @param $id
*
Expand Down
22 changes: 22 additions & 0 deletions lib/Api/Devices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* @package Mautic
* @copyright 2014 Mautic, NP. All rights reserved.
* @author Mautic
* @link http://mautic.org
* @license MIT http://opensource.org/licenses/MIT
*/

namespace Mautic\Api;

/**
* Devices Context
*/
class Devices extends Api
{

/**
* {@inheritdoc}
*/
protected $endpoint = 'devices';
}
74 changes: 66 additions & 8 deletions lib/Auth/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ class OAuth extends ApiAuth implements AuthInterface
*/
protected $_debug = false;

/**
* Holds string of HTTP response headers
*
* @var string
*/
protected $_httpResponseHeaders;

/**
* Holds array of HTTP response CURL info
*
* @var array
*/
protected $_httpResponseInfo;

/**
* @param string $baseUrl URL of the Mautic instance
* @param string $version ['OAuth1a', ''OAuth2'']. 'OAuth2' is default value
Expand Down Expand Up @@ -785,17 +799,17 @@ public function makeRequest($url, array $parameters = array(), $method = 'GET',
$curl = curl_init();
curl_setopt_array($curl, $options);

$response = curl_exec($curl);
$responseArray = explode("\r\n\r\n", $response);
$body = array_pop($responseArray);
$header = implode("\r\n\r\n", $responseArray);
$info = curl_getinfo($curl);
$response = curl_exec($curl);
$responseArray = explode("\r\n\r\n", $response);
$body = array_pop($responseArray);
$this->_httpResponseHeaders = implode("\r\n\r\n", $responseArray);
$this->_httpResponseInfo = curl_getinfo($curl);

curl_close($curl);

if ($this->_debug) {
$_SESSION['oauth']['debug']['info'] = $info;
$_SESSION['oauth']['debug']['returnedHeaders'] = $header;
$_SESSION['oauth']['debug']['info'] = $this->_httpResponseInfo;
$_SESSION['oauth']['debug']['returnedHeaders'] = $this->_httpResponseHeaders;
$_SESSION['oauth']['debug']['returnedBody'] = $body;
}

Expand All @@ -815,7 +829,7 @@ public function makeRequest($url, array $parameters = array(), $method = 'GET',
}

//Show error when http_code is not appropriate
if (!in_array($info['http_code'], array(200, 201))) {
if (!in_array($this->_httpResponseInfo['http_code'], array(200, 201))) {
if ($responseGood) {
return $parsed;
}
Expand All @@ -826,6 +840,50 @@ public function makeRequest($url, array $parameters = array(), $method = 'GET',
return ($responseGood) ? $parsed : $body;
}

/**
* Build the HTTP response array out of the headers string
*
* @param string $headersStr
*
* @return array
*/
protected function parseHeaders($headersStr)
{
$headersArr = array();
$headersHlpr = explode("\r\n", $headersStr);

foreach ($headersHlpr as $header) {
$pos = strpos($header, ':');
if ($pos === false) {
$headersArr[] = trim($header);
} else {
$headersArr[trim(substr($header, 0, $pos))] = trim(substr($header, ($pos + 1)));
}
}

return $headersArr;
}

/**
* Returns array of HTTP response headers
*
* @return array
*/
public function getResponseHeaders()
{
return $this->parseHeaders($this->_httpResponseHeaders);
}

/**
* Returns array of HTTP response headers
*
* @return array
*/
public function getResponseInfo()
{
return $this->_httpResponseInfo;
}

/**
* Build the CURL file based on PHP version
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Api/CompaniesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ public function testAddAndRemove()
$this->assertErrors($response);
$this->assertSuccess($response);

// Test get contact companies API endpoint
$contactContext = $this->getContext('contacts');
$response = $contactContext->getContactCompanies($contact['id']);
$this->assertErrors($response);
$this->assertEquals($response['total'], 1);
$this->assertFalse(empty($response['companies']));

// Remove the contact from the company
$response = $apiContext->removeContact($company['id'], $contact['id']);
$this->assertErrors($response);
Expand Down
14 changes: 11 additions & 3 deletions tests/Api/ContactsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ public function setUp()
$this->testPayload = array(
'firstname' => 'test',
'lastname' => 'test',
'points' => 3
'points' => 3,
'tags' => array(
'APItag1',
'APItag2',
)
);
}

protected $skipPayloadAssertion = array('firstname', 'lastname', 'tags');


protected function assertEventResponse($response, $expectedEvents = array())
{
$this->assertErrors($response);
Expand Down Expand Up @@ -99,11 +106,12 @@ public function testCreateGetAndDelete()

// Test Create
$response = $apiContext->create($this->testPayload);
$this->assertErrors($response);
$this->assertPayload($response);
$this->assertEquals(count($response[$this->itemName]['tags']), count($this->testPayload['tags']));

// Test Get
$response = $apiContext->get($response[$this->itemName]['id']);
$this->assertErrors($response);
$this->assertPayload($response);

// Test Delete
$response = $apiContext->delete($response[$this->itemName]['id']);
Expand Down
104 changes: 104 additions & 0 deletions tests/Api/DevicesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* @package Mautic
* @copyright 2014 Mautic, NP. All rights reserved.
* @author Mautic
* @link http://mautic.org
* @license MIT http://opensource.org/licenses/MIT
*/

namespace Mautic\Tests\Api;

class DevicesTest extends MauticApiTestCase
{
protected $testPayload = array(
'device' => 'desktop',
'deviceOsName' => 'Ubuntu',
'deviceOsShortName' => 'UBT',
'deviceOsPlatform' => 'x64',
);

protected $context = 'devices';

protected $itemName = 'device';

protected $skipPayloadAssertion = array('lead');

public function setUp() {
// Create a contact for test
$apiContext = $this->getContext('contacts');
$response = $apiContext->create(array('firstname' => 'Device API test'));
$this->assertErrors($response);
$this->testPayload['lead'] = $response['contact']['id'];
}

public function tearDown() {
// Delete a contact from test
$apiContext = $this->getContext('contacts');
$response = $apiContext->delete($this->testPayload['lead']);
$this->assertErrors($response);
}

public function testGetList()
{
$apiContext = $this->getContext($this->context);
$response = $apiContext->getList();
$this->assertErrors($response);
$this->assertTrue(isset($response[$this->context]));
}

public function testCreateGetAndDelete()
{
$apiContext = $this->getContext($this->context);

$response = $apiContext->create($this->testPayload);
$this->assertPayload($response);

// Test get contact notes endpoint
$contactContext = $this->getContext('contacts');
$responseDevices = $contactContext->getContactDevices($this->testPayload['lead']);
$this->assertErrors($responseDevices);
$this->assertEquals($responseDevices['total'], 1);
$this->assertFalse(empty($responseDevices['devices']));

$response = $apiContext->get($response[$this->itemName]['id']);
$this->assertPayload($response);

$response = $apiContext->delete($response[$this->itemName]['id']);
$this->assertErrors($response);
}

public function testEditPatch()
{
$apiContext = $this->getContext($this->context);
$response = $apiContext->edit(10000, $this->testPayload);

//there should be an error as the form shouldn't exist
$this->assertTrue(isset($response['error']), $response['error']['message']);

$response = $apiContext->create($this->testPayload);
$this->assertErrors($response);

$updatePayload = array(
'deviceOsName' => 'Edubuntu',
);

$response = $apiContext->edit($response[$this->itemName]['id'], $updatePayload);
$this->assertPayload($response, $updatePayload);

//now delete the form
$response = $apiContext->delete($response[$this->itemName]['id']);
$this->assertErrors($response);
}

public function testEditPut()
{
$apiContext = $this->getContext($this->context);
$response = $apiContext->edit(10000, $this->testPayload, true);
$this->assertPayload($response);

//now delete the form
$response = $apiContext->delete($response[$this->itemName]['id']);
$this->assertErrors($response);
}
}
Loading

0 comments on commit 7d3e676

Please sign in to comment.