Skip to content

Commit

Permalink
Merge pull request #110 from yan12125/investigate-crates-io-403
Browse files Browse the repository at this point in the history
Fix 403 errors from crates.io
  • Loading branch information
lilydjwg authored Jul 6, 2019
2 parents 5e324ef + 851e141 commit e12a9c4
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 9 deletions.
11 changes: 8 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ python:
- "3.6"
- "3.7"
- "nightly"
- "pypy3.5"
install: pip install -U $DEPS pytest pytest-asyncio flaky structlog
- "pypy3.6-7.1.1"
install: pip install -U $DEPS pytest pytest-asyncio pytest-httpbin flaky structlog
script: pytest
env:
global:
Expand All @@ -26,5 +26,10 @@ matrix:
fast_finish: true
allow_failures:
# doesn't work well, see https://travis-ci.org/lilydjwg/nvchecker/jobs/376326582
- python: pypy3.5
- python: pypy3.6-7.1.1
env: DEPS=aiohttp

addons:
apt:
packages:
- libgnutls-dev
4 changes: 4 additions & 0 deletions nvchecker/source/aiohttp_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import atexit
import asyncio
import aiohttp
from .httpclient import DEFAULT_USER_AGENT

connector = aiohttp.TCPConnector(limit=20)

__all__ = ['session', 'HTTPError', 'NetworkErrors']
Expand All @@ -19,6 +21,8 @@ async def _request(self, *args, **kwargs):
if hasattr(self, "nv_config") and self.nv_config.get("proxy"):
kwargs.setdefault("proxy", self.nv_config.get("proxy"))

kwargs.setdefault("headers", {}).setdefault('User-Agent', DEFAULT_USER_AGENT)

res = await super(BetterClientSession, self)._request(
*args, **kwargs)
if res.status >= 400:
Expand Down
1 change: 0 additions & 1 deletion nvchecker/source/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ async def get_version_real(name, conf, **kwargs):
url += '?' + urlencode(parameters)
headers = {
'Accept': 'application/vnd.github.quicksilver-preview+json',
'User-Agent': 'lilydjwg/nvchecker',
}
if 'NVCHECKER_GITHUB_TOKEN' in os.environ:
headers['Authorization'] = 'token %s' % os.environ['NVCHECKER_GITHUB_TOKEN']
Expand Down
1 change: 1 addition & 0 deletions nvchecker/source/httpclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEFAULT_USER_AGENT = 'lilydjwg/nvchecker'
3 changes: 3 additions & 0 deletions nvchecker/source/tornado_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
except ImportError:
pycurl = None

from .httpclient import DEFAULT_USER_AGENT

__all__ = ['session', 'HTTPError', 'NetworkErrors']

client = AsyncHTTPClient()
Expand Down Expand Up @@ -51,6 +53,7 @@ def get(self, url, **kwargs):
q = urlencode(params)
url += '?' + q

kwargs.setdefault("headers", {}).setdefault('User-Agent', DEFAULT_USER_AGENT)
r = HTTPRequest(url, **kwargs)
return ResponseManager(r)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
tests_require = [
'pytest',
'pytest-asyncio',
'pytest-httpbin',
'flaky',
],
entry_points = {
Expand Down
14 changes: 9 additions & 5 deletions tests/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import pytest
pytestmark = pytest.mark.asyncio

@pytest.mark.skipif(True,
reason='httpbin is overloaded?')
async def test_regex_httpbin(get_version):
async def test_regex_httpbin_default_user_agent(get_version, httpbin):
assert await get_version("example", {
"url": "https://httpbin.org/get",
"regex": r'"User-Agent": "(\w+)"',
"url": httpbin.url + "/get",
"regex": r'"User-Agent":\s*"([^"]+)"',
}) == "lilydjwg/nvchecker"

async def test_regex_httpbin(get_version, httpbin):
assert await get_version("example", {
"url": httpbin.url + "/get",
"regex": r'"User-Agent":\s*"([^"]+)"',
"user_agent": "Meow",
}) == "Meow"

Expand Down

0 comments on commit e12a9c4

Please sign in to comment.