Skip to content

Commit

Permalink
Correct header usage. (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c authored Apr 13, 2023
1 parent d1b560b commit 50f9a04
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 10 additions & 0 deletions ocean_provider/http_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import lru
import requests
from ocean_provider.version import get_version
from requests.adapters import HTTPAdapter
from requests.sessions import Session
from web3 import HTTPProvider
Expand Down Expand Up @@ -42,6 +43,15 @@ def make_request(self, method: str, params: Any) -> Dict[str, Any]:

def make_post_request(endpoint_uri: str, data: bytes, *args, **kwargs) -> bytes:
kwargs.setdefault("timeout", 10)

version = get_version()
version_header = {"User-Agent": f"OceanProvider/{version}"}

if "headers" in kwargs:
kwargs["headers"].update(version_header)
else:
kwargs["headers"] = version_header

session = _get_session(endpoint_uri)
response = session.post(endpoint_uri, data=data, *args, **kwargs)
response.raise_for_status()
Expand Down
6 changes: 1 addition & 5 deletions ocean_provider/utils/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from eth_account import Account
from hexbytes import HexBytes
from ocean_provider.http_provider import CustomHTTPProvider
from ocean_provider.version import get_version
from web3 import WebsocketProvider
from web3.exceptions import ExtraDataLengthError
from web3.main import Web3
Expand Down Expand Up @@ -150,11 +149,8 @@ def get_web3(chain_id, cached=True) -> Web3:
def get_web3_connection_provider(
network_url: str,
) -> Union[CustomHTTPProvider, WebsocketProvider]:
version = get_version()
request_kwargs = {"headers": {"User-Agent": f"OceanProvider/{version}"}}

if network_url.startswith("http"):
return CustomHTTPProvider(network_url, request_kwargs=request_kwargs)
return CustomHTTPProvider(network_url)
elif network_url.startswith("ws"):
return WebsocketProvider(network_url)
else:
Expand Down

0 comments on commit 50f9a04

Please sign in to comment.