Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxies property based on environment variables for HTTP requests #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion portainer_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class PortainerCLI(object):
local = False
_base_url = 'http://localhost:9000/'
_jwt = None
_proxies = {}

@property
def base_url(self):
Expand All @@ -67,6 +68,23 @@ def jwt(self, value):
self._jwt = value
self.persist()

@property
def proxies(self):
return self._proxies

@proxies.setter
def proxies(self, value):
try:
self._proxies['http'] = os.environ['HTTP_PROXY']
except KeyError:
self._proxies['http'] = ''
try:
self._proxies['https'] = os.environ['HTTPS_PROXY']
except KeyError:
self._proxies['https'] = ''
if self._proxies['http'] == '' and self._proxies['https'] == '':
self._proxies = {}

@property
def data_path(self):
if self.local:
Expand Down Expand Up @@ -231,7 +249,7 @@ def request(self, path, method=METHOD_GET, data='', printc=False):
prepped.headers['Content-Length'] = len(prepped.body)
if self.jwt:
prepped.headers['Authorization'] = 'Bearer {}'.format(self.jwt)
response = session.send(prepped)
response = session.send(prepped, proxies=self.proxies)
logger.debug('request response: {}'.format(response.content))
response.raise_for_status()
if printc:
Expand All @@ -254,6 +272,7 @@ def main(self, command, debug=False, local=local, *args):
logging.basicConfig(level=logging.DEBUG)
self.local = local
self.load()
self.proxies = {}
if command == self.COMMAND_CONFIGURE:
plac.call(self.configure, args)
elif command == self.COMMAND_LOGIN:
Expand Down