From b7b5e27596c3db33dba866154f8521fe6185923b Mon Sep 17 00:00:00 2001 From: Federico Bond Date: Wed, 15 May 2024 00:33:19 +1000 Subject: [PATCH] feat: take a headers_factory callable instead of fixed headers in OFREPProvider Signed-off-by: Federico Bond --- .../src/openfeature/contrib/provider/ofrep/__init__.py | 10 ++++------ .../openfeature-provider-ofrep/tests/test_provider.py | 5 ++++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/providers/openfeature-provider-ofrep/src/openfeature/contrib/provider/ofrep/__init__.py b/providers/openfeature-provider-ofrep/src/openfeature/contrib/provider/ofrep/__init__.py index 7cd601ad..90002d9f 100644 --- a/providers/openfeature-provider-ofrep/src/openfeature/contrib/provider/ofrep/__init__.py +++ b/providers/openfeature-provider-ofrep/src/openfeature/contrib/provider/ofrep/__init__.py @@ -1,7 +1,7 @@ import re from datetime import datetime, timedelta, timezone from email.utils import parsedate_to_datetime -from typing import Any, Dict, List, NoReturn, Optional, Tuple, Type, Union +from typing import Any, Callable, Dict, List, NoReturn, Optional, Tuple, Type, Union from urllib.parse import urljoin import requests @@ -42,17 +42,14 @@ def __init__( self, base_url: str, *, - headers: Optional[Dict[str, str]] = None, + headers_factory: Optional[Callable[[], Dict[str, str]]] = None, timeout: float = 5.0, ): self.base_url = base_url - self.headers = headers + self.headers_factory = headers_factory self.timeout = timeout self.retry_after: Optional[datetime] = None self.session = requests.Session() - self.session.headers["User-Agent"] = "OpenFeature/1.0.0" - if headers: - self.session.headers.update(headers) def get_metadata(self) -> Metadata: return Metadata(name="OpenFeature Remote Evaluation Protocol Provider") @@ -130,6 +127,7 @@ def _resolve( urljoin(self.base_url, f"/ofrep/v1/evaluate/flags/{flag_key}"), json=_build_request_data(evaluation_context), timeout=self.timeout, + headers=self.headers_factory() if self.headers_factory else None, ) response.raise_for_status() diff --git a/providers/openfeature-provider-ofrep/tests/test_provider.py b/providers/openfeature-provider-ofrep/tests/test_provider.py index e33087b3..ca7dbba1 100644 --- a/providers/openfeature-provider-ofrep/tests/test_provider.py +++ b/providers/openfeature-provider-ofrep/tests/test_provider.py @@ -13,7 +13,10 @@ def test_provider_init(): - OFREPProvider("http://localhost:8080", headers={"Authorization": "Bearer token"}) + OFREPProvider( + "http://localhost:8080", + headers_factory=lambda: {"Authorization": "Bearer token"}, + ) @pytest.mark.parametrize(