reach_commons 0.18.35__tar.gz → 0.18.36__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.35 → reach_commons-0.18.36}/PKG-INFO +2 -4
  2. {reach_commons-0.18.35 → reach_commons-0.18.36}/README.md +25 -25
  3. {reach_commons-0.18.35 → reach_commons-0.18.36}/pyproject.toml +40 -40
  4. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/app_logging/logging_utils.py +17 -17
  5. reach_commons-0.18.36/reach_commons/mongo/mongo_token_bucket.py +108 -0
  6. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/redis_manager.py +11 -0
  7. reach_commons-0.18.35/reach_commons/.DS_Store +0 -0
  8. reach_commons-0.18.35/reach_commons/clients/.DS_Store +0 -0
  9. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/__init__.py +0 -0
  10. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/app_logging/__init__.py +0 -0
  11. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/app_logging/http_logger.py +0 -0
  12. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/app_logging/log_deprecated_endpoints.py +0 -0
  13. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/app_logging/logger.py +0 -0
  14. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/app_logging/logging_config.py +0 -0
  15. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/clients/__init__.py +0 -0
  16. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/clients/event_processor.py +0 -0
  17. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/clients/hubspot.py +0 -0
  18. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/clients/outscraper.py +0 -0
  19. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/clients/reach_data_bridge.py +0 -0
  20. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/clients/reach_ops_api.py +0 -0
  21. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/mongo/__init__.py +0 -0
  22. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/mongo/customer_persistence.py +0 -0
  23. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/mongo/customer_persistence_async.py +0 -0
  24. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/mongo/validation/__init__.py +0 -0
  25. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/reach_aws/__init__.py +0 -0
  26. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/reach_aws/commons.py +0 -0
  27. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/reach_aws/dynamo_db.py +0 -0
  28. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/reach_aws/exceptions.py +0 -0
  29. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/reach_aws/firehose.py +0 -0
  30. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/reach_aws/kms.py +0 -0
  31. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/reach_aws/s3.py +0 -0
  32. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/reach_aws/sqs.py +0 -0
  33. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/reach_base_model.py +0 -0
  34. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/sms_smart_encoding.py +0 -0
  35. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/utils.py +0 -0
  36. {reach_commons-0.18.35 → reach_commons-0.18.36}/reach_commons/validations.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.1
2
2
  Name: reach_commons
3
- Version: 0.18.35
3
+ Version: 0.18.36
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
@@ -14,8 +14,6 @@ Classifier: Programming Language :: Python :: 3.9
14
14
  Classifier: Programming Language :: Python :: 3.10
15
15
  Classifier: Programming Language :: Python :: 3.11
16
16
  Classifier: Programming Language :: Python :: 3.12
17
- Classifier: Programming Language :: Python :: 3.13
18
- Classifier: Programming Language :: Python :: 3.14
19
17
  Requires-Dist: curlify (==3.0.0)
20
18
  Requires-Dist: fastapi (>=0.115.5)
21
19
  Requires-Dist: pydantic (>=2.9.2)
@@ -1,26 +1,26 @@
1
- # reach_commons
2
-
3
- ## Description
4
-
5
- `reach_commons` is a Python library that provides various classes and utilities to assist developers in their projects. This library aims to simplify and streamline processes by offering reusable components that can be easily integrated into various Python applications.
6
-
7
- ## Installation
8
-
9
- Install `reach_commons` using pip:
10
-
11
- ```bash
12
- pip install reach_commons
13
-
14
- from reach_commons.api_client_v2 import ReachApiGatewayV2
15
-
16
- api_gateway = ReachApiGatewayV2(
17
- base_url=os.environ["api_gateway_v2_endpoint"],
18
- access_token=os.environ["event_processor_token"],
19
- )
20
- response = api_gateway.stripe_create_booking_guarantee(
21
- business_id=business_id, booking_price=booking_price
22
- )
23
- {
24
- "statusCode": stripe_customer.status_code,
25
- "body": json.dumps(response.json()),
1
+ # reach_commons
2
+
3
+ ## Description
4
+
5
+ `reach_commons` is a Python library that provides various classes and utilities to assist developers in their projects. This library aims to simplify and streamline processes by offering reusable components that can be easily integrated into various Python applications.
6
+
7
+ ## Installation
8
+
9
+ Install `reach_commons` using pip:
10
+
11
+ ```bash
12
+ pip install reach_commons
13
+
14
+ from reach_commons.api_client_v2 import ReachApiGatewayV2
15
+
16
+ api_gateway = ReachApiGatewayV2(
17
+ base_url=os.environ["api_gateway_v2_endpoint"],
18
+ access_token=os.environ["event_processor_token"],
19
+ )
20
+ response = api_gateway.stripe_create_booking_guarantee(
21
+ business_id=business_id, booking_price=booking_price
22
+ )
23
+ {
24
+ "statusCode": stripe_customer.status_code,
25
+ "body": json.dumps(response.json()),
26
26
  }
