2323from gcloud ._helpers import _ClientProxy
2424from gcloud .pubsub ._implicit_environ import _require_connection
2525from gcloud .pubsub import api
26+ from gcloud .pubsub .connection import Connection
2627from gcloud .pubsub .subscription import Subscription
2728from gcloud .pubsub .topic import Topic
2829
@@ -35,8 +36,8 @@ class Client(object):
3536 from the environment.
3637
3738 :type project: str or None
38- :param connection : The configured project. Defaults to the value inferred
39- from the environment.
39+ :param project : The configured project. Defaults to the value inferred
40+ from the environment.
4041 """
4142
4243 def __init__ (self , connection = None , project = None ):
@@ -45,6 +46,73 @@ def __init__(self, connection=None, project=None):
4546 project = get_default_project ()
4647 self .project = project
4748
49+ @classmethod
50+ def from_service_account_json (cls , json_credentials_path , project = None ,
51+ * args , ** kwargs ):
52+ """Factory to retrieve JSON credentials while creating connection.
53+
54+ :type json_credentials_path: string
55+ :param json_credentials_path: The path to a private key file (this file
56+ was given to you when you created the
57+ service account). This file must contain
58+ a JSON object with a private key and
59+ other credentials information (downloaded
60+ from the Google APIs console).
61+
62+ :type project: str or None
63+ :param project: The configured project. Defaults to the value inferred
64+ from the environment.
65+
66+ :rtype: :class:`gcloud.pubsub.client.Client`
67+ :returns: A client, configured with a connection created with the
68+ retrieved JSON credentials, and the supplied project.
69+ """
70+ connection = Connection .from_service_account_json (
71+ json_credentials_path , * args , ** kwargs )
72+ return cls (connection = connection , project = project )
73+
74+ @classmethod
75+ def from_service_account_p12 (cls , client_email , private_key_path ,
76+ project = None , * args , ** kwargs ):
77+ """Factory to retrieve P12 credentials while creating connection.
78+
79+ .. note::
80+ Unless you have an explicit reason to use a PKCS12 key for your
81+ service account, we recommend using a JSON key.
82+
83+ :type client_email: string
84+ :param client_email: The e-mail attached to the service account.
85+
86+ :type private_key_path: string
87+ :param private_key_path: The path to a private key file (this file was
88+ given to you when you created the service
89+ account). This file must be in P12 format.
90+
91+ :type project: str or None
92+ :param project: The configured project. Defaults to the value inferred
93+ from the environment.
94+
95+ :rtype: :class:`gcloud.pubsub.client.Client`
96+ :returns: A client, configured with a connection created with the
97+ retrieved P12 credentials, and the supplied project.
98+ """
99+ connection = Connection .from_service_account_p12 (
100+ client_email , private_key_path , * args , ** kwargs )
101+ return cls (connection = connection , project = project )
102+
103+ @classmethod
104+ def from_environment (cls , project = None , * args , ** kwargs ):
105+ """Factory to retrieve implicit credentials while creating connection.
106+
107+ :rtype: :class:`gcloud.pubsub.client.Client`
108+ :returns: The connection created with the retrieved implicit
109+ credentials.
110+ :returns: A client, configured with a connection created with the
111+ retrieved implicit credentials, and the supplied project
112+ """
113+ connection = Connection .from_environment (* args , ** kwargs )
114+ return cls (connection = connection , project = project )
115+
48116 def topic (self , name ):
49117 """Proxy for :class:`gcloud.pubsub.topic.Topic`.
50118
0 commit comments