diff --git a/README.md b/README.md index 6239e7e..401e4f0 100644 --- a/README.md +++ b/README.md @@ -14,18 +14,27 @@ from nocodb.infra.requests_client import NocoDBRequestsClient # Usage with API Token client = NocoDBRequestsClient( + # Your nocodb root path + "http://localhost:8080", # Your API Token retrieved from NocoDB conf APIToken("YOUR-API-TOKEN"), - # Your nocodb root path - "http://localhost:8080" ) # Usage with JWT Token client = NocoDBRequestsClient( + # Your nocodb root path + "http://localhost:8080", # Your API Token retrieved from NocoDB conf JWTAuthToken("your.jwt.token"), +) + +# Usage with email and password +client = NocoDBRequestsClient( # Your nocodb root path - "http://localhost:8080" + "http://localhost:8080", + # Your profile credentials + email="admin@example.com", + password="123qwe123!" ) ``` diff --git a/nocodb/api.py b/nocodb/api.py index aed549a..d46e7cb 100644 --- a/nocodb/api.py +++ b/nocodb/api.py @@ -5,6 +5,8 @@ class NocoDBAPIUris(Enum): V1_DB_DATA_PREFIX = "api/v1/db/data" V1_DB_META_PREFIX = "api/v1/db/meta" + V1_AUTH_USER_PREFIX = "api/v1/auth/user" + class NocoDBAPI: @@ -14,6 +16,15 @@ def __init__(self, base_uri: str): ) self.__base_meta_uri = ( f"{base_uri}/{NocoDBAPIUris.V1_DB_META_PREFIX.value}" + self.__base_auth_uri = ( + f"{base_uri}/{NocoDBAPIUris.V1_AUTH_USER_PREFIX.value}" + ) + def get_auth_uri(self): + return "/".join( + ( + self.__base_auth_uri, + "signin" + ) ) def get_table_uri(self, project: NocoDBProject, table: str) -> str: diff --git a/nocodb/infra/requests_client.py b/nocodb/infra/requests_client.py index fd223ed..9d08f1c 100644 --- a/nocodb/infra/requests_client.py +++ b/nocodb/infra/requests_client.py @@ -1,5 +1,6 @@ from typing import Optional from ..nocodb import ( + JWTAuthToken, NocoDBClient, NocoDBProject, AuthToken, @@ -12,13 +13,33 @@ class NocoDBRequestsClient(NocoDBClient): - def __init__(self, auth_token: AuthToken, base_uri: str): + def __init__( + self, + base_uri: str, + auth_token: AuthToken = None, + email: str = None, + password: str = None, + ): self.__session = requests.Session() + self.__api_info = NocoDBAPI(base_uri) + + if not auth_token and not (email and password): + raise ValueError("Either сredentials or token must be provided") + + if not auth_token and (email and password): + auth_token = JWTAuthToken(self.get_auth_token(email, password)) + self.__session.headers.update( - auth_token.get_header(), - ) + auth_token.get_header(), + ) self.__session.headers.update({"Content-Type": "application/json"}) - self.__api_info = NocoDBAPI(base_uri) + + def get_auth_token(self, email: str, password: str) -> str: + auth_token = self.__session.post( + self.__api_info.get_auth_uri(), + json=dict(email=email, password=password) + ).json()['token'] + return auth_token def table_row_list( self,