das-cli 1.6.7__tar.gz → 1.7.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {das_cli-1.6.7/das_cli.egg-info → das_cli-1.7.3}/PKG-INFO +115 -3
- {das_cli-1.6.7 → das_cli-1.7.3}/README.md +114 -2
- das_cli-1.7.3/das/app.py +113 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/cli.py +177 -14
- {das_cli-1.6.7 → das_cli-1.7.3}/das/common/config.py +110 -13
- das_cli-1.7.3/das/services/session.py +94 -0
- {das_cli-1.6.7 → das_cli-1.7.3/das_cli.egg-info}/PKG-INFO +115 -3
- {das_cli-1.6.7 → das_cli-1.7.3}/das_cli.egg-info/SOURCES.txt +1 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/pyproject.toml +1 -1
- das_cli-1.6.7/das/app.py +0 -54
- {das_cli-1.6.7 → das_cli-1.7.3}/LICENSE +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/MANIFEST.in +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/__init__.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/ai/plugins/dasai.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/ai/plugins/entries/entries_plugin.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/authentication/auth.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/authentication/oauth.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/authentication/secure_input.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/common/api.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/common/entry_fields_constants.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/common/enums.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/common/errors.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/common/exceptions.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/common/file_utils.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/managers/__init__.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/managers/digital_objects_manager.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/managers/download_manager.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/managers/entries_manager.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/managers/search_manager.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/attributes.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/cache.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/das_elastic_search.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/digital_objects.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/downloads.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/entries.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/entry_fields.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/hangfire.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/search.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/service_base.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das/services/users.py +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das_cli.egg-info/dependency_links.txt +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das_cli.egg-info/entry_points.txt +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das_cli.egg-info/requires.txt +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/das_cli.egg-info/top_level.txt +0 -0
- {das_cli-1.6.7 → das_cli-1.7.3}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: das-cli
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.7.3
|
|
4
4
|
Summary: DAS api client.
|
|
5
5
|
Author: Royal Netherlands Institute for Sea Research
|
|
6
6
|
License-Expression: MIT
|
|
@@ -51,6 +51,21 @@ cd das-cli
|
|
|
51
51
|
pip install -e .
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
To exercise or debug the CLI from the repo without relying on the installed
|
|
55
|
+
`das` console script, run it as a module:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# From the repo root (after pip install -e ., or with PYTHONPATH set to the repo)
|
|
59
|
+
python -m das.cli --help
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
On PowerShell, if you have not done an editable install yet:
|
|
63
|
+
|
|
64
|
+
```powershell
|
|
65
|
+
$env:PYTHONPATH = "C:\Workspace\das-cli" # path to your clone
|
|
66
|
+
python -m das.cli --help
|
|
67
|
+
```
|
|
68
|
+
|
|
54
69
|
## Quickstart (CLI)
|
|
55
70
|
|
|
56
71
|
```bash
|
|
@@ -71,6 +86,7 @@ das entry get --code zb.b.1ub
|
|
|
71
86
|
- [Username/password](#usernamepassword)
|
|
72
87
|
- [API key](#api-key)
|
|
73
88
|
- [OAuth / Azure Entra](#oauth--azure-entra)
|
|
89
|
+
- [Who am I?](#who-am-i)
|
|
74
90
|
- [Search and Read](#search-and-read)
|
|
75
91
|
- [Entries](#entries)
|
|
76
92
|
- [Create entries](#create-entries)
|
|
@@ -93,16 +109,82 @@ If `--username` or `--password` is omitted, the CLI prompts securely.
|
|
|
93
109
|
#### API key
|
|
94
110
|
```bash
|
|
95
111
|
das login --api-url https://your-das-instance/api --api-key <your-api-key>
|
|
112
|
+
|
|
113
|
+
# Save the key without contacting the server
|
|
114
|
+
das login --api-url https://your-das-instance/api --api-key <your-api-key> --skip-verify
|
|
96
115
|
```
|
|
97
116
|
|
|
98
117
|
Generate the key in DAS Web UI: **My Settings -> API Keys**.
|
|
99
118
|
|
|
119
|
+
The key is stored *and* checked against the server, so you find out immediately
|
|
120
|
+
whether it works and which account it belongs to:
|
|
121
|
+
|
|
122
|
+
```text
|
|
123
|
+
✓ API key saved and verified.
|
|
124
|
+
Authenticated as: alovelace (Ada Lovelace, ada@example.org)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
A key the server refuses (mistyped, revoked, expired, or issued for a different
|
|
128
|
+
DAS environment) is still saved — so nothing is lost if the URL was wrong — but
|
|
129
|
+
reported clearly:
|
|
130
|
+
|
|
131
|
+
```text
|
|
132
|
+
✓ API key saved.
|
|
133
|
+
❌ The server did not accept this API key.
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
If the server cannot be reached the key is saved with a warning instead of a
|
|
137
|
+
verdict. Use `--skip-verify` to skip the check altogether.
|
|
138
|
+
|
|
100
139
|
#### OAuth / Azure Entra
|
|
101
140
|
```bash
|
|
102
141
|
das oauth configure --client-id <client-id> --authority https://login.microsoftonline.com/<tenant>/v2.0
|
|
103
142
|
das login --api-url https://your-das-instance/api --oauth
|
|
104
143
|
```
|
|
105
144
|
|
|
145
|
+
#### Who am I?
|
|
146
|
+
|
|
147
|
+
`das whoami` answers two questions: **which** credential the CLI sends, and
|
|
148
|
+
**whose** it is according to the server.
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
# Which credential is used, and is it still valid?
|
|
152
|
+
das whoami
|
|
153
|
+
|
|
154
|
+
# Local information only, no server call
|
|
155
|
+
das whoami --offline
|
|
156
|
+
|
|
157
|
+
# Machine-readable (the credential itself is never printed, only a masked form)
|
|
158
|
+
das whoami --format json
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
```text
|
|
162
|
+
Current DAS credential
|
|
163
|
+
────────────────────────────────────────
|
|
164
|
+
API URL : https://your-das-instance/api
|
|
165
|
+
Auth type : api_key
|
|
166
|
+
Credential : das_2702…fe84
|
|
167
|
+
Source : DAS_API_KEY environment variable
|
|
168
|
+
Defined in : C:\Workspace\my-project\.env
|
|
169
|
+
⚠ DAS_API_KEY overrides the credential saved by 'das login' (eyJhbGci…zZt0).
|
|
170
|
+
|
|
171
|
+
Server
|
|
172
|
+
Status : ✓ credential accepted
|
|
173
|
+
User name : alovelace
|
|
174
|
+
Full name : Ada Lovelace
|
|
175
|
+
Email : ada@example.org
|
|
176
|
+
User ID : 42
|
|
177
|
+
Tenant : Default (id 1)
|
|
178
|
+
DAS version: 12.3.0.0
|
|
179
|
+
────────────────────────────────────────
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
**Credential precedence.** A `DAS_API_KEY` in your environment or `.env` file
|
|
183
|
+
always wins over whatever `das login` saved. That is the usual reason commands
|
|
184
|
+
run as someone else than expected, so `das whoami` (and `das config show`) name
|
|
185
|
+
the source, point at the `.env` file that defines it, and warn when the
|
|
186
|
+
environment variable shadows a different stored credential.
|
|
187
|
+
|
|
106
188
|
### Search and Read
|
|
107
189
|
|
|
108
190
|
```bash
|
|
@@ -252,6 +334,7 @@ Two common patterns are supported.
|
|
|
252
334
|
|
|
253
335
|
- [Pattern A: login once with CLI, then use managers in Python](#pattern-a-login-once-with-cli-then-use-managers-in-python)
|
|
254
336
|
- [Pattern B: authenticate in Python with `Das`](#pattern-b-authenticate-in-python-with-das)
|
|
337
|
+
- [Checking the active credential](#checking-the-active-credential)
|
|
255
338
|
- Managers — full API reference below:
|
|
256
339
|
- [`EntryManager`](#entrymanager)
|
|
257
340
|
- [`SearchManager`](#searchmanager)
|
|
@@ -281,15 +364,40 @@ from das.managers.entries_manager import EntryManager
|
|
|
281
364
|
client = Das("https://your-das-instance/api")
|
|
282
365
|
client.authenticate("your_user", "your_password")
|
|
283
366
|
|
|
284
|
-
# Or API key
|
|
367
|
+
# Or API key — validate=True returns the user the key belongs to (None if refused)
|
|
285
368
|
# client = Das("https://your-das-instance/api")
|
|
286
|
-
# client.
|
|
369
|
+
# user = client.set_api_key("your-api-key", validate=True)
|
|
370
|
+
# if user is None:
|
|
371
|
+
# raise SystemExit("The server did not accept this API key.")
|
|
287
372
|
|
|
288
373
|
entry_manager = EntryManager()
|
|
289
374
|
entry = entry_manager.get(code="zb.b.1ub")
|
|
290
375
|
print(entry.get("Name"))
|
|
291
376
|
```
|
|
292
377
|
|
|
378
|
+
### Checking the active credential
|
|
379
|
+
|
|
380
|
+
`Das.whoami()` is the programmatic form of `das whoami`: it reports which
|
|
381
|
+
credential will be sent, where it came from, and who the server says that is.
|
|
382
|
+
|
|
383
|
+
```python
|
|
384
|
+
from das.app import Das
|
|
385
|
+
|
|
386
|
+
info = Das("https://your-das-instance/api").whoami()
|
|
387
|
+
|
|
388
|
+
print(info["status"]) # 'valid' | 'invalid' | 'no-credential' | 'unreachable'
|
|
389
|
+
print(info["source"]) # 'env' (DAS_API_KEY) | 'keyring' | 'file'
|
|
390
|
+
print(info["masked"]) # 'das_2702…fe84' — never the raw credential
|
|
391
|
+
if info["status"] == "valid":
|
|
392
|
+
print(info["user"]["userName"], info["user"]["emailAddress"])
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
`whoami(verify=False)` skips the server call and reports local information only
|
|
396
|
+
(`status` is then `"unverified"`). Lower-level access is available through
|
|
397
|
+
`client.session` (`SessionService`): `get_current_login_information()`,
|
|
398
|
+
`get_current_user()` and `validate_api_key(key)` — the last one checks a key
|
|
399
|
+
without storing it.
|
|
400
|
+
|
|
293
401
|
## Manager Layer API Reference (Domain Layer)
|
|
294
402
|
|
|
295
403
|
Managers are the recommended Python API. They translate user-friendly field names, codes, and query syntax into the lower-level DAS API calls.
|
|
@@ -874,6 +982,10 @@ The tests are a good source of realistic payload examples, especially:
|
|
|
874
982
|
- raise verbosity with `das config log-level DEBUG` (optionally
|
|
875
983
|
`das config log-file <path>`) to see the full HTTP requests and error
|
|
876
984
|
details. See [Logging](#logging).
|
|
985
|
+
- Debug or inspect the CLI from source:
|
|
986
|
+
- run `python -m das.cli --help` from the repo root (after `pip install -e .`,
|
|
987
|
+
or with `PYTHONPATH` pointing at the clone) to confirm the package loads and
|
|
988
|
+
list available commands without using the `das` console script.
|
|
877
989
|
|
|
878
990
|
## Contributing
|
|
879
991
|
|
|
@@ -28,6 +28,21 @@ cd das-cli
|
|
|
28
28
|
pip install -e .
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
To exercise or debug the CLI from the repo without relying on the installed
|
|
32
|
+
`das` console script, run it as a module:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# From the repo root (after pip install -e ., or with PYTHONPATH set to the repo)
|
|
36
|
+
python -m das.cli --help
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
On PowerShell, if you have not done an editable install yet:
|
|
40
|
+
|
|
41
|
+
```powershell
|
|
42
|
+
$env:PYTHONPATH = "C:\Workspace\das-cli" # path to your clone
|
|
43
|
+
python -m das.cli --help
|
|
44
|
+
```
|
|
45
|
+
|
|
31
46
|
## Quickstart (CLI)
|
|
32
47
|
|
|
33
48
|
```bash
|
|
@@ -48,6 +63,7 @@ das entry get --code zb.b.1ub
|
|
|
48
63
|
- [Username/password](#usernamepassword)
|
|
49
64
|
- [API key](#api-key)
|
|
50
65
|
- [OAuth / Azure Entra](#oauth--azure-entra)
|
|
66
|
+
- [Who am I?](#who-am-i)
|
|
51
67
|
- [Search and Read](#search-and-read)
|
|
52
68
|
- [Entries](#entries)
|
|
53
69
|
- [Create entries](#create-entries)
|
|
@@ -70,16 +86,82 @@ If `--username` or `--password` is omitted, the CLI prompts securely.
|
|
|
70
86
|
#### API key
|
|
71
87
|
```bash
|
|
72
88
|
das login --api-url https://your-das-instance/api --api-key <your-api-key>
|
|
89
|
+
|
|
90
|
+
# Save the key without contacting the server
|
|
91
|
+
das login --api-url https://your-das-instance/api --api-key <your-api-key> --skip-verify
|
|
73
92
|
```
|
|
74
93
|
|
|
75
94
|
Generate the key in DAS Web UI: **My Settings -> API Keys**.
|
|
76
95
|
|
|
96
|
+
The key is stored *and* checked against the server, so you find out immediately
|
|
97
|
+
whether it works and which account it belongs to:
|
|
98
|
+
|
|
99
|
+
```text
|
|
100
|
+
✓ API key saved and verified.
|
|
101
|
+
Authenticated as: alovelace (Ada Lovelace, ada@example.org)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
A key the server refuses (mistyped, revoked, expired, or issued for a different
|
|
105
|
+
DAS environment) is still saved — so nothing is lost if the URL was wrong — but
|
|
106
|
+
reported clearly:
|
|
107
|
+
|
|
108
|
+
```text
|
|
109
|
+
✓ API key saved.
|
|
110
|
+
❌ The server did not accept this API key.
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
If the server cannot be reached the key is saved with a warning instead of a
|
|
114
|
+
verdict. Use `--skip-verify` to skip the check altogether.
|
|
115
|
+
|
|
77
116
|
#### OAuth / Azure Entra
|
|
78
117
|
```bash
|
|
79
118
|
das oauth configure --client-id <client-id> --authority https://login.microsoftonline.com/<tenant>/v2.0
|
|
80
119
|
das login --api-url https://your-das-instance/api --oauth
|
|
81
120
|
```
|
|
82
121
|
|
|
122
|
+
#### Who am I?
|
|
123
|
+
|
|
124
|
+
`das whoami` answers two questions: **which** credential the CLI sends, and
|
|
125
|
+
**whose** it is according to the server.
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
# Which credential is used, and is it still valid?
|
|
129
|
+
das whoami
|
|
130
|
+
|
|
131
|
+
# Local information only, no server call
|
|
132
|
+
das whoami --offline
|
|
133
|
+
|
|
134
|
+
# Machine-readable (the credential itself is never printed, only a masked form)
|
|
135
|
+
das whoami --format json
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
```text
|
|
139
|
+
Current DAS credential
|
|
140
|
+
────────────────────────────────────────
|
|
141
|
+
API URL : https://your-das-instance/api
|
|
142
|
+
Auth type : api_key
|
|
143
|
+
Credential : das_2702…fe84
|
|
144
|
+
Source : DAS_API_KEY environment variable
|
|
145
|
+
Defined in : C:\Workspace\my-project\.env
|
|
146
|
+
⚠ DAS_API_KEY overrides the credential saved by 'das login' (eyJhbGci…zZt0).
|
|
147
|
+
|
|
148
|
+
Server
|
|
149
|
+
Status : ✓ credential accepted
|
|
150
|
+
User name : alovelace
|
|
151
|
+
Full name : Ada Lovelace
|
|
152
|
+
Email : ada@example.org
|
|
153
|
+
User ID : 42
|
|
154
|
+
Tenant : Default (id 1)
|
|
155
|
+
DAS version: 12.3.0.0
|
|
156
|
+
────────────────────────────────────────
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Credential precedence.** A `DAS_API_KEY` in your environment or `.env` file
|
|
160
|
+
always wins over whatever `das login` saved. That is the usual reason commands
|
|
161
|
+
run as someone else than expected, so `das whoami` (and `das config show`) name
|
|
162
|
+
the source, point at the `.env` file that defines it, and warn when the
|
|
163
|
+
environment variable shadows a different stored credential.
|
|
164
|
+
|
|
83
165
|
### Search and Read
|
|
84
166
|
|
|
85
167
|
```bash
|
|
@@ -229,6 +311,7 @@ Two common patterns are supported.
|
|
|
229
311
|
|
|
230
312
|
- [Pattern A: login once with CLI, then use managers in Python](#pattern-a-login-once-with-cli-then-use-managers-in-python)
|
|
231
313
|
- [Pattern B: authenticate in Python with `Das`](#pattern-b-authenticate-in-python-with-das)
|
|
314
|
+
- [Checking the active credential](#checking-the-active-credential)
|
|
232
315
|
- Managers — full API reference below:
|
|
233
316
|
- [`EntryManager`](#entrymanager)
|
|
234
317
|
- [`SearchManager`](#searchmanager)
|
|
@@ -258,15 +341,40 @@ from das.managers.entries_manager import EntryManager
|
|
|
258
341
|
client = Das("https://your-das-instance/api")
|
|
259
342
|
client.authenticate("your_user", "your_password")
|
|
260
343
|
|
|
261
|
-
# Or API key
|
|
344
|
+
# Or API key — validate=True returns the user the key belongs to (None if refused)
|
|
262
345
|
# client = Das("https://your-das-instance/api")
|
|
263
|
-
# client.
|
|
346
|
+
# user = client.set_api_key("your-api-key", validate=True)
|
|
347
|
+
# if user is None:
|
|
348
|
+
# raise SystemExit("The server did not accept this API key.")
|
|
264
349
|
|
|
265
350
|
entry_manager = EntryManager()
|
|
266
351
|
entry = entry_manager.get(code="zb.b.1ub")
|
|
267
352
|
print(entry.get("Name"))
|
|
268
353
|
```
|
|
269
354
|
|
|
355
|
+
### Checking the active credential
|
|
356
|
+
|
|
357
|
+
`Das.whoami()` is the programmatic form of `das whoami`: it reports which
|
|
358
|
+
credential will be sent, where it came from, and who the server says that is.
|
|
359
|
+
|
|
360
|
+
```python
|
|
361
|
+
from das.app import Das
|
|
362
|
+
|
|
363
|
+
info = Das("https://your-das-instance/api").whoami()
|
|
364
|
+
|
|
365
|
+
print(info["status"]) # 'valid' | 'invalid' | 'no-credential' | 'unreachable'
|
|
366
|
+
print(info["source"]) # 'env' (DAS_API_KEY) | 'keyring' | 'file'
|
|
367
|
+
print(info["masked"]) # 'das_2702…fe84' — never the raw credential
|
|
368
|
+
if info["status"] == "valid":
|
|
369
|
+
print(info["user"]["userName"], info["user"]["emailAddress"])
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
`whoami(verify=False)` skips the server call and reports local information only
|
|
373
|
+
(`status` is then `"unverified"`). Lower-level access is available through
|
|
374
|
+
`client.session` (`SessionService`): `get_current_login_information()`,
|
|
375
|
+
`get_current_user()` and `validate_api_key(key)` — the last one checks a key
|
|
376
|
+
without storing it.
|
|
377
|
+
|
|
270
378
|
## Manager Layer API Reference (Domain Layer)
|
|
271
379
|
|
|
272
380
|
Managers are the recommended Python API. They translate user-friendly field names, codes, and query syntax into the lower-level DAS API calls.
|
|
@@ -851,6 +959,10 @@ The tests are a good source of realistic payload examples, especially:
|
|
|
851
959
|
- raise verbosity with `das config log-level DEBUG` (optionally
|
|
852
960
|
`das config log-file <path>`) to see the full HTTP requests and error
|
|
853
961
|
details. See [Logging](#logging).
|
|
962
|
+
- Debug or inspect the CLI from source:
|
|
963
|
+
- run `python -m das.cli --help` from the repo root (after `pip install -e .`,
|
|
964
|
+
or with `PYTHONPATH` pointing at the clone) to confirm the package loads and
|
|
965
|
+
list available commands without using the `das` console script.
|
|
854
966
|
|
|
855
967
|
## Contributing
|
|
856
968
|
|
das_cli-1.7.3/das/app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
from das.authentication.auth import Auth
|
|
2
|
+
from das.services.attributes import AttributesService
|
|
3
|
+
from das.services.cache import CacheService
|
|
4
|
+
from das.services.entries import EntriesService
|
|
5
|
+
from das.services.entry_fields import EntryFieldsService
|
|
6
|
+
from das.services.hangfire import HangfireService
|
|
7
|
+
from das.services.search import SearchService
|
|
8
|
+
from das.services.das_elastic_search import DasElasticSearch
|
|
9
|
+
from das.services.session import SessionService, pick
|
|
10
|
+
from das.common.config import (
|
|
11
|
+
save_api_url, load_api_url, save_token, set_auth_type, describe_credential,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
# $env:PYTHONPATH="C:\Workspace\das-cli"
|
|
15
|
+
|
|
16
|
+
class Das:
|
|
17
|
+
def __init__(self, base_url: str = None):
|
|
18
|
+
if base_url is None or base_url == "":
|
|
19
|
+
base_url = load_api_url()
|
|
20
|
+
if base_url is None or base_url == "":
|
|
21
|
+
raise ValueError("No API URL configured. Pass base_url, set DAS_API_URL in your .env, or run 'das login'.")
|
|
22
|
+
save_api_url(base_url)
|
|
23
|
+
self.base_url = base_url
|
|
24
|
+
self.attributes = AttributesService(base_url)
|
|
25
|
+
self.cache = CacheService(base_url)
|
|
26
|
+
self.entries = EntriesService(base_url)
|
|
27
|
+
self.hangfire = HangfireService(base_url)
|
|
28
|
+
self.entry_fields = EntryFieldsService(base_url)
|
|
29
|
+
self.search = SearchService(base_url)
|
|
30
|
+
self.elastic_search = DasElasticSearch(base_url)
|
|
31
|
+
self.session = SessionService(base_url)
|
|
32
|
+
|
|
33
|
+
def set_api_key(self, api_key: str, validate: bool = False):
|
|
34
|
+
"""
|
|
35
|
+
Authenticate using a DAS API key.
|
|
36
|
+
|
|
37
|
+
Generate an API key in the DAS web interface under My Settings → API Keys.
|
|
38
|
+
The key is stored securely and sent as an X-Api-Key header on every request.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
api_key (str): The API key to authenticate with.
|
|
42
|
+
validate (bool): When True, ask the server whether the key is
|
|
43
|
+
accepted and return the user it belongs to.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
dict | None: The user the key belongs to when ``validate`` is True
|
|
47
|
+
(``None`` if the server does not accept the key); always
|
|
48
|
+
``None`` when ``validate`` is False.
|
|
49
|
+
|
|
50
|
+
Raises:
|
|
51
|
+
DasApiError: With ``validate=True``, if the server could not be
|
|
52
|
+
reached. The key is stored either way.
|
|
53
|
+
"""
|
|
54
|
+
save_token(api_key)
|
|
55
|
+
set_auth_type("api_key")
|
|
56
|
+
if validate:
|
|
57
|
+
return self.session.validate_api_key(api_key)
|
|
58
|
+
return None
|
|
59
|
+
|
|
60
|
+
def whoami(self, verify: bool = True) -> dict:
|
|
61
|
+
"""
|
|
62
|
+
Report which credential the client uses, and who the server says that is.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
verify (bool): When True (default) contact the server to resolve the
|
|
66
|
+
credential to a user; when False report only local information.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
dict: Everything :func:`describe_credential` returns, plus
|
|
70
|
+
``status`` — ``"valid"``, ``"invalid"`` (server rejected the
|
|
71
|
+
credential), ``"no-credential"`` (nothing stored),
|
|
72
|
+
``"unreachable"`` (with ``error``) or ``"unverified"`` (when
|
|
73
|
+
``verify`` is False) — and, when valid, ``user``, ``tenant`` and
|
|
74
|
+
``application``.
|
|
75
|
+
"""
|
|
76
|
+
info = describe_credential()
|
|
77
|
+
info["api_url"] = self.base_url
|
|
78
|
+
|
|
79
|
+
if not verify:
|
|
80
|
+
info["status"] = "unverified"
|
|
81
|
+
return info
|
|
82
|
+
|
|
83
|
+
if not info.get("token"):
|
|
84
|
+
info["status"] = "no-credential"
|
|
85
|
+
return info
|
|
86
|
+
|
|
87
|
+
try:
|
|
88
|
+
login_info = self.session.get_current_login_information()
|
|
89
|
+
except Exception as exc:
|
|
90
|
+
info["status"] = "unreachable"
|
|
91
|
+
info["error"] = str(exc)
|
|
92
|
+
return info
|
|
93
|
+
|
|
94
|
+
user = pick(login_info, "user")
|
|
95
|
+
info["status"] = "valid" if user else "invalid"
|
|
96
|
+
info["user"] = user
|
|
97
|
+
info["tenant"] = pick(login_info, "tenant")
|
|
98
|
+
info["application"] = pick(login_info, "application")
|
|
99
|
+
return info
|
|
100
|
+
|
|
101
|
+
def authenticate(self, username: str, password: str) -> str:
|
|
102
|
+
"""
|
|
103
|
+
Authenticate with the DAS API and return the token.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
username (str): The username to authenticate with
|
|
107
|
+
password (str): The password to authenticate with
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
str: The authentication token or None if authentication fails
|
|
111
|
+
"""
|
|
112
|
+
auth = Auth(self.base_url, username, password)
|
|
113
|
+
return auth.token
|