Skip to content

Commit a9d0756

Browse files
authored
Add ability for Voice to get Recording (#393)
* untested method * Added and tested the new getRecording method
1 parent 09cac4a commit a9d0756

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/Client/APIResource.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function delete(string $id, array $headers = []): ?array
187187
* @throws ClientExceptionInterface
188188
* @throws Exception\Exception
189189
*/
190-
public function get($id, array $query = [], array $headers = [])
190+
public function get($id, array $query = [], array $headers = [], bool $jsonResponse = true)
191191
{
192192
$uri = $this->getBaseUrl() . $this->baseUri . '/' . $id;
193193

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

229+
if (!$jsonResponse) {
230+
return $response->getBody();
231+
}
232+
229233
return json_decode($response->getBody()->getContents(), true);
230234
}
231235

src/Voice/Client.php

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use DateTimeZone;
1616
use Exception;
1717
use Psr\Http\Client\ClientExceptionInterface;
18+
use Psr\Http\Message\StreamInterface;
1819
use Vonage\Client\APIClient;
1920
use Vonage\Client\APIResource;
2021
use Vonage\Entity\Filter\FilterInterface;
@@ -266,4 +267,9 @@ public function unmuteCall(string $callId): void
266267
{
267268
$this->modifyCall($callId, CallAction::UNMUTE);
268269
}
270+
271+
public function getRecording(string $url): StreamInterface
272+
{
273+
return $this->getAPIResource()->get($url, [], [], false);
274+
}
269275
}

test/Voice/ClientTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace VonageTest\Voice;
1313

1414
use Laminas\Diactoros\Response;
15+
use Psr\Http\Message\ResponseInterface;
16+
use Psr\Http\Message\StreamInterface;
1517
use VonageTest\VonageTestCase;
1618
use Prophecy\Argument;
1719
use Psr\Http\Client\ClientExceptionInterface;
@@ -598,11 +600,44 @@ public function testCanSearchCalls(): void
598600
$this->assertEquals($data['_embedded']['calls'][0]['uuid'], $call->getUuid());
599601
}
600602

603+
public function testCanDownloadRecording(): void
604+
{
605+
$fixturePath = __DIR__ . '/Fixtures/mp3fixture.mp3';
606+
$url = 'recordings/mp3fixture.mp3';
607+
608+
$this->vonageClient->send(Argument::that(function (RequestInterface $request) {
609+
$this->assertEquals(
610+
'Bearer ',
611+
mb_substr($request->getHeaders()['Authorization'][0], 0, 7)
612+
);
613+
614+
$uri = $request->getUri();
615+
$uriString = $uri->__toString();
616+
$this->assertEquals(
617+
'https://api.nexmo.com/v1/calls/recordings/mp3fixture.mp3',
618+
$uriString
619+
);
620+
return true;
621+
}))->willReturn($this->getResponseStream($fixturePath));
622+
623+
$result = $this->voiceClient->getRecording($url);
624+
625+
$this->assertStringEqualsFile($fixturePath, $result->getContents());
626+
}
627+
601628
/**
602629
* Get the API response we'd expect for a call to the API.
603630
*/
604631
protected function getResponse(string $type = 'success', int $status = 200): Response
605632
{
606633
return new Response(fopen(__DIR__ . '/responses/' . $type . '.json', 'rb'), $status);
607634
}
635+
636+
/**
637+
* Get the API response we'd expect for a call to the API.
638+
*/
639+
protected function getResponseStream(string $streamPath, int $status = 200): Response
640+
{
641+
return new Response(fopen($streamPath, 'rb'), $status);
642+
}
608643
}

test/Voice/Fixtures/mp3fixture.mp3

746 KB
Binary file not shown.

0 commit comments

Comments
 (0)