Fix: Compat netrequests for PY2 Requests - #3515
Open
Andrew-Waring wants to merge 2 commits into
Open
Conversation
Contributor
|
Unable to trigger custom agent "Code Reviewer". You have run out of credits 😔 |
Contributor
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). |
Comment on lines
+41
to
+47
| try: | ||
| if PY3: | ||
| import requests | ||
| else: | ||
| import pyrevit.netrequests as requests | ||
| except Exception: | ||
| import requests |
Comment on lines
+423
to
+433
| 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 | ||
|
|
Comment on lines
+294
to
+312
| 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 | ||
|
|
Comment on lines
+382
to
+384
| if not isinstance(data, str): | ||
| data = str(data) | ||
|
|
Comment on lines
+186
to
+202
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Name of your PR
Compat netrequests for PY2 Requests
Description
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! 🎉