@@ -1,41 +1,41 @@
1
- # isort .; black .; poetry build; poetry publish
2
- [tool.poetry]
3
- name = "reach_commons"
4
- version = "0.18.35"
5
- description = "Reach Commons is a versatile utility library designed to streamline and enhance development workflows within the Reach ecosystem."
6
- authors = ["Engineering <engineering@getreach.ai>"]
7
- license = "MIT"
8
- readme = "README.md"
9
-
10
- classifiers = [
11
- "Programming Language :: Python :: 3",
12
- "License :: OSI Approved :: MIT License",
13
- "Operating System :: OS Independent",
14
- ]
15
-
16
- [tool.poetry.dependencies]
17
- python = "^3.8"
18
- #pip = "23.3.1"
19
- setuptools = "70.0.0"
20
- wheel = "0.41.2"
21
- twine = "4.0.2"
22
- curlify = "3.0.0"
23
- pydantic = ">=2.9.2"
24
- fastapi = ">=0.115.5"
25
-
26
-
27
- [tool.poetry.dev-dependencies]
28
- black = "23.10.0"
29
- isort = "5.12.0"
30
- requests = "2.31.0"
31
- boto3 = "1.28.68"
32
- botocore = "^1.31.68"
33
- redis = "^4.1.3"
34
- pydantic-settings = "2.3.1"
35
- jsonschema = "4.23.0"
36
- motor = "3.6.0"
37
- pymongo = "4.9.2"
38
-
39
- [build-system]
40
- requires = ["poetry-core>=1.0.0"]
1
+ # isort .; black .; poetry build; poetry publish
2
+ [tool.poetry]
3
+ name = "reach_commons"
4
+ version = "0.18.36"
5
+ description = "Reach Commons is a versatile utility library designed to streamline and enhance development workflows within the Reach ecosystem."
6
+ authors = ["Engineering <engineering@getreach.ai>"]
7
+ license = "MIT"
8
+ readme = "README.md"
9
+
10
+ classifiers = [
11
+ "Programming Language :: Python :: 3",
12
+ "License :: OSI Approved :: MIT License",
13
+ "Operating System :: OS Independent",
14
+ ]
15
+
16
+ [tool.poetry.dependencies]
17
+ python = "^3.8"
18
+ #pip = "23.3.1"
19
+ setuptools = "70.0.0"
20
+ wheel = "0.41.2"
21
+ twine = "4.0.2"
22
+ curlify = "3.0.0"
23
+ pydantic = ">=2.9.2"
24
+ fastapi = ">=0.115.5"
25
+
26
+
27
+ [tool.poetry.dev-dependencies]
28
+ black = "23.10.0"
29
+ isort = "5.12.0"
30
+ requests = "2.31.0"
31
+ boto3 = "1.28.68"
32
+ botocore = "^1.31.68"
33
+ redis = "^4.1.3"
34
+ pydantic-settings = "2.3.1"
35
+ jsonschema = "4.23.0"
36
+ motor = "3.6.0"
37
+ pymongo = "4.9.2"
38
+
39
+ [build-system]
40
+ requires = ["poetry-core>=1.0.0"]
41
41
  build-backend = "poetry.core.masonry.api"
