|
| 1 | +// internalFooClient is an interface that defines the methods available from Awesome Foo API. |
| 2 | +type internalFooClient interface { |
| 3 | + Close() error |
| 4 | + setGoogleClientInfo(...string) |
| 5 | + Connection() *grpc.ClientConn |
| 6 | + Zip(context.Context, *mypackagepb.Bar, ...gax.CallOption) (*ZipRegistryOperation, error) |
| 7 | + ZipRegistryOperation(name string) *ZipRegistryOperation |
| 8 | + ListOperations(context.Context, *longrunningpb.ListOperationsRequest, ...gax.CallOption) *OperationIterator |
| 9 | + GetOperation(context.Context, *longrunningpb.GetOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error) |
| 10 | + DeleteOperation(context.Context, *longrunningpb.DeleteOperationRequest, ...gax.CallOption) error |
| 11 | + CancelOperation(context.Context, *longrunningpb.CancelOperationRequest, ...gax.CallOption) error |
| 12 | + WaitOperation(context.Context, *longrunningpb.WaitOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error) |
| 13 | +} |
| 14 | + |
| 15 | +// FooClient is a client for interacting with Awesome Foo API. |
| 16 | +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. |
| 17 | +// |
| 18 | +// Foo service does stuff. |
| 19 | +type FooClient struct { |
| 20 | + // The internal transport-dependent client. |
| 21 | + internalClient internalFooClient |
| 22 | + |
| 23 | + // The call options for this service. |
| 24 | + CallOptions *FooCallOptions |
| 25 | + |
| 26 | + // LROClient is used internally to handle long-running operations. |
| 27 | + // It is exposed so that its CallOptions can be modified if required. |
| 28 | + // Users should not Close this client. |
| 29 | + LROClient *lroauto.OperationsClient |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +// Wrapper methods routed to the internal client. |
| 34 | + |
| 35 | +// Close closes the connection to the API service. The user should invoke this when |
| 36 | +// the client is no longer required. |
| 37 | +func (c *FooClient) Close() error { |
| 38 | + return c.internalClient.Close() |
| 39 | +} |
| 40 | + |
| 41 | +// setGoogleClientInfo sets the name and version of the application in |
| 42 | +// the `x-goog-api-client` header passed on each request. Intended for |
| 43 | +// use by Google-written clients. |
| 44 | +func (c *FooClient) setGoogleClientInfo(keyval ...string) { |
| 45 | + c.internalClient.setGoogleClientInfo(keyval...) |
| 46 | +} |
| 47 | + |
| 48 | +// Connection returns a connection to the API service. |
| 49 | +// |
| 50 | +// Deprecated: Connections are now pooled so this method does not always |
| 51 | +// return the same resource. |
| 52 | +func (c *FooClient) Connection() *grpc.ClientConn { |
| 53 | + return c.internalClient.Connection() |
| 54 | +} |
| 55 | + |
| 56 | +// Zip does some stuff. |
| 57 | +func (c *FooClient) Zip(ctx context.Context, req *mypackagepb.Bar, opts ...gax.CallOption) (*ZipRegistryOperation, error) { |
| 58 | + return c.internalClient.Zip(ctx, req, opts...) |
| 59 | +} |
| 60 | + |
| 61 | +// ZipRegistryOperation returns a new ZipRegistryOperation from a given name. |
| 62 | +// The name must be that of a previously created ZipRegistryOperation, possibly from a different process. |
| 63 | +func (c *FooClient) ZipRegistryOperation(name string) *ZipRegistryOperation { |
| 64 | + return c.internalClient.ZipRegistryOperation(name) |
| 65 | +} |
| 66 | + |
| 67 | +func (c *FooClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator { |
| 68 | + return c.internalClient.ListOperations(ctx, req, opts...) |
| 69 | +} |
| 70 | + |
| 71 | +func (c *FooClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { |
| 72 | + return c.internalClient.GetOperation(ctx, req, opts...) |
| 73 | +} |
| 74 | + |
| 75 | +func (c *FooClient) DeleteOperation(ctx context.Context, req *longrunningpb.DeleteOperationRequest, opts ...gax.CallOption) error { |
| 76 | + return c.internalClient.DeleteOperation(ctx, req, opts...) |
| 77 | +} |
| 78 | + |
| 79 | +func (c *FooClient) CancelOperation(ctx context.Context, req *longrunningpb.CancelOperationRequest, opts ...gax.CallOption) error { |
| 80 | + return c.internalClient.CancelOperation(ctx, req, opts...) |
| 81 | +} |
| 82 | + |
| 83 | +func (c *FooClient) WaitOperation(ctx context.Context, req *longrunningpb.WaitOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { |
| 84 | + return c.internalClient.WaitOperation(ctx, req, opts...) |
| 85 | +} |
| 86 | + |
| 87 | +// fooGRPCClient is a client for interacting with Awesome Foo API over gRPC transport. |
| 88 | +// |
| 89 | +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. |
| 90 | +type fooGRPCClient struct { |
| 91 | + // Connection pool of gRPC connections to the service. |
| 92 | + connPool gtransport.ConnPool |
| 93 | + |
| 94 | + // Points back to the CallOptions field of the containing FooClient |
| 95 | + CallOptions **FooCallOptions |
| 96 | + |
| 97 | + // The gRPC API client. |
| 98 | + fooClient mypackagepb.FooClient |
| 99 | + |
| 100 | + // LROClient is used internally to handle long-running operations. |
| 101 | + // It is exposed so that its CallOptions can be modified if required. |
| 102 | + // Users should not Close this client. |
| 103 | + LROClient **lroauto.OperationsClient |
| 104 | + |
| 105 | + operationsClient longrunningpb.OperationsClient |
| 106 | + |
| 107 | + // The x-goog-* metadata to be sent with each request. |
| 108 | + xGoogHeaders []string |
| 109 | +} |
| 110 | + |
| 111 | +// NewFooClient creates a new foo client based on gRPC. |
| 112 | +// The returned client must be Closed when it is done being used to clean up its underlying connections. |
| 113 | +// |
| 114 | +// Foo service does stuff. |
| 115 | +func NewFooClient(ctx context.Context, opts ...option.ClientOption) (*FooClient, error) { |
| 116 | + clientOpts := defaultFooGRPCClientOptions() |
| 117 | + if newFooClientHook != nil { |
| 118 | + hookOpts, err := newFooClientHook(ctx, clientHookParams{}) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + clientOpts = append(clientOpts, hookOpts...) |
| 123 | + } |
| 124 | + |
| 125 | + connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + client := FooClient{CallOptions: defaultFooCallOptions()} |
| 130 | + |
| 131 | + c := &fooGRPCClient{ |
| 132 | + connPool: connPool, |
| 133 | + fooClient: mypackagepb.NewFooClient(connPool), |
| 134 | + CallOptions: &client.CallOptions, |
| 135 | + operationsClient: longrunningpb.NewOperationsClient(connPool), |
| 136 | + |
| 137 | + } |
| 138 | + c.setGoogleClientInfo() |
| 139 | + |
| 140 | + client.internalClient = c |
| 141 | + |
| 142 | + client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) |
| 143 | + if err != nil { |
| 144 | + // This error "should not happen", since we are just reusing old connection pool |
| 145 | + // and never actually need to dial. |
| 146 | + // If this does happen, we could leak connp. However, we cannot close conn: |
| 147 | + // If the user invoked the constructor with option.WithGRPCConn, |
| 148 | + // we would close a connection that's still in use. |
| 149 | + // TODO: investigate error conditions. |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + c.LROClient = &client.LROClient |
| 153 | + return &client, nil |
| 154 | +} |
| 155 | + |
| 156 | +// Connection returns a connection to the API service. |
| 157 | +// |
| 158 | +// Deprecated: Connections are now pooled so this method does not always |
| 159 | +// return the same resource. |
| 160 | +func (c *fooGRPCClient) Connection() *grpc.ClientConn { |
| 161 | + return c.connPool.Conn() |
| 162 | +} |
| 163 | + |
| 164 | +// setGoogleClientInfo sets the name and version of the application in |
| 165 | +// the `x-goog-api-client` header passed on each request. Intended for |
| 166 | +// use by Google-written clients. |
| 167 | +func (c *fooGRPCClient) setGoogleClientInfo(keyval ...string) { |
| 168 | + kv := append([]string{"gl-go", gax.GoVersion}, keyval...) |
| 169 | + kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version) |
| 170 | + c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)} |
| 171 | +} |
| 172 | + |
| 173 | +// Close closes the connection to the API service. The user should invoke this when |
| 174 | +// the client is no longer required. |
| 175 | +func (c *fooGRPCClient) Close() error { |
| 176 | + return c.connPool.Close() |
| 177 | +} |
| 178 | + |
0 commit comments