Skip to content

Commit e80a1cf

Browse files
authored
Merge pull request #13 from shiningflash/refactoring-2.0
Refactoring 2.0: Improved Project Structure and Exception Handling
2 parents c8eef52 + e689893 commit e80a1cf

33 files changed

+551
-437
lines changed

README.md

Lines changed: 71 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The **Messaging SDK** is a Python library that allows developers to interact wit
5252

5353
1. Clone the repository:
5454
```bash
55-
git clone https://github.com/your-repo/messaging-sdk.git
55+
git clone https://github.com/shiningflash/messaging-sdk.git
5656
cd messaging-sdk
5757
```
5858

@@ -64,40 +64,22 @@ The **Messaging SDK** is a Python library that allows developers to interact wit
6464
```
6565

6666
3. Configure environment variables:
67-
- Copy `.env.example` to `.env`:
68-
```bash
69-
cp .env.example .env
70-
```
71-
- Edit `.env` and adjust the values:
72-
```env
73-
BASE_URL=http://localhost:3000
74-
API_KEY=your-api-key
75-
WEBHOOK_SECRET=mySecret
76-
```
77-
78-
4. **Browse the API:**
79-
- The repository includes an **OpenAPI Specification** file located at: `./docs/openapi.yaml`. This file describes the API's endpoints and can be viewed using tools like **SwaggerUI** or **Redocly**.
67+
- Copy `.env.example` to `.env`: `cp .env.example .env`
68+
- Edit `.env` and adjust the values.
8069

70+
4. Browse the API:
71+
- The repository includes an **OpenAPI Specification** file located at: `./docs/openapi.yaml`.
8172
- To explore the API visually, you can use Docker to run the provided tools:
8273
1. Ensure Docker is installed on your machine.
83-
2. Start the servers:
84-
```bash
85-
docker compose up
86-
```
87-
(If prompted, update the Docker images using `docker compose pull`).
88-
89-
3. The following servers will be available:
90-
- **Swagger UI**: [http://localhost:8080](http://localhost:8080)
91-
- **Redocly**: [http://localhost:8090](http://localhost:8090)
92-
- **API Server**: [http://localhost:3000](http://localhost:3000) (uses a local database).
93-
94-
You can use either SwaggerUI or Redocly to browse and understand the API endpoints.
74+
2. Start the servers: `docker compose up`.
75+
3. The following servers will be available: **Swagger UI**: [http://localhost:8080](http://localhost:8080), **Redocly**: [http://localhost:8090](http://localhost:8090), **API Server**: [http://localhost:3000](http://localhost:3000) (uses a local database).
9576

9677
---
9778

9879
## Usage Guide
9980

10081
### SDK Usage
82+
10183
1. Initialize the SDK:
10284
```python
10385
from src.sdk.client import ApiClient
@@ -121,19 +103,22 @@ The **Messaging SDK** is a Python library that allows developers to interact wit
121103
print(response)
122104
```
123105

124-
#### Comprehensive User Guide for **[SDK Usage](docs/sdk_usage.md)**
125-
For detailed usage and examples, please refer to the **[User Guide](docs/sdk_usage.md)** 👈.
106+
#### Comprehensive User Guide for SDK Usage
107+
108+
[![SDK Usage Documentation](https://img.shields.io/badge/Docs-SDK%20Usage%20Guide-blue?style=for-the-badge)](docs/sdk_usage.md)
126109

127110
### Webhook Setup
111+
128112
1. Run the webhook server:
129113
```bash
130114
uvicorn src.server.app:app --reload --port 3010
131115
```
132116

133117
2. Configure the API to send events to your webhook endpoint (e.g., `http://localhost:3010/webhooks`).
134118

