-
Notifications
You must be signed in to change notification settings - Fork 23
Update OFREP provider to use httpx and support async #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here aclose()
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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", {}), | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for this
| def __del__(self): | ||
| # Ensure clients get cleaned up | ||
| self.shutdown() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.