Skip to content

Commit

Permalink
feat: take a headers_factory callable instead of fixed headers in OFR…
Browse files Browse the repository at this point in the history
…EPProvider

Signed-off-by: Federico Bond <federicobond@gmail.com>
  • Loading branch information
federicobond committed May 21, 2024
1 parent 873f5e2 commit b7b5e27
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()

Expand Down
5 changes: 4 additions & 1 deletion providers/openfeature-provider-ofrep/tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b7b5e27

Please sign in to comment.