135-
#### Comprehensive User Guide for **[Webhook](docs/webhook_guide.md)**
136-
For detailed usage and examples, please refer to the **[User Guide](docs/webhook_guide.md)** 👈.
119+
#### Comprehensive User Guide for Webhook
120+
121+
[![Webhook Documentation](https://img.shields.io/badge/Docs-Webhook%20Guide-blue?style=for-the-badge)](docs/webhook_guide.md)
137122

138123
---
139124

@@ -161,86 +146,76 @@ For detailed usage and examples, please refer to the **[User Guide](docs/webhook
161146

162147
A detailed overview of the project structure, including descriptions of key files and directories.
163148

164-
## Root Directory
165-
166-
```
149+
```plaintext
150+
.
167151
├── .github/ # GitHub workflows for CI/CD
152+
│ └── workflows/
153+
│ └── ci.yml # Continuous Integration pipeline configuration
168154
├── src/ # Source code directory
155+
│ ├── core/ # Core modules for shared logic
156+
│ │ ├── __init__.py # Core module initialization
157+
│ │ ├── exceptions/ # Exception handling modules
158+
│ │ │ ├── __init__.py # Consolidated exception imports
159+
│ │ │ ├── api.py # API-specific exceptions
160+
│ │ │ ├── decorators.py # Decorators for exception handling
161+
│ │ │ └── resource.py # Resource-specific exceptions
162+
│ │ ├── config.py # Global configuration file for SDK and server
163+
│ │ ├── logger.py # Logging utilities
164+
│ │ ├── requests.py # Request helpers for SDK
165+
│ │ ├── retry.py # Retry logic for transient failures
166+
│ │ ├── security.py # HMAC validation and signature generation
167+
│ │ └── validators.py # Common validation logic
168+
│ ├── schemas/ # Schema definitions for request/response
169+
│ │ ├── __init__.py # Schemas initialization
170+
│ │ ├── contacts.py # Contact-related schemas
171+
│ │ ├── errors.py # Error schemas (aligned with OpenAPI specs)
172+
│ │ ├── messages.py # Message-related schemas
173+
│ │ └── webhook.py # Webhook payload schemas
174+
│ ├── sdk/ # SDK-related functionalities
175+
│ │ ├── __init__.py # SDK initialization
176+
│ │ ├── client.py # API client for interacting with the server
177+
│ │ └── features/ # API feature modules
178+
│ │ ├── __init__.py # Features initialization
179+
│ │ ├── contacts.py # Contacts-related SDK operations
180+
│ │ └── messages.py # Messages-related SDK operations
181+
│ ├── server/ # Webhook server implementation
182+
│ │ ├── __init__.py # Server initialization
183+
│ │ ├── app.py # Main FastAPI application
184+
│ │ └── schemas.py # Schemas specific to the webhook server
169185
├── tests/ # Testing files for unit, integration, and E2E
186+
│ ├── __init__.py # Test package initialization
187+
│ ├── conftest.py # Pytest fixtures and test setup
188+
│ ├── e2e/ # End-to-End (E2E) tests
189+
│ │ ├── __init__.py # E2E tests initialization
190+
│ │ ├── test_contacts_e2e.py # E2E tests for contacts feature
191+
│ │ └── test_messages_e2e.py # E2E tests for messages feature
192+
│ ├── integration/ # Integration tests
193+
│ │ ├── __init__.py # Integration tests initialization
194+
│ │ ├── test_contacts_integration.py # Integration tests for contacts
195+
│ │ ├── test_messages_integration.py # Integration tests for messages
196+
│ │ └── test_webhook.py # Integration tests for webhook functionality
197+
│ └── unit/ # Unit tests for SDK and server
198+
│ ├── test_sdk/ # SDK-specific unit tests
199+
│ │ ├── __init__.py # SDK unit tests initialization
200+
│ │ ├── test_client.py # Unit tests for API client
201+
│ │ ├── test_contacts.py # Unit tests for contacts module
202+
│ │ └── test_messages.py # Unit tests for messages module
203+
│ └── test_server/ # Server-specific unit tests
204+
│ ├── __init__.py # Server unit tests initialization
205+
│ ├── test_route.py # Unit tests for API routes
206+
│ └── test_signature_validation.py # Unit tests for signature validation
170207
├── venv/ # Python virtual environment (not versioned)
171208
├── .env.example # Example environment variables
172-
├── config.py # Global configuration file for SDK and server
173209
├── docker-compose.yml # Docker Compose configuration
174210
├── pytest.ini # Pytest configuration
175211
├── requirements.in # Base Python dependencies
176212
├── requirements.txt # Locked Python dependencies
177213
├── README.md # Project documentation and usage guide
178214
├── docs/ # Additional documentation
215+
│ ├── openapi.yaml # OpenAPI docs
179216
│ ├── sdk_usage.md # Comprehensive SDK usage documentation
180217
│ └── webhook_guide.md # Webhook-specific documentation
181-
```
182-
183-
---
184-
185-
#### `/src` Directory
186-
187-
The main application source code is organized as follows:
188-
189-
```
190-
/src
191-
├── sdk/ # SDK-related functionalities
192-
│ ├── __init__.py # SDK initialization
193-
│ ├── client.py # API client for interacting with the server
194-
│ ├── features/ # API feature modules
195-
│ │ ├── __init__.py # Features initialization
196-
│ │ ├── contacts.py # Contacts-related SDK operations
197-
│ │ └── messages.py # Messages-related SDK operations
198-
│ ├── schemas/ # Schema definitions for request/response
199-
│ │ ├── __init__.py # Schemas initialization
200-
│ │ ├── contacts.py # Contact-related schemas
201-
│ │ └── messages.py # Message-related schemas
202-
│ └── utils/ # Utility modules
203-
│ ├── __init__.py # Utilities initialization
204-
│ ├── exceptions.py # Custom exceptions for error handling
205-
│ ├── logger.py # Logging utilities
206-
│ ├── requests.py # Request helpers for SDK
207-
│ ├── retry.py # Retry logic for transient failures
208-
│ └── validators.py # Validators for request/response data
209-
├── server/ # Webhook server implementation
210-
│ ├── __init__.py # Server initialization
211-
│ ├── app.py # Main FastAPI application
212-
│ └── schemas.py # Schemas specific to the webhook server
213-
```
214218
215-
---
216-
217-
#### `/tests` Directory
218-
219-
The testing framework is organized as follows:
220-
221-
```
222-
/tests
223-
├── __init__.py # Test package initialization
224-
├── conftest.py # Pytest fixtures and test setup
225-
├── e2e/ # End-to-End (E2E) tests
226-
│ ├── __init__.py # E2E tests initialization
227-
│ ├── test_contacts_e2e.py # E2E tests for contacts feature
228-
│ └── test_messages_e2e.py # E2E tests for messages feature
229-
├── integration/ # Integration tests
230-
│ ├── __init__.py # Integration tests initialization
231-
│ ├── test_contacts_integration.py # Integration tests for contacts
232-
│ ├── test_end_to_end_workflows.py # Comprehensive workflow tests
233-
│ ├── test_messages_integration.py # Integration tests for messages
234-
│ └── test_webhook.py # Integration tests for webhook functionality
235-
└── unit/ # Unit tests for SDK and server
236-
├── test_sdk/ # SDK-specific unit tests
237-
│ ├── __init__.py # SDK unit tests initialization
238-
│ ├── test_client.py # Unit tests for API client
239-
│ ├── test_contacts.py # Unit tests for contacts module
240-
│ └── test_messages.py # Unit tests for messages module
241-
└── test_server/ # Server-specific unit tests
242-
├── test_route.py # Unit tests for API routes
243-
└── test_signature_validation.py # Unit tests for signature validation
244219
```
245220

246221
---

docs/sdk_usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ The SDK includes comprehensive logging for debugging and auditing. Logs are cate
171171
Example of enabling logger in your application:
172172

173173
```python
174-
from src.common.logger import logger
174+
from src.core.logger import logger
175175
176176
logger.info("Application started.")
177177
```

docs/webhook_guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The SDK provides a `verify_signature` utility to validate the HMAC signature. He
7474
Example Code:
7575

7676
```python
77-
from src.common.validators import verify_signature
77+
from src.core.security import verify_signature
7878

7979
try:
8080
verify_signature(message=raw_payload, signature=auth_header, secret=WEBHOOK_SECRET)

src/common/exceptions.py

Lines changed: 0 additions & 121 deletions
This file was deleted.
File renamed without changes.

config.py renamed to src/core/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pydantic import Field, field_validator, ConfigDict
22
from pydantic_settings import BaseSettings
3-
from src.common.logger import logger
3+
from src.core.logger import logger
44

55

66
class Settings(BaseSettings):

src/core/exceptions/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .api import ApiError, UnauthorizedError, NotFoundError, ServerError, TransientError
2+
from .resource import ContactNotFoundError, MessageNotFoundError, ResourceNotFoundError
3+
from .decorators import handle_exceptions, handle_404_error
4+
5+
__all__ = [
6+
"ApiError",
7+
"UnauthorizedError",
8+
"NotFoundError",
9+
"ServerError",
10+
"TransientError",
11+
"ContactNotFoundError",
12+
"MessageNotFoundError",
13+
"ResourceNotFoundError",
14+
"handle_exceptions",
15+
"handle_404_error",
16+
]

0 commit comments

Comments
 (0)