-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #537 from zendesk/alex_sh_sell_contacts_support
Zendesk Sell API and Contacts resource support
- Loading branch information
Showing
6 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
]); | ||
} | ||
} |