From 148bbc7c363fa4e015659e2d5464e11233e863e7 Mon Sep 17 00:00:00 2001 From: Matthias Nadler Date: Fri, 19 Jun 2020 21:49:08 +0200 Subject: [PATCH 1/5] feat: add custom user-agent for requests {"User-Agent": f"Brownie/{__version__} (Python/{python_version})"} --- brownie/_config.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/brownie/_config.py b/brownie/_config.py index 3058b0636..91f7abd2d 100644 --- a/brownie/_config.py +++ b/brownie/_config.py @@ -4,6 +4,7 @@ import os import re import shutil +import sys import warnings from collections import defaultdict from pathlib import Path @@ -13,6 +14,7 @@ from hypothesis import settings as hp_settings from hypothesis.database import DirectoryBasedExampleDatabase +from brownie._cli.__main__ import __version__ from brownie._singleton import _Singleton BROWNIE_FOLDER = Path(__file__).parent @@ -22,6 +24,12 @@ EVM_EQUIVALENTS = {"atlantis": "byzantium", "agharta": "petersburg"} +python_version = ( + f"{sys.version_info.major}.{sys.version_info.minor}" + f".{sys.version_info.micro} {sys.version_info.releaselevel}" +) +REQUEST_HEADERS = {"User-Agent": f"Brownie/{__version__} (Python/{python_version})"} + class ConfigContainer: def __init__(self): From 1b32632c47f5f4a6a54a0a2cac2edf838903febe Mon Sep 17 00:00:00 2001 From: Matthias Nadler Date: Fri, 19 Jun 2020 21:49:39 +0200 Subject: [PATCH 2/5] fix: add user-agent to all requests --- brownie/network/contract.py | 4 ++-- brownie/project/main.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/brownie/network/contract.py b/brownie/network/contract.py index 22a94bf67..d3e08639f 100644 --- a/brownie/network/contract.py +++ b/brownie/network/contract.py @@ -17,7 +17,7 @@ from hexbytes import HexBytes from semantic_version import Version -from brownie._config import CONFIG +from brownie._config import CONFIG, REQUEST_HEADERS from brownie.convert.datatypes import Wei from brownie.convert.normalize import format_input, format_output from brownie.convert.utils import ( @@ -1228,8 +1228,8 @@ def _fetch_from_explorer(address: str, action: str, silent: bool) -> Dict: f"Fetching source of {color('bright blue')}{address}{color} " f"from {color('bright blue')}{urlparse(url).netloc}{color}..." ) - response = requests.get(url, params=params) + response = requests.get(url, params=params, headers=REQUEST_HEADERS) if response.status_code != 200: raise ConnectionError(f"Status {response.status_code} when querying {url}: {response.text}") data = response.json() diff --git a/brownie/project/main.py b/brownie/project/main.py index aa70381a3..53702cbde 100644 --- a/brownie/project/main.py +++ b/brownie/project/main.py @@ -22,6 +22,7 @@ from brownie._config import ( CONFIG, + REQUEST_HEADERS, _get_data_folder, _load_project_compiler_config, _load_project_config, @@ -725,13 +726,12 @@ def _install_from_github(package_id: str) -> str: if install_path.exists(): raise FileExistsError("Package is aleady installed") - headers: Dict = {} if os.getenv("GITHUB_TOKEN"): auth = b64encode(os.environ["GITHUB_TOKEN"].encode()).decode() - headers = {"Authorization": "Basic {}".format(auth)} + REQUEST_HEADERS.update({"Authorization": "Basic {}".format(auth)}) response = requests.get( - f"https://api.github.com/repos/{org}/{repo}/tags?per_page=100", headers=headers + f"https://api.github.com/repos/{org}/{repo}/tags?per_page=100", headers=REQUEST_HEADERS ) if response.status_code != 200: msg = "Status {} when getting package versions from Github: '{}'".format( @@ -865,7 +865,7 @@ def _load_sources(project_path: Path, subfolder: str, allow_json: bool) -> Dict: def _stream_download(download_url: str, target_path: str) -> None: - response = requests.get(download_url, stream=True) + response = requests.get(download_url, stream=True, headers=REQUEST_HEADERS) total_size = int(response.headers.get("content-length", 0)) progress_bar = tqdm(total=total_size, unit="iB", unit_scale=True) content = bytes() From 5c58c6f2a2973fad2830e115147cec0f087bd282 Mon Sep 17 00:00:00 2001 From: Matthias Nadler Date: Fri, 19 Jun 2020 22:33:32 +0200 Subject: [PATCH 3/5] fix: moved __version__ to _config.py updated imports and setup.cfg --- brownie/_cli/__main__.py | 4 +--- brownie/_cli/analyze.py | 8 ++++++-- brownie/_config.py | 3 ++- setup.cfg | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/brownie/_cli/__main__.py b/brownie/_cli/__main__.py index a19cc1c6a..6be08dd4e 100644 --- a/brownie/_cli/__main__.py +++ b/brownie/_cli/__main__.py @@ -5,13 +5,11 @@ from pathlib import Path from brownie import network -from brownie._config import CONFIG +from brownie._config import CONFIG, __version__ from brownie.exceptions import ProjectNotFound from brownie.utils import color, notify from brownie.utils.docopt import docopt, levenshtein_norm -__version__ = "1.9.3" - __doc__ = """Usage: brownie [...] [options ] Commands: diff --git a/brownie/_cli/analyze.py b/brownie/_cli/analyze.py index fc52937af..f266a5b4c 100644 --- a/brownie/_cli/analyze.py +++ b/brownie/_cli/analyze.py @@ -13,8 +13,12 @@ from pythx.middleware import ClientToolNameMiddleware, GroupDataMiddleware from brownie import project -from brownie._cli.__main__ import __version__ -from brownie._config import CONFIG, _load_project_structure_config, _update_argv_from_docopt +from brownie._config import ( + CONFIG, + __version__, + _load_project_structure_config, + _update_argv_from_docopt, +) from brownie.exceptions import ProjectNotFound from brownie.utils import color, notify from brownie.utils.docopt import docopt diff --git a/brownie/_config.py b/brownie/_config.py index 91f7abd2d..41d8368fe 100644 --- a/brownie/_config.py +++ b/brownie/_config.py @@ -14,9 +14,10 @@ from hypothesis import settings as hp_settings from hypothesis.database import DirectoryBasedExampleDatabase -from brownie._cli.__main__ import __version__ from brownie._singleton import _Singleton +__version__ = "1.9.3" + BROWNIE_FOLDER = Path(__file__).parent DATA_FOLDER = Path.home().joinpath(".brownie") diff --git a/setup.cfg b/setup.cfg index e91145710..5f1f2ceac 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ current_version = 1.9.3 [bumpversion:file:setup.py] -[bumpversion:file:brownie/_cli/__main__.py] +[bumpversion:file:brownie/_config.py] [flake8] exclude = tests/data/* From 4de19b9f0899bfa347f30f3c87ee76588f1d7cb7 Mon Sep 17 00:00:00 2001 From: Matthias Nadler Date: Fri, 19 Jun 2020 22:55:35 +0200 Subject: [PATCH 4/5] chore: changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07c48c66b..ab9bc364b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/iamdefinitelyahuman/brownie) +### Fixed +- Http Requests now send a custom User-Agent (Fixes Kovan api requests) ([#643](https://github.com/eth-brownie/brownie/pull/643)) + ## [1.9.3](https://github.com/iamdefinitelyahuman/brownie/tree/v1.9.3) - 2020-06-19 ### Added - Accounts can now be unlocked on development networks ([#633](https://github.com/eth-brownie/brownie/pull/633)) From 6ab7164d5632436c534a2a64d989f05344227e0d Mon Sep 17 00:00:00 2001 From: Matthias Nadler Date: Sat, 20 Jun 2020 10:43:26 +0200 Subject: [PATCH 5/5] fix: local request header var requested change --- brownie/project/main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/brownie/project/main.py b/brownie/project/main.py index 53702cbde..fd6ba7598 100644 --- a/brownie/project/main.py +++ b/brownie/project/main.py @@ -726,12 +726,13 @@ def _install_from_github(package_id: str) -> str: if install_path.exists(): raise FileExistsError("Package is aleady installed") + headers = REQUEST_HEADERS.copy() if os.getenv("GITHUB_TOKEN"): auth = b64encode(os.environ["GITHUB_TOKEN"].encode()).decode() - REQUEST_HEADERS.update({"Authorization": "Basic {}".format(auth)}) + headers.update({"Authorization": "Basic {}".format(auth)}) response = requests.get( - f"https://api.github.com/repos/{org}/{repo}/tags?per_page=100", headers=REQUEST_HEADERS + f"https://api.github.com/repos/{org}/{repo}/tags?per_page=100", headers=headers ) if response.status_code != 200: msg = "Status {} when getting package versions from Github: '{}'".format(