@@ -1,17 +1,17 @@
1
- import logging
2
- import os
3
-
4
-
5
- def init_logger(name: str):
6
- logging.basicConfig(
7
- level=getattr(logging, os.getenv("LOG_LEVEL", "INFO").upper(), logging.INFO),
8
- format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
9
- force=True,
10
- )
11
- for noisy in ("botocore", "boto3", "urllib3"):
12
- logging.getLogger(noisy).setLevel(logging.WARNING)
13
- return logging.getLogger(name)
14
-
15
-
16
- def log_with_event(level_fn, msg, event):
17
- level_fn(f"{msg} | event={event}")
1
+ import logging
2
+ import os
3
+
4
+
5
+ def init_logger(name: str):
6
+ logging.basicConfig(
7
+ level=getattr(logging, os.getenv("LOG_LEVEL", "INFO").upper(), logging.INFO),
8
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
9
+ force=True,
10
+ )
11
+ for noisy in ("botocore", "boto3", "urllib3"):
12
+ logging.getLogger(noisy).setLevel(logging.WARNING)
13
+ return logging.getLogger(name)
14
+
15
+
16
+ def log_with_event(level_fn, msg, event):
17
+ level_fn(f"{msg} | event={event}")
@@ -0,0 +1,108 @@
1
+ # mongo_token_bucket.py
2
+ import random
3
+ import time
4
+ from dataclasses import dataclass
5
+ from typing import Optional, Tuple
6
+
7
+ _LUA_WINDOW_LIMITER = """
8
+ -- KEYS[1] = window key
9
+ -- ARGV[1] = tokens_to_consume
10
+ -- ARGV[2] = ttl_seconds
11
+ -- ARGV[3] = limit
12
+
13
+ local tokens = tonumber(ARGV[1])
14
+ local ttl = tonumber(ARGV[2])
15
+ local limit = tonumber(ARGV[3])
16
+
17
+ local current = redis.call('INCRBY', KEYS[1], tokens)
18
+
19
+ -- If this was the first increment (key was 0 / nonexistent), set TTL
20
+ if current == tokens then
21
+ redis.call('EXPIRE', KEYS[1], ttl)
22
+ end
23
+
24
+ if current <= limit then
25
+ return 1
26
+ else
27
+ return 0
28
+ end
29
+ """
30
+
31
+
32
+ @dataclass(frozen=True)
33
+ class AcquireResult:
34
+ allowed: bool
35
+ retry_after_seconds: int # for visibility timeout / delay
36
+
37
+
38
+ class MongoTokenBucketManager:
39
+ """
40
+ Window-based limiter:
41
+ - limit tokens per interval_seconds
42
+ - atomic via Redis Lua
43
+ """
44
+
45
+ def __init__(
46
+ self,
47
+ redis_manager,
48
+ limit_per_window: int,
49
+ interval_seconds: int = 2,
50
+ bucket_key: str = "global",
51
+ jitter_seconds: Optional[int] = None,
52
+ key_prefix: str = "mongo_write_budget",
53
+ ):
54
+ if interval_seconds <= 0:
55
+ raise ValueError("interval_seconds must be > 0")
56
+ if limit_per_window <= 0:
57
+ raise ValueError("limit_per_window must be > 0")
58
+
59
+ self.redis = redis_manager
60
+ self.limit = int(limit_per_window)
61
+ self.interval = int(interval_seconds)
62
+ self.bucket_key = bucket_key
63
+ self.key_prefix = key_prefix
64
+ self.jitter = (
65
+ int(jitter_seconds) if jitter_seconds is not None else self.interval
66
+ ) # default: 0..interval
67
+
68
+ # Cache the script SHA if you want; eval is fine for now (2-day fix).
69
+ self._lua = _LUA_WINDOW_LIMITER
70
+
71
+ def _now(self) -> float:
72
+ return time.time()
73
+
74
+ def _window_start(self, now: float) -> int:
75
+ return int(now // self.interval) * self.interval
76
+
77
+ def _redis_key(self, window_start: int) -> str:
78
+ return f"{self.key_prefix}:{self.bucket_key}:{window_start}"
79
+
80
+ def acquire(self, tokens: int = 1) -> AcquireResult:
81
+ """
82
+ Try to consume tokens. If denied, returns retry_after_seconds to push visibility timeout.
83
+ """
84
+ now = self._now()
85
+ window_start = self._window_start(now)
86
+ window_end = window_start + self.interval
87
+
88
+ key = self._redis_key(window_start)
89
+
90
+ # TTL a bit bigger than the window so old keys go away safely
91
+ ttl_seconds = max(self.interval * 2, 5)
92
+
93
+ allowed = self.redis.eval(
94
+ self._lua,
95
+ numkeys=1,
96
+ keys=[key],
97
+ args=[str(int(tokens)), str(int(ttl_seconds)), str(int(self.limit))],
98
+ )
99
+
100
+ if allowed == 1:
101
+ return AcquireResult(allowed=True, retry_after_seconds=0)
102
+
103
+ # Denied: retry after next window, plus jitter to avoid waves
104
+ base = max(0.0, window_end - now) # seconds until next window
105
+ jitter = random.uniform(0.0, float(self.jitter))
106
+ retry_after = int(max(1.0, base + jitter)) # at least 1s
107
+
108
+ return AcquireResult(allowed=False, retry_after_seconds=retry_after)
@@ -80,3 +80,14 @@ class RedisManager:
80
80
  name=key, value="1", ex=expire_seconds, nx=True
81
81
  )
82
82
  return result is True
83
+
84
+ def incrby(self, key: str, amount: int = 1) -> int:
85
+ return int(self.redis_connection.incrby(key, amount))
86
+
87
+ def expire(self, key: str, seconds: int) -> bool:
88
+ return bool(self.redis_connection.expire(key, seconds))
89
+
90
+ def eval(self, script: str, numkeys: int, keys=None, args=None):
91
+ keys = keys or []
92
+ args = args or []
93
+ return self.redis_connection.eval(script, numkeys, *(keys + args))