Skip to content

Commit 2d83bee

Browse files
author
Felipe Coppola
committed
Implement partner account integration
Integrate the SDK with the partner account API to be able to CRUD sub accounts for the user which has the privilege to use the Partner Account feature. You can find the Partner Account documentation on the MessageBird website (https://developers.messagebird.com/)
1 parent 6a1666b commit 2d83bee

File tree

9 files changed

+362
-0
lines changed

9 files changed

+362
-0
lines changed

examples/partner-account-create.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/../autoload.php');
4+
5+
$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.
6+
7+
$account = new \MessageBird\Objects\PartnerAccount\Account();
8+
$account->name = 'Name Test';
9+
$account->email = '[email protected]';
10+
11+
try {
12+
$partnerAccountResult = $messageBird->partnerAccounts->create($account);
13+
var_dump($partnerAccountResult);
14+
15+
} catch (\MessageBird\Exceptions\AuthenticateException $e) {
16+
// That means that your accessKey is unknown
17+
echo 'wrong login';
18+
19+
} catch (\Exception $e) {
20+
echo $e->getMessage();
21+
}

examples/partner-account-delete.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/../autoload.php');
4+
5+
$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.
6+
7+
try {
8+
$partnerAccountResult = $messageBird->partnerAccounts->delete(1);
9+
var_dump($partnerAccountResult);
10+
11+
} catch (\MessageBird\Exceptions\AuthenticateException $e) {
12+
// That means that your accessKey is unknown
13+
echo 'wrong login';
14+
15+
} catch (\Exception $e) {
16+
echo $e->getMessage();
17+
}

examples/partner-account-list.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/../autoload.php');
4+
5+
$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.
6+
7+
try {
8+
$partnerAccountResult = $messageBird->partnerAccounts->getList();
9+
var_dump($partnerAccountResult);
10+
11+
} catch (\MessageBird\Exceptions\AuthenticateException $e) {
12+
// That means that your accessKey is unknown
13+
echo 'wrong login';
14+
15+
} catch (\Exception $e) {
16+
echo $e->getMessage();
17+
}

examples/partner-account-read.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/../autoload.php');
4+
5+
$messageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.
6+
7+
try {
8+
$partnerAccountResult = $messageBird->partnerAccounts->read(1);
9+
var_dump($partnerAccountResult);
10+
11+
} catch (\MessageBird\Exceptions\AuthenticateException $e) {
12+
// That means that your accessKey is unknown
13+
echo 'wrong login';
14+
15+
} catch (\Exception $e) {
16+
echo $e->getMessage();
17+
}

src/MessageBird/Client.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Client
1313
const CHATAPI_ENDPOINT = 'https://chat.messagebird.com/1';
1414
const CONVERSATIONSAPI_ENDPOINT = 'https://conversations.messagebird.com/v1';
1515
const VOICEAPI_ENDPOINT = 'https://voice.messagebird.com';
16+
const PARTNER_ACCOUNT_ENDPOINT = 'https://partner-accounts.messagebird.com';
1617

1718
const ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX = 'ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX';
1819
const CONVERSATIONSAPI_WHATSAPP_SANDBOX_ENDPOINT = 'https://whatsapp-sandbox.messagebird.com/v1';
@@ -139,6 +140,11 @@ class Client
139140
*/
140141
public $conversationWebhooks;
141142

143+
/**
144+
* @var Resources\PartnerAccount\Accounts;
145+
*/
146+
public $partnerAccounts;
147+
142148
/**
143149
* @var Common\HttpClient
144150
*/
@@ -159,6 +165,11 @@ class Client
159165
*/
160166
protected $VoiceAPIHttpClient;
161167

168+
/**
169+
* @var Common\HttpClient
170+
*/
171+
protected $partnerAccountClient;
172+
162173
/**
163174
* @param string $accessKey
164175
* @param Common\HttpClient $httpClient
@@ -172,11 +183,13 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n
172183
$this->VoiceAPIHttpClient = new Common\HttpClient(self::VOICEAPI_ENDPOINT, 10, 2, array(
173184
'X-MessageBird-Version' => '20170314',
174185
));
186+
$this->partnerAccountClient = new Common\HttpClient(self::PARTNER_ACCOUNT_ENDPOINT);
175187
} else {
176188
$this->ChatAPIHttpClient = $httpClient;
177189
$this->ConversationsAPIHttpClient = $httpClient;
178190
$this->HttpClient = $httpClient;
179191
$this->VoiceAPIHttpClient = $httpClient;
192+
$this->partnerAccountClient = $httpClient;
180193
}
181194

182195
$this->HttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION);
@@ -191,6 +204,9 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n
191204
$this->VoiceAPIHttpClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION);
192205
$this->VoiceAPIHttpClient->addUserAgentString($this->getPhpVersion());
193206

207+
$this->partnerAccountClient->addUserAgentString('MessageBird/ApiClient/' . self::CLIENT_VERSION);
208+
$this->partnerAccountClient->addUserAgentString($this->getPhpVersion());
209+
194210
if ($accessKey !== null) {
195211
$this->setAccessKey($accessKey);
196212
}
@@ -218,6 +234,7 @@ public function __construct($accessKey = null, Common\HttpClient $httpClient = n
218234
$this->conversations = new Resources\Conversation\Conversations($this->ConversationsAPIHttpClient);
219235
$this->conversationMessages = new Resources\Conversation\Messages($this->ConversationsAPIHttpClient);
220236
$this->conversationWebhooks = new Resources\Conversation\Webhooks($this->ConversationsAPIHttpClient);
237+
$this->partnerAccounts = new Resources\PartnerAccount\Accounts($this->partnerAccountClient);
221238
}
222239

223240
/**
@@ -231,6 +248,7 @@ public function setAccessKey ($accessKey)
231248
$this->ConversationsAPIHttpClient->setAuthentication($Authentication);
232249
$this->HttpClient->setAuthentication($Authentication);
233250
$this->VoiceAPIHttpClient->setAuthentication($Authentication);
251+
$this->partnerAccountClient->setAuthentication($Authentication);
234252
}
235253

236254
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace MessageBird\Objects\PartnerAccount;
4+
5+
use MessageBird\Objects\Base;
6+
7+
class AccessKey extends Base
8+
{
9+
public $id;
10+
11+
public $key;
12+
13+
public $mode;
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace MessageBird\Objects\PartnerAccount;
4+
5+
use MessageBird\Objects\Base;
6+
7+
class Account extends Base
8+
{
9+
public $id;
10+
11+
public $name;
12+
13+
public $email;
14+
15+
public $accessKeys = [];
16+
17+
public $signingKey;
18+
19+
public function loadToJson()
20+
{
21+
return json_encode([
22+
'name' => $this->name,
23+
'email' => $this->email,
24+
]);
25+
}
26+
27+
/**
28+
* @param $object
29+
*
30+
* @return $this|void
31+
*/
32+
public function loadFromArray($object)
33+
{
34+
parent::loadFromArray($object);
35+
36+
if (empty($this->accessKeys)) {
37+
return $this;
38+
}
39+
40+
foreach($this->accessKeys as &$item) {
41+
$accessKey = new AccessKey();
42+
$item = $accessKey->loadFromArray($item);
43+
}
44+
45+
return $this;
46+
}
47+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace MessageBird\Resources\PartnerAccount;
4+
5+
use MessageBird\Common\HttpClient;
6+
use MessageBird\Objects\PartnerAccount\Account;
7+
use MessageBird\Resources\Base;
8+
9+
class Accounts extends Base
10+
{
11+
const RESOURCE_NAME = 'child-accounts';
12+
13+
public function __construct(HttpClient $httpClient)
14+
{
15+
parent::__construct($httpClient);
16+
17+
$this->setObject(new Account());
18+
$this->setResourceName(self::RESOURCE_NAME);
19+
}
20+
21+
public function create($object, $query = null)
22+
{
23+
24+
list($status, , $body) = $this->HttpClient->performHttpRequest(
25+
HttpClient::REQUEST_POST,
26+
self::RESOURCE_NAME,
27+
null,
28+
$object->loadToJson()
29+
);
30+
31+
var_dump($status);
32+
var_dump($body);
33+
34+
return $this->processRequest($body);
35+
}
36+
37+
public function getList($parameters = [])
38+
{
39+
list($status, , $body) = $this->HttpClient->performHttpRequest(
40+
HttpClient::REQUEST_GET,
41+
self::RESOURCE_NAME,
42+
$parameters
43+
);
44+
45+
if ($status !== 200) {
46+
return $this->processRequest($body);
47+
}
48+
49+
50+
$response = json_decode($body, true);
51+
52+
foreach ($response as &$item) {
53+
$item = $this->getObject()->loadFromArray($item);
54+
}
55+
56+
return $response;
57+
}
58+
}

0 commit comments

Comments
 (0)