Skip to content

Commit 009b003

Browse files
authored
Merge pull request #16 from shiningflash/refactoring-2.1.0
Refactor SDK and Documentation (2.1.0)
2 parents b8a7539 + 9d2adbc commit 009b003

File tree

16 files changed

+400
-109
lines changed

16 files changed

+400
-109
lines changed

README.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
A Python SDK designed to simplify interaction with the messaging API and webhook functionalities. It ensures seamless message management, automatic signature validation, and provides a robust foundation for developing scalable messaging applications.
44

5+
[![SDK Usage Documentation](https://img.shields.io/badge/Docs-SDK%20Usage%20Guide-blue?style=for-the-badge)](docs/sdk_usage.md) [![Webhook Documentation](https://img.shields.io/badge/Docs-Webhook%20Guide-blue?style=for-the-badge)](docs/webhook_guide.md)
6+
57
---
68

79
## Table of Contents
@@ -80,33 +82,66 @@ The **Messaging SDK** is a Python library that allows developers to interact wit
8082

8183
### SDK Usage
8284

83-
1. Initialize the SDK:
85+
1. **Initialize the SDK:**
86+
8487
```python
85-
from src.sdk.client import ApiClient
86-
from src.sdk.features.messages import Messages
88+
from sdk.client import ApiClient
89+
from sdk.features.messages import Messages
8790

91+
# Initialize the API client and Messages module
8892
api_client = ApiClient()
8993
messages = Messages(api_client)
9094

9195
# Send a message
9296
response = messages.send_message({
93-
"to": "+123456789",
97+
"to": {"id": "contact123"}, # Use the contact ID format for the recipient
9498
"content": "Hello, World!",
95-
"sender": "+987654321"
99+
"from": "+987654321" # Sender's phone number
96100
})
97101
print(response)
98102
```
99103

100-
2. List messages:
104+
2. **List Messages:**
105+
101106
```python
107+
# List sent messages with pagination
102108
response = messages.list_messages(page=1, limit=10)
103109
print(response)
104110
```
105-
111+
112+
**Example Response:**
113+
```json
114+
{
115+
"messages": [
116+
{
117+
"id": "msg123",
118+
"to": {
119+
"id": "contact123",
120+
"name": "John Doe",
121+
"phone": "+1234567890"
122+
},
123+
"from": "+987654321",
124+
"content": "Hello, World!",
125+
"status": "delivered",
126+
"createdAt": "2024-12-01T00:00:00Z"
127+
}
128+
],
129+
"page": 1,
130+
"quantityPerPage": 10
131+
}
132+
```
133+
134+
#### Additional Features
135+
136+
- **Contact Management:** Add, update, delete, and list contacts.
137+
- **Webhook Integration:** Validate and handle webhook payloads with ease.
138+
106139
#### Comprehensive User Guide for SDK Usage
107140

108141
[![SDK Usage Documentation](https://img.shields.io/badge/Docs-SDK%20Usage%20Guide-blue?style=for-the-badge)](docs/sdk_usage.md)
109142

143+
---
144+
110145
### Webhook Setup
111146

112147
1. Run the webhook server:
@@ -151,6 +186,10 @@ A detailed overview of the project structure, including descriptions of key file
151186
├── .github/ # GitHub workflows for CI/CD
152187
│ └── workflows/
153188
│ └── ci.yml # Continuous Integration pipeline configuration
189+
├── docs/ # Additional documentation
190+
│ ├── openapi.yaml # OpenAPI docs
191+
│ ├── sdk_usage.md # Comprehensive SDK usage documentation
192+
│ └── webhook_guide.md # Webhook-specific documentation
154193
├── src/ # Source code directory
155194
│ ├── core/ # Core modules for shared logic
156195
│ │ ├── __init__.py # Core module initialization
@@ -211,10 +250,7 @@ A detailed overview of the project structure, including descriptions of key file
211250
├── requirements.in # Base Python dependencies
212251
├── requirements.txt # Locked Python dependencies
213252
├── README.md # Project documentation and usage guide
214-
├── docs/ # Additional documentation
215-
│ ├── openapi.yaml # OpenAPI docs
216-
│ ├── sdk_usage.md # Comprehensive SDK usage documentation
217-
│ └── webhook_guide.md # Webhook-specific documentation
253+
├── setup.py # Setup file to enable 'pip install .'
218254
219255
```
220256

@@ -335,7 +371,7 @@ Here is the comprehensive example for lifecycle of Messaging SDK and Webhook for
335371
| Example Code: |
336372
| sdk.messages.send_message( |
337373
| to="+123456789", |
338-
| sender="+987654321", |
374+
| from_sender="+987654321", |
339375
| content="Hello, World!" |
340376
| ) |
341377
+----------------------------------------------------------+
@@ -357,7 +393,7 @@ Here is the comprehensive example for lifecycle of Messaging SDK and Webhook for
357393
| { |
358394
| "to": "+123456789", |
359395
| "content": "Hello, World!", |
360-
| "sender": "+987654321" |
396+
| "from_sender": "+987654321" |
361397
| } |
362398
+----------------------------------------------------------+
363399
|

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ services:
55
ports:
66
- "3000:3000"
77
environment:
8-
WEBHOOK_URL: ${WEBHOOK_URL}
98
WEBHOOK_SECRET: ${WEBHOOK_SECRET}
109

1110
swagger-ui:

docs/sdk_usage.md

Lines changed: 85 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SDK Usage Guide
22

3-
Welcome to the SDK Usage Guide for the **messaging-sdk**. This guide will walk you through the installation, configuration, and practical usage of the SDK, providing detailed examples and instructions for integrating the SDK into your applications.
3+
Welcome to the **messaging-sdk** SDK Usage Guide. This document provides detailed instructions for installing, configuring, and utilizing the SDK, including advanced features and best practices.
44

55
---
66

@@ -13,90 +13,114 @@ Welcome to the SDK Usage Guide for the **messaging-sdk**. This guide will walk y
1313
- [Sending Messages](#sending-messages)
1414
- [Managing Contacts](#managing-contacts)
1515
5. [Advanced Usage](#advanced-usage)
16+
- [Pagination](#pagination)
17+
- [Retry Mechanism](#retry-mechanism)
1618
6. [Error Handling](#error-handling)
1719
7. [Testing](#testing)
1820
8. [Logging](#logging)
21+
9. [Complete Functionalities](#complete-functionalities)
1922

2023
---
2124

2225
## Introduction
2326

24-
The `messaging-sdk` is a Python library designed to simplify interactions with the messaging and contacts API. The SDK provides:
27+
The `messaging-sdk` is a Python library designed for seamless integration with messaging and contacts APIs. It provides:
2528

26-
- Simplified API interactions without requiring manual configuration of authentication headers.
27-
- IDE-friendly auto-completion for seamless development.
28-
- Robust retry mechanisms for handling transient errors.
29-
- Built-in validation to ensure API requests meet expected formats.
29+
- Simplified API interactions with minimal setup.
30+
- Robust error handling and retry mechanisms.
31+
- Logging for debugging and monitoring.
32+
- Easy-to-use methods for sending messages and managing contacts.
3033

3134
---
3235

3336
## Installation
3437

35-
To install the SDK, use `pip`:
38+
To install the SDK locally:
3639

37-
```bash
38-
pip install -r requirements.txt
39-
```
40+
1. Clone the repository:
41+
42+
```bash
43+
git clone https://github.com/shiningflash/messaging-sdk.git
44+
cd messaging-sdk
45+
```
46+
47+
2. Install additional dependencies if required:
48+
49+
```bash
50+
pip install -r requirements.txt
51+
```
4052

4153
---
4254

4355
## Configuration
4456

45-
1. Copy the `.env.example` file to `.env` in the root directory:
57+
1. Copy the `.env.example` file and rename it to `.env`:
4658

4759
```bash
4860
cp .env.example .env
4961
```
5062

51-
2. Open `.env` and update the variables accordingly.
63+
2. Open `.env` and configure the following variables:
64+
65+
- `BASE_URL`: Base URL for the API (e.g., `http://localhost:3000`).
66+
- `API_KEY`: Your API key for authentication.
67+
- `WEBHOOK_SECRET`: Secret key for validating webhooks.
68+
69+
Install the SDK using pip in editable mode:
70+
71+
```bash
72+
pip install -e .
73+
```
5274

5375
---
5476

5577
## Basic Usage
5678

5779
### Sending Messages
5880

59-
The SDK provides a straightforward way to send messages:
81+
The SDK allows you to send messages easily:
6082

6183
```python
62-
from src.sdk.features.messages import Messages
63-
from src.sdk.client import ApiClient
84+
from sdk.client import ApiClient
85+
from sdk.features.messages import Messages
6486
6587
client = ApiClient()
6688
messages = Messages(client)
6789
6890
# Prepare the payload
6991
payload = {
70-
"to": "+123456789",
92+
"to": {"id": "contact-id"}, # Contact ID
7193
"content": "Hello, world!",
72-
"sender": "+987654321"
94+
"from": "+9876543210" # Sender's phone number
7395
}
7496
7597
# Send the message
7698
response = messages.send_message(payload=payload)
99+
print(response)
77100
```
78101

79102
### Managing Contacts
80103

81-
You can create, list, and delete contacts using the SDK:
104+
You can create, list, and delete contacts:
82105

83106
```python
84-
from src.sdk.features.contacts import Contacts
107+
from sdk.features.contacts import Contacts
85108
86109
contacts = Contacts(client)
87110
88111
# Create a new contact
89112
new_contact = {
90113
"name": "John Doe",
91-
"phone": "+123456789"
114+
"phone": "+1234567890"
92115
}
93116
response = contacts.create_contact(new_contact)
94117
95118
# List all contacts
96-
contacts_list = contacts.list_contacts()
119+
contacts_list = contacts.list_contacts(page=1, max=5)
120+
print(contacts_list)
97121
98122
# Delete a contact
99-
contacts.delete_contact(contact_id="contact123")
123+
contacts.delete_contact(contact_id="contact-id")
100124
```
101125

102126
---
@@ -105,55 +129,57 @@ contacts.delete_contact(contact_id="contact123")
105129

106130
### Pagination
107131

108-
Retrieve paginated lists for messages or contacts:
132+
The SDK supports pagination for listing messages and contacts:
109133

110134
```python
111135
# Retrieve paginated messages
112-
messages_list = messages.list_messages(page=1, limit=5)
136+
messages_list = messages.list_messages(page=1, limit=10)
113137
print(messages_list)
114138
115139
# Retrieve paginated contacts
116140
contacts_list = contacts.list_contacts(page=1, max=5)
117141
print(contacts_list)
118142
```
119143

120-
### Retry Mechanisms
144+
### Retry Mechanism
121145

122-
The SDK automatically retries failed requests for transient errors (e.g., `503 Service Unavailable`). You can customize retry logic in the `src/sdk/utils/retry.py` module.
146+
The SDK automatically retries requests for transient errors (e.g., HTTP 503). The retry logic is located in `src/core/retry.py` and can be customized.
123147

124148
---
125149

126150
## Error Handling
127151

128-
The SDK raises specific exceptions for various error scenarios:
152+
The SDK provides built-in exceptions for various scenarios:
129153

130-
- `UnauthorizedError`: Raised for `401 Unauthorized` responses.
131-
- `NotFoundError`: Raised for `404 Not Found` responses.
132-
- `ServerError`: Raised for `500 Internal Server Error` responses.
133-
- `ApiError`: Raised for other unexpected API errors.
154+
- `UnauthorizedError`: Raised for authentication errors (`401 Unauthorized`).
155+
- `NotFoundError`: Raised when a resource is not found (`404 Not Found`).
156+
- `ServerError`: Raised for server-side errors (`500 Internal Server Error`).
157+
- `ContactNotFoundError`: Raised for missing contacts.
158+
- `MessageNotFoundError`: Raised for missing messages.
159+
- `ApiError`: Raised for other API-related issues.
134160

135161
Example:
136162

137163
```python
138164
try:
139-
messages.list_messages()
140-
except UnauthorizedError as e:
141-
print(f"Authentication failed: {e}")
165+
messages.get_message("invalid-id")
166+
except MessageNotFoundError as e:
167+
print(f"Message not found: {e}")
142168
except ApiError as e:
143-
print(f"Unexpected error: {e}")
169+
print(f"API Error: {e}")
144170
```
145171

146172
---
147173

148174
## Testing
149175

150-
The SDK includes unit, integration, and end-to-end tests. To run all tests:
176+
Run tests using `pytest`:
151177

152178
```bash
153179
pytest
154180
```
155181

156-
To generate a coverage report:
182+
To check code coverage:
157183

158184
```bash
159185
pytest --cov=src --cov-report=term-missing
@@ -163,15 +189,31 @@ pytest --cov=src --cov-report=term-missing
163189

164190
## Logging
165191

166-
The SDK includes comprehensive logging for debugging and auditing. Logs are categorized as follows:
192+
Logs provide detailed insights into SDK operations:
167193

168-
- **Console Logs**: Informational and error logs for immediate feedback.
169-
- **File Logs**: Warnings and errors logged to `logs/app.log`.
194+
- **Console Logs**: Informational logs for debugging.
195+
- **File Logs**: Errors and warnings logged to `logs/app.log`.
170196

171-
Example of enabling logger in your application:
197+
Example:
172198

173199
```python
174-
from src.core.logger import logger
200+
from sdk.core.logger import logger
175201
176-
logger.info("Application started.")
202+
logger.info("Starting application...")
177203
```
204+
205+
---
206+
207+
## Complete Functionalities
208+
209+
### Messages
210+
211+
- **Send Message**: `send_message(payload)`
212+
- **List Messages**: `list_messages(page, limit)`
213+
- **Get Message by ID**: `get_message(message_id)`
214+
215+
### Contacts
216+
217+
- **Create Contact**: `create_contact(contact_payload)`
218+
- **List Contacts**: `list_contacts(page, max)`
219+
- **Delete Contact**: `delete_contact(contact_id)`

0 commit comments

Comments
 (0)