lithora 0.1.0__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.
- lithora-0.1.0/LICENSE +21 -0
- lithora-0.1.0/MANIFEST.in +9 -0
- lithora-0.1.0/PKG-INFO +130 -0
- lithora-0.1.0/README.md +102 -0
- lithora-0.1.0/lithora/__init__.py +32 -0
- lithora-0.1.0/lithora/client.py +237 -0
- lithora-0.1.0/lithora/errors.py +79 -0
- lithora-0.1.0/lithora/resources.py +605 -0
- lithora-0.1.0/lithora.egg-info/PKG-INFO +130 -0
- lithora-0.1.0/lithora.egg-info/SOURCES.txt +13 -0
- lithora-0.1.0/lithora.egg-info/dependency_links.txt +1 -0
- lithora-0.1.0/lithora.egg-info/requires.txt +1 -0
- lithora-0.1.0/lithora.egg-info/top_level.txt +1 -0
- lithora-0.1.0/pyproject.toml +38 -0
- lithora-0.1.0/setup.cfg +4 -0
lithora-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lithora
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
lithora-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lithora
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python SDK for the Lithora REST API (teams, projects, tasks, work graph, AI workspace).
|
|
5
|
+
Author-email: Lithora <support@lithora.io>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://lithora.io
|
|
8
|
+
Project-URL: Documentation, https://github.com/AADI0009/SaaS-App/tree/main/sdk/python
|
|
9
|
+
Project-URL: Repository, https://github.com/AADI0009/SaaS-App
|
|
10
|
+
Project-URL: Issues, https://github.com/AADI0009/SaaS-App/issues
|
|
11
|
+
Project-URL: API, https://api.lithora.io
|
|
12
|
+
Keywords: lithora,sdk,api,project-management,work-graph
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: requests>=2.28
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# lithora
|
|
30
|
+
|
|
31
|
+
Official Python SDK for the [Lithora](https://lithora.io) REST API — teams,
|
|
32
|
+
projects, tasks, the unified work graph, and the confirmation-gated AI
|
|
33
|
+
workspace.
|
|
34
|
+
|
|
35
|
+
- Base URL: `https://api.lithora.io` (local dev: `http://localhost:8000`)
|
|
36
|
+
- Auth: JWT bearer (HS256). Get a token with `client.login(...)`.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install lithora
|
|
42
|
+
# or, from this source tree:
|
|
43
|
+
pip install -e /path/to/sdk/python
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Requires Python 3.9+ and `requests>=2.28`.
|
|
47
|
+
|
|
48
|
+
## Quickstart
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from lithora import Lithora, AuthChallengeError
|
|
52
|
+
|
|
53
|
+
client = Lithora(base_url="https://api.lithora.io") # or http://localhost:8000
|
|
54
|
+
|
|
55
|
+
# 1. Authenticate. Token is stored on the client and reused automatically.
|
|
56
|
+
try:
|
|
57
|
+
user = client.login("you@example.com", "your-strong-password")
|
|
58
|
+
except AuthChallengeError as e:
|
|
59
|
+
# Login asked for a second factor instead of returning a token.
|
|
60
|
+
print("2FA:", e.requires_2fa, " OTP:", e.requires_login_otp)
|
|
61
|
+
raise
|
|
62
|
+
|
|
63
|
+
# 2. Create a project (needs a team_id; list teams with client.teams.list()).
|
|
64
|
+
project = client.projects.create("Apollo", team_id="team_123",
|
|
65
|
+
description="Q3 launch", status="active")
|
|
66
|
+
|
|
67
|
+
# 3. Create a task in that project.
|
|
68
|
+
task = client.tasks.create("Write the spec", project_id=project["id"],
|
|
69
|
+
priority="high", status="todo")
|
|
70
|
+
client.tasks.set_status(task["id"], "in_progress")
|
|
71
|
+
|
|
72
|
+
# 4. Use the AI workspace (confirmation-gated agent over the work graph).
|
|
73
|
+
session = client.ai.create_session(title="Planning")
|
|
74
|
+
reply = client.ai.chat(session["session_id"], "Break the spec into subtasks")
|
|
75
|
+
if reply.get("requires_confirmation"):
|
|
76
|
+
action = reply["action_plan"]["actions"][0] # shape per server
|
|
77
|
+
client.ai.confirm_action(session["session_id"], action["id"], confirmed=True)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Already have a token? Skip `login`: `Lithora(token="eyJ...")`.
|
|
81
|
+
|
|
82
|
+
## API surface
|
|
83
|
+
|
|
84
|
+
All methods map 1:1 to verified endpoints. Namespaces on the client:
|
|
85
|
+
|
|
86
|
+
| Namespace | Methods |
|
|
87
|
+
|----------------------|---------|
|
|
88
|
+
| `client` (auth) | `login`, `register`, `set_token` |
|
|
89
|
+
| `client.auth` | `me` |
|
|
90
|
+
| `client.teams` | `list`, `get`, `create`, `members` |
|
|
91
|
+
| `client.projects` | `list`, `get`, `create`, `update`, `patch`, `delete` |
|
|
92
|
+
| `client.tasks` | `list(project_id)`, `my`, `get`, `create`, `update`, `set_status`, `delete`, `bulk`, `link_github`, `push_to_github` |
|
|
93
|
+
| `client.work_items` | `list`, `get`, `create`, `update`, `delete`, `create_relation`, `relations`, `children`, `parents`, `delete_relation`, `cycle_time` |
|
|
94
|
+
| `client.ai` | `create_session`, `chat`, `link_item`, `search`, `confirm_action` |
|
|
95
|
+
|
|
96
|
+
## Errors
|
|
97
|
+
|
|
98
|
+
Every non-2xx response raises a typed exception (all subclass `LithoraError`):
|
|
99
|
+
|
|
100
|
+
| Exception | When |
|
|
101
|
+
|----------------------|------|
|
|
102
|
+
| `AuthError` | 401 — missing/invalid/expired token |
|
|
103
|
+
| `ServerError` | 5xx — failure on Lithora's side |
|
|
104
|
+
| `ApiError` | any other non-2xx; has `.status` and `.detail` |
|
|
105
|
+
| `AuthChallengeError` | login needs a 2FA/OTP code (no token issued); has `.requires_2fa`, `.requires_login_otp` |
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from lithora import ApiError
|
|
109
|
+
|
|
110
|
+
try:
|
|
111
|
+
client.projects.get("does-not-exist")
|
|
112
|
+
except ApiError as e:
|
|
113
|
+
print(e.status, e.detail) # e.g. 404 "Project not found"
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Known gaps
|
|
117
|
+
|
|
118
|
+
- **No API-key / PAT auth.** Lithora only supports JWT bearer tokens today,
|
|
119
|
+
so this SDK authenticates via `login(email, password)`. There is no
|
|
120
|
+
long-lived key mechanism to wrap.
|
|
121
|
+
- Some endpoints (`tasks.link_github`, `tasks.push_to_github`,
|
|
122
|
+
`work_items.create`) accept fields the public contract abbreviates; those
|
|
123
|
+
methods pass extra keyword arguments straight through to the request body.
|
|
124
|
+
- **`work_items.cycle_time`** targets a newly added analytics endpoint; its
|
|
125
|
+
response shape is provisional and fields may change. It requires a
|
|
126
|
+
`team_id` or `project_id` you have access to.
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT — see [LICENSE](LICENSE).
|
lithora-0.1.0/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# lithora
|
|
2
|
+
|
|
3
|
+
Official Python SDK for the [Lithora](https://lithora.io) REST API — teams,
|
|
4
|
+
projects, tasks, the unified work graph, and the confirmation-gated AI
|
|
5
|
+
workspace.
|
|
6
|
+
|
|
7
|
+
- Base URL: `https://api.lithora.io` (local dev: `http://localhost:8000`)
|
|
8
|
+
- Auth: JWT bearer (HS256). Get a token with `client.login(...)`.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install lithora
|
|
14
|
+
# or, from this source tree:
|
|
15
|
+
pip install -e /path/to/sdk/python
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Requires Python 3.9+ and `requests>=2.28`.
|
|
19
|
+
|
|
20
|
+
## Quickstart
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from lithora import Lithora, AuthChallengeError
|
|
24
|
+
|
|
25
|
+
client = Lithora(base_url="https://api.lithora.io") # or http://localhost:8000
|
|
26
|
+
|
|
27
|
+
# 1. Authenticate. Token is stored on the client and reused automatically.
|
|
28
|
+
try:
|
|
29
|
+
user = client.login("you@example.com", "your-strong-password")
|
|
30
|
+
except AuthChallengeError as e:
|
|
31
|
+
# Login asked for a second factor instead of returning a token.
|
|
32
|
+
print("2FA:", e.requires_2fa, " OTP:", e.requires_login_otp)
|
|
33
|
+
raise
|
|
34
|
+
|
|
35
|
+
# 2. Create a project (needs a team_id; list teams with client.teams.list()).
|
|
36
|
+
project = client.projects.create("Apollo", team_id="team_123",
|
|
37
|
+
description="Q3 launch", status="active")
|
|
38
|
+
|
|
39
|
+
# 3. Create a task in that project.
|
|
40
|
+
task = client.tasks.create("Write the spec", project_id=project["id"],
|
|
41
|
+
priority="high", status="todo")
|
|
42
|
+
client.tasks.set_status(task["id"], "in_progress")
|
|
43
|
+
|
|
44
|
+
# 4. Use the AI workspace (confirmation-gated agent over the work graph).
|
|
45
|
+
session = client.ai.create_session(title="Planning")
|
|
46
|
+
reply = client.ai.chat(session["session_id"], "Break the spec into subtasks")
|
|
47
|
+
if reply.get("requires_confirmation"):
|
|
48
|
+
action = reply["action_plan"]["actions"][0] # shape per server
|
|
49
|
+
client.ai.confirm_action(session["session_id"], action["id"], confirmed=True)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Already have a token? Skip `login`: `Lithora(token="eyJ...")`.
|
|
53
|
+
|
|
54
|
+
## API surface
|
|
55
|
+
|
|
56
|
+
All methods map 1:1 to verified endpoints. Namespaces on the client:
|
|
57
|
+
|
|
58
|
+
| Namespace | Methods |
|
|
59
|
+
|----------------------|---------|
|
|
60
|
+
| `client` (auth) | `login`, `register`, `set_token` |
|
|
61
|
+
| `client.auth` | `me` |
|
|
62
|
+
| `client.teams` | `list`, `get`, `create`, `members` |
|
|
63
|
+
| `client.projects` | `list`, `get`, `create`, `update`, `patch`, `delete` |
|
|
64
|
+
| `client.tasks` | `list(project_id)`, `my`, `get`, `create`, `update`, `set_status`, `delete`, `bulk`, `link_github`, `push_to_github` |
|
|
65
|
+
| `client.work_items` | `list`, `get`, `create`, `update`, `delete`, `create_relation`, `relations`, `children`, `parents`, `delete_relation`, `cycle_time` |
|
|
66
|
+
| `client.ai` | `create_session`, `chat`, `link_item`, `search`, `confirm_action` |
|
|
67
|
+
|
|
68
|
+
## Errors
|
|
69
|
+
|
|
70
|
+
Every non-2xx response raises a typed exception (all subclass `LithoraError`):
|
|
71
|
+
|
|
72
|
+
| Exception | When |
|
|
73
|
+
|----------------------|------|
|
|
74
|
+
| `AuthError` | 401 — missing/invalid/expired token |
|
|
75
|
+
| `ServerError` | 5xx — failure on Lithora's side |
|
|
76
|
+
| `ApiError` | any other non-2xx; has `.status` and `.detail` |
|
|
77
|
+
| `AuthChallengeError` | login needs a 2FA/OTP code (no token issued); has `.requires_2fa`, `.requires_login_otp` |
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from lithora import ApiError
|
|
81
|
+
|
|
82
|
+
try:
|
|
83
|
+
client.projects.get("does-not-exist")
|
|
84
|
+
except ApiError as e:
|
|
85
|
+
print(e.status, e.detail) # e.g. 404 "Project not found"
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Known gaps
|
|
89
|
+
|
|
90
|
+
- **No API-key / PAT auth.** Lithora only supports JWT bearer tokens today,
|
|
91
|
+
so this SDK authenticates via `login(email, password)`. There is no
|
|
92
|
+
long-lived key mechanism to wrap.
|
|
93
|
+
- Some endpoints (`tasks.link_github`, `tasks.push_to_github`,
|
|
94
|
+
`work_items.create`) accept fields the public contract abbreviates; those
|
|
95
|
+
methods pass extra keyword arguments straight through to the request body.
|
|
96
|
+
- **`work_items.cycle_time`** targets a newly added analytics endpoint; its
|
|
97
|
+
response shape is provisional and fields may change. It requires a
|
|
98
|
+
`team_id` or `project_id` you have access to.
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Lithora -- official Python SDK for the Lithora REST API.
|
|
2
|
+
|
|
3
|
+
Quickstart:
|
|
4
|
+
>>> from lithora import Lithora
|
|
5
|
+
>>> client = Lithora(base_url="https://api.lithora.io")
|
|
6
|
+
>>> client.login("you@example.com", "your-strong-password")
|
|
7
|
+
>>> project = client.projects.create("Apollo", team_id="team_123")
|
|
8
|
+
>>> client.tasks.create("Write spec", project_id=project["id"])
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from .client import Lithora
|
|
14
|
+
from .errors import (
|
|
15
|
+
ApiError,
|
|
16
|
+
AuthChallengeError,
|
|
17
|
+
AuthError,
|
|
18
|
+
LithoraError,
|
|
19
|
+
ServerError,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__version__ = "0.1.0"
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"Lithora",
|
|
26
|
+
"LithoraError",
|
|
27
|
+
"ApiError",
|
|
28
|
+
"AuthError",
|
|
29
|
+
"AuthChallengeError",
|
|
30
|
+
"ServerError",
|
|
31
|
+
"__version__",
|
|
32
|
+
]
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"""HTTP client for the Lithora REST API.
|
|
2
|
+
|
|
3
|
+
The :class:`Lithora` client owns a single :class:`requests.Session`, attaches
|
|
4
|
+
the ``Authorization: Bearer <token>`` header to authenticated calls, maps
|
|
5
|
+
non-2xx responses to typed exceptions, and exposes the API surface through
|
|
6
|
+
resource namespaces (``client.projects``, ``client.tasks``, ...).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Any, Dict, Optional
|
|
12
|
+
|
|
13
|
+
import requests
|
|
14
|
+
|
|
15
|
+
from .errors import ApiError, AuthChallengeError, AuthError, ServerError
|
|
16
|
+
from .resources import (
|
|
17
|
+
AIResource,
|
|
18
|
+
AuthResource,
|
|
19
|
+
AutomationsResource,
|
|
20
|
+
GitHubResource,
|
|
21
|
+
ProjectsResource,
|
|
22
|
+
SearchResource,
|
|
23
|
+
TasksResource,
|
|
24
|
+
TeamsResource,
|
|
25
|
+
TokensResource,
|
|
26
|
+
WorkItemsResource,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
DEFAULT_BASE_URL = "https://api.lithora.io"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Lithora:
|
|
33
|
+
"""Client for the Lithora REST API.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
base_url: API root. Defaults to ``https://api.lithora.io``.
|
|
37
|
+
For local development use ``http://localhost:8000``.
|
|
38
|
+
(The SDK appends the ``/api`` prefix itself, so pass the bare host.)
|
|
39
|
+
token: An existing JWT bearer token. If omitted, call :meth:`login`.
|
|
40
|
+
timeout: Per-request timeout in seconds.
|
|
41
|
+
|
|
42
|
+
Example:
|
|
43
|
+
>>> client = Lithora(base_url="https://api.lithora.io")
|
|
44
|
+
>>> client.login("you@example.com", "hunter2-strong-pw")
|
|
45
|
+
>>> client.projects.list()
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def __init__(
|
|
49
|
+
self,
|
|
50
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
51
|
+
token: Optional[str] = None,
|
|
52
|
+
timeout: int = 30,
|
|
53
|
+
) -> None:
|
|
54
|
+
# Normalize: strip a trailing slash and a trailing "/api" so callers can
|
|
55
|
+
# pass either "https://api.lithora.io" or "https://api.lithora.io/".
|
|
56
|
+
self.base_url = base_url.rstrip("/")
|
|
57
|
+
if self.base_url.endswith("/api"):
|
|
58
|
+
self.base_url = self.base_url[: -len("/api")]
|
|
59
|
+
|
|
60
|
+
self.timeout = timeout
|
|
61
|
+
self.token = token
|
|
62
|
+
|
|
63
|
+
self._session = requests.Session()
|
|
64
|
+
from . import __version__ as _v
|
|
65
|
+
self._session.headers.update({
|
|
66
|
+
"Accept": "application/json",
|
|
67
|
+
"User-Agent": "lithora-python/{}".format(_v),
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
# Resource namespaces -- each one calls back into this client's _request.
|
|
71
|
+
self.auth = AuthResource(self)
|
|
72
|
+
self.tokens = TokensResource(self)
|
|
73
|
+
self.teams = TeamsResource(self)
|
|
74
|
+
self.projects = ProjectsResource(self)
|
|
75
|
+
self.tasks = TasksResource(self)
|
|
76
|
+
self.work_items = WorkItemsResource(self)
|
|
77
|
+
self.automations = AutomationsResource(self)
|
|
78
|
+
self.github = GitHubResource(self)
|
|
79
|
+
self.search = SearchResource(self)
|
|
80
|
+
self.ai = AIResource(self)
|
|
81
|
+
|
|
82
|
+
# ------------------------------------------------------------------ #
|
|
83
|
+
# Token management
|
|
84
|
+
# ------------------------------------------------------------------ #
|
|
85
|
+
def set_token(self, token: Optional[str]) -> None:
|
|
86
|
+
"""Set (or clear) the bearer token used for authenticated requests."""
|
|
87
|
+
self.token = token
|
|
88
|
+
|
|
89
|
+
# ------------------------------------------------------------------ #
|
|
90
|
+
# Auth helpers (kept on the client so token state lives in one place)
|
|
91
|
+
# ------------------------------------------------------------------ #
|
|
92
|
+
def login(self, email: str, password: str) -> Dict[str, Any]:
|
|
93
|
+
"""Authenticate and store the returned JWT on the client.
|
|
94
|
+
|
|
95
|
+
Calls ``POST /api/auth/login``. On success the token is saved on the
|
|
96
|
+
client (used automatically for subsequent calls) and the ``user``
|
|
97
|
+
object is returned.
|
|
98
|
+
|
|
99
|
+
Raises:
|
|
100
|
+
AuthChallengeError: if the response asks for a login OTP or 2FA
|
|
101
|
+
code instead of returning a token.
|
|
102
|
+
AuthError: if the credentials are rejected (401).
|
|
103
|
+
ApiError: for any other non-2xx response.
|
|
104
|
+
"""
|
|
105
|
+
data = self._request(
|
|
106
|
+
"POST",
|
|
107
|
+
"/auth/login",
|
|
108
|
+
json={"email": email, "password": password},
|
|
109
|
+
auth=False,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
# The login endpoint may withhold the token pending a second factor.
|
|
113
|
+
if data.get("requires_2fa") or data.get("requires_login_otp"):
|
|
114
|
+
raise AuthChallengeError(
|
|
115
|
+
requires_2fa=bool(data.get("requires_2fa")),
|
|
116
|
+
requires_login_otp=bool(data.get("requires_login_otp")),
|
|
117
|
+
payload=data,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
token = data.get("token")
|
|
121
|
+
if not token:
|
|
122
|
+
# Defensive: no token and no recognized challenge flag.
|
|
123
|
+
raise AuthChallengeError(payload=data)
|
|
124
|
+
|
|
125
|
+
self.set_token(token)
|
|
126
|
+
return data.get("user", {})
|
|
127
|
+
|
|
128
|
+
def register(
|
|
129
|
+
self,
|
|
130
|
+
email: str,
|
|
131
|
+
password: str,
|
|
132
|
+
name: str,
|
|
133
|
+
role: str = "member",
|
|
134
|
+
) -> Dict[str, Any]:
|
|
135
|
+
"""Create a new account via ``POST /api/auth/register``.
|
|
136
|
+
|
|
137
|
+
Args:
|
|
138
|
+
email: Account email.
|
|
139
|
+
password: Min 8 chars; must pass the server's strength check.
|
|
140
|
+
name: Display name.
|
|
141
|
+
role: One of ``"admin"``, ``"manager"``, ``"member"``.
|
|
142
|
+
|
|
143
|
+
Returns:
|
|
144
|
+
The decoded JSON response from the register endpoint.
|
|
145
|
+
"""
|
|
146
|
+
return self._request(
|
|
147
|
+
"POST",
|
|
148
|
+
"/auth/register",
|
|
149
|
+
json={
|
|
150
|
+
"email": email,
|
|
151
|
+
"password": password,
|
|
152
|
+
"name": name,
|
|
153
|
+
"role": role,
|
|
154
|
+
},
|
|
155
|
+
auth=False,
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
# ------------------------------------------------------------------ #
|
|
159
|
+
# Core request plumbing
|
|
160
|
+
# ------------------------------------------------------------------ #
|
|
161
|
+
def _request(
|
|
162
|
+
self,
|
|
163
|
+
method: str,
|
|
164
|
+
path: str,
|
|
165
|
+
*,
|
|
166
|
+
json: Optional[Any] = None,
|
|
167
|
+
params: Optional[Dict[str, Any]] = None,
|
|
168
|
+
auth: bool = True,
|
|
169
|
+
) -> Any:
|
|
170
|
+
"""Send a request and return parsed JSON, raising typed errors.
|
|
171
|
+
|
|
172
|
+
Args:
|
|
173
|
+
method: HTTP verb, e.g. ``"GET"``, ``"POST"``.
|
|
174
|
+
path: Path *relative to* the ``/api`` prefix, e.g. ``"/projects"``.
|
|
175
|
+
json: Optional JSON-serializable request body.
|
|
176
|
+
params: Optional query-string parameters (``None`` values dropped).
|
|
177
|
+
auth: When True, attach the ``Authorization: Bearer`` header.
|
|
178
|
+
|
|
179
|
+
Raises:
|
|
180
|
+
AuthError: on a 401 response.
|
|
181
|
+
ServerError: on a 5xx response.
|
|
182
|
+
ApiError: on any other non-2xx response.
|
|
183
|
+
"""
|
|
184
|
+
url = "{}/api{}".format(self.base_url, path)
|
|
185
|
+
|
|
186
|
+
headers: Dict[str, str] = {}
|
|
187
|
+
if auth:
|
|
188
|
+
if not self.token:
|
|
189
|
+
# Surface the missing-token case as an auth failure up front,
|
|
190
|
+
# instead of waiting for the server to 401.
|
|
191
|
+
raise AuthError(
|
|
192
|
+
401,
|
|
193
|
+
"No token set. Call client.login(...) or pass token=... to Lithora().",
|
|
194
|
+
)
|
|
195
|
+
headers["Authorization"] = "Bearer {}".format(self.token)
|
|
196
|
+
|
|
197
|
+
# Drop params whose value is None so callers can pass optional filters.
|
|
198
|
+
clean_params = (
|
|
199
|
+
{k: v for k, v in params.items() if v is not None} if params else None
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
response = self._session.request(
|
|
203
|
+
method,
|
|
204
|
+
url,
|
|
205
|
+
json=json,
|
|
206
|
+
params=clean_params,
|
|
207
|
+
headers=headers,
|
|
208
|
+
timeout=self.timeout,
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
return self._handle(response)
|
|
212
|
+
|
|
213
|
+
@staticmethod
|
|
214
|
+
def _handle(response: requests.Response) -> Any:
|
|
215
|
+
"""Map a ``requests.Response`` to parsed JSON or a typed exception."""
|
|
216
|
+
if response.ok: # 2xx
|
|
217
|
+
if response.status_code == 204 or not response.content:
|
|
218
|
+
return None
|
|
219
|
+
try:
|
|
220
|
+
return response.json()
|
|
221
|
+
except ValueError:
|
|
222
|
+
return response.text
|
|
223
|
+
|
|
224
|
+
# Error path: pull "detail" out of the FastAPI error envelope if we can.
|
|
225
|
+
detail: Any
|
|
226
|
+
try:
|
|
227
|
+
body = response.json()
|
|
228
|
+
detail = body.get("detail", body) if isinstance(body, dict) else body
|
|
229
|
+
except ValueError:
|
|
230
|
+
detail = response.text or response.reason
|
|
231
|
+
|
|
232
|
+
status = response.status_code
|
|
233
|
+
if status == 401:
|
|
234
|
+
raise AuthError(status, detail, response)
|
|
235
|
+
if status >= 500:
|
|
236
|
+
raise ServerError(status, detail, response)
|
|
237
|
+
raise ApiError(status, detail, response)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Typed exceptions raised by the Lithora SDK.
|
|
2
|
+
|
|
3
|
+
The hierarchy is intentionally shallow so callers can catch broadly
|
|
4
|
+
(``except LithoraError``) or narrowly (``except AuthError``).
|
|
5
|
+
|
|
6
|
+
LithoraError base for everything this SDK raises
|
|
7
|
+
+-- ApiError any non-2xx HTTP response (carries status + detail)
|
|
8
|
+
+-- AuthError 401 -- bad/missing/expired token
|
|
9
|
+
+-- ServerError 5xx -- something broke on Lithora's side
|
|
10
|
+
+-- AuthChallengeError login succeeded but needs OTP / 2FA before a token is issued
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from typing import Any, Optional
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class LithoraError(Exception):
|
|
19
|
+
"""Base class for all errors raised by the Lithora SDK."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ApiError(LithoraError):
|
|
23
|
+
"""Raised for any non-2xx HTTP response from the API.
|
|
24
|
+
|
|
25
|
+
FastAPI returns errors as ``{"detail": "..."}``; ``detail`` is parsed
|
|
26
|
+
out of the body when present, otherwise it falls back to the raw text.
|
|
27
|
+
|
|
28
|
+
Attributes:
|
|
29
|
+
status: The HTTP status code (e.g. 400, 404, 422).
|
|
30
|
+
detail: The ``detail`` field from the JSON body, or the raw response text.
|
|
31
|
+
response: The underlying ``requests.Response`` (may be ``None``).
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, status: int, detail: Any, response: Optional[Any] = None) -> None:
|
|
35
|
+
self.status = status
|
|
36
|
+
self.detail = detail
|
|
37
|
+
self.response = response
|
|
38
|
+
super().__init__("HTTP {}: {}".format(status, detail))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class AuthError(ApiError):
|
|
42
|
+
"""Raised on a 401 -- the token is missing, invalid, or expired."""
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class ServerError(ApiError):
|
|
46
|
+
"""Raised on a 5xx -- the failure originated on Lithora's side."""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class AuthChallengeError(LithoraError):
|
|
50
|
+
"""Login did not return a token because a second factor is required.
|
|
51
|
+
|
|
52
|
+
The Lithora ``POST /api/auth/login`` endpoint may respond with
|
|
53
|
+
``{"requires_login_otp": true}`` or ``{"requires_2fa": true}`` and *no*
|
|
54
|
+
token. Rather than silently pretend authentication succeeded, the SDK
|
|
55
|
+
raises this so the caller can prompt the user for the challenge.
|
|
56
|
+
|
|
57
|
+
Attributes:
|
|
58
|
+
requires_2fa: True when an authenticator/2FA code is required.
|
|
59
|
+
requires_login_otp: True when an emailed/SMS login OTP is required.
|
|
60
|
+
payload: The full JSON body returned by the login call.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
def __init__(
|
|
64
|
+
self,
|
|
65
|
+
*,
|
|
66
|
+
requires_2fa: bool = False,
|
|
67
|
+
requires_login_otp: bool = False,
|
|
68
|
+
payload: Optional[dict] = None,
|
|
69
|
+
) -> None:
|
|
70
|
+
self.requires_2fa = requires_2fa
|
|
71
|
+
self.requires_login_otp = requires_login_otp
|
|
72
|
+
self.payload = payload or {}
|
|
73
|
+
if requires_2fa:
|
|
74
|
+
reason = "two-factor authentication (2FA) code required"
|
|
75
|
+
elif requires_login_otp:
|
|
76
|
+
reason = "login one-time passcode (OTP) required"
|
|
77
|
+
else:
|
|
78
|
+
reason = "an additional authentication challenge is required"
|
|
79
|
+
super().__init__("Login incomplete: {}".format(reason))
|