1+ # -*- coding: utf-8 -*-
2+ #
13# Copyright 2018 Google LLC
24#
35# Licensed under the Apache License, Version 2.0 (the "License");
1517
1618import functools
1719import pkg_resources
20+ import warnings
1821
22+ from google .oauth2 import service_account
1923import google .api_core .gapic_v1 .client_info
2024import google .api_core .gapic_v1 .config
2125import google .api_core .gapic_v1 .method
2226import google .api_core .grpc_helpers
2327import google .api_core .page_iterator
28+ import grpc
2429
2530from google .cloud .trace_v1 .gapic import enums
2631from google .cloud .trace_v1 .gapic import trace_service_client_config
32+ from google .cloud .trace_v1 .gapic .transports import trace_service_grpc_transport
2733from google .cloud .trace_v1 .proto import trace_pb2
34+ from google .cloud .trace_v1 .proto import trace_pb2_grpc
35+ from google .protobuf import empty_pb2
2836from google .protobuf import timestamp_pb2
2937
3038_GAPIC_LIBRARY_VERSION = pkg_resources .get_distribution (
@@ -43,94 +51,117 @@ class TraceServiceClient(object):
4351 SERVICE_ADDRESS = 'cloudtrace.googleapis.com:443'
4452 """The default address of the service."""
4553
46- # The scopes needed to make gRPC calls to all of the methods defined in
47- # this service
48- _DEFAULT_SCOPES = (
49- 'https://www.googleapis.com/auth/cloud-platform' ,
50- 'https://www.googleapis.com/auth/trace.append' ,
51- 'https://www.googleapis.com/auth/trace.readonly' ,
52- )
53-
54- # The name of the interface for this client. This is the key used to find
55- # method configuration in the client_config dictionary.
54+ # The name of the interface for this client. This is the key used to
55+ # find the method configuration in the client_config dictionary.
5656 _INTERFACE_NAME = 'google.devtools.cloudtrace.v1.TraceService'
5757
58+ @classmethod
59+ def from_service_account_file (cls , filename , * args , ** kwargs ):
60+ """Creates an instance of this client using the provided credentials
61+ file.
62+
63+ Args:
64+ filename (str): The path to the service account private key json
65+ file.
66+ args: Additional arguments to pass to the constructor.
67+ kwargs: Additional arguments to pass to the constructor.
68+
69+ Returns:
70+ TraceServiceClient: The constructed client.
71+ """
72+ credentials = service_account .Credentials .from_service_account_file (
73+ filename )
74+ kwargs ['credentials' ] = credentials
75+ return cls (* args , ** kwargs )
76+
77+ from_service_account_json = from_service_account_file
78+
5879 def __init__ (self ,
80+ transport = None ,
5981 channel = None ,
6082 credentials = None ,
6183 client_config = trace_service_client_config .config ,
6284 client_info = None ):
6385 """Constructor.
6486
6587 Args:
66- channel (grpc.Channel): A ``Channel`` instance through
67- which to make calls. This argument is mutually exclusive
88+ transport (Union[~.TraceServiceGrpcTransport,
89+ Callable[[~.Credentials, type], ~.TraceServiceGrpcTransport]): A transport
90+ instance, responsible for actually making the API calls.
91+ The default transport uses the gRPC protocol.
92+ This argument may also be a callable which returns a
93+ transport instance. Callables will be sent the credentials
94+ as the first argument and the default transport class as
95+ the second argument.
96+ channel (grpc.Channel): DEPRECATED. A ``Channel`` instance
97+ through which to make calls. This argument is mutually exclusive
6898 with ``credentials``; providing both will raise an exception.
6999 credentials (google.auth.credentials.Credentials): The
70100 authorization credentials to attach to requests. These
71101 credentials identify this application to the service. If none
72102 are specified, the client will attempt to ascertain the
73103 credentials from the environment.
74- client_config (dict): A dictionary of call options for each
75- method. If not specified, the default configuration is used.
104+ This argument is mutually exclusive with providing a
105+ transport instance to ``transport``; doing so will raise
106+ an exception.
107+ client_config (dict): DEPRECATED. A dictionary of call options for
108+ each method. If not specified, the default configuration is used.
76109 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
77110 The client info used to send a user-agent string along with
78111 API requests. If ``None``, then default info will be used.
79112 Generally, you only need to set this if you're developing
80113 your own client library.
81114 """
82- # If both `channel` and `credentials` are specified, raise an
83- # exception (channels come with credentials baked in already).
84- if channel is not None and credentials is not None :
85- raise ValueError (
86- 'The `channel` and `credentials` arguments to {} are mutually '
87- 'exclusive.' .format (self .__class__ .__name__ ), )
88-
89- # Create the channel.
90- if channel is None :
91- channel = google .api_core .grpc_helpers .create_channel (
92- self .SERVICE_ADDRESS ,
115+ # Raise deprecation warnings for things we want to go away.
116+ if client_config :
117+ warnings .warn ('The `client_config` argument is deprecated.' ,
118+ PendingDeprecationWarning )
119+ if channel :
120+ warnings .warn (
121+ 'The `channel` argument is deprecated; use '
122+ '`transport` instead.' , PendingDeprecationWarning )
123+
124+ # Instantiate the transport.
125+ # The transport is responsible for handling serialization and
126+ # deserialization and actually sending data to the service.
127+ if transport :
128+ if callable (transport ):
129+ self .transport = transport (
130+ credentials = credentials ,
131+ default_class = trace_service_grpc_transport .
132+ TraceServiceGrpcTransport ,
133+ )
134+ else :
135+ if credentials :
136+ raise ValueError (
137+ 'Received both a transport instance and '
138+ 'credentials; these are mutually exclusive.' )
139+ self .transport = transport
140+ else :
141+ self .transport = trace_service_grpc_transport .TraceServiceGrpcTransport (
142+ address = self .SERVICE_ADDRESS ,
143+ channel = channel ,
93144 credentials = credentials ,
94- scopes = self ._DEFAULT_SCOPES ,
95145 )
96146
97- # Create the gRPC stubs.
98- self .trace_service_stub = (trace_pb2 .TraceServiceStub (channel ))
99-
100147 if client_info is None :
101148 client_info = (
102149 google .api_core .gapic_v1 .client_info .DEFAULT_CLIENT_INFO )
103150 client_info .gapic_version = _GAPIC_LIBRARY_VERSION
151+ self ._client_info = client_info
104152
105153 # Parse out the default settings for retry and timeout for each RPC
106154 # from the client configuration.
107155 # (Ordinarily, these are the defaults specified in the `*_config.py`
108156 # file next to this one.)
109- method_configs = google .api_core .gapic_v1 .config .parse_method_configs (
157+ self . _method_configs = google .api_core .gapic_v1 .config .parse_method_configs (
110158 client_config ['interfaces' ][self ._INTERFACE_NAME ], )
111159
112- # Write the "inner API call" methods to the class.
113- # These are wrapped versions of the gRPC stub methods, with retry and
114- # timeout configuration applied, called by the public methods on
115- # this class.
116- self ._patch_traces = google .api_core .gapic_v1 .method .wrap_method (
117- self .trace_service_stub .PatchTraces ,
118- default_retry = method_configs ['PatchTraces' ].retry ,
119- default_timeout = method_configs ['PatchTraces' ].timeout ,
120- client_info = client_info ,
121- )
122- self ._get_trace = google .api_core .gapic_v1 .method .wrap_method (
123- self .trace_service_stub .GetTrace ,
124- default_retry = method_configs ['GetTrace' ].retry ,
125- default_timeout = method_configs ['GetTrace' ].timeout ,
126- client_info = client_info ,
127- )
128- self ._list_traces = google .api_core .gapic_v1 .method .wrap_method (
129- self .trace_service_stub .ListTraces ,
130- default_retry = method_configs ['ListTraces' ].retry ,
131- default_timeout = method_configs ['ListTraces' ].timeout ,
132- client_info = client_info ,
133- )
160+ # Save a dictionary of cached API call functions.
161+ # These are the actual callables which invoke the proper
162+ # transport methods, wrapped with `wrap_method` to add retry,
163+ # timeout, and the like.
164+ self ._inner_api_calls = {}
134165
135166 # Service calls
136167 def patch_traces (self ,
@@ -151,7 +182,10 @@ def patch_traces(self,
151182 >>>
152183 >>> client = trace_v1.TraceServiceClient()
153184 >>>
185+ >>> # TODO: Initialize ``project_id``:
154186 >>> project_id = ''
187+ >>>
188+ >>> # TODO: Initialize ``traces``:
155189 >>> traces = {}
156190 >>>
157191 >>> client.patch_traces(project_id, traces)
@@ -177,14 +211,22 @@ def patch_traces(self,
177211 to a retryable error and retry attempts failed.
178212 ValueError: If the parameters are invalid.
179213 """
180- if metadata is None :
181- metadata = []
182- metadata = list (metadata )
214+ # Wrap the transport method to add retry and timeout logic.
215+ if 'patch_traces' not in self ._inner_api_calls :
216+ self ._inner_api_calls [
217+ 'patch_traces' ] = google .api_core .gapic_v1 .method .wrap_method (
218+ self .transport .patch_traces ,
219+ default_retry = self ._method_configs ['PatchTraces' ].retry ,
220+ default_timeout = self ._method_configs ['PatchTraces' ].
221+ timeout ,
222+ client_info = self ._client_info ,
223+ )
224+
183225 request = trace_pb2 .PatchTracesRequest (
184226 project_id = project_id ,
185227 traces = traces ,
186228 )
187- self ._patch_traces (
229+ self ._inner_api_calls [ 'patch_traces' ] (
188230 request , retry = retry , timeout = timeout , metadata = metadata )
189231
190232 def get_trace (self ,
@@ -201,7 +243,10 @@ def get_trace(self,
201243 >>>
202244 >>> client = trace_v1.TraceServiceClient()
203245 >>>
246+ >>> # TODO: Initialize ``project_id``:
204247 >>> project_id = ''
248+ >>>
249+ >>> # TODO: Initialize ``trace_id``:
205250 >>> trace_id = ''
206251 >>>
207252 >>> response = client.get_trace(project_id, trace_id)
@@ -228,14 +273,21 @@ def get_trace(self,
228273 to a retryable error and retry attempts failed.
229274 ValueError: If the parameters are invalid.
230275 """
231- if metadata is None :
232- metadata = []
233- metadata = list (metadata )
276+ # Wrap the transport method to add retry and timeout logic.
277+ if 'get_trace' not in self ._inner_api_calls :
278+ self ._inner_api_calls [
279+ 'get_trace' ] = google .api_core .gapic_v1 .method .wrap_method (
280+ self .transport .get_trace ,
281+ default_retry = self ._method_configs ['GetTrace' ].retry ,
282+ default_timeout = self ._method_configs ['GetTrace' ].timeout ,
283+ client_info = self ._client_info ,
284+ )
285+
234286 request = trace_pb2 .GetTraceRequest (
235287 project_id = project_id ,
236288 trace_id = trace_id ,
237289 )
238- return self ._get_trace (
290+ return self ._inner_api_calls [ 'get_trace' ] (
239291 request , retry = retry , timeout = timeout , metadata = metadata )
240292
241293 def list_traces (self ,
@@ -257,15 +309,18 @@ def list_traces(self,
257309 >>>
258310 >>> client = trace_v1.TraceServiceClient()
259311 >>>
312+ >>> # TODO: Initialize ``project_id``:
260313 >>> project_id = ''
261314 >>>
262- >>>
263315 >>> # Iterate over all results
264316 >>> for element in client.list_traces(project_id):
265317 ... # process element
266318 ... pass
267319 >>>
268- >>> # Or iterate over results one page at a time
320+ >>>
321+ >>> # Alternatively:
322+ >>>
323+ >>> # Iterate over results one page at a time
269324 >>> for page in client.list_traces(project_id, options=CallOptions(page_token=INITIAL_PAGE)):
270325 ... for element in page:
271326 ... # process element
@@ -351,9 +406,16 @@ def list_traces(self,
351406 to a retryable error and retry attempts failed.
352407 ValueError: If the parameters are invalid.
353408 """
354- if metadata is None :
355- metadata = []
356- metadata = list (metadata )
409+ # Wrap the transport method to add retry and timeout logic.
410+ if 'list_traces' not in self ._inner_api_calls :
411+ self ._inner_api_calls [
412+ 'list_traces' ] = google .api_core .gapic_v1 .method .wrap_method (
413+ self .transport .list_traces ,
414+ default_retry = self ._method_configs ['ListTraces' ].retry ,
415+ default_timeout = self ._method_configs ['ListTraces' ].timeout ,
416+ client_info = self ._client_info ,
417+ )
418+
357419 request = trace_pb2 .ListTracesRequest (
358420 project_id = project_id ,
359421 view = view ,
@@ -366,7 +428,7 @@ def list_traces(self,
366428 iterator = google .api_core .page_iterator .GRPCIterator (
367429 client = None ,
368430 method = functools .partial (
369- self ._list_traces ,
431+ self ._inner_api_calls [ 'list_traces' ] ,
370432 retry = retry ,
371433 timeout = timeout ,
372434 metadata = metadata ),
0 commit comments