Skip to content

Commit a0777d8

Browse files
Added headers parameter which can be used to pass custom headers (#10)
* Added headers paramether which can be used to pass custom headers for API calls made to flagsmith * Update docstring comment * change headers argument name to custom_headers and avoid mutable defaults * Run black format on flagsmith.py * remove dict check on custom_headers
1 parent 6317cdb commit a0777d8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

flagsmith/flagsmith.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313

1414

1515
class Flagsmith:
16-
def __init__(self, environment_id, api=SERVER_URL, request_timeout=None):
16+
def __init__(
17+
self, environment_id, api=SERVER_URL, custom_headers=None, request_timeout=None
18+
):
1719
"""
1820
Initialise Flagsmith environment.
1921
2022
:param environment_id: environment key obtained from the Flagsmith UI
2123
:param api: (optional) api url to override when using self hosted version
24+
:param custom_headers: (optional) dict which will be passed in headers for each api call
2225
:param request_timeout: (optional) request timeout in seconds
2326
"""
2427

@@ -27,6 +30,7 @@ def __init__(self, environment_id, api=SERVER_URL, request_timeout=None):
2730
self.flags_endpoint = api + FLAGS_ENDPOINT
2831
self.identities_endpoint = api + IDENTITY_ENDPOINT
2932
self.traits_endpoint = api + TRAIT_ENDPOINT
33+
self.custom_headers = custom_headers or {}
3034
self.request_timeout = request_timeout
3135
self._analytics_processor = AnalyticsProcessor(
3236
environment_id, api, self.request_timeout
@@ -155,7 +159,7 @@ def set_trait(self, trait_key, trait_value, identity):
155159
requests.post(
156160
self.traits_endpoint,
157161
json=payload,
158-
headers=self._generate_header_content(),
162+
headers=self._generate_header_content(self.custom_headers),
159163
timeout=self.request_timeout,
160164
)
161165

@@ -175,14 +179,14 @@ def _get_flags_response(self, feature_name=None, identity=None):
175179
response = requests.get(
176180
self.identities_endpoint,
177181
params=params,
178-
headers=self._generate_header_content(),
182+
headers=self._generate_header_content(self.custom_headers),
179183
timeout=self.request_timeout,
180184
)
181185
else:
182186
response = requests.get(
183187
self.flags_endpoint,
184188
params=params,
185-
headers=self._generate_header_content(),
189+
headers=self._generate_header_content(self.custom_headers),
186190
timeout=self.request_timeout,
187191
)
188192

0 commit comments

Comments
 (0)