Update OFREP provider to use httpx and support async#311
Update OFREP provider to use httpx and support async#311dmontagu wants to merge 1 commit intoopen-feature:mainfrom
Conversation
|
Hey there and thank you for opening this pull request! 👋🏼 We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted. Details: |
gruebel
left a comment
There was a problem hiding this comment.
thanks for the contribution 🍻 I don't mind switching to httpx, but please add some test case(s) for the async variant and you also need to adjust the pyproject.toml
| def initialize(self, evaluation_context: EvaluationContext) -> None: | ||
| self.client.__enter__() |
There was a problem hiding this comment.
why enter the client here? when you initialize it in the constructor then it should be already entered or not?
| self.client.__enter__() | ||
|
|
||
| def shutdown(self) -> None: | ||
| self.client.__exit__(None, None, None) |
There was a problem hiding this comment.
why not just call .close() on it
|
|
||
| try: | ||
| # TODO(someday): support non asyncio runtimes here | ||
| asyncio.get_running_loop().create_task(self.client_async.__aexit__(None, None, None)) |
| if not self._client_async_is_entered: | ||
| await self.client_async.__aenter__() | ||
| self._client_async_is_entered = True |
There was a problem hiding this comment.
as far as I can tell from the official docs this is not needed
| now = datetime.now(timezone.utc) | ||
| if self.retry_after and now <= self.retry_after: | ||
| raise GeneralError( | ||
| f"OFREP evaluation paused due to TooManyRequests until {self.retry_after}" | ||
| ) | ||
| elif self.retry_after: | ||
| self.retry_after = None |
There was a problem hiding this comment.
duplicated code, please create a small private method for it
| try: | ||
| data = response.json() | ||
| except JSONDecodeError as e: | ||
| raise ParseError(str(e)) from e | ||
|
|
||
| _typecheck_flag_value(data["value"], flag_type) | ||
|
|
||
| return FlagResolutionDetails( | ||
| value=data["value"], | ||
| reason=Reason[data["reason"]], | ||
| variant=data["variant"], | ||
| flag_metadata=data.get("metadata", {}), | ||
| ) |
| def __del__(self): | ||
| # Ensure clients get cleaned up | ||
| self.shutdown() |
There was a problem hiding this comment.
not sure, if this is really needed, but it is better to use weakref for this.
This PR
Updates the OFREP provider to use httpx and have proper async support.
I have not yet added tests etc. to this PR, because I wanted to get feedback on it before doing more work. But I have used this version of the provider code internally and it has worked fine, so I at least wanted to share.
Related Issues
Closes #310 if merged.
Follow-up Tasks
Need to update pyproject.toml, add tests, etc. Probably will take a decent effort so I don't want to undertake it if there's not consensus this is a good approach.
I'd be potentially willing to port all existing usage of requests to httpx if there was interest in expanding beyond just this OFREP provider (and with that, adding async support for all providers in this package). I'm not sure what the right replacement for requests-mock is with httpx (which I can see is what is used in tests) but I'm sure something reasonable exists.
How to test
This won't work yet unless you manually (uv) pip install httpx. I'm mostly looking to get confirmation of interest in the approach / replacing requests with httpx at this time.