reach_commons 0.18.37__tar.gz → 0.18.39__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 (36) hide show
  1. {reach_commons-0.18.37 → reach_commons-0.18.39}/PKG-INFO +1 -1
  2. {reach_commons-0.18.37 → reach_commons-0.18.39}/pyproject.toml +1 -1
  3. reach_commons-0.18.39/reach_commons/reach_aws/__init__.py +3 -0
  4. reach_commons-0.18.39/reach_commons/reach_aws/db_config.py +87 -0
  5. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/reach_aws/reach_rate_limiter.py +17 -3
  6. reach_commons-0.18.37/reach_commons/reach_aws/__init__.py +0 -0
  7. {reach_commons-0.18.37 → reach_commons-0.18.39}/README.md +0 -0
  8. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/__init__.py +0 -0
  9. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/app_logging/__init__.py +0 -0
  10. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/app_logging/http_logger.py +0 -0
  11. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/app_logging/log_deprecated_endpoints.py +0 -0
  12. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/app_logging/logger.py +0 -0
  13. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/app_logging/logging_config.py +0 -0
  14. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/app_logging/logging_utils.py +0 -0
  15. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/clients/__init__.py +0 -0
  16. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/clients/event_processor.py +0 -0
  17. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/clients/hubspot.py +0 -0
  18. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/clients/outscraper.py +0 -0
  19. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/clients/reach_data_bridge.py +0 -0
  20. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/clients/reach_ops_api.py +0 -0
  21. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/mongo/__init__.py +0 -0
  22. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/mongo/customer_persistence.py +0 -0
  23. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/mongo/customer_persistence_async.py +0 -0
  24. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/mongo/validation/__init__.py +0 -0
  25. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/reach_aws/commons.py +0 -0
  26. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/reach_aws/dynamo_db.py +0 -0
  27. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/reach_aws/exceptions.py +0 -0
  28. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/reach_aws/firehose.py +0 -0
  29. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/reach_aws/kms.py +0 -0
  30. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/reach_aws/s3.py +0 -0
  31. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/reach_aws/sqs.py +0 -0
  32. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/reach_base_model.py +0 -0
  33. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/redis_manager.py +0 -0
  34. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/sms_smart_encoding.py +0 -0
  35. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/utils.py +0 -0
  36. {reach_commons-0.18.37 → reach_commons-0.18.39}/reach_commons/validations.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: reach_commons
3
- Version: 0.18.37
3
+ Version: 0.18.39
4
4
  Summary: Reach Commons is a versatile utility library designed to streamline and enhance development workflows within the Reach ecosystem.
5
5
  License: MIT
6
6
  Author: Engineering
@@ -1,7 +1,7 @@
1
1
  # isort .; black .; poetry build; poetry publish
2
2
  [tool.poetry]
3
3
  name = "reach_commons"
4
- version = "0.18.37"
4
+ version = "0.18.39"
5
5
  description = "Reach Commons is a versatile utility library designed to streamline and enhance development workflows within the Reach ecosystem."
6
6
  authors = ["Engineering <engineering@getreach.ai>"]
7
7
  license = "MIT"
