Skip to content

Commit

Permalink
Fixed #465 httpx 0.28.0 not compatible with rollbar v1.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmorell committed Jan 10, 2025
1 parent a809a6b commit 4607033
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
8 changes: 7 additions & 1 deletion rollbar/lib/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,16 @@ async def _post_api_httpx(path, payload_str, access_token=None):
'proxy_password': rollbar.SETTINGS.get('http_proxy_password'),
}
proxies = transport._get_proxy_cfg(proxy_cfg)
mounts = None
if proxies:
mounts = {
'http://': httpx.HTTPTransport(proxy=proxies['http']),
'https://': httpx.HTTPTransport(proxy=proxies['https']),
}

url = urljoin(rollbar.SETTINGS['endpoint'], path)
async with httpx.AsyncClient(
proxies=proxies, verify=rollbar.SETTINGS.get('verify_https', True)
mounts=mounts, verify=rollbar.SETTINGS.get('verify_https', True)
) as client:
resp = await client.post(
url,
Expand Down
12 changes: 7 additions & 5 deletions rollbar/lib/transport.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

import requests
import threading

Expand All @@ -12,19 +14,19 @@ def _session():
return _local.session


def _get_proxy_cfg(kw):
def _get_proxy_cfg(kw: dict) -> Optional[dict]:
proxy = kw.pop('proxy', None)
proxy_user = kw.pop('proxy_user', None)
proxy_password = kw.pop('proxy_password', None)
if proxy and proxy_user and proxy_password:
return {
'http': 'http://{}:{}@{}'.format(proxy_user, proxy_password, proxy),
'https': 'http://{}:{}@{}'.format(proxy_user, proxy_password, proxy),
'http': f'http://{proxy_user}:{proxy_password}@{proxy}',
'https': f'http://{proxy_user}:{proxy_password}@{proxy}',
}
elif proxy:
return {
'http': 'http://{}'.format(proxy),
'https': 'http://{}'.format(proxy),
'http': f'http://{proxy}',
'https': f'http://{proxy}',
}


Expand Down
17 changes: 17 additions & 0 deletions rollbar/test/test_lib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rollbar.lib import dict_merge, prefix_match, key_match, key_depth
from rollbar.lib.transport import _get_proxy_cfg

from rollbar.test import BaseTest

Expand Down Expand Up @@ -108,3 +109,19 @@ def test_dict_merge_dicts_select_poll(self):
self.assertEqual(42, result['a']['b'])
self.assertIn('y', result['a'])
self.assertRegex(result['a']['y'], r'Uncopyable obj')

def test_transport_get_proxy_cfg(self):
result = _get_proxy_cfg({})
self.assertEqual(None, result)

result = _get_proxy_cfg({'proxy': 'localhost'})
self.assertEqual({'http': 'http://localhost', 'https': 'http://localhost'}, result)

result = _get_proxy_cfg({'proxy': 'localhost:8080'})
self.assertEqual({'http': 'http://localhost:8080', 'https': 'http://localhost:8080'}, result)

result = _get_proxy_cfg({'proxy': 'localhost', 'proxy_user': 'username', 'proxy_password': 'password'})
self.assertEqual({
'http': 'http://username:password@localhost',
'https': 'http://username:password@localhost',
}, result)

0 comments on commit 4607033

Please sign in to comment.