Skip to content

Commit 4802946

Browse files
committed
Add support for loading proxy config from the environment
The popular python deployment platform PythonAnywhere requires that http proxy settings be loaded from the environment when using their free plan (to whitelist domains). This change adds a flag to enable loading this configuration. Fixes #24
1 parent dec67aa commit 4802946

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

pusher_push_notifications/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
import datetime
55
import json
6+
import os
67
import re
78
import time
89
import warnings
@@ -76,12 +77,20 @@ def _make_url(scheme, host, path):
7677
])
7778

7879

80+
def _get_proxies_from_env():
81+
return {
82+
'http': os.environ.get('HTTP_PROXY') or os.environ.get('http_proxy'),
83+
'https': os.environ.get('HTTPS_PROXY') or os.environ.get('https_proxy'),
84+
}
85+
86+
7987
class PushNotifications(object):
8088
"""Pusher Push Notifications API client
8189
This client class can be used to publish notifications to the Pusher
8290
Push Notifications service"""
8391

84-
def __init__(self, instance_id, secret_key, endpoint=None):
92+
def __init__(self, instance_id, secret_key,
93+
endpoint=None, use_proxy_env_vars=False):
8594
if not isinstance(instance_id, six.string_types):
8695
raise TypeError('instance_id must be a string')
8796
if instance_id == '':
@@ -99,6 +108,7 @@ def __init__(self, instance_id, secret_key, endpoint=None):
99108
self.instance_id = instance_id
100109
self.secret_key = secret_key
101110
self._endpoint = endpoint
111+
self._use_proxy_env_vars = use_proxy_env_vars
102112

103113
@property
104114
def endpoint(self):
@@ -117,6 +127,9 @@ def _make_request(self, method, path, path_params, body=None):
117127
url = _make_url(scheme='https', host=self.endpoint, path=path)
118128

119129
session = requests.Session()
130+
if self._use_proxy_env_vars:
131+
session.proxies = _get_proxies_from_env()
132+
120133
request = requests.Request(
121134
method,
122135
url,

0 commit comments

Comments
 (0)