Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gml 1894 doc fixes #264

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions pyTigerGraph/pyTigerGraphAuth.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,41 @@ def getToken(self,
secret: str = None,
setToken: bool = True,
lifetime: int = None) -> Union[Tuple[str, str], str]:
"""Requests an authorization token.

This function returns a token only if REST++ authentication is enabled. If not, an exception
will be raised.
See https://docs.tigergraph.com/admin/admin-guide/user-access-management/user-privileges-and-authentication#rest-authentication

Args:
secret (str, Optional):
The secret (string) generated in GSQL using `CREATE SECRET`.
See https://docs.tigergraph.com/tigergraph-server/current/user-access/managing-credentials#_create_a_secret
setToken (bool, Optional):
Set the connection's API token to the new value (default: `True`).
lifetime (int, Optional):
Duration of token validity (in seconds, default 30 days = 2,592,000 seconds).

Returns:
If your TigerGraph instance is running version <=3.10, the return value is
a tuple of `(<token>, <expiration_timestamp_unixtime>, <expiration_timestamp_ISO8601>)`.
The return value can be ignored, as the token is automatically set for the connection after this call.

If your TigerGraph instance is running version 4.0, the return value is a tuple of `(<token>, <expiration_timestamp_with_local_time>).

[NOTE]
The expiration timestamp's time zone might be different from your computer's local time
zone.

Raises:
`TigerGraphException` if REST++ authentication is not enabled or if an authentication
error occurred.

Endpoint:
- `POST /requesttoken` (In TigerGraph versions 3.x)
See https://docs.tigergraph.com/tigergraph-server/current/api/built-in-endpoints#_request_a_token
- `POST /gsql/v1/tokens` (In TigerGraph versions 4.x)
"""
logger.info("entry: getToken")
if logger.level == logging.DEBUG:
logger.debug("params: " + self._locals(locals()))
Expand All @@ -209,6 +244,44 @@ def getToken(self,
return token

def refreshToken(self, secret: str = None, setToken: bool = True, lifetime: int = None, token: str = None) -> Union[Tuple[str, str], str]:
"""Extends a token's lifetime.

This function works only if REST++ authentication is enabled. If not, an exception will be
raised.
See https://docs.tigergraph.com/admin/admin-guide/user-access-management/user-privileges-and-authentication#rest-authentication

Args:
secret:
The secret (string) generated in GSQL using `CREATE SECRET`.
See https://docs.tigergraph.com/tigergraph-server/current/user-access/managing-credentials#_create_a_secret
token:
The token requested earlier. If not specified, refreshes current connection's token.
lifetime:
Duration of token validity (in seconds, default 30 days = 2,592,000 seconds) from
current system timestamp.

Returns:
A tuple of `(<token>, <expiration_timestamp_unixtime>, <expiration_timestamp_ISO8601>)`.
The return value can be ignored. /
New expiration timestamp will be now + lifetime seconds, _not_ current expiration
timestamp + lifetime seconds.

[NOTE]
The expiration timestamp's time zone might be different from your computer's local time
zone.


Raises:
`TigerGraphException` if REST++ authentication is not enabled, if an authentication error
occurs, or if calling while using TigerGraph 4.x.

Note:
Not avaliable on TigerGraph version 4.x

Endpoint:
- `PUT /requesttoken`
See https://docs.tigergraph.com/tigergraph-server/current/api/built-in-endpoints#_refresh_a_token
"""
logger.info("entry: refreshToken")
if logger.level == logging.DEBUG:
logger.debug("params: " + self._locals(locals()))
Expand All @@ -229,6 +302,35 @@ def refreshToken(self, secret: str = None, setToken: bool = True, lifetime: int
return newToken

def deleteToken(self, secret: str, token: str = None, skipNA: bool = False) -> bool:
"""Deletes a token.

This function works only if REST++ authentication is enabled. If not, an exception will be
raised.
See https://docs.tigergraph.com/tigergraph-server/current/user-access/enabling-user-authentication#_enable_restpp_authentication

Args:
secret:
The secret (string) generated in GSQL using `CREATE SECRET`.
See https://docs.tigergraph.com/tigergraph-server/current/user-access/managing-credentials#_create_a_secret
token:
The token requested earlier. If not specified, deletes current connection's token,
so be careful.
skipNA:
Don't raise an exception if the specified token does not exist.

Returns:
`True`, if deletion was successful, or if the token did not exist but `skipNA` was
`True`.

Raises:
`TigerGraphException` if REST++ authentication is not enabled or an authentication error
occurred, for example if the specified token does not exist.

Endpoint:
- `DELETE /requesttoken` (In TigerGraph version 3.x)
See https://docs.tigergraph.com/tigergraph-server/current/api/built-in-endpoints#_delete_a_token
- `DELETE /gsql/v1/tokens` (In TigerGraph version 4.x)
"""
if not token:
token = self.apiToken
res, _ = self._token(secret, None, token, "DELETE")
Expand Down
30 changes: 30 additions & 0 deletions pyTigerGraph/pyTigerGraphBase.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
"""`TigerGraphConnection`

A TigerGraphConnection object provides the HTTP(S) communication used by all other modules.
This object is the **synchronous** version of the connection object.
If you want to use pyTigerGraph in an asynchronous environment, use the `AsyncTigerGraphConnection` object.

The `TigerGraphConnection` object is the main object that you will interact with when using pyTigerGraph.

To test your connection, you can use the `echo()` method. This method sends a simple request to the server and returns the response.

```python
from pyTigerGraph import TigerGraphConnection

conn = TigerGraphConnection(
host="http://localhost",
graphname="MyGraph",
username="tigergraph",
password="tigergraph")

print(conn.echo())
```
"""
import base64
import json
Expand Down Expand Up @@ -517,6 +533,20 @@ def getVer(self, component: str = "product", full: bool = False) -> str:
logger.info("exit: getVer")

return ret

def customizeHeader(self, timeout:int = 16_000, responseSize:int = 3.2e+7):
"""Method to configure the request header.

Args:
tiemout (int, optional):
The timeout value desired in milliseconds. Defaults to 16,000 ms (16 sec)
responseSize:
The size of the response in bytes. Defaults to 3.2E7 bytes (32 MB).

Returns:
Nothing. Sets `responseConfigHeader` class attribute.
"""
self.responseConfigHeader = {"GSQL-TIMEOUT": str(timeout), "RESPONSE-LIMIT": str(responseSize)}

def _version_greater_than_4_0(self) -> bool:
"""Gets if the TigerGraph database version is greater than 4.0 using gerVer().
Expand Down
42 changes: 42 additions & 0 deletions pyTigerGraph/pytgasync/pyTigerGraphBase.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
"""`AsyncTigerGraphConnection`

A TigerGraphConnection object provides the HTTP(S) communication used by all other modules.
This object is the **asynchronous** version of the connection object. If you want to use pyTigerGraph in an synchronous
environment, use the `TigerGraphConnection` object.

The `AsyncTigerGraphConnection` object is the main object that you will interact with when using pyTigerGraph.
It provides the same core functionality as the synchronous `TigerGraphConnection` object, but with asynchronous methods.

**Note:** `AsyncTigerGraphConnection` does not currently support the GDS or TigerGraph CoPilot APIs found in the synchronous version.

To test your connection, you can use the `echo()` method. This method sends a simple request to the server and returns the response.

```python
from pyTigerGraph import TigerGraphConnection

conn = AsyncTigerGraphConnection(
host="http://localhost",
graphname="MyGraph",
username="tigergraph",
password="tigergraph")

resp = await conn.echo()

print(resp)
```
"""

import json
import logging
import httpx
Expand Down Expand Up @@ -326,6 +354,20 @@ async def getVer(self, component: str = "product", full: bool = False) -> str:
logger.info("exit: getVer")

return ret

async def customizeHeader(self, timeout:int = 16_000, responseSize:int = 3.2e+7):
"""Method to configure the request header.

Args:
tiemout (int, optional):
The timeout value desired in milliseconds. Defaults to 16,000 ms (16 sec)
responseSize:
The size of the response in bytes. Defaults to 3.2E7 bytes (32 MB).

Returns:
Nothing. Sets `responseConfigHeader` class attribute.
"""
self.responseConfigHeader = {"GSQL-TIMEOUT": str(timeout), "RESPONSE-LIMIT": str(responseSize)}

async def _version_greater_than_4_0(self) -> bool:
"""Gets if the TigerGraph database version is greater than 4.0 using gerVer().
Expand Down
1 change: 1 addition & 0 deletions pyTigerGraph/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ def __init__(self, conn: TigerGraphConnection = None):
self.setUpConn(conn, db_rep)

def setUpConn(self, conn, db_rep):
"""NO DOC: function to set up the connection"""
self.graphname = db_rep["GraphName"]
for v_type in db_rep["VertexTypes"]:
vert = make_dataclass(v_type["Name"],
Expand Down