From 92db0852d175a0905cb9783bc82ba30725546236 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Thu, 23 Sep 2021 14:05:09 +0530 Subject: [PATCH 1/5] Added headers paramether which can be used to pass custom headers for API calls made to flagsmith --- flagsmith/flagsmith.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/flagsmith/flagsmith.py b/flagsmith/flagsmith.py index 1a23cb8..ecf2d59 100644 --- a/flagsmith/flagsmith.py +++ b/flagsmith/flagsmith.py @@ -11,18 +11,20 @@ class Flagsmith: - def __init__(self, environment_id, api=SERVER_URL): + def __init__(self, environment_id, api=SERVER_URL, headers={}): """ Initialise Flagsmith environment. :param environment_id: environment key obtained from the Flagsmith UI :param api: (optional) api url to override when using self hosted version + :param headers: (optional) headers dictionary which will be passed in headers for each api call made to flagsmith """ self.environment_id = environment_id self.api = api self.flags_endpoint = api + FLAGS_ENDPOINT self.identities_endpoint = api + IDENTITY_ENDPOINT self.traits_endpoint = api + TRAIT_ENDPOINT + self.headers = headers if type(headers) == dict else {} def get_flags(self, identity=None): """ @@ -152,7 +154,7 @@ def set_trait(self, trait_key, trait_value, identity): } requests.post( - self.traits_endpoint, json=payload, headers=self._generate_header_content() + self.traits_endpoint, json=payload, headers=self._generate_header_content(self.headers) ) def _get_flags_response(self, feature_name=None, identity=None): @@ -171,13 +173,13 @@ def _get_flags_response(self, feature_name=None, identity=None): response = requests.get( self.identities_endpoint, params=params, - headers=self._generate_header_content(), + headers=self._generate_header_content(self.headers), ) else: response = requests.get( self.flags_endpoint, params=params, - headers=self._generate_header_content(), + headers=self._generate_header_content(self.headers), ) if response.status_code == 200: From 4818e0fb8f8fa581078b06a640819b2617c62b49 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Thu, 23 Sep 2021 14:38:26 +0530 Subject: [PATCH 2/5] Update docstring comment --- flagsmith/flagsmith.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flagsmith/flagsmith.py b/flagsmith/flagsmith.py index ecf2d59..07dfb36 100644 --- a/flagsmith/flagsmith.py +++ b/flagsmith/flagsmith.py @@ -17,7 +17,7 @@ def __init__(self, environment_id, api=SERVER_URL, headers={}): :param environment_id: environment key obtained from the Flagsmith UI :param api: (optional) api url to override when using self hosted version - :param headers: (optional) headers dictionary which will be passed in headers for each api call made to flagsmith + :param headers: (optional) dict which will be passed in headers for each api call """ self.environment_id = environment_id self.api = api From cac5e048eaefa0cb797e7a23ae24e19348acf435 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Wed, 29 Sep 2021 12:41:27 +0530 Subject: [PATCH 3/5] change headers argument name to custom_headers and avoid mutable defaults --- flagsmith/flagsmith.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/flagsmith/flagsmith.py b/flagsmith/flagsmith.py index 07dfb36..729500c 100644 --- a/flagsmith/flagsmith.py +++ b/flagsmith/flagsmith.py @@ -11,20 +11,20 @@ class Flagsmith: - def __init__(self, environment_id, api=SERVER_URL, headers={}): + def __init__(self, environment_id, api=SERVER_URL, custom_headers=None): """ Initialise Flagsmith environment. :param environment_id: environment key obtained from the Flagsmith UI :param api: (optional) api url to override when using self hosted version - :param headers: (optional) dict which will be passed in headers for each api call + :param custom_headers: (optional) dict which will be passed in headers for each api call """ self.environment_id = environment_id self.api = api self.flags_endpoint = api + FLAGS_ENDPOINT self.identities_endpoint = api + IDENTITY_ENDPOINT self.traits_endpoint = api + TRAIT_ENDPOINT - self.headers = headers if type(headers) == dict else {} + self.custom_headers = custom_headers if custom_headers and type(custom_headers) == dict else {} def get_flags(self, identity=None): """ @@ -154,7 +154,7 @@ def set_trait(self, trait_key, trait_value, identity): } requests.post( - self.traits_endpoint, json=payload, headers=self._generate_header_content(self.headers) + self.traits_endpoint, json=payload, headers=self._generate_header_content(self.custom_headers) ) def _get_flags_response(self, feature_name=None, identity=None): @@ -173,13 +173,13 @@ def _get_flags_response(self, feature_name=None, identity=None): response = requests.get( self.identities_endpoint, params=params, - headers=self._generate_header_content(self.headers), + headers=self._generate_header_content(self.custom_headers), ) else: response = requests.get( self.flags_endpoint, params=params, - headers=self._generate_header_content(self.headers), + headers=self._generate_header_content(self.custom_headers), ) if response.status_code == 200: From 3496e2af71f1a0c7bb6fb69a95c80c3e6047413d Mon Sep 17 00:00:00 2001 From: Kshitij Date: Wed, 29 Sep 2021 21:50:20 +0530 Subject: [PATCH 4/5] Run black format on flagsmith.py --- flagsmith/flagsmith.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/flagsmith/flagsmith.py b/flagsmith/flagsmith.py index 729500c..ddbf314 100644 --- a/flagsmith/flagsmith.py +++ b/flagsmith/flagsmith.py @@ -24,7 +24,9 @@ def __init__(self, environment_id, api=SERVER_URL, custom_headers=None): self.flags_endpoint = api + FLAGS_ENDPOINT self.identities_endpoint = api + IDENTITY_ENDPOINT self.traits_endpoint = api + TRAIT_ENDPOINT - self.custom_headers = custom_headers if custom_headers and type(custom_headers) == dict else {} + self.custom_headers = ( + custom_headers if custom_headers and type(custom_headers) == dict else {} + ) def get_flags(self, identity=None): """ @@ -154,7 +156,9 @@ def set_trait(self, trait_key, trait_value, identity): } requests.post( - self.traits_endpoint, json=payload, headers=self._generate_header_content(self.custom_headers) + self.traits_endpoint, + json=payload, + headers=self._generate_header_content(self.custom_headers), ) def _get_flags_response(self, feature_name=None, identity=None): From 2d80f452d6f33bea02723463ff14a04ea2842a2a Mon Sep 17 00:00:00 2001 From: Kshitij Date: Sat, 2 Oct 2021 00:19:59 +0530 Subject: [PATCH 5/5] remove dict check on custom_headers --- flagsmith/flagsmith.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/flagsmith/flagsmith.py b/flagsmith/flagsmith.py index ddbf314..5db0757 100644 --- a/flagsmith/flagsmith.py +++ b/flagsmith/flagsmith.py @@ -24,9 +24,7 @@ def __init__(self, environment_id, api=SERVER_URL, custom_headers=None): self.flags_endpoint = api + FLAGS_ENDPOINT self.identities_endpoint = api + IDENTITY_ENDPOINT self.traits_endpoint = api + TRAIT_ENDPOINT - self.custom_headers = ( - custom_headers if custom_headers and type(custom_headers) == dict else {} - ) + self.custom_headers = custom_headers or {} def get_flags(self, identity=None): """