spamdetector-client 0.1.0__py3-none-any.whl
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.
- spamdetector/__init__.py +12 -0
- spamdetector/client.py +97 -0
- spamdetector/exceptions.py +10 -0
- spamdetector_client-0.1.0.dist-info/METADATA +91 -0
- spamdetector_client-0.1.0.dist-info/RECORD +8 -0
- spamdetector_client-0.1.0.dist-info/WHEEL +5 -0
- spamdetector_client-0.1.0.dist-info/licenses/LICENSE +21 -0
- spamdetector_client-0.1.0.dist-info/top_level.txt +1 -0
spamdetector/__init__.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from .client import DEFAULT_BASE_URL, PredictionResult, SpamDetector, predict
|
|
2
|
+
from .exceptions import SpamDetectorAPIError, SpamDetectorAuthError, SpamDetectorError
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"DEFAULT_BASE_URL",
|
|
6
|
+
"PredictionResult",
|
|
7
|
+
"SpamDetector",
|
|
8
|
+
"SpamDetectorAPIError",
|
|
9
|
+
"SpamDetectorAuthError",
|
|
10
|
+
"SpamDetectorError",
|
|
11
|
+
"predict",
|
|
12
|
+
]
|
spamdetector/client.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import Any, Mapping, Optional
|
|
6
|
+
|
|
7
|
+
import requests
|
|
8
|
+
|
|
9
|
+
from .exceptions import SpamDetectorAPIError, SpamDetectorAuthError
|
|
10
|
+
|
|
11
|
+
DEFAULT_BASE_URL = "https://spamdetactor5-9bz78yna.b4a.run"
|
|
12
|
+
DEFAULT_TIMEOUT = 30
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class PredictionResult:
|
|
17
|
+
prediction: str
|
|
18
|
+
confidence: float
|
|
19
|
+
raw: Mapping[str, Any]
|
|
20
|
+
|
|
21
|
+
def to_dict(self) -> dict[str, Any]:
|
|
22
|
+
return {
|
|
23
|
+
"prediction": self.prediction,
|
|
24
|
+
"confidence": self.confidence,
|
|
25
|
+
"raw": dict(self.raw),
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class MessagesResource:
|
|
30
|
+
def __init__(self, client: "SpamDetector") -> None:
|
|
31
|
+
self._client = client
|
|
32
|
+
|
|
33
|
+
def create(self, *, message: str) -> PredictionResult:
|
|
34
|
+
return self._client.predict(message)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class SpamDetector:
|
|
38
|
+
"""Client for the Spam Detector API."""
|
|
39
|
+
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
api_key: Optional[str] = None,
|
|
43
|
+
*,
|
|
44
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
45
|
+
timeout: int = DEFAULT_TIMEOUT,
|
|
46
|
+
session: Optional[requests.Session] = None,
|
|
47
|
+
) -> None:
|
|
48
|
+
self.api_key = api_key or os.getenv("SPAMDETECTOR_API_KEY")
|
|
49
|
+
if not self.api_key:
|
|
50
|
+
raise SpamDetectorAuthError(
|
|
51
|
+
"API key is required. Pass api_key='...' or set SPAMDETECTOR_API_KEY."
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
self.base_url = base_url.rstrip("/")
|
|
55
|
+
self.timeout = timeout
|
|
56
|
+
self.session = session or requests.Session()
|
|
57
|
+
self.messages = MessagesResource(self)
|
|
58
|
+
|
|
59
|
+
def predict(self, message: str) -> PredictionResult:
|
|
60
|
+
"""Send a message to the API and return the spam prediction."""
|
|
61
|
+
if not isinstance(message, str) or not message.strip():
|
|
62
|
+
raise ValueError("message must be a non-empty string.")
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
response = self.session.post(
|
|
66
|
+
f"{self.base_url}/predict",
|
|
67
|
+
json={"message": message},
|
|
68
|
+
headers={"X-API-Key": self.api_key},
|
|
69
|
+
timeout=self.timeout,
|
|
70
|
+
)
|
|
71
|
+
except requests.RequestException as exc:
|
|
72
|
+
raise SpamDetectorAPIError(f"API request failed: {exc}") from exc
|
|
73
|
+
|
|
74
|
+
if response.status_code in {401, 403}:
|
|
75
|
+
raise SpamDetectorAuthError(
|
|
76
|
+
f"API authentication failed: {response.status_code} {response.text}"
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
if not response.ok:
|
|
80
|
+
raise SpamDetectorAPIError(
|
|
81
|
+
f"API request failed: {response.status_code} {response.text}"
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
try:
|
|
85
|
+
payload = response.json()
|
|
86
|
+
return PredictionResult(
|
|
87
|
+
prediction=str(payload["prediction"]),
|
|
88
|
+
confidence=float(payload["confidence"]),
|
|
89
|
+
raw=payload,
|
|
90
|
+
)
|
|
91
|
+
except (KeyError, TypeError, ValueError) as exc:
|
|
92
|
+
raise SpamDetectorAPIError(f"API returned an invalid response: {response.text}") from exc
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def predict(message: str, *, api_key: Optional[str] = None) -> PredictionResult:
|
|
96
|
+
"""Convenience function for one-off predictions."""
|
|
97
|
+
return SpamDetector(api_key=api_key).predict(message)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class SpamDetectorError(Exception):
|
|
2
|
+
"""Base exception for Spam Detector client errors."""
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SpamDetectorAuthError(SpamDetectorError):
|
|
6
|
+
"""Raised when the API key is missing or rejected."""
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SpamDetectorAPIError(SpamDetectorError):
|
|
10
|
+
"""Raised when the API returns a non-success response."""
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: spamdetector-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for the Spam Detector API.
|
|
5
|
+
Author: Bilal Khan
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://spamdetactor5-9bz78yna.b4a.run
|
|
8
|
+
Project-URL: API, https://spamdetactor5-9bz78yna.b4a.run/predict
|
|
9
|
+
Keywords: spam,detector,api,client
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: requests>=2.31.0
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# Spam Detector Client
|
|
26
|
+
|
|
27
|
+
Python library for calling the Spam Detector API.
|
|
28
|
+
|
|
29
|
+
## Install locally
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install -e .
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
After publishing to PyPI, users will install it with:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install spamdetector-client
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
OpenAI-style client usage:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from spamdetector import SpamDetector
|
|
47
|
+
|
|
48
|
+
client = SpamDetector(api_key="YOUR_API_KEY")
|
|
49
|
+
result = client.messages.create(message="Win a free iPhone today!")
|
|
50
|
+
|
|
51
|
+
print(result.prediction)
|
|
52
|
+
print(result.confidence)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Simple function usage:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from spamdetector import predict
|
|
59
|
+
|
|
60
|
+
result = predict("Win a free iPhone today!", api_key="YOUR_API_KEY")
|
|
61
|
+
print(result.to_dict())
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
You can also keep the API key in an environment variable:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
set SPAMDETECTOR_API_KEY=YOUR_API_KEY
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from spamdetector import predict
|
|
72
|
+
|
|
73
|
+
result = predict("Your message here")
|
|
74
|
+
print(result.to_dict())
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Publish to PyPI
|
|
78
|
+
|
|
79
|
+
Update `pyproject.toml` first:
|
|
80
|
+
|
|
81
|
+
- Change `authors`
|
|
82
|
+
- Confirm the package name is available on PyPI
|
|
83
|
+
- Bump `version` whenever you publish a new release
|
|
84
|
+
|
|
85
|
+
Then run:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
python -m pip install --upgrade build twine
|
|
89
|
+
python -m build
|
|
90
|
+
python -m twine upload dist/*
|
|
91
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
spamdetector/__init__.py,sha256=9ahF_CmL13BugHW4rYZawUFR0Zj-koTc2C-7RtZnEK0,345
|
|
2
|
+
spamdetector/client.py,sha256=O7b0ygpeSwTSPA6RhQ60jX3msHyLFmH-t9x4tN5o0WQ,3116
|
|
3
|
+
spamdetector/exceptions.py,sha256=bfiUWLjdHp49UEL6IEYgjQ1Y7rjnhkc0VBpt0GDgtrY,313
|
|
4
|
+
spamdetector_client-0.1.0.dist-info/licenses/LICENSE,sha256=ESYyLizI0WWtxMeS7rGVcX3ivMezm-HOd5WdeOh-9oU,1056
|
|
5
|
+
spamdetector_client-0.1.0.dist-info/METADATA,sha256=MCfWQlWJGqHjQomDbs-OhJLswRY5-z-o1x99RVWHN_o,2149
|
|
6
|
+
spamdetector_client-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
7
|
+
spamdetector_client-0.1.0.dist-info/top_level.txt,sha256=Y_iYRvNo4AyCRBpNNowNDjdVVMUzaUajggoDquIX-xQ,13
|
|
8
|
+
spamdetector_client-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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 @@
|
|
|
1
|
+
spamdetector
|