|
13 | 13 |
|
14 | 14 | conn = TigerGraphConnection( |
15 | 15 | host="http://localhost", |
16 | | - graphname="MyGraph", |
| 16 | + graphname="your_graph_name", |
17 | 17 | username="tigergraph", |
18 | 18 | password="tigergraph") |
19 | 19 |
|
@@ -64,7 +64,7 @@ def __init__(self, host: str = "http://127.0.0.1", graphname: str = "MyGraph", |
64 | 64 | protocol (http:// or https://). If `certPath` is `None` and the protocol is https, |
65 | 65 | a self-signed certificate will be used. |
66 | 66 | graphname: |
67 | | - The default graph for running queries. |
| 67 | + The graph name for running queries. **Required** - must be specified. |
68 | 68 | gsqlSecret: |
69 | 69 | The secret key for GSQL. See https://docs.tigergraph.com/tigergraph-server/current/user-access/managing-credentials#_secrets. |
70 | 70 | username: |
@@ -102,144 +102,17 @@ def __init__(self, host: str = "http://127.0.0.1", graphname: str = "MyGraph", |
102 | 102 | TigerGraphException: In case on invalid URL scheme. |
103 | 103 |
|
104 | 104 | """ |
105 | | - logger.info("entry: __init__") |
106 | | - if logger.level == logging.DEBUG: |
107 | | - logger.debug("params: " + self._locals(locals())) |
| 105 | + super().__init__(host=host, graphname=graphname, gsqlSecret=gsqlSecret, |
| 106 | + username=username, password=password, tgCloud=tgCloud, |
| 107 | + restppPort=restppPort, gsPort=gsPort, gsqlVersion=gsqlVersion, |
| 108 | + version=version, apiToken=apiToken, useCert=useCert, certPath=certPath, |
| 109 | + debug=debug, sslPort=sslPort, gcp=gcp, jwtToken=jwtToken) |
108 | 110 |
|
109 | | - inputHost = urlparse(host) |
110 | | - if inputHost.scheme not in ["http", "https"]: |
111 | | - raise TigerGraphException("Invalid URL scheme. Supported schemes are http and https.", |
112 | | - "E-0003") |
113 | | - self.netloc = inputHost.netloc |
114 | | - self.host = "{0}://{1}".format(inputHost.scheme, self.netloc) |
115 | | - if gsqlSecret != "": |
116 | | - self.username = "__GSQL__secret" |
117 | | - self.password = gsqlSecret |
118 | | - else: |
119 | | - self.username = username |
120 | | - self.password = password |
121 | | - self.graphname = graphname |
122 | | - self.responseConfigHeader = {} |
123 | | - self.awsIamHeaders = {} |
124 | | - |
125 | | - self.jwtToken = jwtToken |
126 | | - self.apiToken = apiToken |
127 | | - self.base64_credential = base64.b64encode( |
128 | | - "{0}:{1}".format(self.username, self.password).encode("utf-8")).decode("utf-8") |
129 | | - |
130 | | - self.authHeader = self._set_auth_header() |
131 | | - |
132 | | - # TODO Eliminate version and use gsqlVersion only, meaning TigerGraph server version |
133 | | - if gsqlVersion: |
134 | | - self.version = gsqlVersion |
135 | | - elif version: |
| 111 | + if graphname == "MyGraph": |
136 | 112 | warnings.warn( |
137 | | - "The `version` parameter is deprecated; use the `gsqlVersion` parameter instead.", |
138 | | - DeprecationWarning) |
139 | | - self.version = version |
140 | | - else: |
141 | | - self.version = "" |
142 | | - |
143 | | - if debug is not None: |
144 | | - warnings.warn( |
145 | | - "The `debug` parameter is deprecated; configure standard logging in your app.", |
146 | | - DeprecationWarning) |
147 | | - if not debug: |
148 | | - sys.excepthook = excepthook # TODO Why was this necessary? Can it be removed? |
149 | | - sys.tracebacklimit = None |
150 | | - |
151 | | - self.schema = None |
152 | | - |
153 | | - # TODO Remove useCert parameter |
154 | | - if useCert is not None: |
155 | | - warnings.warn( |
156 | | - "The `useCert` parameter is deprecated; the need for a CA certificate is now determined by URL scheme.", |
157 | | - DeprecationWarning) |
158 | | - if inputHost.scheme == "http": |
159 | | - self.downloadCert = False |
160 | | - self.useCert = False |
161 | | - self.certPath = "" |
162 | | - elif inputHost.scheme == "https": |
163 | | - if not certPath: |
164 | | - self.downloadCert = True |
165 | | - else: |
166 | | - self.downloadCert = False |
167 | | - self.useCert = True |
168 | | - self.certPath = certPath |
169 | | - self.sslPort = str(sslPort) |
170 | | - |
171 | | - # TODO Remove gcp parameter |
172 | | - if gcp: |
173 | | - warnings.warn("The `gcp` parameter is deprecated.", |
174 | | - DeprecationWarning) |
175 | | - self.tgCloud = tgCloud or gcp |
176 | | - if "tgcloud" in self.netloc.lower(): |
177 | | - try: # If get request succeeds, using TG Cloud instance provisioned after 6/20/2022 |
178 | | - self._get(self.host + "/api/ping", resKey="message") |
179 | | - self.tgCloud = True |
180 | | - # If get request fails, using TG Cloud instance provisioned before 6/20/2022, before new firewall config |
181 | | - except requests.exceptions.RequestException: |
182 | | - self.tgCloud = False |
183 | | - except TigerGraphException: |
184 | | - raise (TigerGraphException("Incorrect graphname.")) |
185 | | - |
186 | | - restppPort = str(restppPort) |
187 | | - gsPort = str(gsPort) |
188 | | - sslPort = str(sslPort) |
189 | | - if restppPort == gsPort: |
190 | | - self.restppPort = restppPort |
191 | | - self.restppUrl = self.host + ":" + restppPort + "/restpp" |
192 | | - elif (self.tgCloud and (restppPort == "9000" or restppPort == "443")): |
193 | | - if restppPort == gsPort: |
194 | | - sslPort = gsPort |
195 | | - self.restppPort = sslPort |
196 | | - self.restppUrl = self.host + ":" + sslPort + "/restpp" |
197 | | - else: |
198 | | - self.restppPort = restppPort |
199 | | - self.restppUrl = self.host + ":" + self.restppPort |
200 | | - |
201 | | - self.gsPort = gsPort |
202 | | - if self.tgCloud and (gsPort == "14240" or gsPort == "443"): |
203 | | - self.gsPort = sslPort |
204 | | - self.gsUrl = self.host + ":" + sslPort |
205 | | - else: |
206 | | - self.gsPort = gsPort |
207 | | - self.gsUrl = self.host + ":" + self.gsPort |
208 | | - self.url = "" |
209 | | - |
210 | | - if self.username.startswith("arn:aws:iam::"): |
211 | | - import boto3 |
212 | | - from botocore.awsrequest import AWSRequest |
213 | | - from botocore.auth import SigV4Auth |
214 | | - # Prepare a GetCallerIdentity request. |
215 | | - request = AWSRequest( |
216 | | - method="POST", |
217 | | - url="https://sts.amazonaws.com/?Action=GetCallerIdentity&Version=2011-06-15", |
218 | | - headers={ |
219 | | - 'Host': 'sts.amazonaws.com' |
220 | | - }) |
221 | | - # Get headers |
222 | | - SigV4Auth(boto3.Session().get_credentials(), |
223 | | - "sts", "us-east-1").add_auth(request) |
224 | | - self.awsIamHeaders["X-Amz-Date"] = request.headers["X-Amz-Date"] |
225 | | - self.awsIamHeaders["X-Amz-Security-Token"] = request.headers["X-Amz-Security-Token"] |
226 | | - self.awsIamHeaders["Authorization"] = request.headers["Authorization"] |
227 | | - |
228 | | - if self.jwtToken: |
229 | | - self._verify_jwt_token_support() |
230 | | - |
231 | | - self.asynchronous = False |
232 | | - |
233 | | - logger.info("exit: __init__") |
234 | | - |
235 | | - def _set_auth_header(self): |
236 | | - """Set the authentication header based on available tokens or credentials.""" |
237 | | - if self.jwtToken: |
238 | | - return {"Authorization": "Bearer " + self.jwtToken} |
239 | | - elif self.apiToken: |
240 | | - return {"Authorization": "Bearer " + self.apiToken} |
241 | | - else: |
242 | | - return {"Authorization": "Basic {0}".format(self.base64_credential)} |
| 113 | + "The default graphname 'MyGraph' is deprecated. Please explicitly specify your graph name.", |
| 114 | + DeprecationWarning |
| 115 | + ) |
243 | 116 |
|
244 | 117 | def _verify_jwt_token_support(self): |
245 | 118 | try: |
@@ -305,8 +178,8 @@ def _req(self, method: str, url: str, authMode: str = "token", headers: dict = N |
305 | 178 | Returns: |
306 | 179 | The (relevant part of the) response from the request (as a dictionary). |
307 | 180 | """ |
308 | | - _headers, _data, verify = self._prep_req( |
309 | | - authMode, headers, url, method, data) |
| 181 | + # Deprecated: authMode |
| 182 | + _headers, _data, verify = self._prep_req(headers, url, method, data) |
310 | 183 |
|
311 | 184 | if "GSQL-TIMEOUT" in _headers: |
312 | 185 | http_timeout = (10, int(int(_headers["GSQL-TIMEOUT"])/1000) + 10) |
@@ -361,6 +234,7 @@ def _req(self, method: str, url: str, authMode: str = "token", headers: dict = N |
361 | 234 | self.restppUrl = newRestppUrl |
362 | 235 | self.restppPort = self.gsPort |
363 | 236 | else: |
| 237 | + e.add_note(f"headers: {_headers}") |
364 | 238 | raise e |
365 | 239 |
|
366 | 240 | return self._parse_req(res, jsonResponse, strictJson, skipCheck, resKey) |
@@ -569,3 +443,11 @@ def _version_greater_than_4_0(self) -> bool: |
569 | 443 | if version[0] >= "4" and version[1] > "0": |
570 | 444 | return True |
571 | 445 | return False |
| 446 | + |
| 447 | + def _validate_graphname(self, operation_name=""): |
| 448 | + """Validate that graphname is set for operations that require it.""" |
| 449 | + if not self.graphname: |
| 450 | + raise TigerGraphException( |
| 451 | + f"Graph name is required for {operation_name}. Please specify graphname when creating the connection.", |
| 452 | + "E-0004" |
| 453 | + ) |
0 commit comments