Skip to content

Commit

Permalink
Merge pull request #180 from plivo/VT-1545_New_Voice_API_Changes
Browse files Browse the repository at this point in the history
Voice Retry Logic Changes - Dev Tested
  • Loading branch information
nixonsam authored Aug 5, 2020
2 parents 437ef4c + 265b414 commit 1c4b36a
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 51 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## [v4.10.0](https://github.com/plivo/plivo-php/releases/tag/v4.10.0) - 2020-08-03
- Add retries to multiple regions for voice requests.

## [v4.9.1](https://github.com/plivo/plivo-php/releases/tag/v4.9.1) - 2020-07-21
- Fix Get Call Details API response.

Expand Down
48 changes: 42 additions & 6 deletions src/Plivo/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class BaseClient
* @const BASE API URL
*/
const BASE_API_URL = 'https://api.plivo.com/';
const VOICE_BASE_API_URL = 'https://voice.plivo.com/';
const VOICE_BASE_API_FALLBACK_URL_1 = 'https://voice-usw1.plivo.com/';
const VOICE_BASE_API_FALLBACK_URL_2 = 'https://voice-use1.plivo.com/';
/**
* @const Default timeout for request
*/
const DEFAULT_REQUEST_TIMEOUT = 5;
const DEFAULT_REQUEST_TIMEOUT = 20;

/**
* @var int|null Request timeout
Expand All @@ -43,6 +46,10 @@ class BaseClient
*/
public static $requestCount = 0;

public static $voiceRetryCount = 0;

public static $isVoiceRequest = false;

/**
* Instantiates a new BaseClient object.
*
Expand Down Expand Up @@ -158,7 +165,17 @@ public function sendRequest(PlivoRequest $request, $url = null)
$fullUrl = $url ? $url : null;
list($url, $method, $headers, $body) =
$this->prepareRequestMessage($request, $fullUrl);

if(static::$isVoiceRequest){
if(static::$voiceRetryCount == 0){
$url = self::VOICE_BASE_API_URL . $request->getUrl();
}
elseif(static::$voiceRetryCount == 1){
$url = self::VOICE_BASE_API_FALLBACK_URL_1 . $request->getUrl();
}
elseif(static::$voiceRetryCount == 2){
$url = self::VOICE_BASE_API_FALLBACK_URL_2 . $request->getUrl();
}
}
$timeout = $this->timeout ?: static::DEFAULT_REQUEST_TIMEOUT;

$plivoResponse =
Expand All @@ -167,10 +184,18 @@ public function sendRequest(PlivoRequest $request, $url = null)

static::$requestCount++;

if (!$plivoResponse->ok()) {
if (!$plivoResponse->ok() && !static::$isVoiceRequest) {
return $plivoResponse;
}

if($plivoResponse->getStatusCode() >= 500 && static::$isVoiceRequest){
static::$voiceRetryCount++;
if(static::$voiceRetryCount > 2){
static::$voiceRetryCount = 0;
return $plivoResponse;
}
return $this->sendRequest($request, null);
}
static::$voiceRetryCount = 0;
return $plivoResponse;
}

Expand All @@ -181,7 +206,11 @@ public function sendRequest(PlivoRequest $request, $url = null)
* @return PlivoResponse
*/
public function fetch($uri, $params)
{
{
if (array_key_exists("isVoiceRequest", $params)){
static::$isVoiceRequest = true;
unset($params['isVoiceRequest']);
}
$request =
new PlivoRequest(
'GET', $uri, ArrayOperations::removeNull($params));
Expand All @@ -203,14 +232,17 @@ public function update($uri, $params)
$url = $params['CallInsightsEndpoint'];
unset($params['CallInsightsEndpoint']);
}
elseif (array_key_exists("isVoiceRequest", $params)){
static::$isVoiceRequest = true;
unset($params['isVoiceRequest']);
}
$request =
new PlivoRequest(
'POST', $uri, ArrayOperations::removeNull($params));

if ($isCallInsightsRequest) {
return $this->sendRequest($request, $url);
}

return $this->sendRequest($request);
}

