From fc8baef83e4b79ce0f7eb93cc6c5f55cd3f93fb1 Mon Sep 17 00:00:00 2001 From: Ilya Sapunov Date: Thu, 15 Sep 2022 12:21:34 +0300 Subject: [PATCH 1/2] add auth with credentials --- README.md | 15 ++++++++++++--- nocodb/api.py | 11 +++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 25e8c1d..80d855a 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 efd7ccc..3082ae6 100644 --- a/nocodb/api.py +++ b/nocodb/api.py @@ -4,6 +4,7 @@ class NocoDBAPIUris(Enum): V1_DB_DATA_PREFIX = "api/v1/db/data" + V1_AUTH_USER_PREFIX = "api/v1/auth/user" class NocoDBAPI: @@ -11,6 +12,16 @@ def __init__(self, base_uri: str): self.__base_data_uri = ( f"{base_uri}/{NocoDBAPIUris.V1_DB_DATA_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: return "/".join( From 597984da128fac2f87e15cb609ad7c2ecbf731a6 Mon Sep 17 00:00:00 2001 From: Ilya Sapunov Date: Thu, 15 Sep 2022 12:31:30 +0300 Subject: [PATCH 2/2] add auth with credentials --- nocodb/infra/requests_client.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/nocodb/infra/requests_client.py b/nocodb/infra/requests_client.py index 1b57df9..f303447 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,