Skip to content

Commit

Permalink
Merge pull request #170 from Nexmo/application-v2
Browse files Browse the repository at this point in the history
Application v2
  • Loading branch information
Lorna Jane Mitchell authored May 7, 2019
2 parents 81e5839 + 68a1d51 commit f74df3e
Show file tree
Hide file tree
Showing 20 changed files with 1,074 additions and 189 deletions.
66 changes: 54 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,22 +413,66 @@ foreach($client->calls($filter) as $call){
Application are configuration containers. You can create one using a simple array structure:

```php
$application = $client->applications()->create([
'name' => 'My Application',
'answer_url' => 'https://example.com/answer',
'event_url' => 'https://example.com/event'
]);
$application = [
'name' => 'test application',
'keys' => [
'public_key' => '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCA\nKOxjsU4pf/sMFi9N0jqcSLcjxu33G\nd/vynKnlw9SENi+UZR44GdjGdmfm1\ntL1eA7IBh2HNnkYXnAwYzKJoa4eO3\n0kYWekeIZawIwe/g9faFgkev+1xsO\nOUNhPx2LhuLmgwWSRS4L5W851Xe3f\nUQIDAQAB\n-----END PUBLIC KEY-----\n'
],
'capabilities' => [
'voice' => [
'webhooks' => [
'answer_url' => [
'address' => 'https://example.com/answer',
'http_method' => 'GET',
],
'event_url' => [
'address' => 'https://example.com/event',
'http_method' => 'POST',
],
]
],
'messages' => [
'webhooks' => [
'inbound_url' => [
'address' => 'https://example.com/inbound',
'http_method' => 'POST'

],
'status_url' => [
'address' => 'https://example.com/status',
'http_method' => 'POST'
]
]
],
'rtc' => [
'webhooks' => [
'event_url' => [
'address' => 'https://example.com/event',
'http_method' => 'POST',
],
]
],
'vbc' => []
]
];

$client->applications()->create($application);
```

You can also pass the client an application object:

```php
$application = new Nexmo\Application\Application();
$application->setName('My Application');
$application->getVoiceConfig()->setWebhook(VoiceConfig::ANSWER, 'https://example.com/answer');
$application->getVoiceConfig()->setWebhook(VoiceConfig::EVENT, 'https://example.com/event');
$a = new Nexmo\Application\Application;

$a->setName('PHP Client Example');
$a->getVoiceConfig()->setWebhook('answer_url', 'https://example.com/answer', 'GET');
$a->getVoiceConfig()->setWebhook('event_url', 'https://example.com/event', 'POST');
$a->getMessagesConfig()->setWebhook('status_url', 'https://example.com/status', 'POST');
$a->getMessagesConfig()->setWebhook('inbound_url', 'https://example.com/inbound', 'POST');
$a->getRtcConfig()->setWebhook('event_url', 'https://example.com/event', 'POST');
$a->disableVbc();

$client->appliations()->create($application);
$client->applications()->create($a);
```

### Fetching Applications
Expand Down Expand Up @@ -466,8 +510,6 @@ You can also pass an array and the application UUID to the client:
```php
$application = $client->applications()->update([
'name' => 'Updated Application',
'answer_url' => 'https://example.com/v2/answer',
'event_url' => 'https://example.com/v2/event'
], '1a20a124-1775-412b-b623-e6985f4aace0');
```

Expand Down
19 changes: 19 additions & 0 deletions examples/create-application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

$client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic(API_KEY, API_SECRET));

$a = new Nexmo\Application\Application;

$a->setName('PHP Client Example');
$a->getVoiceConfig()->setWebhook('answer_url', 'https://example.com/answer', 'GET');
$a->getVoiceConfig()->setWebhook('event_url', 'https://example.com/event', 'POST');
$a->getMessagesConfig()->setWebhook('status_url', 'https://example.com/status', 'POST');
$a->getMessagesConfig()->setWebhook('inbound_url', 'https://example.com/inbound', 'POST');
$a->getRtcConfig()->setWebhook('event_url', 'https://example.com/event', 'POST');
$a->getVbcConfig()->enable();

$r = $client->applications()->create($a);

print_r($r);
10 changes: 10 additions & 0 deletions examples/delete-application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

$client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic(API_KEY, API_SECRET));

$a = $client->applications()->get(APPLICATION_ID);

