PollyClient.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 getSpeechSynthesisTask(array $args = [])
  18. * @method \GuzzleHttp\Promise\Promise getSpeechSynthesisTaskAsync(array $args = [])
  19. * @method \Aws\Result listLexicons(array $args = [])
  20. * @method \GuzzleHttp\Promise\Promise listLexiconsAsync(array $args = [])
  21. * @method \Aws\Result listSpeechSynthesisTasks(array $args = [])
  22. * @method \GuzzleHttp\Promise\Promise listSpeechSynthesisTasksAsync(array $args = [])
  23. * @method \Aws\Result putLexicon(array $args = [])
  24. * @method \GuzzleHttp\Promise\Promise putLexiconAsync(array $args = [])
  25. * @method \Aws\Result startSpeechSynthesisTask(array $args = [])
  26. * @method \GuzzleHttp\Promise\Promise startSpeechSynthesisTaskAsync(array $args = [])
  27. * @method \Aws\Result synthesizeSpeech(array $args = [])
  28. * @method \GuzzleHttp\Promise\Promise synthesizeSpeechAsync(array $args = [])
  29. */
  30. class PollyClient extends AwsClient
  31. {
  32. /** @var JsonBody */
  33. private $formatter;
  34. /**
  35. * Create a pre-signed URL for Polly operation `SynthesizeSpeech`
  36. *
  37. * @param array $args parameters array for `SynthesizeSpeech`
  38. * More information @see Aws\Polly\PollyClient::SynthesizeSpeech
  39. *
  40. * @return string
  41. */
  42. public function createSynthesizeSpeechPreSignedUrl(array $args)
  43. {
  44. $uri = new Uri($this->getEndpoint());
  45. $uri = $uri->withPath('/v1/speech');
  46. // Formatting parameters follows rest-json protocol
  47. $this->formatter = $this->formatter ?: new JsonBody($this->getApi());
  48. $queryArray = json_decode(
  49. $this->formatter->build(
  50. $this->getApi()->getOperation('SynthesizeSpeech')->getInput(),
  51. $args
  52. ),
  53. true
  54. );
  55. // Mocking a 'GET' request in pre-signing the Url
  56. $query = Psr7\Query::build($queryArray);
  57. $uri = $uri->withQuery($query);
  58. $request = new Request('GET', $uri);
  59. $request = $request->withBody(Psr7\Utils::streamFor(''));
  60. $signer = new SignatureV4('polly', $this->getRegion());
  61. return (string) $signer->presign(
  62. $request,
  63. $this->getCredentials()->wait(),
  64. '+15 minutes'
  65. )->getUri();
  66. }
  67. }