Skip to content

Commit 88cb308

Browse files
committed
add subreddit_id to event_dict & new identifiers to DeciderContext, fix type T
1 parent 6581e0b commit 88cb308

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

reddit_decider/__init__.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from baseplate.lib.events import DebugLogger
2121
from baseplate.lib.events import EventLogger
2222
from baseplate.lib.file_watcher import FileWatcher
23-
from baseplate.lib.file_watcher import T
2423
from baseplate.lib.file_watcher import WatchedFileNotAvailableError
2524
from reddit_edgecontext import ValidatedAuthenticationToken
2625
from rust_decider import Decider as RustDecider
@@ -62,33 +61,53 @@ class ExperimentConfig:
6261

6362

6463
class DeciderContext:
65-
"""DeciderContext() is used to contain all fields necessary for
64+
""":code:`DeciderContext` is used to contain all fields necessary for
6665
bucketing, targeting, and overrides.
67-
:code:`DeciderContext()` is populated in :code:`make_object_for_context()`.
66+
:code:`DeciderContext` is populated in :code:`make_object_for_context()`
67+
68+
:param user_id: user's t2 id
69+
:param device_id: device installation uuid
70+
:param canonical_url: an url string
71+
:param subreddit_id: subreddit link's t3 identifier
72+
:param ad_account_id: an ad_account id string identifier
73+
:param business_id: business id identifier used by ads
74+
:param country_code: 2-letter country codes
75+
:param locale: ISO 639-1 primary language subtag and an optional ISO 3166-1 alpha-2 region subtag
76+
:param user_is_employee:
77+
:param logged_in: is user logged in
78+
:param oauth_client_id: OAuth Client ID
79+
:param origin_service: Service where request originated
80+
:param cookie_created_timestamp: When the authentication cookie was created
81+
:param loid_created_timestamp: Epoch milliseconds when the current LoID cookie was created
82+
:param extracted_fields: Optional dict of additional fields, e.g. app_name & build_number
6883
"""
6984

70-
T = TypeVar("T")
71-
7285
def __init__(
7386
self,
7487
user_id: Optional[str] = None,
88+
device_id: Optional[str] = None,
89+
subreddit_id: Optional[str] = None,
90+
ad_account_id: Optional[str] = None,
91+
business_id: Optional[str] = None,
7592
country_code: Optional[str] = None,
7693
locale: Optional[str] = None,
7794
user_is_employee: Optional[bool] = None,
7895
logged_in: Optional[bool] = None,
79-
device_id: Optional[str] = None,
8096
oauth_client_id: Optional[str] = None,
8197
origin_service: Optional[str] = None,
8298
cookie_created_timestamp: Optional[float] = None,
8399
loid_created_timestamp: Optional[float] = None,
84100
extracted_fields: Optional[dict] = None,
85101
):
86102
self._user_id = user_id
103+
self._device_id = device_id
104+
self._subreddit_id = subreddit_id
105+
self._ad_account_id = ad_account_id
106+
self._business_id = business_id
87107
self._country_code = country_code
88108
self._locale = locale
89109
self._user_is_employee = user_is_employee
90110
self._logged_in = logged_in
91-
self._device_id = device_id
92111
self._oauth_client_id = oauth_client_id
93112
self._origin_service = origin_service
94113
self._cookie_created_timestamp = cookie_created_timestamp
@@ -100,11 +119,14 @@ def to_dict(self) -> Dict:
100119

101120
return {
102121
"user_id": self._user_id,
122+
"device_id": self._device_id,
123+
"subreddit_id": self._subreddit_id,
124+
"ad_account_id": self._ad_account_id,
125+
"business_id": self._business_id,
103126
"country_code": self._country_code,
104127
"locale": self._locale,
105128
"user_is_employee": self._user_is_employee,
106129
"logged_in": self._logged_in,
107-
"device_id": self._device_id,
108130
"oauth_client_id": self._oauth_client_id,
109131
"origin_service": self._origin_service,
110132
"cookie_created_timestamp": self._cookie_created_timestamp,
@@ -145,6 +167,10 @@ def to_event_dict(self) -> Dict:
145167
if self._device_id:
146168
platform_fields["device_id"] = self._device_id
147169

170+
subreddit_fields = {}
171+
if self._subreddit:
172+
subreddit_fields["id"] = self._subreddit
173+
148174
return {
149175
"user_id": self._user_id,
150176
"country_code": self._country_code,
@@ -159,6 +185,7 @@ def to_event_dict(self) -> Dict:
159185
"geo": geo_fields,
160186
"request": request_fields,
161187
"platform": platform_fields,
188+
"subreddit": subreddit_fields,
162189
**ef,
163190
}
164191

@@ -175,6 +202,8 @@ class Decider:
175202
It will automatically reload the cache when changed.
176203
"""
177204

205+
T = TypeVar("T")
206+
178207
def __init__(
179208
self,
180209
decider_context: DeciderContext,

tests/decider_tests.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import logging
44
import tempfile
55
import unittest
6-
import warnings
76

87
from unittest import mock
98

109
from baseplate import RequestContext
1110
from baseplate import ServerSpan
1211
from baseplate.lib.events import DebugLogger
13-
from baseplate.lib.file_watcher import FileWatcher
1412
from reddit_edgecontext import ValidatedAuthenticationToken
1513

1614
from reddit_decider import Decider
@@ -1409,7 +1407,7 @@ def test_get_all_variants_for_identifier_without_expose_bogus_identifier_type(se
14091407

14101408
with self.assertLogs() as captured:
14111409
variant_arr = decider.get_all_variants_for_identifier_without_expose(
1412-
identifier=identifier, identifier_type="blah"
1410+
identifier=identifier, identifier_type=identifier_type
14131411
)
14141412

14151413
self.assertEqual(len(variant_arr), 0)
@@ -1497,7 +1495,7 @@ def test_get_variant_without_expose_with_HG_as_control_1_and_child_returns_none_
14971495
self.assertEqual(self.event_logger.log.call_count, 0)
14981496
variant = decider.get_variant_without_expose("exp_1")
14991497

1500-
assert variant == None
1498+
assert variant is None
15011499

15021500
# exposure from control_1 of "hg"
15031501
self.assertEqual(self.event_logger.log.call_count, 1)

0 commit comments

Comments
 (0)