smartlead-cli 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.
- smartlead_cli-0.1.0/LICENSE +21 -0
- smartlead_cli-0.1.0/PKG-INFO +163 -0
- smartlead_cli-0.1.0/README.md +120 -0
- smartlead_cli-0.1.0/pyproject.toml +50 -0
- smartlead_cli-0.1.0/smartlead_cli/__init__.py +5 -0
- smartlead_cli-0.1.0/smartlead_cli/args.py +57 -0
- smartlead_cli-0.1.0/smartlead_cli/client.py +179 -0
- smartlead_cli-0.1.0/smartlead_cli/commands/__init__.py +1 -0
- smartlead_cli-0.1.0/smartlead_cli/commands/campaigns.py +250 -0
- smartlead_cli-0.1.0/smartlead_cli/commands/config_cmd.py +80 -0
- smartlead_cli-0.1.0/smartlead_cli/commands/leads.py +32 -0
- smartlead_cli-0.1.0/smartlead_cli/commands/raw.py +67 -0
- smartlead_cli-0.1.0/smartlead_cli/commands/webhooks.py +55 -0
- smartlead_cli-0.1.0/smartlead_cli/config.py +149 -0
- smartlead_cli-0.1.0/smartlead_cli/main.py +66 -0
- smartlead_cli-0.1.0/smartlead_cli/output.py +179 -0
- smartlead_cli-0.1.0/smartlead_cli/serialize.py +39 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 jzakirov
|
|
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.
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: smartlead-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Smartlead platform CLI
|
|
5
|
+
Keywords: smartlead,cli,sales,outreach
|
|
6
|
+
Author: OpenAI Codex
|
|
7
|
+
License: MIT License
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2025 jzakirov
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software without restriction, including without limitation the rights
|
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
27
|
+
SOFTWARE.
|
|
28
|
+
Classifier: Programming Language :: Python :: 3
|
|
29
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
30
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Environment :: Console
|
|
33
|
+
Classifier: Topic :: Utilities
|
|
34
|
+
Requires-Dist: httpx>=0.27.0
|
|
35
|
+
Requires-Dist: rich>=13.7.0
|
|
36
|
+
Requires-Dist: tomlkit>=0.13.0
|
|
37
|
+
Requires-Dist: typer>=0.12.0
|
|
38
|
+
Requires-Python: >=3.11
|
|
39
|
+
Project-URL: Homepage, https://github.com/jzakirov/smartlead-cli
|
|
40
|
+
Project-URL: Source, https://github.com/jzakirov/smartlead-cli
|
|
41
|
+
Project-URL: Issues, https://github.com/jzakirov/smartlead-cli/issues
|
|
42
|
+
Description-Content-Type: text/markdown
|
|
43
|
+
|
|
44
|
+
# smartlead-cli
|
|
45
|
+
|
|
46
|
+
`smartlead-cli` provides a `smartlead` command for working with the Smartlead API from a terminal.
|
|
47
|
+
|
|
48
|
+
It follows the same project structure as `plane-cli` and `wildberries-cli`: Typer commands, a shared config layer, JSON-first output, and a `raw` command for full API coverage.
|
|
49
|
+
|
|
50
|
+
## Install
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install smartlead-cli
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Authentication
|
|
57
|
+
|
|
58
|
+
Smartlead uses an API key passed as `api_key` query parameter. Configure it once:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
smartlead config init
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
You can also override at runtime:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
SMARTLEAD_API_KEY=... smartlead campaigns list
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Config
|
|
71
|
+
|
|
72
|
+
Config file path:
|
|
73
|
+
|
|
74
|
+
- `~/.config/smartlead-cli/config.toml`
|
|
75
|
+
|
|
76
|
+
Supported keys (examples):
|
|
77
|
+
|
|
78
|
+
- `core.api_key`
|
|
79
|
+
- `core.base_url`
|
|
80
|
+
- `core.timeout_seconds`
|
|
81
|
+
- `core.retries`
|
|
82
|
+
- `defaults.limit`
|
|
83
|
+
|
|
84
|
+
Examples:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
smartlead config show
|
|
88
|
+
smartlead config set core.timeout_seconds 30
|
|
89
|
+
smartlead config set core.retries 3
|
|
90
|
+
smartlead config set defaults.limit 100
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Commands (v1)
|
|
94
|
+
|
|
95
|
+
- `config` - manage local config
|
|
96
|
+
- `campaigns` - list/get/create/delete/update status + analytics/statistics + campaign leads
|
|
97
|
+
- `leads` - a few global lead operations
|
|
98
|
+
- `webhooks` - campaign webhook helpers
|
|
99
|
+
- `raw` - direct API access for unsupported endpoints
|
|
100
|
+
|
|
101
|
+
## Examples
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
smartlead campaigns list
|
|
105
|
+
smartlead campaigns get 12345
|
|
106
|
+
smartlead campaigns create --name "New Campaign"
|
|
107
|
+
smartlead campaigns status 12345 --status PAUSED
|
|
108
|
+
smartlead campaigns statistics 12345 --limit 50 --offset 0
|
|
109
|
+
smartlead campaigns analytics top 12345
|
|
110
|
+
smartlead campaigns analytics by-date 12345 --start-date 2025-01-01 --end-date 2025-01-31
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Campaign leads:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
smartlead campaigns leads list 12345
|
|
117
|
+
smartlead campaigns leads add 12345 --body-file leads.json
|
|
118
|
+
smartlead campaigns leads update 12345 67890 --body-file lead-update.json
|
|
119
|
+
smartlead campaigns leads pause 12345 67890
|
|
120
|
+
smartlead campaigns leads resume 12345 67890 --delay-days 2
|
|
121
|
+
smartlead campaigns leads unsubscribe 12345 67890
|
|
122
|
+
smartlead campaigns leads delete 12345 67890 --yes
|
|
123
|
+
smartlead campaigns leads message-history 12345 67890
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Global lead helpers:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
smartlead leads get-by-email --email person@example.com
|
|
130
|
+
smartlead leads unsubscribe-all 67890
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Campaign webhooks:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
smartlead webhooks list 12345
|
|
137
|
+
smartlead webhooks upsert 12345 --body-file webhook.json
|
|
138
|
+
smartlead webhooks delete 12345 --webhook-id 555 --yes
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Raw endpoint access (fallback for full API surface):
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
smartlead raw request --method GET --path /campaigns
|
|
145
|
+
smartlead raw request --method POST --path /campaigns/12345/status --query status=PAUSED
|
|
146
|
+
smartlead raw request --method POST --path /campaigns/12345/leads --body-file leads.json
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Output
|
|
150
|
+
|
|
151
|
+
- Default output is JSON on stdout
|
|
152
|
+
- Errors are structured JSON on stderr
|
|
153
|
+
- Use `--pretty` for Rich tables on selected list commands
|
|
154
|
+
|
|
155
|
+
## Publishing to PyPI
|
|
156
|
+
|
|
157
|
+
The repo includes a GitHub Actions workflow (`.github/workflows/publish.yml`) for trusted publishing on tags matching `v*`.
|
|
158
|
+
|
|
159
|
+
## Notes
|
|
160
|
+
|
|
161
|
+
- Smartlead API docs (official): https://helpcenter.smartlead.ai/en/articles/125-full-api-documentation
|
|
162
|
+
- Smartlead API base URL defaulted in this CLI: `https://server.smartlead.ai/api/v1`
|
|
163
|
+
- Rate limiting is handled with simple retries/backoff for `429` and `5xx`
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# smartlead-cli
|
|
2
|
+
|
|
3
|
+
`smartlead-cli` provides a `smartlead` command for working with the Smartlead API from a terminal.
|
|
4
|
+
|
|
5
|
+
It follows the same project structure as `plane-cli` and `wildberries-cli`: Typer commands, a shared config layer, JSON-first output, and a `raw` command for full API coverage.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install smartlead-cli
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Authentication
|
|
14
|
+
|
|
15
|
+
Smartlead uses an API key passed as `api_key` query parameter. Configure it once:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
smartlead config init
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
You can also override at runtime:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
SMARTLEAD_API_KEY=... smartlead campaigns list
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Config
|
|
28
|
+
|
|
29
|
+
Config file path:
|
|
30
|
+
|
|
31
|
+
- `~/.config/smartlead-cli/config.toml`
|
|
32
|
+
|
|
33
|
+
Supported keys (examples):
|
|
34
|
+
|
|
35
|
+
- `core.api_key`
|
|
36
|
+
- `core.base_url`
|
|
37
|
+
- `core.timeout_seconds`
|
|
38
|
+
- `core.retries`
|
|
39
|
+
- `defaults.limit`
|
|
40
|
+
|
|
41
|
+
Examples:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
smartlead config show
|
|
45
|
+
smartlead config set core.timeout_seconds 30
|
|
46
|
+
smartlead config set core.retries 3
|
|
47
|
+
smartlead config set defaults.limit 100
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Commands (v1)
|
|
51
|
+
|
|
52
|
+
- `config` - manage local config
|
|
53
|
+
- `campaigns` - list/get/create/delete/update status + analytics/statistics + campaign leads
|
|
54
|
+
- `leads` - a few global lead operations
|
|
55
|
+
- `webhooks` - campaign webhook helpers
|
|
56
|
+
- `raw` - direct API access for unsupported endpoints
|
|
57
|
+
|
|
58
|
+
## Examples
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
smartlead campaigns list
|
|
62
|
+
smartlead campaigns get 12345
|
|
63
|
+
smartlead campaigns create --name "New Campaign"
|
|
64
|
+
smartlead campaigns status 12345 --status PAUSED
|
|
65
|
+
smartlead campaigns statistics 12345 --limit 50 --offset 0
|
|
66
|
+
smartlead campaigns analytics top 12345
|
|
67
|
+
smartlead campaigns analytics by-date 12345 --start-date 2025-01-01 --end-date 2025-01-31
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Campaign leads:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
smartlead campaigns leads list 12345
|
|
74
|
+
smartlead campaigns leads add 12345 --body-file leads.json
|
|
75
|
+
smartlead campaigns leads update 12345 67890 --body-file lead-update.json
|
|
76
|
+
smartlead campaigns leads pause 12345 67890
|
|
77
|
+
smartlead campaigns leads resume 12345 67890 --delay-days 2
|
|
78
|
+
smartlead campaigns leads unsubscribe 12345 67890
|
|
79
|
+
smartlead campaigns leads delete 12345 67890 --yes
|
|
80
|
+
smartlead campaigns leads message-history 12345 67890
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Global lead helpers:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
smartlead leads get-by-email --email person@example.com
|
|
87
|
+
smartlead leads unsubscribe-all 67890
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Campaign webhooks:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
smartlead webhooks list 12345
|
|
94
|
+
smartlead webhooks upsert 12345 --body-file webhook.json
|
|
95
|
+
smartlead webhooks delete 12345 --webhook-id 555 --yes
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Raw endpoint access (fallback for full API surface):
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
smartlead raw request --method GET --path /campaigns
|
|
102
|
+
smartlead raw request --method POST --path /campaigns/12345/status --query status=PAUSED
|
|
103
|
+
smartlead raw request --method POST --path /campaigns/12345/leads --body-file leads.json
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Output
|
|
107
|
+
|
|
108
|
+
- Default output is JSON on stdout
|
|
109
|
+
- Errors are structured JSON on stderr
|
|
110
|
+
- Use `--pretty` for Rich tables on selected list commands
|
|
111
|
+
|
|
112
|
+
## Publishing to PyPI
|
|
113
|
+
|
|
114
|
+
The repo includes a GitHub Actions workflow (`.github/workflows/publish.yml`) for trusted publishing on tags matching `v*`.
|
|
115
|
+
|
|
116
|
+
## Notes
|
|
117
|
+
|
|
118
|
+
- Smartlead API docs (official): https://helpcenter.smartlead.ai/en/articles/125-full-api-documentation
|
|
119
|
+
- Smartlead API base URL defaulted in this CLI: `https://server.smartlead.ai/api/v1`
|
|
120
|
+
- Rate limiting is handled with simple retries/backoff for `429` and `5xx`
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.10.4,<0.11.0"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "smartlead-cli"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Smartlead platform CLI"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
authors = [{ name = "OpenAI Codex" }]
|
|
13
|
+
dependencies = [
|
|
14
|
+
"httpx>=0.27.0",
|
|
15
|
+
"rich>=13.7.0",
|
|
16
|
+
"tomlkit>=0.13.0",
|
|
17
|
+
"typer>=0.12.0",
|
|
18
|
+
]
|
|
19
|
+
keywords = ["smartlead", "cli", "sales", "outreach"]
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"License :: OSI Approved :: MIT License",
|
|
25
|
+
"Environment :: Console",
|
|
26
|
+
"Topic :: Utilities",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/jzakirov/smartlead-cli"
|
|
31
|
+
Source = "https://github.com/jzakirov/smartlead-cli"
|
|
32
|
+
Issues = "https://github.com/jzakirov/smartlead-cli/issues"
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
smartlead = "smartlead_cli.main:app"
|
|
36
|
+
|
|
37
|
+
[tool.uv.build-backend]
|
|
38
|
+
module-name = "smartlead_cli"
|
|
39
|
+
module-root = ""
|
|
40
|
+
|
|
41
|
+
[dependency-groups]
|
|
42
|
+
dev = ["ruff>=0.9.0"]
|
|
43
|
+
|
|
44
|
+
[tool.ruff]
|
|
45
|
+
target-version = "py311"
|
|
46
|
+
line-length = 100
|
|
47
|
+
|
|
48
|
+
[tool.ruff.format]
|
|
49
|
+
quote-style = "double"
|
|
50
|
+
indent-style = "space"
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""CLI argument parsing helpers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any, Iterable
|
|
8
|
+
|
|
9
|
+
from smartlead_cli.output import read_text_arg
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def load_json_input(body_json: str | None = None, body_file: str | None = None) -> Any:
|
|
13
|
+
if body_json and body_file:
|
|
14
|
+
raise ValueError("Use only one of --body-json or --body-file")
|
|
15
|
+
if body_json is None and body_file is None:
|
|
16
|
+
raise ValueError("Body JSON is required")
|
|
17
|
+
|
|
18
|
+
if body_json is not None:
|
|
19
|
+
text = read_text_arg(body_json)
|
|
20
|
+
elif body_file == "-":
|
|
21
|
+
text = read_text_arg("-")
|
|
22
|
+
else:
|
|
23
|
+
text = Path(str(body_file)).read_text()
|
|
24
|
+
|
|
25
|
+
return json.loads(text)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def maybe_load_json_input(body_json: str | None = None, body_file: str | None = None) -> Any | None:
|
|
29
|
+
if body_json is None and body_file is None:
|
|
30
|
+
return None
|
|
31
|
+
return load_json_input(body_json=body_json, body_file=body_file)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def parse_kv_pairs(items: Iterable[str]) -> dict[str, str]:
|
|
35
|
+
out: dict[str, str] = {}
|
|
36
|
+
for item in items:
|
|
37
|
+
key, value = _split_kv(item)
|
|
38
|
+
out[key] = value
|
|
39
|
+
return out
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def parse_json_kv_pairs(items: Iterable[str]) -> dict[str, Any]:
|
|
43
|
+
out: dict[str, Any] = {}
|
|
44
|
+
for item in items:
|
|
45
|
+
key, value = _split_kv(item)
|
|
46
|
+
out[key] = json.loads(value)
|
|
47
|
+
return out
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _split_kv(item: str) -> tuple[str, str]:
|
|
51
|
+
if "=" not in item:
|
|
52
|
+
raise ValueError(f"Expected KEY=VALUE, got '{item}'")
|
|
53
|
+
key, value = item.split("=", 1)
|
|
54
|
+
key = key.strip()
|
|
55
|
+
if not key:
|
|
56
|
+
raise ValueError(f"Expected KEY=VALUE, got '{item}'")
|
|
57
|
+
return key, value
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""HTTP client wrapper for Smartlead API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import TYPE_CHECKING, Any
|
|
8
|
+
from urllib.parse import urljoin
|
|
9
|
+
|
|
10
|
+
import httpx
|
|
11
|
+
import typer
|
|
12
|
+
|
|
13
|
+
from smartlead_cli.output import print_error
|
|
14
|
+
from smartlead_cli.serialize import to_data
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from smartlead_cli.config import Config
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
RETRYABLE_STATUS_CODES = {429, 500, 502, 503, 504}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class ResponseEnvelope:
|
|
25
|
+
status_code: int
|
|
26
|
+
headers: dict[str, str]
|
|
27
|
+
data: Any
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class SmartleadClient:
|
|
32
|
+
cfg: "Config"
|
|
33
|
+
|
|
34
|
+
def request(
|
|
35
|
+
self,
|
|
36
|
+
method: str,
|
|
37
|
+
path: str,
|
|
38
|
+
*,
|
|
39
|
+
params: dict[str, Any] | None = None,
|
|
40
|
+
json_body: Any | None = None,
|
|
41
|
+
headers: dict[str, str] | None = None,
|
|
42
|
+
require_auth: bool = True,
|
|
43
|
+
) -> ResponseEnvelope:
|
|
44
|
+
if require_auth and not self.cfg.api_key:
|
|
45
|
+
print_error(
|
|
46
|
+
"auth_error",
|
|
47
|
+
"No Smartlead API key configured. Set SMARTLEAD_API_KEY, use --api-key, or run `smartlead config init`.",
|
|
48
|
+
)
|
|
49
|
+
raise typer.Exit(1)
|
|
50
|
+
|
|
51
|
+
merged_params: dict[str, Any] = {}
|
|
52
|
+
if params:
|
|
53
|
+
merged_params.update({k: v for k, v in params.items() if v is not None})
|
|
54
|
+
if require_auth and self.cfg.api_key:
|
|
55
|
+
merged_params.setdefault("api_key", self.cfg.api_key)
|
|
56
|
+
|
|
57
|
+
url = _resolve_url(self.cfg.base_url, path)
|
|
58
|
+
max_attempts = max(1, int(self.cfg.retries))
|
|
59
|
+
|
|
60
|
+
for attempt in range(max_attempts):
|
|
61
|
+
try:
|
|
62
|
+
with httpx.Client(timeout=self.cfg.timeout_seconds) as client:
|
|
63
|
+
resp = client.request(method.upper(), url, params=merged_params, json=json_body, headers=headers)
|
|
64
|
+
except httpx.TimeoutException as exc:
|
|
65
|
+
if attempt < max_attempts - 1:
|
|
66
|
+
time.sleep(min(2**attempt, 5))
|
|
67
|
+
continue
|
|
68
|
+
print_error("timeout", f"Request timed out after {self.cfg.timeout_seconds}s", detail=str(exc))
|
|
69
|
+
raise typer.Exit(1)
|
|
70
|
+
except httpx.HTTPError as exc:
|
|
71
|
+
print_error("network_error", f"Network error: {exc}")
|
|
72
|
+
raise typer.Exit(1)
|
|
73
|
+
|
|
74
|
+
if resp.status_code in RETRYABLE_STATUS_CODES and attempt < max_attempts - 1:
|
|
75
|
+
delay = _retry_after_seconds(resp) or min(2**attempt, 5)
|
|
76
|
+
time.sleep(delay)
|
|
77
|
+
continue
|
|
78
|
+
|
|
79
|
+
if resp.status_code >= 400:
|
|
80
|
+
_handle_error_response(resp)
|
|
81
|
+
raise typer.Exit(1)
|
|
82
|
+
|
|
83
|
+
return ResponseEnvelope(status_code=resp.status_code, headers=dict(resp.headers), data=_parse_response(resp))
|
|
84
|
+
|
|
85
|
+
print_error("cli_error", "Unexpected request failure")
|
|
86
|
+
raise typer.Exit(1)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def get_client(cfg: "Config") -> SmartleadClient:
|
|
90
|
+
return SmartleadClient(cfg)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def api_request(
|
|
94
|
+
cfg: "Config",
|
|
95
|
+
method: str,
|
|
96
|
+
path: str,
|
|
97
|
+
*,
|
|
98
|
+
params: dict[str, Any] | None = None,
|
|
99
|
+
json_body: Any | None = None,
|
|
100
|
+
headers: dict[str, str] | None = None,
|
|
101
|
+
require_auth: bool = True,
|
|
102
|
+
include_meta: bool = False,
|
|
103
|
+
) -> Any:
|
|
104
|
+
result = get_client(cfg).request(
|
|
105
|
+
method,
|
|
106
|
+
path,
|
|
107
|
+
params=params,
|
|
108
|
+
json_body=json_body,
|
|
109
|
+
headers=headers,
|
|
110
|
+
require_auth=require_auth,
|
|
111
|
+
)
|
|
112
|
+
if include_meta:
|
|
113
|
+
return {
|
|
114
|
+
"status_code": result.status_code,
|
|
115
|
+
"headers": result.headers,
|
|
116
|
+
"data": to_data(result.data),
|
|
117
|
+
}
|
|
118
|
+
return result.data
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _resolve_url(base_url: str, path: str) -> str:
|
|
122
|
+
if path.startswith("http://") or path.startswith("https://"):
|
|
123
|
+
return path
|
|
124
|
+
base = base_url.rstrip("/") + "/"
|
|
125
|
+
rel = path.lstrip("/")
|
|
126
|
+
if rel.startswith("api/v1/"):
|
|
127
|
+
rel = rel[len("api/v1/") :]
|
|
128
|
+
return urljoin(base, rel)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _retry_after_seconds(resp: httpx.Response) -> int | None:
|
|
132
|
+
value = resp.headers.get("Retry-After")
|
|
133
|
+
if not value:
|
|
134
|
+
return None
|
|
135
|
+
try:
|
|
136
|
+
return max(1, int(value))
|
|
137
|
+
except ValueError:
|
|
138
|
+
return None
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _parse_response(resp: httpx.Response) -> Any:
|
|
142
|
+
content_type = (resp.headers.get("content-type") or "").lower()
|
|
143
|
+
if "application/json" in content_type:
|
|
144
|
+
try:
|
|
145
|
+
return resp.json()
|
|
146
|
+
except Exception:
|
|
147
|
+
return {"text": resp.text}
|
|
148
|
+
if not resp.content:
|
|
149
|
+
return {"ok": True, "status_code": resp.status_code}
|
|
150
|
+
try:
|
|
151
|
+
text = resp.text
|
|
152
|
+
return {"text": text}
|
|
153
|
+
except UnicodeDecodeError:
|
|
154
|
+
return resp.content
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _handle_error_response(resp: httpx.Response) -> None:
|
|
158
|
+
detail: Any = None
|
|
159
|
+
message = f"Smartlead API request failed ({resp.status_code})"
|
|
160
|
+
try:
|
|
161
|
+
detail = resp.json()
|
|
162
|
+
if isinstance(detail, dict):
|
|
163
|
+
message = str(detail.get("message") or detail.get("error") or message)
|
|
164
|
+
except Exception:
|
|
165
|
+
detail = resp.text[:1000] if resp.text else None
|
|
166
|
+
|
|
167
|
+
status = resp.status_code
|
|
168
|
+
if status == 401:
|
|
169
|
+
print_error("auth_error", "Authentication failed. Check SMARTLEAD_API_KEY.", status_code=status, detail=detail)
|
|
170
|
+
elif status == 403:
|
|
171
|
+
print_error("forbidden", "Permission denied.", status_code=status, detail=detail)
|
|
172
|
+
elif status == 404:
|
|
173
|
+
print_error("not_found", "Resource not found.", status_code=status, detail=detail)
|
|
174
|
+
elif status == 429:
|
|
175
|
+
print_error("rate_limit", "Rate limit exceeded.", status_code=status, detail=detail)
|
|
176
|
+
elif status == 400:
|
|
177
|
+
print_error("validation_error", message, status_code=status, detail=detail)
|
|
178
|
+
else:
|
|
179
|
+
print_error("api_error", message, status_code=status, detail=detail)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Command modules for smartlead-cli."""
|