$client->applications()->delete($a);

25 changes: 25 additions & 0 deletions examples/get-application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
require_once '../vendor/autoload.php';

$client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic(API_KEY, API_SECRET));

$a = $client->applications()->get(APPLICATION_ID);
echo $a->getName().PHP_EOL;

echo "\nPUBLIC KEY\n-----\n";
echo $a->getPublicKey();

echo "\nVOICE\n-----\n";
echo $a->getVoiceConfig()->getWebhook('answer_url').PHP_EOL;
echo $a->getVoiceConfig()->getWebhook('event_url').PHP_EOL;

echo "\nMessages\n-----\n";
echo $a->getMessagesConfig()->getWebhook('inbound_url').PHP_EOL;
echo $a->getMessagesConfig()->getWebhook('status_url').PHP_EOL;

echo "\nRTC\n-----\n";
echo $a->getRtcConfig()->getWebhook('event_url').PHP_EOL;

echo "\nVBC\n-----\n";
echo $a->getVbcConfig()->isEnabled() ? 'Enabled' : 'Disabled';
echo "\n\n";
8 changes: 8 additions & 0 deletions examples/list-applications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
require_once '../vendor/autoload.php';

$client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic(API_KEY, API_SECRET));

foreach($client->applications() as $application){
echo $application->getName() .' - '.$application->getId() . PHP_EOL;
}
24 changes: 24 additions & 0 deletions examples/make-call-ncco.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
require_once '../vendor/autoload.php';

$keypair = new \Nexmo\Client\Credentials\Keypair(
file_get_contents('/Users/michael/development/nexmo/empty-voice-project/private.key'),
'75196d98-d3a0-476c-9fbc-7df9aa326aff'
);

$client = new \Nexmo\Client($keypair);

use Nexmo\Call\Call;
$call = new Call();
$call->setTo('447908249481')
->setFrom('123456')
->setNcco([
[
'action' => 'talk',
'text' => 'This is a text to speech call from Nexmo'
]
]);

$response = $client->calls()->create($call);

echo $response->getId();
16 changes: 16 additions & 0 deletions examples/update-application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

$client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic('7c9738e6', 'n3w-Api-key!'));

$a = $client->applications()->get('a8f93ca3-2a6f-4722-a53e-b7cccabc0bb9');

$a->getVoiceConfig()->setWebhook('answer_url', 'https://example.com/answer', 'GET');
$a->getVoiceConfig()->setWebhook('event_url', 'https://example.com/event', 'POST');
$a->getMessagesConfig()->setWebhook('status_url', 'https://example.com/status', 'POST');
$a->getMessagesConfig()->setWebhook('inbound_url', 'https://example.com/inbound', 'POST');
$a->getRtcConfig()->setWebhook('event_url', 'https://example.com/event', 'POST');
$a->getVbcConfig()->disable();

$client->applications()->update($a);
153 changes: 144 additions & 9 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Application implements EntityInterface, \JsonSerializable, JsonUnserializa
use JsonResponseTrait;

protected $voiceConfig;
protected $messagesConfig;
protected $rtcConfig;
protected $vbcConfig;

protected $name;

Expand All @@ -45,6 +48,24 @@ public function setVoiceConfig(VoiceConfig $config)
return $this;
}

public function setMessagesConfig(MessagesConfig $config)
{
$this->messagesConfig = $config;
return $this;
}

public function setRtcConfig(RtcConfig $config)
{
$this->rtcConfig = $config;
return $this;
}

public function setVbcConfig(VbcConfig $config)
{
$this->vbcConfig = $config;
return $this;
}

/**
* @return VoiceConfig
*/
Expand All @@ -63,6 +84,60 @@ public function getVoiceConfig()
return $this->voiceConfig;
}

/**
* @return MessagesConfig
*/
public function getMessagesConfig()
{
if(!isset($this->messagesConfig)){
$this->setMessagesConfig(new MessagesConfig());
$data = $this->getResponseData();
if(isset($data['messages']) AND isset($data['messages']['webhooks'])){
foreach($data['messages']['webhooks'] as $webhook){
$this->getMessagesConfig()->setWebhook($webhook['endpoint_type'], $webhook['endpoint'], $webhook['http_method']);
}
}
}

return $this->messagesConfig;
}

