You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

161 lines
5.4 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 expr
  16. import (
  17. "context"
  18. gax "github.com/googleapis/gax-go/v2"
  19. "google.golang.org/api/option"
  20. "google.golang.org/api/transport"
  21. exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
  22. "google.golang.org/grpc"
  23. "google.golang.org/grpc/metadata"
  24. )
  25. // CelCallOptions contains the retry settings for each method of CelClient.
  26. type CelCallOptions struct {
  27. Parse []gax.CallOption
  28. Check []gax.CallOption
  29. Eval []gax.CallOption
  30. }
  31. func defaultCelClientOptions() []option.ClientOption {
  32. return []option.ClientOption{
  33. option.WithEndpoint("cel.googleapis.com:443"),
  34. option.WithScopes(DefaultAuthScopes()...),
  35. }
  36. }
  37. func defaultCelCallOptions() *CelCallOptions {
  38. retry := map[[2]string][]gax.CallOption{}
  39. return &CelCallOptions{
  40. Parse: retry[[2]string{"default", "non_idempotent"}],
  41. Check: retry[[2]string{"default", "non_idempotent"}],
  42. Eval: retry[[2]string{"default", "non_idempotent"}],
  43. }
  44. }
  45. // CelClient is a client for interacting with Common Expression Language.
  46. //
  47. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  48. type CelClient struct {
  49. // The connection to the service.
  50. conn *grpc.ClientConn
  51. // The gRPC API client.
  52. celClient exprpb.CelServiceClient
  53. // The call options for this service.
  54. CallOptions *CelCallOptions
  55. // The x-goog-* metadata to be sent with each request.
  56. xGoogMetadata metadata.MD
  57. }
  58. // NewCelClient creates a new cel service client.
  59. //
  60. // Access a CEL implementation from another process or machine.
  61. // A CEL implementation is decomposed as a parser, a static checker,
  62. // and an evaluator. Every CEL implementation is expected to provide
  63. // a server for this API. The API will be used for conformance testing,
  64. // utilities, and execution as a service.
  65. func NewCelClient(ctx context.Context, opts ...option.ClientOption) (*CelClient, error) {
  66. conn, err := transport.DialGRPC(ctx, append(defaultCelClientOptions(), opts...)...)
  67. if err != nil {
  68. return nil, err
  69. }
  70. c := &CelClient{
  71. conn: conn,
  72. CallOptions: defaultCelCallOptions(),
  73. celClient: exprpb.NewCelServiceClient(conn),
  74. }
  75. c.setGoogleClientInfo()
  76. return c, nil
  77. }
  78. // Connection returns the client's connection to the API service.
  79. func (c *CelClient) Connection() *grpc.ClientConn {
  80. return c.conn
  81. }
  82. // Close closes the connection to the API service. The user should invoke this when
  83. // the client is no longer required.
  84. func (c *CelClient) Close() error {
  85. return c.conn.Close()
  86. }
  87. // setGoogleClientInfo sets the name and version of the application in
  88. // the `x-goog-api-client` header passed on each request. Intended for
  89. // use by Google-written clients.
  90. func (c *CelClient) setGoogleClientInfo(keyval ...string) {
  91. kv := append([]string{"gl-go", versionGo()}, keyval...)
  92. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  93. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  94. }
  95. // Parse transforms CEL source text into a parsed representation.
  96. func (c *CelClient) Parse(ctx context.Context, req *exprpb.ParseRequest, opts ...gax.CallOption) (*exprpb.ParseResponse, error) {
  97. ctx = insertMetadata(ctx, c.xGoogMetadata)
  98. opts = append(c.CallOptions.Parse[0:len(c.CallOptions.Parse):len(c.CallOptions.Parse)], opts...)
  99. var resp *exprpb.ParseResponse
  100. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  101. var err error
  102. resp, err = c.celClient.Parse(ctx, req, settings.GRPC...)
  103. return err
  104. }, opts...)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return resp, nil
  109. }
  110. // Check runs static checks on a parsed CEL representation and return
  111. // an annotated representation, or a set of issues.
  112. func (c *CelClient) Check(ctx context.Context, req *exprpb.CheckRequest, opts ...gax.CallOption) (*exprpb.CheckResponse, error) {
  113. ctx = insertMetadata(ctx, c.xGoogMetadata)
  114. opts = append(c.CallOptions.Check[0:len(c.CallOptions.Check):len(c.CallOptions.Check)], opts...)
  115. var resp *exprpb.CheckResponse
  116. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  117. var err error
  118. resp, err = c.celClient.Check(ctx, req, settings.GRPC...)
  119. return err
  120. }, opts...)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return resp, nil
  125. }
  126. // Eval evaluates a parsed or annotation CEL representation given
  127. // values of external bindings.
  128. func (c *CelClient) Eval(ctx context.Context, req *exprpb.EvalRequest, opts ...gax.CallOption) (*exprpb.EvalResponse, error) {
  129. ctx = insertMetadata(ctx, c.xGoogMetadata)
  130. opts = append(c.CallOptions.Eval[0:len(c.CallOptions.Eval):len(c.CallOptions.Eval)], opts...)
  131. var resp *exprpb.EvalResponse
  132. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  133. var err error
  134. resp, err = c.celClient.Eval(ctx, req, settings.GRPC...)
  135. return err
  136. }, opts...)
  137. if err != nil {
  138. return nil, err
  139. }
  140. return resp, nil
  141. }