Expand Down Expand Up @@ -264,6 +296,10 @@ public function getPhlorunner($uri, $params)
*/
public function delete($uri, $params)
{
if (array_key_exists("isVoiceRequest", $params)){
static::$isVoiceRequest = true;
unset($params['isVoiceRequest']);
}
$request =
new PlivoRequest(
'DELETE', $uri, ArrayOperations::removeNull($params));
Expand Down
2 changes: 1 addition & 1 deletion src/Plivo/HttpClients/PlivoGuzzleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function send_request($url, $method, $body, $headers, $timeOut, $request)
$headers["Authorization"] = "Basic " . base64_encode("$this->authId:$this->authToken");
$request->setHeaders($headers);
$options =[];
$requestBody = json_encode($request->getParams());
$requestBody = json_encode($request->getParams(), JSON_FORCE_OBJECT);
if (array_key_exists("isCallInsightsRequest", $request->getParams())) {
unset($request->getParams()['isCallInsightsRequest']);
$requestBody = $requestBody;
Expand Down
9 changes: 6 additions & 3 deletions src/Plivo/Resources/Application/ApplicationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function create(
$mandaoryArgs = [
'app_name' => $appName
];

$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->update(
$this->uri,
array_merge($mandaoryArgs, $optionalArgs)
Expand Down Expand Up @@ -113,6 +113,7 @@ public function create(
*/
public function update($appId, array $optionalArgs = [])
{
$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->update(
$this->uri . $appId . '/',
$optionalArgs
Expand Down Expand Up @@ -151,6 +152,7 @@ public function update($appId, array $optionalArgs = [])
*/
public function delete($appId, array $optionalArgs = [])
{
$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->delete(
$this->uri . $appId . '/',
$optionalArgs
Expand All @@ -172,10 +174,10 @@ public function get($appId)
new PlivoValidationException(
'app id is mandatory');
}

$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->fetch(
$this->uri . $appId . '/',
[]
$optionalArgs
);

return new Application(
Expand All @@ -198,6 +200,7 @@ public function get($appId)
*/
public function getList(array $optionalArgs = [])
{
$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->fetch(
$this->uri,
$optionalArgs
Expand Down
40 changes: 23 additions & 17 deletions src/Plivo/Resources/Call/CallInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function create($from, array $to, $answerUrl, $answerMethod,
'answer_url' => $answerUrl,
'answer_method' => $answerMethod
];
$optionalArgs['isVoiceRequest'] = true;

if (ArrayOperations::checkNull($mandatoryArgs)) {
throw new PlivoValidationException(
Expand Down Expand Up @@ -128,9 +129,10 @@ public function get($callUuid)
new PlivoValidationException(
'call uuid is mandatory');
}
$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->fetch(
$this->uri . $callUuid . '/',
[]
$optionalArgs
);

return new Call(
Expand All @@ -155,12 +157,11 @@ public function getLive($liveCallUuid)
}

$params = ['status' => 'live'];

$params['isVoiceRequest'] = true;
$response = $this->client->fetch(
$this->uri . $liveCallUuid . '/',
$params
);

return new CallLive(
$this->client,
$response->getContent(),
Expand All @@ -183,7 +184,7 @@ public function getQueued($queuedCallUuid)
}

$params = ['status' => 'queued'];

$params['isVoiceRequest'] = true;
$response = $this->client->fetch(
$this->uri . $queuedCallUuid . '/',
$params
Expand Down Expand Up @@ -227,6 +228,7 @@ public function getQueued($queuedCallUuid)
*/
public function getList(array $optionalArgs = [])
{
$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->fetch(
$this->uri,
$optionalArgs
Expand Down Expand Up @@ -254,7 +256,7 @@ public function getList(array $optionalArgs = [])
public function getListLive(array $optionalArgs = [])
{
$optionalArgs['status'] = 'live';

$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->fetch(
$this->uri,
$optionalArgs
Expand All @@ -273,7 +275,7 @@ public function getListLive(array $optionalArgs = [])
public function getListQueued()
{
$params = ['status' => 'queued'];

$params['isVoiceRequest'] = true;
$response = $this->client->fetch(
$this->uri,
$params
Expand All @@ -291,9 +293,10 @@ public function getListQueued()
*/
public function delete($callUuid = null)
{
$optionalArgs['isVoiceRequest'] = true;
$this->client->delete(
$this->uri . $callUuid . '/',
[]
$optionalArgs
);
}

Expand Down Expand Up @@ -354,7 +357,7 @@ public function transfer($liveCallUuid, array $optionalArgs = [])
"default is aleg, hence alegUrl is mandatory"
);
}

$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->update(
$this->uri . $liveCallUuid . '/',
$optionalArgs
Expand Down Expand Up @@ -447,7 +450,7 @@ public function startRecording($liveCallUuid, array $optionalArgs = [])
throw new PlivoValidationException(
"Which call to record? No callUuid given");
}

$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->update(
$this->uri . $liveCallUuid . '/Record/',
$optionalArgs
Expand Down Expand Up @@ -497,7 +500,7 @@ public function stopRecording($liveCallUuid, $url = null)
$params = ['URL' => $url];
}


$params['isVoiceRequest'] = true;
$this->client->delete(
$this->uri . $liveCallUuid . '/Record/',
$params
Expand Down Expand Up @@ -543,7 +546,7 @@ public function startPlaying($liveCallUuid, array $urls, array $optionalArgs = [
throw new PlivoValidationException(
"urls cannot be null");
}

$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->update(
$this->uri . $liveCallUuid . '/Play/',
array_merge(
Expand Down Expand Up @@ -585,10 +588,11 @@ public function stopPlaying($liveCallUuid)
throw new PlivoValidationException(
"Which call to stop playing in? No callUuid given");
}
$optionalArgs['isVoiceRequest'] = true;

$this->client->delete(
$this->uri . $liveCallUuid . '/Play/',
[]
$optionalArgs
);
}

Expand Down Expand Up @@ -639,7 +643,7 @@ public function startSpeaking($liveCallUuid, $text, array $optionalArgs = [])
throw new PlivoValidationException(
"text cannot be null");
}

$optionalArgs['isVoiceRequest'] = true;
$response = $this->client->update(
$this->uri . $liveCallUuid . '/Speak/',
array_merge(['text'=>$text], $optionalArgs)
Expand Down Expand Up @@ -677,10 +681,10 @@ public function stopSpeaking($liveCallUuid)
throw new PlivoValidationException(
"Which call to stop speaking in? No callUuid given");
}

$optionalArgs['isVoiceRequest'] = true;
$this->client->delete(
$this->uri . $liveCallUuid . '/Speak/',
[]
$optionalArgs
);
}

Expand Down Expand Up @@ -711,7 +715,8 @@ public function dtmf($liveCallUuid, $digits, $leg = null)
$this->uri . $liveCallUuid . '/DTMF/',
[
'digits' => $digits,
'leg' => $leg
'leg' => $leg,
'isVoiceRequest' => true
]
);

Expand Down Expand Up @@ -747,13 +752,14 @@ public function cancel($requestUuid)
throw new PlivoValidationException(
"Which call request to cancel? No requestUuid given");
}
$optionalArgs['isVoiceRequest'] = true;
$this->client->delete(
"Account/".
$this->pathParams['authId'].
"/Request/".
$requestUuid.
'/',
[]
$optionalArgs
);
}
}
Loading

0 comments on commit 1c4b36a

Please sign in to comment.