Fix: Compat netrequests for PY2 Requests - #3515
Conversation
|
Unable to trigger custom agent "Code Reviewer". You have run out of credits 😔 |
There was a problem hiding this comment.
Pull request overview
This PR introduces a .NET HttpClient-based shim (netrequests.py) intended to provide a minimal requests-like API for IronPython 2 on Revit/.NET 8, and wires it into pyrevit.compat so consumers can import requests from there for better backwards compatibility.
Changes:
- Added
pyrevit.netrequestsimplementing a small subset of therequestsAPI usingSystem.Net.Http.HttpClient. - Updated
pyrevit.compatto conditionally exposerequests(realrequestson PY3, shim on PY2).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| pyrevitlib/pyrevit/netrequests.py | New requests-compatible shim using .NET HttpClient, including streaming support and basic response helpers. |
| pyrevitlib/pyrevit/compat.py | Adds conditional import to expose requests via pyrevit.compat (real vs shim). |
| try: | ||
| if PY3: | ||
| import requests | ||
| else: | ||
| import pyrevit.netrequests as requests | ||
| except Exception: | ||
| import requests |
| if timeout is not None: | ||
| try: | ||
| client = HttpClient() | ||
| client.Timeout = TimeSpan.FromSeconds( | ||
| float(timeout) | ||
| ) | ||
| temporary_client = True | ||
| except Exception: | ||
| client = _shared_client | ||
| temporary_client = False | ||
|
|
| def close(self): | ||
| try: | ||
| if self._reader: | ||
| self._reader.Close() | ||
| except Exception: | ||
| pass | ||
|
|
||
| try: | ||
| if self._response: | ||
| self._response.Dispose() | ||
| except Exception: | ||
| pass | ||
|
|
||
| try: | ||
| if self._client: | ||
| self._client.Dispose() | ||
| except Exception: | ||
| pass | ||
|
|
| if not isinstance(data, str): | ||
| data = str(data) | ||
|
|
| if not stream: | ||
| try: | ||
| self._content = ( | ||
| dotnet_response.Content | ||
| .ReadAsByteArrayAsync() | ||
| .Result | ||
| ) | ||
| except Exception: | ||
| self._content = b"" | ||
|
|
||
| try: | ||
| self._text = ( | ||
| dotnet_response.Content | ||
| .ReadAsStringAsync() | ||
| .Result | ||
| ) | ||
| except Exception: |
sanzoghenzo
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
I encountered this issue just today, so I'm all in for this PR!😉
Please fix the issues raised by Copilot and those below.
Also, please format the code with black as instructed in the PR template, so that we got a consistent code style.
Co-authored-by: Andrea Ghensi <andrea.ghensi@gmail.com>
fix: raise ImportError for failed System.Net.Http reference
Co-authored-by: Andrea Ghensi <andrea.ghensi@gmail.com>
…ring/pyRevit into Requests-Fix-DotNet8.0
|
Me pressing the button blindfolded because Andrea and Andrew are on it 😉😁 |
|
I've got a final commit to do to fix some of the copilot flagged issues |
|
Shall I open another pull request for these updates? |
|
📦 New work-in-progress (wip) builds are available for 7.0.0 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0.26237+2139 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0.26237+2139 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0.26237+2139 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0.26237+2139 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0.26237+2139 |
|
📦 New work-in-progress (wip) builds are available for 7.0.0.26237+2139 |
Requests urllib not compatible with PY2 ironpython going forward for .Net 8.0
netrequests.py shim created and referenced in Compat to allow minimal update for backwards compatibility with Requests
pyrevit.compat import requests to replace import requests
Checklist
Before submitting your pull request, ensure the following requirements are met:
pipenv run black {source_file_or_directory}I have tested in revit 2024, 2025, with the latest 6.5.4 WIP installer.
Related Issues
Additional Notes
It may be better to put this higher up the stack to allow backwards compatibility with requests without the need for minimal code change.
This is my first ever pull request on literally anything public on github. So please be gentle!
I did use some copilot to help write this.
Thank you for contributing to pyRevit! 🎉