agrifrika-shared 0.2.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.
Files changed (27) hide show
  1. agrifrika_shared-0.2.0/PKG-INFO +267 -0
  2. agrifrika_shared-0.2.0/README.md +243 -0
  3. agrifrika_shared-0.2.0/agrifrika_shared/__init__.py +7 -0
  4. agrifrika_shared-0.2.0/agrifrika_shared/aws/__init__.py +38 -0
  5. agrifrika_shared-0.2.0/agrifrika_shared/aws/clients.py +219 -0
  6. agrifrika_shared-0.2.0/agrifrika_shared/aws/cognito_client.py +403 -0
  7. agrifrika_shared-0.2.0/agrifrika_shared/aws/dynamo_client.py +377 -0
  8. agrifrika_shared-0.2.0/agrifrika_shared/config/__init__.py +3 -0
  9. agrifrika_shared-0.2.0/agrifrika_shared/identity.py +211 -0
  10. agrifrika_shared-0.2.0/agrifrika_shared/models/__init__.py +50 -0
  11. agrifrika_shared-0.2.0/agrifrika_shared/models/base.py +187 -0
  12. agrifrika_shared-0.2.0/agrifrika_shared/models/common.py +193 -0
  13. agrifrika_shared-0.2.0/agrifrika_shared/notification.py +81 -0
  14. agrifrika_shared-0.2.0/agrifrika_shared/utils/__init__.py +90 -0
  15. agrifrika_shared-0.2.0/agrifrika_shared/utils/exceptions.py +72 -0
  16. agrifrika_shared-0.2.0/agrifrika_shared/utils/logger.py +198 -0
  17. agrifrika_shared-0.2.0/agrifrika_shared/utils/request_parser.py +211 -0
  18. agrifrika_shared-0.2.0/agrifrika_shared/utils/response_builder.py +232 -0
  19. agrifrika_shared-0.2.0/agrifrika_shared/utils/validators.py +173 -0
  20. agrifrika_shared-0.2.0/agrifrika_shared.egg-info/PKG-INFO +267 -0
  21. agrifrika_shared-0.2.0/agrifrika_shared.egg-info/SOURCES.txt +25 -0
  22. agrifrika_shared-0.2.0/agrifrika_shared.egg-info/dependency_links.txt +1 -0
  23. agrifrika_shared-0.2.0/agrifrika_shared.egg-info/requires.txt +11 -0
  24. agrifrika_shared-0.2.0/agrifrika_shared.egg-info/top_level.txt +1 -0
  25. agrifrika_shared-0.2.0/pyproject.toml +67 -0
  26. agrifrika_shared-0.2.0/setup.cfg +4 -0
  27. agrifrika_shared-0.2.0/setup.py +47 -0