@@ -0,0 +1,3 @@
1
+ from reach_commons.reach_aws.db_config import get_secret
2
+
3
+ __all__ = ["get_secret"]
@@ -0,0 +1,87 @@
1
+ import base64
2
+ import json
3
+ import os
4
+ from typing import Any, Dict
5
+
6
+ import boto3
7
+ from botocore.exceptions import ClientError
8
+
9
+ ENV = os.environ.get("ENV", "Staging")
10
+
11
+
12
+ def _get_secret_json(secret_arn: str, region_name: str = "us-east-1") -> Dict[str, Any]:
13
+ """Fetch and parse a JSON secret from AWS Secrets Manager."""
14
+ session = boto3.Session(region_name=region_name)
15
+ client = session.client("secretsmanager")
16
+
17
+ try:
18
+ response = client.get_secret_value(SecretId=secret_arn)
19
+ except ClientError as exc:
20
+ raise RuntimeError(
21
+ f"Failed to fetch secret from AWS Secrets Manager: secret_arn={secret_arn}"
22
+ ) from exc
23
+
24
+ secret_string = _extract_secret_string(response, secret_arn)
25
+ try:
26
+ return json.loads(secret_string)
27
+ except json.JSONDecodeError as exc:
28
+ raise ValueError(
29
+ f"Secret value is not valid JSON: secret_arn={secret_arn}"
30
+ ) from exc
31
+
32
+
33
+ def _extract_secret_string(response: Dict[str, Any], secret_arn: str) -> str:
34
+ if response.get("SecretBinary"):
35
+ decoded = base64.b64decode(response["SecretBinary"])
36
+ return decoded.decode("utf-8")
37
+ secret_string = response.get("SecretString")
38
+ if not secret_string:
39
+ raise ValueError(
40
+ f"Secret did not contain SecretString or SecretBinary: secret_arn={secret_arn}"
41
+ )
42
+ return secret_string
43
+
44
+
45
+ def get_secret(
46
+ secret_arn: str,
47
+ region_name: str = "us-east-1",
48
+ host=os.getenv("db_host_proxy"),
49
+ db_name=os.getenv("db_name"),
50
+ ) -> Dict[str, Any]:
51
+ """
52
+ Load DB credentials from AWS Secrets Manager and host from SSM Parameter Store.
53
+
54
+ Example:
55
+ # from reach_commons.reach_aws import get_secret
56
+ # config = get_secret(
57
+ # os.environ[ "RDS_SECRET_ARN"],
58
+ #)
59
+ """
60
+
61
+ if not secret_arn:
62
+ raise ValueError(f"RDS secret ARN is not configured")
63
+ if not host:
64
+ raise ValueError(f"RDS host is not configured")
65
+
66
+ secrets_data = _get_secret_json(secret_arn, region_name)
67
+
68
+ if not isinstance(secrets_data, dict):
69
+ raise ValueError(
70
+ f"Secret payload must be a JSON object: secret_arn={secret_arn}"
71
+ )
72
+
73
+ secrets_data["host"] = host
74
+ secrets_data["dbname"] = db_name
75
+
76
+ missing = [
77
+ key
78
+ for key in ("host", "username", "password", "dbname")
79
+ if key not in secrets_data
80
+ ]
81
+ if missing:
82
+ raise ValueError(
83
+ "Secret is missing required fields: "
84
+ f"missing={missing}, secret_arn={secret_arn}"
85
+ )
86
+
87
+ return secrets_data
@@ -133,7 +133,10 @@ class ReachRateLimiter:
133
133
  Loads config from Redis hash:
134
134
  limit_per_window, interval_seconds, jitter_seconds
135
135
 
136
- Uses short in-memory cache to avoid hammering Redis under high throughput.
136
+ Behavior:
137
+ - If config exists in Redis: read only (never overwrite).
138
+ - If config does NOT exist yet: seed Redis ONCE with defaults (so you can edit live).
139
+ - If Redis is unavailable: fallback to defaults (no writes).
137
140
  """
138
141
  now = self._now()
139
142
 
@@ -149,7 +152,18 @@ class ReachRateLimiter:
149
152
  jitter = self.default_jitter
150
153
 
151
154
  try:
152
- raw = self.redis.hgetall(self._cfg_key()) or {}
155
+ cfg_key = self._cfg_key()
156
+ raw = self.redis.hgetall(cfg_key) or {}
157
+
158
+ # If config was never created, seed it once with defaults
159
+ if not raw:
160
+ rc = self.redis.redis_connection
161
+ rc.hsetnx(cfg_key, "limit_per_window", str(self.default_limit))
162
+ rc.hsetnx(cfg_key, "interval_seconds", str(self.default_interval))
163
+ rc.hsetnx(cfg_key, "jitter_seconds", str(self.default_jitter))
164
+
165
+ # Re-read after seeding (so we now depend on Redis config)
166
+ raw = self.redis.hgetall(cfg_key) or {}
153
167
 
154
168
  # raw typically has bytes keys/values
155
169
  limit = self._parse_int(
@@ -162,7 +176,7 @@ class ReachRateLimiter:
162
176
  raw.get(b"jitter_seconds") or raw.get("jitter_seconds"), jitter
163
177
  )
164
178
 
165
- # Guard rails
179
+ # If someone puts garbage in Redis
166
180
  if limit <= 0:
167
181
  limit = self.default_limit
168
182
  if interval <= 0:
File without changes