Skip to content

Commit 64e6c73

Browse files
Merge pull request #52 from Bandwidth/DX-2467
DX-2467 added modifyCallBxml function
2 parents f13e4f9 + 3dc1994 commit 64e6c73

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

src/Voice/Bxml/Bxml.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Bxml.php
4+
*
5+
* Class that represents the BXML response. Is built by adding verbs to it
6+
*
7+
* * @copyright Bandwidth INC
8+
*/
9+
10+
namespace BandwidthLib\Voice\Bxml;
11+
12+
use DOMDocument;
13+
14+
class Bxml {
15+
16+
/**
17+
* Creates the Bxml class with an empty list of verbs
18+
*/
19+
public function __construct() {
20+
$this->verbs = array();
21+
}
22+
23+
/**
24+
* Adds the verb to the verbs
25+
*
26+
* @param Verb $verb The verb to add to the list
27+
*/
28+
public function addVerb($verb) {
29+
array_push($this->verbs, $verb);
30+
}
31+
32+
/**
33+
* Converts the Response class into its BXML representation
34+
*
35+
* @return string The xml representation of the class
36+
*/
37+
public function toBxml() {
38+
$ssmlRegex = '/&lt;([a-zA-Z\/\/].*?)&gt;/';
39+
$doc = new DOMDocument('1.0', 'UTF-8');
40+
$bxmlElement = $doc->createElement("Bxml");
41+
42+
foreach ($this->verbs as $verb) {
43+
$bxmlElement->appendChild($verb->toBxml($doc));
44+
}
45+
46+
$doc->appendChild($bxmlElement);
47+
return str_replace("\n", '', preg_replace($ssmlRegex, "<$1>", $doc->saveXML()));
48+
}
49+
}

src/Voice/Controllers/APIController.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,114 @@ public function getCall(
249249
return new ApiResponse($response->code, $response->headers, $deserializedResponse);
250250
}
251251

252+
/**
253+
* Replaces the bxml for an active call
254+
*
255+
* @param string $accountId TODO: type description here
256+
* @param string $callId TODO: type description here
257+
* @param string $body Valid BXML string
258+
* @return ApiResponse response from the API call
259+
* @throws APIException Thrown if API call fails
260+
*/
261+
public function modifyCallBxml(
262+
$accountId,
263+
$callId,
264+
$body
265+
) {
266+
267+
//prepare query string for API call
268+
$_queryBuilder = '/api/v2/accounts/{accountId}/calls/{callId}';
269+
270+
//process optional query parameters
271+
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
272+
'accountId' => $accountId,
273+
'callId' => $callId,
274+
));
275+
276+
//validate and preprocess url
277+
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::VOICEDEFAULT) . $_queryBuilder);
278+
279+
//prepare headers
280+
$_headers = array (
281+
'user-agent' => BaseController::USER_AGENT,
282+
'content-type' => 'application/xml; charset=utf-8'
283+
);
284+
285+
//set HTTP basic auth parameters
286+
Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword());
287+
288+
$_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl);
289+
290+
//call on-before Http callback
291+
if ($this->getHttpCallBack() != null) {
292+
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
293+
}
294+
// Set request timeout
295+
Request::timeout($this->config->getTimeout());
296+
297+
// and invoke the API call request to fetch the response
298+
$response = Request::post($_queryUrl, $_headers, $body);
299+
300+
$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
301+
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);
302+
303+
//call on-after Http callback
304+
if ($this->getHttpCallBack() != null) {
305+
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
306+
}
307+
308+
//Error handling using HTTP status codes
309+
if ($response->code == 400) {
310+
throw new Exceptions\ApiErrorException(
311+
'Something\'s not quite right... Your request is invalid. Please fix it before trying again.',
312+
$_httpContext
313+
);
314+
}
315+
316+
if ($response->code == 401) {
317+
throw new APIException(
318+
'Your credentials are invalid. Please use your Bandwidth dashboard credentials to authenticate to ' .
319+
'the API.',
320+
$_httpContext
321+
);
322+
}
323+
324+
if ($response->code == 403) {
325+
throw new Exceptions\ApiErrorException('User unauthorized to perform this action.', $_httpContext);
326+
}
327+
328+
if ($response->code == 404) {
329+
throw new Exceptions\ApiErrorException(
330+
'The resource specified cannot be found or does not belong to you.',
331+
$_httpContext
332+
);
333+
}
334+
335+
if ($response->code == 415) {
336+
throw new Exceptions\ApiErrorException(
337+
'We don\'t support that media type for this endpoint. If a request body is required, please send it to us as ' .
338+
'`application/xml`.',
339+
$_httpContext
340+
);
341+
}
342+
343+
if ($response->code == 429) {
344+
throw new Exceptions\ApiErrorException(
345+
'You\'re sending requests to this endpoint too frequently. Please slow your request rate down and ' .
346+
'try again.',
347+
$_httpContext
348+
);
349+
}
350+
351+
if ($response->code == 500) {
352+
throw new Exceptions\ApiErrorException('Something unexpected happened. Please try again.', $_httpContext);
353+
}
354+
355+
//handle errors defined at the API level
356+
$this->validateResponse($_httpResponse, $_httpContext);
357+
return new ApiResponse($response->code, $response->headers, null);
358+
}
359+
252360
/**
253361
* Interrupts and replaces an active call's BXML document.
254362
*

tests/BxmlTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212

1313
final class BxmlTest extends TestCase
1414
{
15+
public function testBxml() {
16+
$speakSentence = new BandwidthLib\Voice\Bxml\SpeakSentence("Test");
17+
$pause = new BandwidthLib\Voice\Bxml\Pause();
18+
$pause->duration("3");
19+
$speakSentence->voice("susan");
20+
$speakSentence->locale("en_US");
21+
$speakSentence->gender("female");
22+
$bxml = new BandwidthLib\Voice\Bxml\Bxml();
23+
$bxml->addVerb($speakSentence, $pause);
24+
$bxml->addVerb($pause);
25+
$expectedXml = '<?xml version="1.0" encoding="UTF-8"?><Bxml><SpeakSentence locale="en_US" gender="female" voice="susan">Test</SpeakSentence><Pause duration="3"/></Bxml>';
26+
$responseXml = $bxml->toBxml();
27+
$this->assertEquals($expectedXml, $responseXml);
28+
}
29+
1530
public function testForward() {
1631
$forward = new BandwidthLib\Voice\Bxml\Forward();
1732
$forward->to("+18888888888");

0 commit comments

Comments
 (0)