smsflow 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.
smsflow/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .client import SmsFlowClient, SmsFlowError
2
+
3
+ __all__ = ["SmsFlowClient", "SmsFlowError"]
smsflow/client.py ADDED
@@ -0,0 +1,137 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from datetime import datetime, timedelta, timezone
5
+ from typing import Any, Iterable
6
+
7
+ try:
8
+ import requests
9
+ except ImportError: # pragma: no cover - exercised only when dependencies are not installed.
10
+ requests = None
11
+
12
+
13
+ class SmsFlowError(Exception):
14
+ def __init__(self, message: str, *, status_code: int | None = None, body: Any = None) -> None:
15
+ super().__init__(message)
16
+ self.status_code = status_code
17
+ self.body = body
18
+
19
+
20
+ @dataclass
21
+ class _CachedAuth:
22
+ token: str
23
+ refresh_at: datetime
24
+
25
+
26
+ class SmsFlowClient:
27
+ def __init__(
28
+ self,
29
+ *,
30
+ client_id: str,
31
+ client_secret: str,
32
+ base_url: str = "https://portal.smsflow.co.za/",
33
+ session: Any = None,
34
+ timeout: int = 30,
35
+ ) -> None:
36
+ if not client_id or not client_secret:
37
+ raise SmsFlowError("SMSFlow client_id and client_secret are required.")
38
+
39
+ self.client_id = client_id
40
+ self.client_secret = client_secret
41
+ self.base_url = base_url.rstrip("/")
42
+ if session is None and requests is None:
43
+ raise SmsFlowError("The requests package is required when no custom session is supplied.")
44
+
45
+ self.session = session or requests.Session()
46
+ self.timeout = timeout
47
+ self._cached_auth: _CachedAuth | None = None
48
+
49
+ def authenticate(self) -> dict[str, Any]:
50
+ response = self.session.get(
51
+ f"{self.base_url}/api/integration/authentication",
52
+ auth=(self.client_id, self.client_secret),
53
+ timeout=self.timeout,
54
+ )
55
+ body = _read_body(response)
56
+
57
+ if not response.ok:
58
+ raise SmsFlowError("SMSFlow authentication failed.", status_code=response.status_code, body=body)
59
+
60
+ token = body.get("token")
61
+ expires_in = int(body.get("expiresInMinutes", 1))
62
+ if not token:
63
+ raise SmsFlowError("SMSFlow authentication did not return a token.")
64
+
65
+ self._cached_auth = _CachedAuth(
66
+ token=token,
67
+ refresh_at=datetime.now(timezone.utc) + timedelta(minutes=max(expires_in - 5, 1)),
68
+ )
69
+ return body
70
+
71
+ def send_sms(
72
+ self,
73
+ *,
74
+ campaign_name: str,
75
+ messages: Iterable[dict[str, str]],
76
+ start_delivery_utc: str | None = None,
77
+ check_opt_outs: bool = True,
78
+ ) -> dict[str, Any]:
79
+ message_list = list(messages)
80
+ if not message_list:
81
+ raise SmsFlowError("At least one SMS message is required.")
82
+
83
+ response = self.session.post(
84
+ f"{self.base_url}/api/integration/BulkMessages",
85
+ headers={"Authorization": f"Bearer {self._get_token()}"},
86
+ json={
87
+ "SendOptions": {
88
+ "startDeliveryUtc": start_delivery_utc,
89
+ "campaignName": campaign_name,
90
+ "checkOptOuts": check_opt_outs,
91
+ },
92
+ "messages": [
93
+ {
94
+ "content": message["content"],
95
+ "destination": message["destination"],
96
+ }
97
+ for message in message_list
98
+ ],
99
+ },
100
+ timeout=self.timeout,
101
+ )
102
+ body = _read_body(response)
103
+
104
+ if not response.ok:
105
+ raise SmsFlowError("SMSFlow send failed.", status_code=response.status_code, body=body)
106
+
107
+ return body
108
+
109
+ def get_balance(self) -> dict[str, Any]:
110
+ response = self.session.get(
111
+ f"{self.base_url}/api/integration/Balance",
112
+ headers={"Authorization": f"Bearer {self._get_token()}"},
113
+ timeout=self.timeout,
114
+ )
115
+ body = _read_body(response)
116
+
117
+ if not response.ok:
118
+ raise SmsFlowError("SMSFlow balance request failed.", status_code=response.status_code, body=body)
119
+
120
+ return body
121
+
122
+ def _get_token(self) -> str:
123
+ if self._cached_auth and datetime.now(timezone.utc) < self._cached_auth.refresh_at:
124
+ return self._cached_auth.token
125
+
126
+ auth = self.authenticate()
127
+ return auth["token"]
128
+
129
+
130
+ def _read_body(response: Any) -> Any:
131
+ if not response.content:
132
+ return None
133
+
134
+ try:
135
+ return response.json()
136
+ except ValueError:
137
+ return response.text
@@ -0,0 +1,102 @@
1
+ Metadata-Version: 2.4
2
+ Name: smsflow
3
+ Version: 0.1.0
4
+ Summary: Python client library for the SMSFlow HTTPS API.
5
+ Author: SMSFlow
6
+ License-Expression: MIT
7
+ Project-URL: Documentation, https://docs.smsflow.co.za/
8
+ Project-URL: Repository, https://github.com/SMSFlow-ZA/smsflow-python
9
+ Project-URL: Issues, https://github.com/SMSFlow-ZA/smsflow-python/issues
10
+ Keywords: sms,sms-api,smsflow,notifications
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
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
+ Classifier: Topic :: Communications
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Python: >=3.9
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: requests>=2.31.0
25
+ Dynamic: license-file
26
+
27
+ # SMSFlow Python SDK
28
+
29
+ The SMSFlow Python SDK makes it easy to send SMS messages and check SMS credit balance from backend Python applications, automation scripts, scheduled jobs, CRM integrations, ERP integrations, and operational tooling.
30
+
31
+ Documentation: https://docs.smsflow.co.za/
32
+
33
+ ## Install
34
+
35
+ Package publishing is not enabled yet. During development, install locally:
36
+
37
+ ```bash
38
+ pip install smsflow
39
+ ```
40
+
41
+ ## Configuration
42
+
43
+ Store credentials in environment variables or your platform's secret manager.
44
+
45
+ ```bash
46
+ export SMSFLOW_CLIENT_ID="YOUR_CLIENT_ID"
47
+ export SMSFLOW_CLIENT_SECRET="YOUR_CLIENT_SECRET"
48
+ ```
49
+
50
+ Do not put SMSFlow Client Secrets in source code, logs, notebooks, screenshots, or public issues.
51
+
52
+ ## Usage
53
+
54
+ ```python
55
+ import os
56
+ from smsflow import SmsFlowClient
57
+
58
+ client = SmsFlowClient(
59
+ client_id=os.environ["SMSFLOW_CLIENT_ID"],
60
+ client_secret=os.environ["SMSFLOW_CLIENT_SECRET"],
61
+ )
62
+
63
+ response = client.send_sms(
64
+ campaign_name="Python SDK example",
65
+ messages=[
66
+ {
67
+ "destination": "27000000000",
68
+ "content": "Your SMSFlow Python test message was sent successfully.",
69
+ }
70
+ ],
71
+ )
72
+
73
+ print(response["sendResponse"]["eventId"])
74
+ ```
75
+
76
+ ## Features
77
+
78
+ - Get and cache SMSFlow login tokens.
79
+ - Send one or more SMS messages.
80
+ - Schedule SMS messages using UTC delivery time.
81
+ - Respect opt-out checks by default.
82
+ - Check account balance.
83
+ - Raise structured exceptions when the API returns an error.
84
+
85
+ ## Local test send
86
+
87
+ This command sends a real SMS and may consume test credits:
88
+
89
+ ```bash
90
+ export SMSFLOW_CLIENT_ID="YOUR_CLIENT_ID"
91
+ export SMSFLOW_CLIENT_SECRET="YOUR_CLIENT_SECRET"
92
+ export SMSFLOW_DESTINATION="27000000000"
93
+ PYTHONPATH=src python examples/send_sms.py
94
+ ```
95
+
96
+ ## Security
97
+
98
+ Never commit real credentials. Use environment variables or a secret manager.
99
+
100
+ ## License
101
+
102
+ MIT
@@ -0,0 +1,7 @@
1
+ smsflow/__init__.py,sha256=MeEAMCx9auJfmoYCwwTpleyaZCU72UmWjawwdHTYWi0,93
2
+ smsflow/client.py,sha256=0WpqWdjiple-eyg8-CTAsXIhnjhhEGb_SUGVQLo3TJ0,4334
3
+ smsflow-0.1.0.dist-info/licenses/LICENSE,sha256=HP9VFmKJEGCkw-hkbQTlV1vlUTnUBU9tsWfgRmu_wiY,1064
4
+ smsflow-0.1.0.dist-info/METADATA,sha256=K1a7ko-9-6PWHlHKMsiWmeutsZ8eXkS9oAP01HrmQQs,2853
5
+ smsflow-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
6
+ smsflow-0.1.0.dist-info/top_level.txt,sha256=5mzoEvmA8Y6TwzmXlH9dYuViuzt7Y4q7TCeEz3mAwcI,8
7
+ smsflow-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 SMSFlow
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
+ smsflow