Skip to content

Commit 6764c78

Browse files
havenbarnesgreptile-apps[bot]marandaneto
authored
feat(flags): Add method for fetching decrypted remote config flag payload (#180)
* feat(flags): Add method for fetching decrypted remote config flag payload * tweak * tweak * tweak * Update posthog/__init__.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * tweak * get example script working * format * sort import * tweak * bump minor version * Update posthog/version.py Co-authored-by: Manoel Aranda Neto <[email protected]> * Use flag key instead of id * tweak * tweak --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Co-authored-by: Manoel Aranda Neto <[email protected]>
1 parent 1b57a96 commit 6764c78

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

Diff for: example.py

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"distinct_id_random_22", person_properties={"$geoip_city_name": "Sydney"}, only_evaluate_locally=True
102102
)
103103
)
104+
print(posthog.get_remote_config_payload("encrypted_payload_flag_key"))
104105

105106

106107
posthog.shutdown()

Diff for: posthog/__init__.py

+20
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ def get_feature_flag_payload(
492492
)
493493

494494

495+
def get_remote_config_payload(
496+
key, # type: str
497+
):
498+
"""Get the payload for a remote config feature flag.
499+
500+
Args:
501+
key: The key of the feature flag
502+
503+
Returns:
504+
The payload associated with the feature flag. If payload is encrypted, the return value will decrypted
505+
506+
Note:
507+
Requires personal_api_key to be set for authentication
508+
"""
509+
return _proxy(
510+
"get_remote_config_payload",
511+
key=key,
512+
)
513+
514+
495515
def get_all_flags_and_payloads(
496516
distinct_id,
497517
groups={},

Diff for: posthog/client.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from posthog.exception_utils import exc_info_from_error, exceptions_from_error_tuple, handle_in_app
1616
from posthog.feature_flags import InconclusiveMatchError, match_feature_flag_properties
1717
from posthog.poller import Poller
18-
from posthog.request import DEFAULT_HOST, APIError, batch_post, decide, determine_server_host, get
18+
from posthog.request import DEFAULT_HOST, APIError, batch_post, decide, determine_server_host, get, remote_config
1919
from posthog.utils import SizeLimitedDict, clean, guess_timezone, remove_trailing_slash
2020
from posthog.version import VERSION
2121

@@ -849,6 +849,26 @@ def get_feature_flag_payload(
849849

850850
return payload
851851

852+
def get_remote_config_payload(self, key: str):
853+
if self.disabled:
854+
return None
855+
856+
if self.personal_api_key is None:
857+
self.log.warning(
858+
"[FEATURE FLAGS] You have to specify a personal_api_key to fetch decrypted feature flag payloads."
859+
)
860+
return None
861+
862+
try:
863+
return remote_config(
864+
self.personal_api_key,
865+
self.host,
866+
key,
867+
timeout=self.feature_flags_request_timeout_seconds,
868+
)
869+
except Exception as e:
870+
self.log.exception(f"[FEATURE FLAGS] Unable to get decrypted feature flag payload: {e}")
871+
852872
def _compute_payload_locally(self, key, match_value):
853873
payload = None
854874

Diff for: posthog/request.py

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ def decide(api_key: str, host: Optional[str] = None, gzip: bool = False, timeout
8383
return _process_response(res, success_message="Feature flags decided successfully")
8484

8585

86+
def remote_config(personal_api_key: str, host: Optional[str] = None, key: str = "", timeout: int = 15) -> Any:
87+
"""Get remote config flag value from remote_config API endpoint"""
88+
return get(personal_api_key, f"/api/projects/@current/feature_flags/{key}/remote_config/", host, timeout)
89+
90+
8691
def batch_post(
8792
api_key: str, host: Optional[str] = None, gzip: bool = False, timeout: int = 15, **kwargs
8893
) -> requests.Response:

Diff for: posthog/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "3.13.0"
1+
VERSION = "3.14.0"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)