Skip to content

Commit

Permalink
Add ability for Voice to get Recording (#393)
Browse files Browse the repository at this point in the history
* untested method

* Added and tested the new getRecording method
  • Loading branch information
SecondeJK authored Mar 30, 2023
1 parent 09cac4a commit a9d0756
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Client/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function delete(string $id, array $headers = []): ?array
* @throws ClientExceptionInterface
* @throws Exception\Exception
*/
public function get($id, array $query = [], array $headers = [])
public function get($id, array $query = [], array $headers = [], bool $jsonResponse = true)
{
$uri = $this->getBaseUrl() . $this->baseUri . '/' . $id;

Expand Down Expand Up @@ -226,6 +226,10 @@ public function get($id, array $query = [], array $headers = [])
throw $e;
}

if (!$jsonResponse) {
return $response->getBody();
}

return json_decode($response->getBody()->getContents(), true);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Voice/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use DateTimeZone;
use Exception;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\StreamInterface;
use Vonage\Client\APIClient;
use Vonage\Client\APIResource;
use Vonage\Entity\Filter\FilterInterface;
Expand Down Expand Up @@ -266,4 +267,9 @@ public function unmuteCall(string $callId): void
{
$this->modifyCall($callId, CallAction::UNMUTE);
}

public function getRecording(string $url): StreamInterface
{
return $this->getAPIResource()->get($url, [], [], false);
}
}
35 changes: 35 additions & 0 deletions test/Voice/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace VonageTest\Voice;

use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use VonageTest\VonageTestCase;
use Prophecy\Argument;
use Psr\Http\Client\ClientExceptionInterface;
Expand Down Expand Up @@ -598,11 +600,44 @@ public function testCanSearchCalls(): void
$this->assertEquals($data['_embedded']['calls'][0]['uuid'], $call->getUuid());
}

public function testCanDownloadRecording(): void
{
$fixturePath = __DIR__ . '/Fixtures/mp3fixture.mp3';
$url = 'recordings/mp3fixture.mp3';

$this->vonageClient->send(Argument::that(function (RequestInterface $request) {
$this->assertEquals(
'Bearer ',
mb_substr($request->getHeaders()['Authorization'][0], 0, 7)
);

$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals(
'https://api.nexmo.com/v1/calls/recordings/mp3fixture.mp3',
$uriString
);
return true;
}))->willReturn($this->getResponseStream($fixturePath));

$result = $this->voiceClient->getRecording($url);

$this->assertStringEqualsFile($fixturePath, $result->getContents());
}

/**
* Get the API response we'd expect for a call to the API.
*/
protected function getResponse(string $type = 'success', int $status = 200): Response
{
return new Response(fopen(__DIR__ . '/responses/' . $type . '.json', 'rb'), $status);
}

/**
* Get the API response we'd expect for a call to the API.
*/
protected function getResponseStream(string $streamPath, int $status = 200): Response
{
return new Response(fopen($streamPath, 'rb'), $status);
}
}
Binary file added test/Voice/Fixtures/mp3fixture.mp3
Binary file not shown.

0 comments on commit a9d0756

Please sign in to comment.