PollyClient.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Aws\Polly;
  3. use Aws\Api\Serializer\JsonBody;
  4. use Aws\AwsClient;
  5. use Aws\Signature\SignatureV4;
  6. use GuzzleHttp\Psr7\Request;
  7. use GuzzleHttp\Psr7\Uri;
  8. use GuzzleHttp\Psr7;
  9. /**
  10. * This client is used to interact with the **Amazon Polly** service.
  11. * @method \Aws\Result deleteLexicon(array $args = [])
  12. * @method \GuzzleHttp\Promise\Promise deleteLexiconAsync(array $args = [])
  13. * @method \Aws\Result describeVoices(array $args = [])
  14. * @method \GuzzleHttp\Promise\Promise describeVoicesAsync(array $args = [])
  15. * @method \Aws\Result getLexicon(array $args = [])
  16. * @method \GuzzleHttp\Promise\Promise getLexiconAsync(array $args = [])
  17. * @method \Aws\Result listLexicons(array $args = [])
  18. * @method \GuzzleHttp\Promise\Promise listLexiconsAsync(array $args = [])
  19. * @method \Aws\Result putLexicon(array $args = [])
  20. * @method \GuzzleHttp\Promise\Promise putLexiconAsync(array $args = [])
  21. * @method \Aws\Result synthesizeSpeech(array $args = [])
  22. * @method \GuzzleHttp\Promise\Promise synthesizeSpeechAsync(array $args = [])
  23. */
  24. class PollyClient extends AwsClient
  25. {
  26. /** @var JsonBody */
  27. private $formatter;
  28. /**
  29. * Create a pre-signed URL for Polly operation `SynthesizeSpeech`
  30. *
  31. * @param array $args parameters array for `SynthesizeSpeech`
  32. * More information @see Aws\Polly\PollyClient::SynthesizeSpeech
  33. *
  34. * @return string
  35. */
  36. public function createSynthesizeSpeechPreSignedUrl(array $args)
  37. {
  38. $uri = new Uri($this->getEndpoint());
  39. $uri = $uri->withPath('/v1/speech');
  40. // Formatting parameters follows rest-json protocol
  41. $this->formatter = $this->formatter ?: new JsonBody($this->getApi());
  42. $queryArray = json_decode(
  43. $this->formatter->build(
  44. $this->getApi()->getOperation('SynthesizeSpeech')->getInput(),
  45. $args
  46. ),
  47. true
  48. );
  49. // Mocking a 'GET' request in pre-signing the Url
  50. $query = Psr7\build_query($queryArray);
  51. $uri = $uri->withQuery($query);
  52. $request = new Request('GET', $uri);
  53. $request = $request->withBody(Psr7\stream_for(''));
  54. $signer = new SignatureV4('polly', $this->getRegion());
  55. return (string) $signer->presign(
  56. $request,
  57. $this->getCredentials()->wait(),
  58. '+15 minutes'
  59. )->getUri();
  60. }
  61. }