/**
* @return RtcConfig
*/
public function getRtcConfig()
{
if(!isset($this->rtcConfig)){
$this->setRtcConfig(new RtcConfig());
$data = $this->getResponseData();
if(isset($data['rtc']) AND isset($data['rtc']['webhooks'])){
foreach($data['rtc']['webhooks'] as $webhook){
$this->getRtcConfig()->setWebhook($webhook['endpoint_type'], $webhook['endpoint'], $webhook['http_method']);
}
}
}

return $this->rtcConfig;
}

/**
* @return RtcConfig
*/
public function getVbcConfig()
{
if(!isset($this->vbcConfig)){
$this->setVbcConfig(new VbcConfig());
}

return $this->vbcConfig;
}

public function setPublicKey($key)
{
$this->keys['public_key'] = $key;
return $this;
}

public function getPublicKey()
{
if(isset($this->keys['public_key'])){
Expand Down Expand Up @@ -94,23 +169,83 @@ public function jsonUnserialize(array $json)
$this->id = $json['id'];
$this->keys = $json['keys'];

//todo: make voice hydrate-able
$this->voiceConfig = new VoiceConfig();
if(isset($json['voice']) AND isset($json['voice']['webhooks'])){
foreach($json['voice']['webhooks'] as $webhook){
$this->voiceConfig->setWebhook($webhook['endpoint_type'], new Webhook($webhook['endpoint'], $webhook['http_method']));
if (isset($json['capabilities'])) {
$capabilities = $json['capabilities'];

//todo: make voice hydrate-able
$this->voiceConfig = new VoiceConfig();
if (isset($capabilities['voice']) AND isset($capabilities['voice']['webhooks'])) {
foreach ($capabilities['voice']['webhooks'] as $name => $details) {
$this->voiceConfig->setWebhook($name, new Webhook($details['address'], $details['http_method']));
}
}

//todo: make messages hydrate-able
$this->messagesConfig = new MessagesConfig();
if (isset($capabilities['messages']) AND isset($capabilities['messages']['webhooks'])) {
foreach ($capabilities['messages']['webhooks'] as $name => $details) {
$this->messagesConfig->setWebhook($name, new Webhook($details['address'], $details['http_method']));
}
}

//todo: make rtc hydrate-able
$this->rtcConfig = new RtcConfig();
if (isset($capabilities['rtc']) AND isset($capabilities['rtc']['webhooks'])) {
foreach ($capabilities['rtc']['webhooks'] as $name => $details) {
$this->rtcConfig->setWebhook($name, new Webhook($details['address'], $details['http_method']));
}
}

if (isset($capabilities['vbc'])) {
$this->getVbcConfig()->enable();
}
}
}

public function jsonSerialize()
{

// Build up capabilities that are set
$availableCapabilities = [
'voice' => [VoiceConfig::ANSWER, VoiceConfig::EVENT],
'messages' => [MessagesConfig::INBOUND, MessagesConfig::STATUS],
'rtc' => [RtcConfig::EVENT]
];

$capabilities = [];
foreach ($availableCapabilities as $type => $values) {
$configAccessorMethod = 'get'.ucfirst($type).'Config';
foreach ($values as $constant) {
$webhook = $this->$configAccessorMethod()->getWebhook($constant);
if ($webhook) {
if (!isset($capabilities[$type])) {
$capabilities[$type]['webhooks'] = [];
}
$capabilities[$type]['webhooks'][$constant] = [
'address' => $webhook->getUrl(),
'http_method' => $webhook->getMethod(),
];
}
}
}

// Handle VBC specifically
if ($this->getVbcConfig()->isEnabled()) {
$capabilities['vbc'] = new \StdClass;
}

// Workaround API bug. It expects an object and throws 500
// if it gets an array
if (!count($capabilities)) {
$capabilities = (object) $capabilities;
}

return [
'name' => $this->getName(),
//currently, the request data does not match the response data
'event_url' => (string) $this->getVoiceConfig()->getWebhook(VoiceConfig::EVENT),
'answer_url' => (string) $this->getVoiceConfig()->getWebhook(VoiceConfig::ANSWER),
'type' => 'voice' //currently the only type
'keys' => [
'public_key' => $this->getPublicKey()
],
'capabilities' => $capabilities
];
}

Expand Down
Loading

0 comments on commit f74df3e

Please sign in to comment.