Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

155 rindas
5.0 KiB

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