Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

153 linhas
5.0 KiB

  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Code generated by gapic-generator. DO NOT EDIT.
  15. package texttospeech
  16. import (
  17. "context"
  18. "time"
  19. gax "github.com/googleapis/gax-go/v2"
  20. "google.golang.org/api/option"
  21. "google.golang.org/api/transport"
  22. texttospeechpb "google.golang.org/genproto/googleapis/cloud/texttospeech/v1"
  23. "google.golang.org/grpc"
  24. "google.golang.org/grpc/codes"
  25. "google.golang.org/grpc/metadata"
  26. )
  27. // CallOptions contains the retry settings for each method of Client.
  28. type CallOptions struct {
  29. ListVoices []gax.CallOption
  30. SynthesizeSpeech []gax.CallOption
  31. }
  32. func defaultClientOptions() []option.ClientOption {
  33. return []option.ClientOption{
  34. option.WithEndpoint("texttospeech.googleapis.com:443"),
  35. option.WithScopes(DefaultAuthScopes()...),
  36. }
  37. }
  38. func defaultCallOptions() *CallOptions {
  39. retry := map[[2]string][]gax.CallOption{
  40. {"default", "idempotent"}: {
  41. gax.WithRetry(func() gax.Retryer {
  42. return gax.OnCodes([]codes.Code{
  43. codes.DeadlineExceeded,
  44. codes.Unavailable,
  45. }, gax.Backoff{
  46. Initial: 100 * time.Millisecond,
  47. Max: 60000 * time.Millisecond,
  48. Multiplier: 1.3,
  49. })
  50. }),
  51. },
  52. }
  53. return &CallOptions{
  54. ListVoices: retry[[2]string{"default", "idempotent"}],
  55. SynthesizeSpeech: retry[[2]string{"default", "idempotent"}],
  56. }
  57. }
  58. // Client is a client for interacting with Cloud Text-to-Speech API.
  59. //
  60. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  61. type Client struct {
  62. // The connection to the service.
  63. conn *grpc.ClientConn
  64. // The gRPC API client.
  65. client texttospeechpb.TextToSpeechClient
  66. // The call options for this service.
  67. CallOptions *CallOptions
  68. // The x-goog-* metadata to be sent with each request.
  69. xGoogMetadata metadata.MD
  70. }
  71. // NewClient creates a new text to speech client.
  72. //
  73. // Service that implements Google Cloud Text-to-Speech API.
  74. func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
  75. conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
  76. if err != nil {
  77. return nil, err
  78. }
  79. c := &Client{
  80. conn: conn,
  81. CallOptions: defaultCallOptions(),
  82. client: texttospeechpb.NewTextToSpeechClient(conn),
  83. }
  84. c.setGoogleClientInfo()
  85. return c, nil
  86. }
  87. // Connection returns the client's connection to the API service.
  88. func (c *Client) Connection() *grpc.ClientConn {
  89. return c.conn
  90. }
  91. // Close closes the connection to the API service. The user should invoke this when
  92. // the client is no longer required.
  93. func (c *Client) Close() error {
  94. return c.conn.Close()
  95. }
  96. // setGoogleClientInfo sets the name and version of the application in
  97. // the `x-goog-api-client` header passed on each request. Intended for
  98. // use by Google-written clients.
  99. func (c *Client) setGoogleClientInfo(keyval ...string) {
  100. kv := append([]string{"gl-go", versionGo()}, keyval...)
  101. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  102. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  103. }
  104. // ListVoices returns a list of Voice supported for synthesis.
  105. func (c *Client) ListVoices(ctx context.Context, req *texttospeechpb.ListVoicesRequest, opts ...gax.CallOption) (*texttospeechpb.ListVoicesResponse, error) {
  106. ctx = insertMetadata(ctx, c.xGoogMetadata)
  107. opts = append(c.CallOptions.ListVoices[0:len(c.CallOptions.ListVoices):len(c.CallOptions.ListVoices)], opts...)
  108. var resp *texttospeechpb.ListVoicesResponse
  109. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  110. var err error
  111. resp, err = c.client.ListVoices(ctx, req, settings.GRPC...)
  112. return err
  113. }, opts...)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return resp, nil
  118. }
  119. // SynthesizeSpeech synthesizes speech synchronously: receive results after all text input
  120. // has been processed.
  121. func (c *Client) SynthesizeSpeech(ctx context.Context, req *texttospeechpb.SynthesizeSpeechRequest, opts ...gax.CallOption) (*texttospeechpb.SynthesizeSpeechResponse, error) {
  122. ctx = insertMetadata(ctx, c.xGoogMetadata)
  123. opts = append(c.CallOptions.SynthesizeSpeech[0:len(c.CallOptions.SynthesizeSpeech):len(c.CallOptions.SynthesizeSpeech)], opts...)
  124. var resp *texttospeechpb.SynthesizeSpeechResponse
  125. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  126. var err error
  127. resp, err = c.client.SynthesizeSpeech(ctx, req, settings.GRPC...)
  128. return err
  129. }, opts...)
  130. if err != nil {
  131. return nil, err
  132. }
  133. return resp, nil
  134. }