@@ -0,0 +1,267 @@
1
+ Metadata-Version: 2.4
2
+ Name: agrifrika-shared
3
+ Version: 0.2.0
4
+ Summary: Shared utilities and models for Agrifrika microservices
5
+ Home-page: https://github.com/agrifrika/agrifrika-backend
6
+ Author: Agrifrika Team
7
+ Author-email: Agrifrika Team <tech@agrifrika.com>
8
+ License: MIT
9
+ Requires-Python: >=3.9
10
+ Description-Content-Type: text/markdown
11
+ Requires-Dist: pydantic>=2.0.0
12
+ Requires-Dist: python-dateutil>=2.8.0
13
+ Provides-Extra: dev
14
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
15
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
16
+ Requires-Dist: pytest-mock>=3.10.0; extra == "dev"
17
+ Requires-Dist: black>=23.0.0; extra == "dev"
18
+ Requires-Dist: isort>=5.12.0; extra == "dev"
19
+ Requires-Dist: mypy>=1.0.0; extra == "dev"
20
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
21
+ Dynamic: author
22
+ Dynamic: home-page
23
+ Dynamic: requires-python
24
+
25
+ # Agrifrika Shared Package
26
+
27
+ Shared utilities, models, and AWS clients for all Agrifrika microservices.
28
+
29
+ ## Features
30
+
31
+ - **Response Builders**: Standardized API response formatting
32
+ - **Request Parsers**: Lambda event parsing with Pydantic validation
33
+ - **Structured Logging**: JSON-formatted logs for CloudWatch Logs Insights
34
+ - **AWS Clients**: Singleton boto3 clients with retry logic
35
+ - **DynamoDB Client**: High-level DynamoDB operations wrapper
36
+ - **Cognito Client**: User management operations wrapper
37
+ - **Exception Classes**: Custom business and validation exceptions
38
+ - **Validators**: Common validation utilities (email, phone, etc.)
39
+
40
+ ## Installation
41
+
42
+ ### Development Installation (Editable Mode)
43
+
44
+ For local development, install in editable mode so changes are reflected immediately:
45
+
46
+ ```bash
47
+ cd agrifrika-backend/shared
48
+ pip install -e .
49
+ ```
50
+
51
+ ### Production Installation
52
+
53
+ ```bash
54
+ pip install agrifrika-shared
55
+ ```
56
+
57
+ ### With Development Dependencies
58
+
59
+ ```bash
60
+ pip install -e ".[dev]"
61
+ ```
62
+
63
+ ## Usage
64
+
65
+ ### Response Builders
66
+
67
+ ```python
68
+ from agrifrika_shared.utils.response_builder import success_response, error_response
69
+
70
+ # Success response
71
+ return success_response({"user_id": "123"}, "User created successfully", 201)
72
+
73
+ # Error response
74
+ return error_response("User not found", 404, error_code="USER_NOT_FOUND")
75
+ ```
76
+
77
+ ### Request Parsing
78
+
79
+ ```python
80
+ from agrifrika_shared.utils.request_parser import parse_lambda_event
81
+ from pydantic import BaseModel
82
+
83
+ class CreateUserRequest(BaseModel):
84
+ email: str
85
+ name: str
86
+
87
+ def handler(event, context):
88
+ # Automatically parse and validate
89
+ request = parse_lambda_event(event, CreateUserRequest)
90
+ print(request.email) # Validated email
91
+ ```
92
+
93
+ ### Structured Logging
94
+
95
+ ```python
96
+ from agrifrika_shared.utils.logger import get_logger
97
+
98
+ logger = get_logger(__name__)
99
+
100
+ logger.info("user_created", user_id="123", email="test@example.com")
101
+ # Output: {"timestamp": "2025-01-15T10:30:00Z", "level": "INFO", "message": "user_created", ...}
102
+ ```
103
+
104
+ ### AWS Clients
105
+
106
+ ```python
107
+ from agrifrika_shared.aws.clients import get_dynamodb_resource, get_cognito_client
108
+
109
+ # DynamoDB
110
+ dynamodb = get_dynamodb_resource()
111
+ table = dynamodb.Table('users')
112
+
113
+ # Cognito
114
+ cognito = get_cognito_client()
115
+ response = cognito.admin_get_user(UserPoolId='...', Username='...')
116
+ ```
117
+
118
+ ### DynamoDB Client
119
+
120
+ ```python
121
+ from agrifrika_shared.aws.dynamo_client import DynamoDBClient
122
+
123
+ client = DynamoDBClient()
124
+
125
+ # Put item
126
+ client.put_item('users', {'id': '123', 'name': 'John'})
127
+
128
+ # Get item
129
+ user = client.get_item('users', {'id': '123'})
130
+
131
+ # Query
132
+ from boto3.dynamodb.conditions import Key
133
+ result = client.query('users', Key('status').eq('active'), index_name='StatusIndex')
134
+ ```
135
+
136
+ ### Cognito Client
137
+
138
+ ```python
139
+ from agrifrika_shared.aws.cognito_client import CognitoClient
140
+
141
+ client = CognitoClient()
142
+
143
+ # Create user
144
+ user = client.create_user(
145
+ 'test@example.com',
146
+ 'TempPass123!',
147
+ attributes={'given_name': 'John', 'family_name': 'Doe'}
148
+ )
149
+
150
+ # Add to group
151
+ client.add_user_to_group('test@example.com', 'Admins')
152
+ ```
153
+
154
+ ### Custom Exceptions
155
+
156
+ ```python
157
+ from agrifrika_shared.utils.exceptions import NotFoundError, ValidationError
158
+
159
+ # Raise exceptions
160
+ if not user:
161
+ raise NotFoundError('User', user_id)
162
+
163
+ if not validate_email(email):
164
+ raise ValidationError('Invalid email format', field='email')
165
+ ```
166
+
167
+ ## Project Structure
168
+
169
+ ```
170
+ agrifrika_shared/
171
+ ├── __init__.py
172
+ ├── models/ # Pydantic models (to be added)
173
+ │ ├── __init__.py
174
+ │ ├── base.py
175
+ │ ├── user.py
176
+ │ └── ...
177
+ ├── utils/ # Utility functions
178
+ │ ├── __init__.py
179
+ │ ├── request_parser.py
180
+ │ ├── response_builder.py
181
+ │ ├── logger.py
182
+ │ ├── exceptions.py
183
+ │ └── validators.py
184
+ ├── aws/ # AWS client wrappers
185
+ │ ├── __init__.py
186
+ │ ├── clients.py
187
+ │ ├── dynamo_client.py
188
+ │ └── cognito_client.py
189
+ └── config/ # Configuration
190
+ ├── __init__.py
191
+ └── settings.py
192
+ ```
193
+
194
+ ## Development
195
+
196
+ ### Running Tests
197
+
198
+ ```bash
199
+ pytest
200
+ ```
201
+
202
+ ### Running Tests with Coverage
203
+
204
+ ```bash
205
+ pytest --cov=agrifrika_shared --cov-report=html
206
+ ```
207
+
208
+ ### Code Formatting
209
+
210
+ ```bash
211
+ # Format with black
212
+ black agrifrika_shared/
213
+
214
+ # Sort imports
215
+ isort agrifrika_shared/
216
+
217
+ # Lint with ruff
218
+ ruff check agrifrika_shared/
219
+ ```
220
+
221
+ ### Type Checking
222
+
223
+ ```bash
224
+ mypy agrifrika_shared/ --strict
225
+ ```
226
+
227
+ ## Integration with Services
228
+
229
+ To use the shared package in a service:
230
+
231
+ 1. Add to `requirements.txt`:
232
+ ```
233
+ -e ../../../shared
234
+ boto3==1.28.0
235
+ pydantic==2.5.0
236
+ ```
237
+
238
+ 2. Install dependencies:
239
+ ```bash
240
+ cd services/aggregator_service
241
+ pip install -r requirements.txt
242
+ ```
243
+
244
+ 3. Import and use:
245
+ ```python
246
+ from agrifrika_shared.utils import success_response, parse_lambda_event
247
+ from agrifrika_shared.aws.dynamo_client import DynamoDBClient
248
+ ```
249
+
250
+ ## Environment Variables
251
+
252
+ The shared package uses the following environment variables:
253
+
254
+ - `AWS_REGION`: AWS region (default: us-east-1)
255
+ - `COGNITO_USER_POOL_ID`: Cognito user pool ID (required for CognitoClient)
256
+ - `LOG_LEVEL`: Logging level (default: INFO)
257
+
258
+ ## Contributing
259
+
260
+ 1. Make changes to the shared package
261
+ 2. Add tests for new functionality
262
+ 3. Run tests and linting
263
+ 4. Update this README if adding new features
264
+
265
+ ## License
266
+
267
+ MIT
@@ -0,0 +1,243 @@
1
+ # Agrifrika Shared Package
2
+
3
+ Shared utilities, models, and AWS clients for all Agrifrika microservices.
4
+
5
+ ## Features
6
+
7
+ - **Response Builders**: Standardized API response formatting
8
+ - **Request Parsers**: Lambda event parsing with Pydantic validation
9
+ - **Structured Logging**: JSON-formatted logs for CloudWatch Logs Insights
10
+ - **AWS Clients**: Singleton boto3 clients with retry logic
11
+ - **DynamoDB Client**: High-level DynamoDB operations wrapper
12
+ - **Cognito Client**: User management operations wrapper
13
+ - **Exception Classes**: Custom business and validation exceptions
14
+ - **Validators**: Common validation utilities (email, phone, etc.)
15
+
16
+ ## Installation
17
+
18
+ ### Development Installation (Editable Mode)
19
+
20
+ For local development, install in editable mode so changes are reflected immediately:
21
+
22
+ ```bash
23
+ cd agrifrika-backend/shared
24
+ pip install -e .
25
+ ```
26
+
27
+ ### Production Installation
28
+
29
+ ```bash
30
+ pip install agrifrika-shared
31
+ ```
32
+
33
+ ### With Development Dependencies
34
+
35
+ ```bash
36
+ pip install -e ".[dev]"
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### Response Builders
42
+
43
+ ```python
44
+ from agrifrika_shared.utils.response_builder import success_response, error_response
45
+
46
+ # Success response
47
+ return success_response({"user_id": "123"}, "User created successfully", 201)
48
+
49
+ # Error response
50
+ return error_response("User not found", 404, error_code="USER_NOT_FOUND")
51
+ ```
52
+
53
+ ### Request Parsing
54
+
55
+ ```python
56
+ from agrifrika_shared.utils.request_parser import parse_lambda_event
57
+ from pydantic import BaseModel
58
+
59
+ class CreateUserRequest(BaseModel):
60
+ email: str
61
+ name: str
62
+
63
+ def handler(event, context):
64
+ # Automatically parse and validate
65
+ request = parse_lambda_event(event, CreateUserRequest)
66
+ print(request.email) # Validated email
67
+ ```
68
+
69
+ ### Structured Logging
70
+
71
+ ```python
72
+ from agrifrika_shared.utils.logger import get_logger
73
+
74
+ logger = get_logger(__name__)
75
+
76
+ logger.info("user_created", user_id="123", email="test@example.com")
77
+ # Output: {"timestamp": "2025-01-15T10:30:00Z", "level": "INFO", "message": "user_created", ...}
78
+ ```
79
+
80
+ ### AWS Clients
81
+
82
+ ```python
83
+ from agrifrika_shared.aws.clients import get_dynamodb_resource, get_cognito_client
84
+
85
+ # DynamoDB
86
+ dynamodb = get_dynamodb_resource()
87
+ table = dynamodb.Table('users')
88
+
89
+ # Cognito
90
+ cognito = get_cognito_client()
91
+ response = cognito.admin_get_user(UserPoolId='...', Username='...')
92
+ ```
93
+
94
+ ### DynamoDB Client
95
+
96
+ ```python
97
+ from agrifrika_shared.aws.dynamo_client import DynamoDBClient
98
+
99
+ client = DynamoDBClient()
100
+
101
+ # Put item
102
+ client.put_item('users', {'id': '123', 'name': 'John'})
103
+
104
+ # Get item
105
+ user = client.get_item('users', {'id': '123'})
106
+
107
+ # Query
108
+ from boto3.dynamodb.conditions import Key
109
+ result = client.query('users', Key('status').eq('active'), index_name='StatusIndex')
110
+ ```
111
+
112
+ ### Cognito Client
113
+
114
+ ```python
115
+ from agrifrika_shared.aws.cognito_client import CognitoClient
116
+
117
+ client = CognitoClient()
118
+
119
+ # Create user
120
+ user = client.create_user(
121
+ 'test@example.com',
122
+ 'TempPass123!',
123
+ attributes={'given_name': 'John', 'family_name': 'Doe'}
124
+ )
125
+
126
+ # Add to group
127
+ client.add_user_to_group('test@example.com', 'Admins')
128
+ ```
129
+
130
+ ### Custom Exceptions
131
+
132
+ ```python
133
+ from agrifrika_shared.utils.exceptions import NotFoundError, ValidationError
134
+
135
+ # Raise exceptions
136
+ if not user:
137
+ raise NotFoundError('User', user_id)
138
+
139
+ if not validate_email(email):
140
+ raise ValidationError('Invalid email format', field='email')
141
+ ```
142
+
143
+ ## Project Structure
144
+
145
+ ```
146
+ agrifrika_shared/
147
+ ├── __init__.py
148
+ ├── models/ # Pydantic models (to be added)
149
+ │ ├── __init__.py
150
+ │ ├── base.py
151
+ │ ├── user.py
152
+ │ └── ...
153
+ ├── utils/ # Utility functions
154
+ │ ├── __init__.py
155
+ │ ├── request_parser.py
156
+ │ ├── response_builder.py
157
+ │ ├── logger.py
158
+ │ ├── exceptions.py
159
+ │ └── validators.py
160
+ ├── aws/ # AWS client wrappers
161
+ │ ├── __init__.py
162
+ │ ├── clients.py
163
+ │ ├── dynamo_client.py
164
+ │ └── cognito_client.py
165
+ └── config/ # Configuration
166
+ ├── __init__.py
167
+ └── settings.py
168
+ ```
169
+
170
+ ## Development
171
+
172
+ ### Running Tests
173
+
174
+ ```bash
175
+ pytest
176
+ ```
177
+
178
+ ### Running Tests with Coverage
179
+
180
+ ```bash
181
+ pytest --cov=agrifrika_shared --cov-report=html
182
+ ```
183
+
184
+ ### Code Formatting
185
+
186
+ ```bash
187
+ # Format with black
188
+ black agrifrika_shared/
189
+
190
+ # Sort imports
191
+ isort agrifrika_shared/
192
+
193
+ # Lint with ruff
194
+ ruff check agrifrika_shared/
195
+ ```
196
+
197
+ ### Type Checking
198
+
199
+ ```bash
200
+ mypy agrifrika_shared/ --strict
201
+ ```
202
+
203
+ ## Integration with Services
204
+
205
+ To use the shared package in a service:
206
+
207
+ 1. Add to `requirements.txt`:
208
+ ```
209
+ -e ../../../shared
210
+ boto3==1.28.0
211
+ pydantic==2.5.0
212
+ ```
213
+
214
+ 2. Install dependencies:
215
+ ```bash
216
+ cd services/aggregator_service
217
+ pip install -r requirements.txt
218
+ ```
219
+
220
+ 3. Import and use:
221
+ ```python
222
+ from agrifrika_shared.utils import success_response, parse_lambda_event
223
+ from agrifrika_shared.aws.dynamo_client import DynamoDBClient
224
+ ```
225
+
226
+ ## Environment Variables
227
+
228
+ The shared package uses the following environment variables:
229
+
230
+ - `AWS_REGION`: AWS region (default: us-east-1)
231
+ - `COGNITO_USER_POOL_ID`: Cognito user pool ID (required for CognitoClient)
232
+ - `LOG_LEVEL`: Logging level (default: INFO)
233
+
234
+ ## Contributing
235
+
236
+ 1. Make changes to the shared package
237
+ 2. Add tests for new functionality
238
+ 3. Run tests and linting
239
+ 4. Update this README if adding new features
240
+
241
+ ## License
242
+
243
+ MIT
@@ -0,0 +1,7 @@
1
+ """
2
+ Agrifrika Shared Package
3
+
4
+ Shared utilities, models, and AWS clients for all Agrifrika microservices.
5
+ """
6
+
7
+ __version__ = "0.2.0"
@@ -0,0 +1,38 @@
1
+ """
2
+ AWS client wrappers with retry logic.
3
+ """
4
+
5
+ from .clients import (
6
+ get_boto3_config,
7
+ get_region,
8
+ get_dynamodb_client,
9
+ get_dynamodb_resource,
10
+ get_cognito_client,
11
+ get_s3_client,
12
+ get_ses_client,
13
+ get_sns_client,
14
+ get_lambda_client,
15
+ get_secrets_client,
16
+ clear_clients,
17
+ )
18
+
19
+ from .dynamo_client import DynamoDBClient
20
+ from .cognito_client import CognitoClient
21
+
22
+ __all__ = [
23
+ # Client factory
24
+ "get_boto3_config",
25
+ "get_region",
26
+ "get_dynamodb_client",
27
+ "get_dynamodb_resource",
28
+ "get_cognito_client",
29
+ "get_s3_client",
30
+ "get_ses_client",
31
+ "get_sns_client",
32
+ "get_lambda_client",
33
+ "get_secrets_client",
34
+ "clear_clients",
35
+ # Client wrappers
36
+ "DynamoDBClient",
37
+ "CognitoClient",
38
+ ]