Skip to content

Commit

Permalink
Merge pull request #537 from zendesk/alex_sh_sell_contacts_support
Browse files Browse the repository at this point in the history
Zendesk Sell API and Contacts resource support
  • Loading branch information
token-cjg authored Sep 24, 2024
2 parents 3a50ecd + ad6802b commit 13adf39
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 0 deletions.
27 changes: 27 additions & 0 deletions samples/sell/getContacts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

require_once __DIR__ . '/../../vendor/autoload.php';

use Zendesk\API\HttpClient as ZendeskAPI;

/**
* Replace the following with your own.
*/

$subdomain = "api.futuresimple.com";
$token = "super_secret_oauth_token"; // this must be your oauth bearer token for sell

$client = new ZendeskAPI($subdomain);
$client->setAuth('oauth', ['token' => $token]);
$sell_client = new Zendesk\API\Resources\Sell($client);

try {
// Find all sell contacts
$contacts = $sell_client->contacts()->findAll();

echo "<pre>";
print_r($contacts);
echo "</pre>";
} catch (\Zendesk\API\Exceptions\ApiResponseException $e) {
echo $e->getMessage().'</br>';
}
12 changes: 12 additions & 0 deletions src/Zendesk/API/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use Zendesk\API\Resources\HelpCenter;
use Zendesk\API\Resources\Talk;
use Zendesk\API\Resources\Voice;
use Zendesk\API\Resources\Sell;
use Zendesk\API\Traits\Utility\InstantiatorTrait;
use Zendesk\API\Utilities\Auth;

Expand Down Expand Up @@ -196,6 +197,11 @@ class HttpClient
*/
public $talk;

/**
* @var Sell
*/
public $sell;

/**
* @param string $subdomain
* @param string $username
Expand Down Expand Up @@ -228,6 +234,11 @@ public function __construct(
$this->scheme = $scheme;
$this->port = $port;

// If sell is set, we need to use a different base uri
if ($this->subdomain === 'api.futuresimple.com') {
$this->apiUrl = 'https://api.futuresimple.com/';
} else

if (empty($subdomain)) {
$this->apiUrl = "$scheme://$hostname:$port/";
} else {
Expand All @@ -240,6 +251,7 @@ public function __construct(
$this->embeddable = new Embeddable($this);
$this->chat = new Chat($this);
$this->talk = new Talk($this);
$this->sell = new Sell($this);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/Zendesk/API/Resources/Sell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Zendesk\API\Resources;

use Zendesk\API\HttpClient;
use Zendesk\API\Resources\Sell\Contacts;
use Zendesk\API\Traits\Utility\ChainedParametersTrait;
use Zendesk\API\Traits\Utility\InstantiatorTrait;

/**
* This class serves as a container to allow $this->client->sell
*
* @method Contacts contacts()
*/
class Sell
{
use ChainedParametersTrait;
use InstantiatorTrait;

public $client;

/**
* Sets the client to be used
*
* @param HttpClient $client
*/
public function __construct(HttpClient $client)
{
$this->client = $client;
}

/**
* @inheritdoc
* @return array
*/
public static function getValidSubResources()
{
return [
'contacts' => Contacts::class,
];
}
}
68 changes: 68 additions & 0 deletions src/Zendesk/API/Resources/Sell/Contacts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Zendesk\API\Resources\Sell;

use Zendesk\API\Traits\Resource\Defaults;
use Zendesk\API\Traits\Utility\InstantiatorTrait;

/**
* Contacts class provides a simple interface to manage your contacts in Sell
* https://developers.getbase.com/docs/rest/reference/contacts
*/
class Contacts extends ResourceAbstract
{
use InstantiatorTrait;
use Defaults;

// Override constructor to set different API base path
public function __construct($client)
{
parent::__construct($client, 'v2/');
}

/**
* {@inheritdoc}
*/
protected $objectName = 'data';

/**
* {@inheritdoc}
*/
protected $resourceName = 'contacts';

/**
* Declares routes to be used by this resource.
*/
protected function setUpRoutes()
{
parent::setUpRoutes();

$this->setRoutes([
"find" => "{$this->resourceName}/{id}",
"findAll" => $this->resourceName,
"create" => $this->resourceName,
"update" => "{$this->resourceName}/{id}",
"delete" => "{$this->resourceName}/{id}",
"upsert" => "{$this->resourceName}/upsert",
]);
}

/**
* Create a new contact or update an existing, based on a value of a filter or a set of filters
* @param array $params
* @param array $updateResourceFields
* @return \stdClass|null
* @throws \Zendesk\API\Exceptions\ApiResponseException
* @throws \Zendesk\API\Exceptions\AuthException
*/
public function upsert(array $params, array $updateResourceFields)
{
$route = $this->getRoute(__FUNCTION__);

return $this->client->post(
$route,
[$this->objectName => $updateResourceFields],
['queryParams' => $params]
);
}
}
11 changes: 11 additions & 0 deletions src/Zendesk/API/Resources/Sell/ResourceAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Zendesk\API\Resources\Sell;

/**
* Abstract class for Sell resources
*/
abstract class ResourceAbstract extends \Zendesk\API\Resources\ResourceAbstract
{
protected $apiBasePath = 'v2/';
}
56 changes: 56 additions & 0 deletions tests/Zendesk/API/UnitTests/Sell/ContactsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Zendesk\API\UnitTests\Sell;

use Faker\Factory;
use Zendesk\API\UnitTests\BasicTest;

class ContactsTest extends BasicTest
{

/**
* Test that the correct traits were added by checking the available methods
*/
public function testMethods()
{
$this->assertTrue(method_exists($this->client->sell->contacts(), 'create'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'delete'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'find'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'findAll'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'update'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'upsert'));
}

/**
* Tests if the upsert endpoint can be called and passed the correct params
*/
public function testUpsert()
{
$faker = Factory::create();

$queryParams = [
'email' => $faker->email,
'phone' => $faker->phoneNumber,
];

$postFields = [
'email' => $faker->email,
'custom_fields' => [
'Some Field' => $faker->text
]
];

$encodedQueryParams = [];
foreach ($queryParams as $key => $value) {
$encodedQueryParams[$key] = $value;
}

$this->assertEndpointCalled(function () use ($queryParams, $postFields) {
$this->client->sell->contacts()->upsert($queryParams, $postFields);
}, '/contacts/upsert', 'POST', [
'queryParams' => $encodedQueryParams,
'postFields' => ['data' => $postFields],
'apiBasePath' => '/v2'
]);
}
}

0 comments on commit 13adf39

Please sign in to comment.