Skip to content

misc(provider): Change User-Agent header signature #2964

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

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/2964.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change User-Agent header signature
12 changes: 12 additions & 0 deletions tests/core/providers/test_async_http_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from web3 import (
AsyncWeb3,
__version__ as web3py_version,
)
from web3._utils import (
request,
Expand Down Expand Up @@ -106,3 +107,14 @@ async def test_async_user_provided_session() -> None:
cached_session = await provider.cache_async_session(session)
assert len(request._async_session_cache) == 1
assert cached_session == session


def test_get_request_headers():
provider = AsyncHTTPProvider()
headers = provider.get_request_headers()
assert len(headers) == 2
assert headers["Content-Type"] == "application/json"
assert (
headers["User-Agent"] == f"web3.py/{web3py_version}/"
f"{AsyncHTTPProvider.__module__}.{AsyncHTTPProvider.__qualname__}"
)
12 changes: 12 additions & 0 deletions tests/core/providers/test_http_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from web3 import (
Web3,
__version__ as web3py_version,
)
from web3._utils import (
request,
Expand Down Expand Up @@ -103,3 +104,14 @@ def test_user_provided_session():
assert isinstance(adapter, HTTPAdapter)
assert adapter._pool_connections == 20
assert adapter._pool_maxsize == 20


def test_get_request_headers():
provider = HTTPProvider()
headers = provider.get_request_headers()
assert len(headers) == 2
assert headers["Content-Type"] == "application/json"
assert (
headers["User-Agent"] == f"web3.py/{web3py_version}/"
f"{HTTPProvider.__module__}.{HTTPProvider.__qualname__}"
)
5 changes: 2 additions & 3 deletions web3/_utils/http.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
def construct_user_agent(class_name: str) -> str:
def construct_user_agent(class_type: type) -> str:
from web3 import (
__version__ as web3_version,
)

user_agent = f"web3.py/{web3_version}/{class_name}"
return user_agent
return f"web3.py/{web3_version}/{class_type.__module__}.{class_type.__qualname__}"
2 changes: 1 addition & 1 deletion web3/providers/rpc/async_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_request_kwargs(self) -> Iterable[Tuple[str, Any]]:
def get_request_headers(self) -> Dict[str, str]:
return {
"Content-Type": "application/json",
"User-Agent": construct_user_agent(str(type(self))),
"User-Agent": construct_user_agent(type(self)),
}

async def _make_request(self, method: RPCEndpoint, request_data: bytes) -> bytes:
Expand Down
2 changes: 1 addition & 1 deletion web3/providers/rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_request_kwargs(self) -> Iterable[Tuple[str, Any]]:
def get_request_headers(self) -> Dict[str, str]:
return {
"Content-Type": "application/json",
"User-Agent": construct_user_agent(str(type(self))),
"User-Agent": construct_user_agent(type(self)),
}

def _make_request(self, method: RPCEndpoint, request_data: bytes) -> bytes:
Expand Down