Skip to content

Commit 72df5b1

Browse files
authored
Merge branch 'master' into feat/project_create
2 parents 2335305 + 8c5a07f commit 72df5b1

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,27 @@ from nocodb.infra.requests_client import NocoDBRequestsClient
1414

1515
# Usage with API Token
1616
client = NocoDBRequestsClient(
17+
# Your nocodb root path
18+
"http://localhost:8080",
1719
# Your API Token retrieved from NocoDB conf
1820
APIToken("YOUR-API-TOKEN"),
19-
# Your nocodb root path
20-
"http://localhost:8080"
2121
)
2222

2323
# Usage with JWT Token
2424
client = NocoDBRequestsClient(
25+
# Your nocodb root path
26+
"http://localhost:8080",
2527
# Your API Token retrieved from NocoDB conf
2628
JWTAuthToken("your.jwt.token"),
29+
)
30+
31+
# Usage with email and password
32+
client = NocoDBRequestsClient(
2733
# Your nocodb root path
28-
"http://localhost:8080"
34+
"http://localhost:8080",
35+
# Your profile credentials
36+
37+
password="123qwe123!"
2938
)
3039
```
3140

nocodb/api.py

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
class NocoDBAPIUris(Enum):
66
V1_DB_DATA_PREFIX = "api/v1/db/data"
77
V1_DB_META_PREFIX = "api/v1/db/meta"
8+
V1_AUTH_USER_PREFIX = "api/v1/auth/user"
9+
810

911

1012
class NocoDBAPI:
@@ -14,6 +16,15 @@ def __init__(self, base_uri: str):
1416
)
1517
self.__base_meta_uri = (
1618
f"{base_uri}/{NocoDBAPIUris.V1_DB_META_PREFIX.value}"
19+
self.__base_auth_uri = (
20+
f"{base_uri}/{NocoDBAPIUris.V1_AUTH_USER_PREFIX.value}"
21+
)
22+
def get_auth_uri(self):
23+
return "/".join(
24+
(
25+
self.__base_auth_uri,
26+
"signin"
27+
)
1728
)
1829

1930
def get_table_uri(self, project: NocoDBProject, table: str) -> str:

nocodb/infra/requests_client.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Optional
22
from ..nocodb import (
3+
JWTAuthToken,
34
NocoDBClient,
45
NocoDBProject,
56
AuthToken,
@@ -12,13 +13,33 @@
1213

1314

1415
class NocoDBRequestsClient(NocoDBClient):
15-
def __init__(self, auth_token: AuthToken, base_uri: str):
16+
def __init__(
17+
self,
18+
base_uri: str,
19+
auth_token: AuthToken = None,
20+
email: str = None,
21+
password: str = None,
22+
):
1623
self.__session = requests.Session()
24+
self.__api_info = NocoDBAPI(base_uri)
25+
26+
if not auth_token and not (email and password):
27+
raise ValueError("Either сredentials or token must be provided")
28+
29+
if not auth_token and (email and password):
30+
auth_token = JWTAuthToken(self.get_auth_token(email, password))
31+
1732
self.__session.headers.update(
18-
auth_token.get_header(),
19-
)
33+
auth_token.get_header(),
34+
)
2035
self.__session.headers.update({"Content-Type": "application/json"})
21-
self.__api_info = NocoDBAPI(base_uri)
36+
37+
def get_auth_token(self, email: str, password: str) -> str:
38+
auth_token = self.__session.post(
39+
self.__api_info.get_auth_uri(),
40+
json=dict(email=email, password=password)
41+
).json()['token']
42+
return auth_token
2243

2344
def table_row_list(
2445
self,

0 commit comments

Comments
 (0)